Highcharts将向下钻取(三层)重置为初始状态

时间:2013-09-18 01:03:02

标签: c# javascript jquery highcharts

我的饼图有三重钻取。我正在尝试创建一个按钮,允许您将整个图表重置为原始的向下钻取状态。我看了一个没有深入分析的旧实现,无法使用我的解决方案(请参阅此处Resetting Highcharts to initial state)。

我当前的JSFiddle有我尝试使用该线程复制的内容。 你可以在这里看到:http://jsfiddle.net/vsFLK/

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script type="text/javascript" src="http://code.highcharts.com/highcharts.js"></script>

<div id="pieContainer"></div>
<br/>
<a id="resetChart" href="#">Reset</a>

         var chart;
  $(document).ready(function () {
      /*begin pie chart render*/
      var colors = Highcharts.getOptions().colors,
          categories = ['A', 'B', 'C'],
          level = 0,
          //name = 'Regions',
          data = [{
              name: 'B',
              y: 10,
              color: colors[0],
              drilldown: {
                  level: 1,
                  name: 'A',
                  color: colors[0],
                  data: [{
                      y: 7,
                      name: 'A',
                      drilldown: {
                          level: 2,
                          data: [
                              ['1', 100],
                              ['2', 200],
                              ['3', 300],
                              ['4', 400],
                              ['5', 500],
                              ['6', 600],
                              ['7', 700]
                          ]
                      }
                  }, {
                      y: 3,
                      name: 'B',
                      drilldown: {
                          level: 2,
                          data: [
                              ['1', 100],
                              ['2', 200],
                              ['3', 300]
                          ]
                      }
                  }]
              }
          }, {
              name: 'C',
              y: 34,
              color: colors[1],
              drilldown: {
                  level: 1,
                  name: 'C',
                  color: colors[1],
                  data: [{
                      y: 1,
                      name: 'A',
                      drilldown: {
                          level: 2,
                          data: [
                              ['1', 100]
                          ]
                      }
                  }, {
                      y: 7,
                      name: 'B',
                      drilldown: {
                          level: 2,
                          data: [
                              ['1', 100],
                              ['2', 200],
                              ['3', 300],
                              ['4', 400],
                              ['5', 500],
                              ['6', 600],
                              ['7', 700]
                          ]
                      }
                  }, {
                      y: 9,
                      name: 'C',
                      drilldown: {
                          level: 2,
                          data: [
                              ['1', 100],
                              ['2', 200],
                              ['3', 300],
                              ['4', 400],
                              ['5', 500],
                              ['6', 600],
                              ['7', 700]
                          ]
                      }
                  }, {
                      y: 3,
                      name: 'D',
                      drilldown: {
                          level: 2,
                          data: [
                              ['1', 100],
                              ['2', 200],
                              ['3', 300]
                          ]
                      }
                  }, {
                      y: 3,
                      name: 'E',
                      drilldown: {
                          level: 2,
                          data: [
                              ['1', 100],
                              ['2', 200],
                              ['3', 300], ]
                      }
                  }, {
                      y: 2,
                      name: 'F',
                      drilldown: {
                          level: 2,
                          data: [
                              ['1', 100],
                              ['2', 200],
                              ['3', 300], ]
                      }
                  }, {
                      y: 2,
                      name: 'G',
                      drilldown: {
                          level: 2,
                          data: [
                              ['1', 300],
                              ['2', 200],
                              ['3', 100]
                          ]
                      }
                  }, {
                      y: 3,
                      name: 'H',
                      drilldown: {
                          level: 2,
                          data: [
                              ['1', 300],
                              ['2', 200],
                              ['3', 100]
                          ]
                      }
                  }, {
                      y: 4,
                      name: 'I',
                      drilldown: {
                          level: 2,
                          data: [
                              ['1', 300],
                              ['2', 200],
                              ['3', 100]
                          ]
                      }
                  }]
              }
          }, {
              name: 'A',
              y: 50,
              color: colors[2],
              drilldown: {
                  level: 1,
                  name: 'A',
                  color: colors[2],
                  data: [{
                      y: 2,
                      name: 'A',
                      drilldown: {
                          level: 2,
                          data: [
                              ['1', 100],
                              ['2', 200]
                          ]
                      }
                  }, {
                      y: 1,
                      name: 'C',
                      drilldown: {
                          level: 2,
                          data: [
                              ['1', 100]
                          ]
                      }
                  }, {
                      y: 53,
                      name: 'A',
                      drilldown: {
                          level: 2,
                          data: [
                              ['1', 100],
                              ['2', 200],
                              ['3', 300],
                              ['4', 400],
                              ['5', 500],
                              ['6', 600],
                              ['7', 700]
                          ]
                      }
                  }]
              }
          }]

          function setChart(name, categories, data, color) {
              //chart.xAxis[0].setCategories(categories);
              chart.series[0].remove();
              chart.addSeries({
                  name: name,
                  data: data,
                  pointPadding: -0.3,
                  borderWidth: 0,
                  pointWidth: 15,
                  shadow: false,
                  color: color || 'white'
              });
          }

      pieChart = new Highcharts.Chart({
          chart: {
              renderTo: 'pieContainer',
              type: 'pie',
              /* changes bar size */
              pointPadding: -0.3,
              borderWidth: 0,
              pointWidth: 10,
              shadow: false,
              backgroundColor: '#ffffff'
          },
          title: {
              text: ''
          },
          xAxis: {
              categories: categories
          },
          yAxis: {
              title: {
                  text: 'Total Brand Value',
                  categories: categories
              }
          },
          //drilldown plot
          plotOptions: {
              pie: {
                  cursor: 'pointer',
                  allowPointSelect: false,
                  pointPadding: -0.3,
                  borderWidth: 0,
                  pointWidth: 15,
                  shadow: false,
                  point: {
                      events: {
                          click: function () {
                              var chart = this.series.chart,
                                  drilldown = this.drilldown;
                              if (drilldown) { // drill down
                                  chart.setChart(drilldown.name, drilldown.categories, drilldown.data, drilldown.color, drilldown.level);

                              } else { // restore
                                  chart.setChart(name, categories, data, null, level);

                              }
                          }
                      }
                  },
                  dataLabels: {
                      enabled: true,
                      color: '#000',
                      //label colors
                      connectorColor: '#000',
                      // connector label colors
                      formatter: function () {
                          return '<b>' + this.point.name + '</b>: ' + Highcharts.numberFormat(this.y, 0);

                      }
                  }
              }
          },
          //formatting over hover tooltip

          tooltip: {
              backgroundColor: '#383838',
              formatter: function () {
                  var point = this.point,
                      s = '';

                  switch (this.series.options.level) {
                      case 0:
                          s = point.name + ':<b>' + Highcharts.numberFormat(this.y, 0) + ' brands</b><br/>';
                          s += 'Click to view drilldown 1';
                          break;

                      case 1:
                          s = point.name + ':<b>' + Highcharts.numberFormat(this.y, 0) + ' brands</b><br/>';
                          s += 'Click to drilldown 2';
                          break;

                      case 2:
                          s = point.name + ':<b>' + Highcharts.numberFormat(this.y, 0) + ' ($m) brand value</b><br/>';
                          s += 'Click to drilldown 3';
                          break;
                  }


                  return s;
              }
          },
          credits: {
              enabled: false
          },
          series: [{
              name: name,
              data: data,
              level: level,
              /* changes bar size */
              shadow: false,
              color: 'black' //Sectors icon
          }],
          exporting: {
              enabled: false
          }
      },
      /*Remember to rename chart functions for pie chart so it is unique from bar*/
      function (chart) {
          chart.upper = [];

          var up = false;

          chart.setChart = function (name, categories, data, color, level) {
              //chart.xAxis[0].setCategories(categories);
              if (name === true && chart.upper.length) {

              }

              if (up === false) {
                  //chart.toolbar.add('up', 'Level up', 'Level up', function(){ chart.setChart(true); });
                  up = true;
              }

              chart.upper.push(chart.series[0].options);
              chart.series[0].remove();
              chart.addSeries({
                  name: name,
                  data: data,
                  level: level,
                  color: color || 'white'
              });




          }



      })



  });

  function drawDefaultChart() {
      pieChart = new Highcharts.Chart();
  }

  $(function () {

      $(document).ready(function () {

          drawDefaultChart();

      });
      $('#resetChart').on("click", function (e) {
          e.preventDefault();
          pieChart = new Highcharts.Chart();
      });


  })

无论如何,我可以让一个按钮将整个图表重置为最初的向下钻取,无论你在哪个级别?

2 个答案:

答案 0 :(得分:1)

首先,为什么要创建两次图表?删除额外代码后,您可以使用下面的简单chart.setChart()功能评论:// restore - 这意味着完全按照您的需要恢复图表。

参见示例:http://jsfiddle.net/vsFLK/2/

答案 1 :(得分:0)

当前,您可以重复使用drillUp方法:

document.getElementById('drillUp').addEventListener('click', function(){
  chart.drillUp();

  if(chart.series[0].name !== "someName"){
    chart.drillUp();
  }
});

实时演示: https://jsfiddle.net/BlackLabel/0ju31xb8/

API参考: https://api.highcharts.com/class-reference/Highcharts.Chart#drillUp