Search code examples
highchartstreemapsubtitledrilldown

Dynamically changing subtitles at various drilldowns in treemap in highcharts?


I have 5 levels of drilldown in my treemap, and I want to change the subtitle at every drilldown. I came across this code:

chart.setTitle({ text: drilldownTitle + e.point.name });

which works well for column charts, but I in treemap i have stored data in array format and there is no drilldown id, how can I achieve this functionality for treemap.

     $('#container').highcharts({
            series: [{
                type: "treemap",
                layoutAlgorithm: 'squarified'
}],

 subtitle: {
            text: 'Click points to drill down </a>.'
        },
        title: {
            text: 'TreeMap Sample'
        }
}],

Solution

  • Unfortunately Highcharts doesn't offer events like drillupToNode & drilldownToNode that would ease manipulating the chart's title.

    Workaroud:

    I add title-handling logic to drillToNode function and overwrite it:

    H.seriesTypes.treemap.prototype.drillToNode = function(id, redraw) {
      var series = this,
        chart = series.chart,
        nodeMap = series.nodeMap,
        node = nodeMap[id],
        title;
      series.idPreviousRoot = series.rootNode;
      series.rootNode = id;
    
      if (id === '') {
        series.drillUpButton = series.drillUpButton.destroy();
        title = chart.userOptions.title.text;
      } else {
        series.showDrillUpButton((node && node.name || id));
        title = node.name;
      }
    
      chart.setTitle({
        text: title
      }, null, false);
    
      this.isDirty = true; // Force redraw
      if (pick(redraw, true)) {
        this.chart.redraw();
      }
    }
    

    Original function can be found in this link: https://github.com/highcharts/highcharts/blob/master/js/modules/treemap.src.js

    point.events.click in no longer required while using this code, but chart needs to have the chart.title.text property defined initially.

    Live demo: https://jsfiddle.net/BlackLabel/7toez0n5/