如何在highcharts中使用javascript重新排列数组元素

时间:2014-01-29 13:08:57

标签: javascript arrays highcharts

我有chart in jsfiddle

此外,这是代码:

$(function () {

    retailerNameArray = ['a', 'b', 'c', 'a', 'b', 'a', 'b', 'a' ],
        clicksArray = [43, 48, 17, 63, 43, 47, 27, 31],
        series = [];

    series = generateData(retailerNameArray, clicksArray);

    function generateData(names, points) {
        var ret = {},
            ps = [],
            series = [],
            len = points.length;

        //concat to get points
        for (var i = 0; i < len; i++) {
            ps[i] = {
                y: points[i],
                n: names[i]
            };
        }
        names = [];
        //generate series and split points
        for (i = 0; i < len; i++) {
            var p = ps[i],
                sIndex = $.inArray(p.n, names);

            if (sIndex < 0) {
                sIndex = names.push(p.n) - 1;
                  series.push({
                    name: p.n,
                    data: []
                });
            }
            series[sIndex].data.push(p);
        }
        return series;
    } 
    window.chart = new Highcharts.Chart({

        chart: {
            renderTo: 'container',
            polar: true,
            type: 'column'
        },

        title: {
            text: 'Highcharts Polar Chart'
        },

         pane: {
                            startAngle: 0,
                            endAngle: 360,
                            size: '85%'
                        },
                        legend: {
                            reversed: true,
                            enabled: false,
                            align: 'right',
                            verticalAlign: 'top',
                            y: 100,
                            layout: 'vertical'
                        },
                        xAxis: {
                            type: "",
                            categories: ['N', 'S', 'E', 'W'],           
                            labels: {
                                formatter: function () {
                                    return this.value + '°';
                                }
                            }
                        },

        yAxis: {
           min: 0
        },

        plotOptions: {
            series: {
                //pointStart: 0,
                //pointInterval: 45
              //  pointPlacement: 'between'
            },
            column: {
                pointPadding: 0,
                groupPadding: 0
            }
        },

        series: series
    });
});

如何显示:N范围内2个数据,S范围内3个数据,E范围内1个数据,W范围内2个数据。

提前致谢!

1 个答案:

答案 0 :(得分:1)

你需要感觉缺少空缺点,例如N缺少a,然后把它放在空。请参阅:http://jsfiddle.net/JaTjk/58/

您案例的示例数据:

    retailerNameArray = ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c' ],
    clicksArray = [null, 48, 17, 63, 43, 47, 27, null, null, 31, 32, null],
    series = [];