Highcharts - 柱形图可以深入到条形图吗?

时间:2014-07-26 15:29:05

标签: highcharts

我有一个项目,主图表中的数据最好用柱形图表示,深入分析中的数据应该是条形图。

但是,当我尝试向下钻取时,即使类型设置为bar,下钻仍然是柱形图。

这可以与提供的Highcharts明细API一起使用吗?我想拥有“后退”按钮并支持多个下钻级别。

以下是演示此问题的基本示例:

$(function () {    

// Create the chart
$('#container').highcharts({
    chart: {
        type: 'column'
    },
    title: {
        text: 'Basic drilldown'
    },
    xAxis: {
        type: 'category'
    },

    legend: {
        enabled: false
    },

    plotOptions: {
        series: {
            borderWidth: 0,
            dataLabels: {
                enabled: true,
            }
        }
    },

    series: [{
        name: 'Things',
        colorByPoint: true,
        data: [{
            name: 'Animals',
            y: 5,
            drilldown: 'animals'
        }, {
            name: 'Fruits',
            y: 2,
            drilldown: 'fruits'
        }, {
            name: 'Cars',
            y: 4,
            drilldown: 'cars'
        }]
    }],
    drilldown: {
        series: [{
            id: 'animals',
            type: 'bar',
            data: [
                ['Cats', 4],
                ['Dogs', 2],
                ['Cows', 1],
                ['Sheep', 2],
                ['Pigs', 1]
            ]
        }, {
            id: 'fruits',
            type: 'bar',
            data: [
                ['Apples', 4],
                ['Oranges', 2]
            ]
        }, {
            id: 'cars',
            type: 'bar',
            data: [
                ['Toyota', 4],
                ['Opel', 2],
                ['Volkswagen', 2]
            ]
        }]
    }
})
});

这是一个JSFIDDLE

http://jsfiddle.net/N2g98/

编辑:这似乎可以被认为是Highcharts中的一个错误,因为它更多的是一个问题,而不仅仅是从列向下钻取到条形图。如果您从条形图开始并向下钻取到折线图,则会在深入分析中弄乱轴。

