0

So, I have been trying to use drill down to multiple levels, problem I am facing is that I couldn't drill down to the third level because the data will be fetched by ajax upon second drilldown's selection.

for example, refer to this link:

https://codepen.io/ajaymalhotra15/pen/aZpxXq

drilldown example

Here, the third level is possible because he has the data already, but mine will be depended on seconds selection.

So, how to make this happen, where am I supposed to call the ajax request and set the drill down series data dynamically?

EDIT:

Highcharts.chart("energy_chart", {
            chart: {
              type: "column",
              spacingBottom: 15,
              spacingTop: 10,
              spacingLeft: 10,
              spacingRight: 10,
              backgroundColor: "#f2f2f2",
              events: {
                load: function() {
                  var fin = new Date();
                  var finDate = fin.getDate();

                  var finMonth = fin.getMonth();
                  var finYear = fin.getFullYear();

                  var ini = new Date();
                  ini.setFullYear(ini.getFullYear() - 1);
                  var iniDate = ini.getDate();
                  var iniMonth = ini.getMonth();
                  var iniYear = ini.getFullYear();
                  if (this.yAxis[0].dataMax == 0) {
                    this.yAxis[0].setExtremes(null, 1);
                  }
                  //this.yAxis.set

                  this.xAxis[0].setExtremes(
                    Date.UTC(iniYear, iniMonth, iniDate),
                    Date.UTC(finYear, finMonth, finDate)
                  );
                },

                drilldown: function(e) {
                  var charts_this = this;
                  var inidrillDate = new Date(e.point.x);
                  setTimeout(function() {
                    inidrillDate.setDate(0);
                    inidrillDate.setMonth(inidrillDate.getMonth());
                    var DateinidrillDate = inidrillDate.getDate();
                    var MonthinidrillDate = inidrillDate.getMonth();
                    var YearinidrillDate = inidrillDate.getFullYear();
                    var findrillDate = inidrillDate;
                    findrillDate.setMonth(findrillDate.getMonth() + 1);
                    findrillDate.setDate(findrillDate.getDate() - 1);
                    var DatefindrillDate = findrillDate.getDate();
                    var MonthfindrillDate = findrillDate.getMonth();
                    var YearfindrillDate = findrillDate.getFullYear();

                    charts_this.xAxis[0].setExtremes(
                      Date.UTC(
                        YearinidrillDate,
                        MonthinidrillDate,
                        DateinidrillDate
                      ),
                      Date.UTC(
                        YearfindrillDate,
                        MonthfindrillDate,
                        DatefindrillDate
                      )
                    );

                    if (charts_this.yAxis[0].dataMax === 0) {
                      charts_this.yAxis[0].setExtremes(null, 1);
                    }
                  }, 0);

                }
              }
            },
            title: {
              text: '<p className="energy_gen">Energy Generated</p>'
            },
            exporting: { enabled: false },
            xAxis: {
              type: "datetime",
              labels: {
                step: 1
              },
              dateTimeLabelFormats: {
                day: "%e"
              }
            },
            yAxis: {
              title: {
                text: "kWh"
              }
            },
            credits: {
              enabled: false
            },
            plotOptions: {
              series: {
                cursor: "pointer",
                dataLabels: {
                  enabled: true,
                  format: "{point.y}"
                },
                color: "#fcd562",
                point:{
                  events:{
                    click:function(event){
                     if(this.options!=null){
                        var dayOfYear=new Date(this.x).getFullYear() +"-"+(new Date(this.x).getMonth()+1)+"-"+new Date(this.x).getDate();
                      var formatted_date = new Date(this.x).getDate() + " " + months[(new Date(this.x).getMonth())] +" "+ new Date(this.x).getFullYear();
                      // document.getElementById('chart_date_id').innerHTML = formatted_date;       //setting modal title with current date
                        $('#container').bind('mousemove touchmove touchstart', function (e) {

                          var chart,
                          point,
                          i,
                          event;
                          var sync_charts = $('.chart');
                          for (i = 0; i < sync_charts.length; i = i + 1) {

                            var chart_1 = sync_charts[i];
                            var chart_2 = chart_1.getAttribute('data-highcharts-chart');
                            chart=Highcharts.charts[chart_2];
                            event = chart.pointer.normalize(e.originalEvent);
                            point = chart.series[0].searchPoint(event, true);

                            if (point) {
                              point.highlight(e);
                            }
                          }
                        });
                        Highcharts.Pointer.prototype.reset = function () {

                          return undefined;
                        };
                        Highcharts.Point.prototype.highlight = function (event) {

                          event = this.series.chart.pointer.normalize(event);
                          this.onMouseOver(); // Show the hover marker
                          this.series.chart.tooltip.refresh(this); // Show the tooltip
                          this.series.chart.xAxis[0].drawCrosshair(event, this); // Show the crosshair
                        };
                        function syncExtremes(e) {

                          var thisChart = this.chart;

                          if (e.trigger !== 'syncExtremes') { // Prevent feedback loop
                            Highcharts.each(Highcharts.charts, function (chart) {
                              if (chart !== thisChart) {
                                if (chart.xAxis[0].setExtremes) { // It is null while updating
                                  chart.xAxis[0].setExtremes(
                                      e.min,
                                      e.max,
                                      undefined,
                                      false,
                                      { trigger: 'syncExtremes' }
                                  );
                                }
                              }
                            });
                          }
                        }
                      axios({
                        url: config.fvcstat,
                        method: "POST",
                        data: {
                          "customerId":self.props.location.state.detail.customerId,"rmsVendorId":self.props.location.state.detail.rmsVendorId,
                          "date":dayOfYear,
                          "powerType":self.props.location.state.detail.powerType
                        },
                        headers: {
                          "Content-Type": "application/json"
                        }
                      }).then((res)=>{
                        let activity = fvc.data;
                        if($('.chart')){
                          $('.chart').remove();
                        }
                        $.each(activity.datasets, function (i, dataset) {
                          console.log(1)
                          var chartDiv = document.createElement('div');
                          chartDiv.className = 'chart';
                          document.getElementById('container').appendChild(chartDiv);
                          Highcharts.chart(chartDiv,{
                            chart: {

                            },
                            plotOptions: {
                              series: {
                                marker:{
                                  enabled:false
                                }
                              }
                            },
                            exporting: { enabled: false },
                            title: {
                              text: dataset.name,
                              align: 'left',
                              margin: 0,
                              x: 30
                            },
                            credits: {
                              enabled: false
                            },
                            legend: {
                              enabled: false
                            },
                            xAxis: {
                              crosshair:{ width: 3},
                              events: {
                                setExtremes: syncExtremes
                              },
                              labels: {
                                format: '{value}'
                              },categories: activity.xData
                            },
                            yAxis: {
                              title: {
                                text: null
                              }
                            },
                            series: [{
                              data: dataset
                            }],
                            tooltip: {
                              positioner: function () {
                                return {
                                  x: this.chart.chartWidth - this.label.width,
                                  y: 10 // align to title
                                };
                              },
                              borderWidth: 0,
                              backgroundColor: 'none',
                              pointFormat: '{point.y}',
                              headerFormat: '',
                              shadow: false,
                              style: {
                                fontSize: '18px'
                              },
                              valueDecimals: dataset.valueDecimals
                            },
                            series: [{
                              data: dataset.data,
                              name: dataset.name,
                              type: dataset.type,
                              color: Highcharts.getOptions().colors[i],
                              fillOpacity: 0.3,
                              tooltip: {
                                valueSuffix: ' ' + dataset.unit
                              }
                            }]
                          });
                        });

                      })
                      }     
                    }
                  }
                }
              }
            },
            tooltip: {
              formatter: function() {
                if (this.point.options.drilldown) {
                  return (
                    "Energy generated: <b> " +
                    this.y +
                    "</b> kWh " +
                    "<br>" +
                    Highcharts.dateFormat("%b %Y", new Date(this.x))
                  );
                } else {
                  return (
                    "Energy generated: <b> " +
                    this.y +
                    "</b> kWh " +
                    "<br>" +
                    Highcharts.dateFormat("%e %b %Y", new Date(this.x))
                  );
                }
              }
            },
            series: [{'data':obj.data,'name':obj.name,"color":"#4848d3"}],
            drilldown: {
              series: obj.data
            }
          });

