简体   繁体   English

Highcharts饼图如何减少由图宽度引起的图和图例之间的空间

[英]Highcharts Pie chart how to reduce space between chart and legend caused by plot width

Is there a way to reduce the white space between the chart and the legend, when the legend is positioned to the right of the chart and its layout is set to vertical? 当图例位于图表的右侧并且其布局设置为垂直时,是否可以减少图表和图例之间的空白? It seems like the problem is caused by the width of the plot area, which becomes unnecessarily big in pie charts since the chart is always going to be a circle. 问题似乎是由绘图区域的宽度引起的,在饼图中,该区域不必要地变大,因为该图表将始终是一个圆形。 The chart's width and height cannot be fixed to allow for responsiveness. 图表的宽度和高度不能固定以允许响应。

https://jsfiddle.net/rredondo/gdh86chg/ https://jsfiddle.net/rredondo/gdh86chg/

The chart options are: 图表选项为:

{
  chart: {
    type: 'pie',
    plotBorderWidth: 1,
    plotBorderColor: '#3F4044',
    borderColor: '#AAAAAA',
    borderWidth: 2
  },
  series: [{
    name: 'Incidents',
    data: [{
      name: "Critical",
      y: 1,
      color: "#FF0000"
    }, {
      name: "Severe",
      y: 8,
      color: "#F57622"
    }, {
      name: "Major",
      y: 13,
      color: "#F0A401"
    }, {
      name: "Minor",
      y: 25,
      color: "#F0C801"
    }, {
      name: "Information",
      y: 30,
      color: "#4AB6FF"
    }],
    size: '80%',
    innerSize: '60%',
    showInLegend: true,
    dataLabels: {
      enabled: false
    }
  }],
  legend: {
    layout: "vertical",
    align: "right",
    verticalAlign: "middle",
  }
}

As I have mentioned in my comment, you should be able to move your legend on chart load and redraw using attr(): 正如我在评论中提到的那样,您应该能够在图表加载时移动图例并使用attr()重绘:

http://api.highcharts.com/highcharts/chart.events.load http://api.highcharts.com/highcharts/chart.events.redraw http://api.highcharts.com/highcharts/Element.attr http://api.highcharts.com/highcharts/chart.events.load http://api.highcharts.com/highcharts/chart.events.redraw http://api.highcharts.com/highcharts/Element.attr

    // Create the chart
var updateLegend = function(chart) {
  var center = chart.series[0].center;
  console.log(chart.legend)
  chart.legend.group.attr({
    translateX: center[0] + center[2] / 2
  });
}
var chart = Highcharts.chart('container', {
  chart: {
    type: 'pie',
    plotBorderColor: '#3F4044',
    borderColor: '#AAAAAA',
    borderWidth: 2,
    marginRight: 0,
    marginLeft: 0,
    events: {
      load: function() {
        updateLegend(this)
      },
      redraw: function() {
        updateLegend(this);
      }
    }
  },
  series: [{
    name: 'Incidents',
    data: [{
      name: "Critical",
      y: 1,
      color: "#FF0000"
    }, {
      name: "Severe",
      y: 8,
      color: "#F57622"
    }, {
      name: "Major",
      y: 13,
      color: "#F0A401"
    }, {
      name: "Minor",
      y: 25,
      color: "#F0C801"
    }, {
      name: "Information",
      y: 30,
      color: "#4AB6FF"
    }],
    size: '80%',
    innerSize: '60%',
    showInLegend: true,
    dataLabels: {
      enabled: false
    }
  }],
  legend: {
    layout: "vertical",
    align: "right",
    verticalAlign: "middle",
  }
});

Live example: https://jsfiddle.net/gdh86chg/11/ 实时示例: https//jsfiddle.net/gdh86chg/11/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM