【问题标题】:Customizing the series data in pie charts with a dynamic json series (HighCharts)使用动态 json 系列 (HighCharts) 自定义饼图中的系列数据
【发布时间】:2014-03-20 16:05:21
【问题描述】:

我对 Highcharts 还很陌生。我有一个使用动态数据 (JSON String) 可以很好地呈现的饼图。但我想将某些切片自定义为 Sliced: true 和 Selected: True。我不知道该怎么做。请在下面找到我的代码,

<div id="container_${page_id}" style="min-width: 310px; height: 400px; margin: 0 auto;"></div>




var series = [];
var total = 0;
$.each(data, function(index, value) {
            series.push([value.statisticData+'_'+value.sizes,parseInt(value.sizes)]);
            console.log("statisticData",value.statisticData);
            console.log("value.sizes",value.sizes);
            total += parseInt(value.count);
        });


        var chartSessionStatus = new Highcharts.Chart({
            chart: {
                renderTo: $("#container_"+testSessionPageId).get(0),                        
                plotBackgroundColor: null,
                plotBorderWidth: null,
                plotShadow: false
            },
            exporting: {
                enabled: true,
                buttons: {
                    contextButton: {
                        menuItems: [{
                            text: 'Statistics',
                        }]
                    }}
                },

            tooltip: {
                formatter: function() {
                    return this.point.name.split('_')[0] + ': <b>'+this.y+'</b>';
                }
            },
            title: {
                text: '<h4 style="font-style: bold">Tenant Statistics</h4>'
            },
            legend: {
                    labelFormatter: function() {
                        return this.name.split('_')[0] + ' (' + this.y+')';
               }
            },

            plotOptions: {
                pie: {
                    allowPointSelect: true,
                    cursor: 'pointer',
                    dataLabels: {
                        enabled: true,
                        distance: 0,
                        useHTML: true,
                        formatter: function() {
                            if( this.y > 0)
                                return '<b>'+this.point.name.split('_')[0] +'('+ this.y +')</b>';
                        }
                    },
                    showInLegend: true,
                    size: 150
                }
            },
            series: [{
                type: 'pie',
                name: 'Tenant Statistics',
                data: series
            }]
        });

    });

主要关注点,在系列中,我可以选择数据为“数据:系列”,但我想自定义本系列中的某些切片。但由于数据是动态的,我不确定如何仅使用 json 字符串的某些部分 我收到的 Json 格式如下:

[{"statisticData":"创建的测试","sizes":"3"},{"statisticData":"Students","sizes":"2"},{"statisticData":"Users", "sizes":"7"},{"statisticData":"Schools","sizes":"10"}]

非常感谢任何帮助!提前致谢。

【问题讨论】:

    标签: highcharts pie-chart


    【解决方案1】:

    得到json后,可以解析/添加切片参数,然后在highcharts中使用。所以不要推:series.push([value.statisticData+'_'+value.sizes,parseInt(value.sizes)]);

    你可以推送类似的东西:

    series.push({
            name: value.statisticData+'_'+value.sizes,
            y: parseInt(value.sizes),
            sliced: true,
            selected: true
    });
    

    【讨论】:

    • 感谢塞巴斯蒂安的回复。这确实有效,但它使所有切片都为真,我只想要切片或选择的切片中的 1 个(饼图值)。有没有办法做到这一点?再次感谢您的帮助。
    • 所以添加任何条件,这将“决定”是否应该切片。一切由你决定。
    • 谢谢塞巴斯蒂安,你能给我一个如何在其中添加条件的例子吗?正如我所说,我对此很陌生
    • 但是应该切哪些切片应该切哪些呢?你怎么定义它?只有你需要的是使用 IF。
    • 我尝试使用 if 条件,但出现错误,请在上面找到我的代码 sn-p 作为第二个答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多