So, here if you notice in plotoptions i am trying to create a whole new chart which is a synced line charts showing frquency, voltage and current.

But, i am guessing my approach is not correct as i am plotting a new highchart. So, how do i make this synced line chart part of my drilldown.

let me know if you require any help in understanding.

I will suggest first minimize the plotoption. Then expand for further fuck up :P

Thanks.

Gaurav Kumar
  • 1,111
  • 6
  • 25
  • 52

1 Answers1

1

You can put all your logic to get the third level data and to create a drilldown series in drilldown event:

chart: {
    type: 'column',
    events: {
        drilldown: function(e) {
            if (!thirdLevel.length) {
                // get data
            }
            if (!e.seriesOptions) {
                var chart = this,
                    drilldowns = {
                        'Animals': {
                            name: 'Animals',
                            data: [
                                ['Cows', 2],
                                ['Sheep', 3]
                            ]
                        },
                        'Fruits': {
                            name: 'Fruits',
                            data: [
                                ['Apples', 5],
                                ['Oranges', 7],
                                ['Bananas', 2]
                            ]
                        },
                        'Cars': {
                            name: 'Cars',
                            data: [
                                ['Toyota', 1],
                                ['Volkswagen', 2],
                                ['Opel', 5]
                            ]
                        }
                    },
                    series = drilldowns[e.point.name];

                chart.addSingleSeriesAsDrilldown(e.point, series);
                chart.applyDrilldown();
            }
        }
    }
}

Live demo : http://jsfiddle.net/BlackLabel/86v3L4ft/

API Reference: https://api.highcharts.com/highcharts/chart.events.drilldown

ppotaczek
  • 36,341
  • 2
  • 14
  • 24
  • So, i added my code for better explaning. your approach i cant understand how i will fit in – Gaurav Kumar Feb 02 '19 at 10:31
  • @Gaurav Kumar, Please analyze this example: http://jsfiddle.net/BlackLabel/8x6njk09/, I simply presented how you can get data for the third drilldown level in `drilldown` event. If you still have a problem with it, please provide me with a full working example on jsfiddle. – ppotaczek Feb 04 '19 at 17:06