简体   繁体   English

Javascript中的嵌套/多级/下钻饼图?

[英]Nested/multi-level/drill-down Pie Chart in Javascript?

I'm trying to create a pie chart with javascript that will allow users to click on a slice and "view what makes up that slice." 我正在尝试使用javascript创建一个饼图,允许用户点击切片并“查看构成该切片的内容”。 If you've ever used mint.com you'll know what I mean - say you're viewing an expenses pie chart and you click the "Automobile" slice, then you see the slice expand into a new chart of Gas, Maintenance, etc. 如果您曾经使用过mint.com,您就会明白我的意思 - 假设您正在查看费用饼图并单击“汽车”切片,然后您会看到切片扩展为新的气体,维护,等等

To take this a step further, I'm dealing with a large amount of data, so being able to fetch (ajax) the new data when the slice is clicked would be a useful option as well (though I can probably get away without it). 为了更进一步,我正在处理大量数据,因此当单击切片时能够获取(ajax)新数据也是一个有用的选项(尽管如果没有它我可能会离开) )。

Perhaps "nested", "multi-level" and "drill-down" are not the right terms because I've been searching all day and cannot seem to find a solution. 也许“嵌套”,“多层次”和“向下钻取”不是正确的术语,因为我一整天都在搜索,似乎无法找到解决方案。

Does anyone know of a library for this? 有没有人知道这个库? Thanks in advance! 提前致谢!

I've implemented similar drilldown systems using the HighCharts point click event. 我使用HighCharts点击事件实现了类似的向下钻取系统。 Here's the rough syntax: 这是粗略的语法:

var chart = new Highcharts.Chart({
    chart: {
        renderTo: 'container',
        type: 'pie'
    },
    series: [{
        data: myInitialDataArray, // make sure each data point has an id
        point: {
            events: {
                click: function () {
                    $.post('/get/data/by/id/' + this.id, function(data) {
                        // you may need to format your data here
                        chart.series[0].setData(data);
                    });
                }
            }
        }
    }]
});

In this example, you define a click event that uses the point's id value (this.id) to perform an Ajax post to a URL. 在此示例中,您定义了一个click事件,该事件使用point的id值(this.id)来执行URL的Ajax发布。 You then use the data from your post to re-bind the chart series. 然后,您可以使用帖子中的数据重新绑定图表系列。

Please note that each time you use the setData function to update the chart, each data point needs to have an id value in order for the drilldown to continue. 请注意,每次使用setData函数更新图表时,每个数据点都需要具有id值才能继续进行深入分析。

Hope this helps! 希望这可以帮助!

Try psd3 pie chart library 试试psd3饼图库
Demo - https://pshivale.github.io/psd3 演示 - https://pshivale.github.io/psd3
Source - https://github.com/pshivale/psd3 来源 - https://github.com/pshivale/psd3
It supports multi-level pie charts, donut charts and sunburst charts. 它支持多级饼图,圆环图和旭日形图。 It also supports drilling down a pie slice by double clicking it. 它还支持通过双击向下钻取饼图。

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

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