繁体   English   中英

替换 highchart 中的系列时,饼图图例与旧图例重叠

[英]pie chart legend overlap with older legend when replace series in highchart

我正在尝试更新饼图,但问题是它与导致问题的图例重叠。 我删除默认系列并添加新系列然后旧图例不隐藏和新图例重叠。 以下是我的代码:

<script src="http://code.highcharts.com/highcharts.js"></script>
    <div id="container" style="height: 300px"></div>
    <button id="update">Update chart</button>

    $(function () {
        $('#container').highcharts({
chart: {
            animation: {
                duration: 1000
            },
            type:'pie'
        },
    plotOptions: {
              pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                  enabled: false,
                  format: '<b>{point.name}</b>: {point.percentage:.1f} %'
                },
                showInLegend: true
              }
            },
series: [{
            name: 'Brands',colorByPoint: true,
            data: [
              { name: 'Data1', y: 60.40,},
              {name: 'Data2', y: 39.60,},
            ]
          }]
    });

    var i = 1;
    $('#update').click(function () {
        var chart = $('#container').highcharts();
        var newData =[{ name: 'ABC', y: 80 },{ name: 'XYZ',
                        y: 20,}];
 while (chart.series.length > 0)
                chart.series[0].remove(true);

            let series={
                name:'new Load',
                data:newData
                }
             chart.addSeries(
                  series, false
              );
      chart.redraw()

    });
    });

那是Highcharts中的一个regression bug,这里转载: https : //github.com/highcharts/highcharts/issues/12627已经在master分支上修复了。

要解决此问题,请使用 master 分支中的代码或添加此插件:

(function(H) {
    H.wrap(H.Point.prototype, 'destroy', function(proceed) {
        if (this.legendItem) { // pies have legend items
            this.series.chart.legend.destroyItem(this);
        }
        proceed.apply(this, Array.prototype.slice.call(arguments, 1));
    });
}(Highcharts));

暂无
暂无

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

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