【问题标题】:Placing labels inside pie chart slices (Highchart)在饼图切片内放置标签(Highchart)
【发布时间】:2014-10-09 16:03:38
【问题描述】:

使用 Highchart。我使用距离将标签定位在切片内,它在移动设备上很好地居中,但在平板电脑或台式机上却不行。随着尺寸变大,标签会离边缘太近。如何根据视口尺寸定位标签?

plotOptions: {
    pie: {
        allowPointSelect: true,
        cursor: "pointer",
        dataLabels: {
            enabled: true,
            format: "<strong>{point.name}</strong><br>{point.percentage:.0f}%",
            distance: -60,
            color: "white"
        }
    },

series: [ {
            type: "pie",
            data: [
                {
                    name: "Not too much text here",
                    color: "#5cb85c",
                    y: 35
                },
                {
                    name: "Not too much text here",
                    color: "#5bc0de",
                    y: 30
                },
                {
                    name: "Less text here",
                    color: "#78629b",
                    y: 15
                },
                {
                    name: "Less text here",
                    color: "#f0ad4e",
                    y: 10
                },
                {
                    name: "Less text here",
                    color: "#d9534f",
                    y: 10
                }
            ]
        } ]

编辑:http://jsfiddle.net/TimNguyenBSM/rL1bwam6/1/

【问题讨论】:

标签: javascript jquery highcharts pie-chart


【解决方案1】:

您可以为您事先准备好的distance-选项使用一个变量。例如:

var windowSize = $(window).width();
var distance = -60;

if(windowSize > 700)
    distance -= 30;
// else if...

在你的plotOptions.pie.dataLabels:

distance: distance

【讨论】:

  • 我认为这是一个很好的解决方案。我希望我错误地使用了 Highcharts 并且有一个内置选项吗?
  • 我同意。这远非最佳。据我所知,没有任何选择可以让这一切变得简单。此外,由于它是一个 plotOption,因此在渲染后也不容易更改。希望有人能找到更可靠的东西,但至少这是一个可能的解决方案。您当然也可以使用您的容器宽度,如果这是距离值更相关的指标。
  • 谢谢!我现在将使用此解决方案,但请先将其打开几天,看看是否有任何新的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-27
相关资源
最近更新 更多