【问题标题】:Place text in center of pie chart - Highcharts将文本放在饼图的中心 - Highcharts
【发布时间】:2012-03-16 05:06:59
【问题描述】:

我想在 HighCharts 的饼图/甜甜圈图的中心放置一个标题。

我在图表选项中看不到任何可以直接启用此功能的内容,因此我认为它需要使用 Highcharts SVG 渲染器 -- chart.renderer.text('My Text', x, y)

我已经设置好了,但我无法让文本在饼图中居中。 x 和 y 值似乎不接受百分比。有没有一种程序化的方法可以让它在情节中居中,并在容器调整大小时幸存下来?

更新:将 x 和 y 设置为 50% 可以很好地使圆居中(使用 renderer.circle(x, y, radius)),但不能使文本居中(使用 renderer.text(text, x, y))。

在此处查看结果:http://jsfiddle.net/supertrue/e2qpa/

【问题讨论】:

  • 我不明白为什么 Highcharts 不支持这种情况。

标签: svg highcharts


【解决方案1】:

使用此代码将图表标题垂直居中:

title: {
    verticalAlign: 'middle',
    floating: true
}

Example

编辑

从 2.1 版开始,可以省略 float 属性,因为 documentation 声明它是由 verticalAlign 属性的存在所暗示的

当给定一个值时,标题表现为浮动。

【讨论】:

  • 我还添加了一些 CSS 来格式化它:
  • 这应该是正确的答案,如果你想要标题和中心文本,请使用中心文本的副标题选项
  • 优秀的答案。当将它用于底部启用图例的饼图时,我发现我还必须添加 y: -10。
  • 优秀!!最简单的解决方案。这应该是选择的答案,而不是 johnstorm 的
  • @LachlanMcD。您在图表中找到带有图例的任何解决方案?
【解决方案2】:

你需要考虑图表的位置,所以如果你使用'left'和'top'属性,你可以添加一半的绘图宽度并减去一半的文本边界框宽度。这将产生确切的中心:

text = chart.renderer.text("My Text").add();
textBBox = text.getBBox();
x = chart.plotLeft + (chart.plotWidth  * 0.5) - (textBBox.width  * 0.5);
y = chart.plotTop  + (chart.plotHeight * 0.5) - (textBBox.height * 0.5);
text.attr({x: x, y: y});

除非您先添加边界框,否则显然边界框将为 0。

更正:

y = chart.plotTop  + (chart.plotHeight * 0.5) + (textBBox.height * 0.25);

所以,我最初的想法,这似乎很好,是文本将由左上角对齐,但它由左下角完成。因此,我们实际上需要添加它,而不是减去一半的高度。让我感到困惑的是,我还不明白的是,要获得中心,您只需增加 25% 的高度,而不是 50%。注意:这似乎不适用于 IE 7 或 8。

重大更新

http://jsfiddle.net/NVX3S/2/

这个新更新的作用是在图表完成后使用 jquery 添加一个新元素。这适用于我测试过的所有浏览器(包括 IE7、8、9、Firefox 和 Chrome)。

var textX = chart.plotLeft + (chart.plotWidth  * 0.5);
var textY = chart.plotTop  + (chart.plotHeight * 0.5);

// The width and height need to be calculated properly, so I made it a span.
var span = '<span id="pieChartInfoText" style="position:absolute; text-align:center;">';
span += '<span style="font-size: 32px">Upper</span><br>';
span += '<span style="font-size: 16px">Lower</span>';
span += '</span>';

$("#addText").append(span);
span = $('#pieChartInfoText');
span.css('left', textX + (span.width() * -0.5));
span.css('top', textY + (span.height() * -0.5));