EDIT2:这是我最终解决的问题

 $(function () {

 (function (H) {

     H.wrap(H.Point.prototype, 'init', function (proceed, series, options, x) {
         var point = proceed.call(this, series, options, x),
             chart = series.chart,
             tick = series.xAxis && series.xAxis.ticks[x],
             tickLabel = tick && tick.label;

         if (point.drilldown) {

             if (tickLabel) {
                 if (!tickLabel._basicStyle) {
                     tickLabel._basicStyle = tickLabel.element.getAttribute('style');
                 }
                 tickLabel.addClass('highcharts-drilldown-axis-label').css({
                     'text-decoration': 'none',
                         'font-weight': 'normal',
                         'cursor': 'auto'
                 }).on('click', function () {
                     if (point.doDrilldown) {
                         return false;
                     }
                 });
             }
         }


         return point;
     });

     H.wrap(H.Pointer.prototype, 'setDOMEvents', function (proceed) {
         proceed.call(this);
         var pointer = this,
             container = pointer.chart.container;
         container.onclick = function (e) {
             if (typeof pointer.onContainerClick === 'function') {
                 pointer.onContainerClick(e);
             }
         };
     });

     H.wrap(H.Chart.prototype, 'drillUp', function (proceed) {
         if (!this.customDrilldown) {
             proceed.call(this);
         } else {
             var userOptions = this.userOptions;
             var drilldownLevels = this.drilldownLevels;
             var level = drilldownLevels.pop();
             var newChartConfig = level.userOptions;

             this.destroy();
             $(userOptions.chart.renderTo).highcharts(newChartConfig);
             var newChart = $(userOptions.chart.renderTo).highcharts();
             newChart.drilldownLevels = drilldownLevels;
             if (drilldownLevels.length !== 0) {
                 newChart.showDrillUpButton();
             }
             newChart.customDrilldown = true;

             HighchartsAdapter.fireEvent(newChart, 'drillup', {
                 seriesOptions: newChartConfig
             });
         }

     });



     H.wrap(H.Point.prototype, 'doDrilldown', function (proceed, _holdRedraw) {

         if (!$.isPlainObject(this.drilldown)) {
             proceed.call(this, _holdRedraw);
         } else {
             var newChartConfig = this.drilldown;
             var oldChart = this.series.chart;
             var userOptions = oldChart.userOptions;
             var drilldownLevels = oldChart.drilldownLevels;
             if (!drilldownLevels) {
                 drilldownLevels = [];
             }

             var oldSeries = this.series;
             var level = {
                 userOptions: userOptions,
                 seriesOptions: oldSeries.userOptions
             };


             drilldownLevels.push(level);

             oldChart.destroy();
             $(userOptions.chart.renderTo).highcharts(newChartConfig);
             var newChart = $(userOptions.chart.renderTo).highcharts();
             newChart.drilldownLevels = drilldownLevels;
             newChart.showDrillUpButton();
             newChart.customDrilldown = true;

             HighchartsAdapter.fireEvent(newChart, 'drilldown', {
                 point: this,
                 seriesOptions: newChartConfig
             });

         }

     });
 }(Highcharts));


 var pieDD1 = {
     chart: {
         type: 'pie'
     },
     title: {
         text: 'Basic drilldown'
     },
     subtitle: {
         text: 'colors'
     },
     series: [{
         name: 'Color',
         data: [{
             name: 'Red',
             y: 5
         }, {
             name: 'Blue',
             y: 25
         }]
     }]

 };

      var pieDD2 = {
     chart: {
         type: 'pie'
     },
     title: {
         text: 'Basic drilldown'
     },
     subtitle: {
         text: 'colors'
     },
     series: [{
         name: 'Color',
         data: [{
             name: 'Red',
             y: 15
         }, {
             name: 'Blue',
             y: 25
         }]
     }]

 };

      var pieDD3 = {
     chart: {
         type: 'pie'
     },
     title: {
         text: 'Basic drilldown'
     },
     subtitle: {
         text: 'colors'
     },
     series: [{
         name: 'Color',
         data: [{
             name: 'Red',
             y: 3
         }, {
             name: 'Blue',
             y: 5
         }]
     }]

 };

      var pieDD4 = {
     chart: {
         type: 'pie'
     },
     title: {
         text: 'Basic drilldown'
     },
     subtitle: {
         text: 'colors'
     },
     series: [{
         name: 'Color',
         data: [{
             name: 'Red',
             y: 50
         }, {
             name: 'Blue',
             y: 3
         }]
     }]

 };

      var pieDD5 = {
     chart: {
         type: 'pie'
     },
     title: {
         text: 'Basic drilldown'
     },
     subtitle: {
         text: 'colors'
     },
     series: [{
         name: 'Color',
         data: [{
             name: 'Red',
             y: 13
         }, {
             name: 'Blue',
             y: 11
         }]
     }]

 };

      var pieDD6 = {
     chart: {
         type: 'pie'
     },
     title: {
         text: 'Basic drilldown'
     },
     subtitle: {
         text: 'colors'
     },
     series: [{
         name: 'Color',
         data: [{
             name: 'Red',
             y: 50
         }, {
             name: 'Blue',
             y: 25
         }]
     }]

 };

      var pieDD7 = {
     chart: {
         type: 'pie'
     },
     title: {
         text: 'Basic drilldown'
     },
     subtitle: {
         text: 'colors'
     },
     series: [{
         name: 'Color',
         data: [{
             name: 'Red',
             y: 5
         }, {
             name: 'Blue',
             y: 5
         }]
     }]

 };

      var pieDD8 = {
     chart: {
         type: 'pie'
     },
     title: {
         text: 'Basic drilldown'
     },
     subtitle: {
         text: 'colors'
     },
     series: [{
         name: 'Color',
         data: [{
             name: 'Red',
             y: 15
         }, {
             name: 'Blue',
             y: 5
         }]
     }]

 };

      var pieDD9 = {
     chart: {
         type: 'pie'
     },
     title: {
         text: 'Basic drilldown'
     },
     subtitle: {
         text: 'colors'
     },
     series: [{
         name: 'Color',
         data: [{
             name: 'Red',
             y: 4
         }, {
             name: 'Blue',
             y: 8
         }]
     }]

 };

      var pieDD10 = {
     chart: {
         type: 'pie'
     },
     title: {
         text: 'Basic drilldown'
     },
     subtitle: {
         text: 'colors'
     },
     series: [{
         name: 'Color',
         data: [{
             name: 'Red',
             y: 33
         }, {
             name: 'Blue',
             y: 66
         }]
     }]

 };

      var pieDD11 = {
     chart: {
         type: 'pie'
     },
     title: {
         text: 'Basic drilldown'
     },
     subtitle: {
         text: 'colors'
     },
     series: [{
         name: 'Color',
         data: [{
             name: 'Red',
             y: 9
         }, {
             name: 'Blue',
             y: 1
         }]
     }]

 };

      var pieDD12 = {
     chart: {
         type: 'pie'
     },
     title: {
         text: 'Basic drilldown'
     },
     subtitle: {
         text: 'colors'
     },
     series: [{
         name: 'Color',
         data: [{
             name: 'Red',
             y: 8
         }, {
             name: 'Blue',
             y: 13
         }]
     }]

 };

      var pieDD13 = {
     chart: {
         type: 'pie'
     },
     title: {
         text: 'Basic drilldown'
     },
     subtitle: {
         text: 'colors'
     },
     series: [{
         name: 'Color',
         data: [{
             name: 'Red',
             y: 70
         }, {
             name: 'Blue',
             y: 25
         }]
     }]

 };

      var pieDD14 = {
     chart: {
         type: 'pie'
     },
     title: {
         text: 'Basic drilldown'
     },
     subtitle: {
         text: 'colors'
     },
     series: [{
         name: 'Color',
         data: [{
             name: 'Red',
             y: 50
         }, {
             name: 'Blue',
             y: 10
         }]
     }]

 };

      var pieDD15 = {
     chart: {
         type: 'pie'
     },
     title: {
         text: 'Basic drilldown'
     },
     subtitle: {
         text: 'colors'
     },
     series: [{
         name: 'Color',
         data: [{
             name: 'Red',
             y: 8
         }, {
             name: 'Blue',
             y: 5
         }]
     }]

 };

      var pieDD16 = {
     chart: {
         type: 'pie'
     },
     title: {
         text: 'Basic drilldown'
     },
     subtitle: {
         text: 'colors'
     },
     series: [{
         name: 'Color',
         data: [{
             name: 'Red',
             y: 5
         }, {
             name: 'Blue',
             y: 8
         }]
     }]

 };



 var drilldown = {
     chart: {
         type: 'column'
     },
     title: {
         text: 'Basic drilldown'
     },
     subtitle: {
         text: 'subtitle'
     },
     xAxis: {
         type: 'category',
         title: {
             text: 'X Axis'
         }
     },
     yAxis: {
         title: {
             text: 'Y Axis'
         }
     },
     plotOptions: {
         column: {
             stacking: 'normal'
         }
     },
     series: [{
         name: 'Widget A',
         data: [{
             name: 'Qtr 1',
             y: 5,
             drilldown: pieDD1
         }, {
             name: 'Qtr 2',
             y: 25,
             drilldown: pieDD2
         }, {
             name: 'Qtr 3',
             y: 25,
             drilldown: pieDD3
         }, {
             name: 'Qtr 4',
             y: 20,
             drilldown: pieDD4
         }]
     }, {
         name: 'Widget B',
         data: [{
             name: 'Qtr 1',
             y: 25,
             drilldown: pieDD5
         }, {
             name: 'Qtr 2',
             y: 5,
             drilldown: pieDD6
         }, {
             name: 'Qtr 3',
             y: 5,
             drilldown: pieDD7
         }, {
             name: 'Qtr 4',
             y: 15,
             drilldown: pieDD8
         }]
     }]


 };

 var drilldown2 = {
     chart: {
         type: 'column'
     },
     title: {
         text: 'Basic drilldown'
     },
     subtitle: {
         text: 'subtitle2'
     },
     xAxis: {
         type: 'category',
         title: {
             text: 'X Axis'
         }
     },
     yAxis: {
         title: {
             text: 'Y Axis'
         }
     },
     plotOptions: {
         column: {
             stacking: 'normal'
         }
     },
     series: [{
         name: 'Widget A',
         data: [{
             name: 'Qtr 1',
             y: 15,
             drilldown: pieDD9
         }, {
             name: 'Qtr 2',
             y: 15,
             drilldown: pieDD10
         }, {
             name: 'Qtr 3',
             y: 30,
             drilldown: pieDD11
         }, {
             name: 'Qtr 4',
             y: 5,
             drilldown: pieDD12
         }]
     }, {
         name: 'Widget B',
         data: [{
             name: 'Qtr 1',
             y: 5,
             drilldown: pieDD13
         }, {
             name: 'Qtr 2',
             y: 25,
             drilldown: pieDD14
         }, {
             name: 'Qtr 3',
             y: 10,
             drilldown: pieDD15
         }, {
             name: 'Qtr 4',
             y: 20,
             drilldown: pieDD16
         }]
     }]


 };

 // Create the chart
 $('#container').highcharts({
     chart: {
         type: 'bar'
     },
     title: {
         text: 'Basic drilldown'
     },
     xAxis: {
         type: 'category'
     },
     plotOptions: {
         column: {
             stacking: 'normal'
         }
     },
     series: [{
         name: 'Yearly Orders',
         data: [{
             name: 'Location 1',
             y: 100,
             drilldown: drilldown
         }, {
             name: 'Location 2',
             y: 150,
             drilldown: drilldown2
         }]
     }]
 });
 });

http://jsfiddle.net/skTHx/22/

0 个答案:

没有答案