首页 > 解决方案 > highcharts 中图表的大小

问题描述

在此处输入图像描述我在我的 django 项目中使用 highcharts 创建了饼图。每个饼图位于单独的 div 中,所有 div 的宽度设置为 25%。我使用 for 循环创建了饼图,因此它们都具有相同的属性但饼图不同在大小。这可能是什么原因。这是我的 div 的代码。

var div = document.createElement("div");
div.id = "container"+i;
div.style.cssFloat = "left";
div.style.width="25%"
document.getElementById("main").appendChild(div);

这是我图表的代码

 for(i=0;i<res.length;i++)

        {
    Highcharts.chart('container'+String(i), {
    chart: {
        plotBackgroundColor: null,
        plotBorderWidth: null,
        plotShadow: false,
        type: 'pie'
    },
    title: {
        text: server[i]
    },
    tooltip: {
        pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
    },
    plotOptions: {
        pie: {
            allowPointSelect: true,
            cursor: 'pointer',
            dataLabels: {
                enabled: true,
                format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                style: {
                    color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                }
            }
        }
    },
    series: [{

          name: 'Success',
          data: res[i]
}]
});

}

标签: javascripthighcharts

解决方案


默认情况下,饼图大小会自动调整,以便数据标签有足够的空间进行渲染。这种行为可以通过设置来改变plotOptions.pie.size- 饼图相对于绘图区域的直径。

代码:

  plotOptions: {
    pie: {
      size: '50%' // Can be a percentage or pixel value
    }
  }

演示:

API参考:


推荐阅读