【讨论】:

  • 谢谢,这是非常有用的信息——我不知道 plotWidth 和 plotHeight 属性。唯一的问题是它在调整大小时不会重新调整。请参阅jsfiddle.net/supertrue/tH8FY(同样由于某种原因,该展示位置看起来偏离了中心。)有没有什么方法可以使用组或基于百分比的定位来实现这一点?
  • 所以,进一步看,显然边界框将是 0,除非你先添加它。我更新了上面的例子。更不幸的是,我找到了如何将它集中在 chrome 上,但这在 IE 7 或 8 上不起作用。我编辑了你的示例来演示:jsfiddle.net/NVX3S
  • 效果很好,谢谢。一个小警告,标题的高度似乎稍微偏离了垂直位置(我在跨度顶部和图表的圆圈之间测量了 60 像素,在底部测量了 80 像素)。将标题高度(大约 18px)添加到 plotHeight 似乎可以修复它var textY = chart.plotTop + ((chart.plotHeight + 18) * 0.5); 现在,如何动态获取标题高度? jsfiddle.net/64td8
  • 我在想 highcharts 可能会提供这样做的选项。我在他们的 API 参考中寻找一个属性。
  • 这个答案中最重要的一行(对我来说)是What confuses me, and something I don't understand yet, is that to get the center you add only 25% of the height rather then 50%
【解决方案3】:

只需添加以下内容。

title: { text : 'title to display in the middle' verticalAlign: 'middle', y: 5 },

【讨论】:

    【解决方案4】:

    最好的解决方案是依靠系列中心,而不是plotHeightplotWidth

    var chart = new Highcharts.Chart({
        chart: {
            renderTo: 'container',
            type: 'pie'
        },
        plotOptions: {
            pie: {
                //innerSize: '60%'
            }
        },
        title: {
            text: ''
        },
        series: [{
            data: [
                ['Firefox', 2262],
                ['IE7', 3800],
                ['IE6', 1000],
                ['Chrome', 1986]
                ]}]
    },
    
    function(chart) { // on complete
        var textX = chart.plotLeft + (chart.series[0].center[0]);
        var textY = chart.plotTop  + (chart.series[0].center[1]);
    
        var span = '<span id="pieChartInfoText" style="position:absolute; text-align:center;">';
        span += '<span style="font-size: 32px">Upper</span><br>';
        span += '<span style="font-size: 16px">Lower</span>';
        span += '</span>';
    
        $("#addText").append(span);
        span = $('#pieChartInfoText');
        span.css('left', textX + (span.width() * -0.5));
        span.css('top', textY + (span.height() * -0.5));
    });
    

    工作演示:http://jsfiddle.net/4dL9S/

    【讨论】:

    • 这是一个非常可靠的解决方案。
    【解决方案5】:

    首先,您可能需要检查小提琴中的 html。 您的容器上的 宽度500 而不是 500px

    <div id="container" style="height: 400px; width: 500px"></div>​
    

    其次,这个小提琴有点快和脏,但我认为它完成了你想要的?

    http://jsfiddle.net/e2qpa/3/

    【讨论】:

    • 我知道我可以手动定位它,但我正在寻找一种无论容器大小如何都能将其居中的解决方案。我更新了我的小提琴以拥有一个没有流体大小的容器-jsfiddle.net/supertrue/e2qpa
    【解决方案6】:
    var newChart = new Highcharts.Chart(options);
    
    //Set No data text
    var textX = newChart.plotLeft + (newChart.plotWidth * 0.5);
    var textY = newChart.plotTop + (newChart.plotHeight * 0.3);
    var textWidth = 500;
    textX = textX - (textWidth / 2);
    
    newChart.renderer.label('<div style="width: ' + textWidth + 'px; text-align: center"><span>Dati non disponibili per questa selezione</span></div>', textX, textY, null, null, null, true)
            .css({
                fontSize: '16px',
            }).add();
    

    【讨论】:

    【解决方案7】:

    当有图例并且您将所需的文本设置在图表中心作为标题时,我发现这很有效:

    events: {
        load: function() {
            const titleHeight = this.title.getBBox().height;
            this.title.attr({
                y: this.plotHeight / 2 + titleHeight / 2,
            });
        },
        redraw: function() {
            const titleHeight = this.title.getBBox().height;
            this.title.attr({
                y: this.plotHeight / 2 + titleHeight / 2,
            });
        },
    },
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多