多系列的树状图

3
我正在尝试创建具有多个系列的TreeMap,系列将与合并在一起绘制在图形上,但我希望每次只显示一个系列。基本上可以使用图例,但对于TreeMap,它显示值。

这是我正在努力工作的 jsfiddleenter image description here

使用的JavaScript文件
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/heatmap.js"></script>
<script src="https://code.highcharts.com/modules/treemap.js"></script>
<div id="container"></div>

根据所选系列更新矩形树图。
使用Highchart绑定。
Highcharts.chart('container', {
    colorAxis: {
        minColor: '#FFFFFF',
        maxColor: Highcharts.getOptions().colors[0]
    },
    legend: {
        enabled: true,
    },
    series: [{
    name:"Series 1",
        stack: 'aeroplanes',
        type: 'treemap',
        layoutAlgorithm: 'squarified',
        data: [{
            name: 'A -Series 1',
            value: 6,
            colorValue: 1
        }, {
            name: 'B -Series 1',
            value: 6,
            colorValue: 2
        }, {
            name: 'C -Series 1',
            value: 4,
            colorValue: 3
        }, {
            name: 'D -Series 1',
            value: 3,
            colorValue: 4
        }, {
            name: 'E -Series 1',
            value: 2,
            colorValue: 5
        }, {
            name: 'F -Series 1',
            value: 2,
            colorValue: 6
        }, {
            name: 'G -Series 1',
            value: 1,
            colorValue: 7
        }]
    },
    // Second Series    
    {
       name:"Series 2",
        type: 'treemap',
        stack: 'aeroplanes2',
        layoutAlgorithm: 'squarified',
        visible:true,
        data: [{
            name: 'AA -Series 2',
            value: 16,
            colorValue: 2
        }, {
            name: 'BB -Series 2',
            value: 26,
            colorValue: 2
        }, {
            name: 'Cc -Series 2',
            value: 14,
            colorValue: 2
        }, {
            name: 'Dd -Series 2',
            value: 13,
            colorValue: 2
        }, {
            name: 'Ee -Series 2',
            value: 12,
            colorValue: 2
        }, {
            name: 'Ff -Series 2',
            value: 12,
            colorValue: 2
        }, {
            name: 'Gg -Series 2',
            value: 11,
            colorValue: 2
        }]
    }],
    title: {
        text: 'Highcharts Treemap'
    },
      plotOptions: {
      treemap: {
        stacking: 'percent'
      }
    },
});

总之,您想要一个图例,可以切换两个系列的可见性(例如,“Series 1”可见,“Series 2”不可见;单击它们任何一个图例项将导致“Series 1”隐藏并显示“Series 2”)? - Kamil Kulig
@KamilKulig 是的,正如您所理解的那样。 - Rohit Kumar
1个回答

2
默认情况下,treemap系列的showInLegend等于false,需要手动启用。在legendItemClick事件中,我处理了系列可见性的切换:
  plotOptions: {
    treemap: {
      showInLegend: true,
      events: {
        legendItemClick: function() {
          this.chart.series.forEach((s) => s.setVisible());
          return false;
        }
      }
    }
  }

现场演示:http://jsfiddle.net/kkulig/0n49vv74/


API 参考文档:


完美,正是我所需要的。感谢你的帮助。 - Rohit Kumar
我该如何让它看起来像这样,为不同系列设置不同颜色的轴。http://prntscr.com/hym0z7 - Rohit Kumar
我已经成功解决了prntscr.com/hym0z7的问题,现在卡在了coloraxis上。 - Rohit Kumar
Highcharts中每个图表只能有一个颜色轴。 - Kamil Kulig
好的想法,但实际上你的算法是错误的!它只在你有两个系列时起作用。你必须使用s.setVisible(false); return true;才能正确地切换系列。 - Vahid
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接