Search code examples
javascriptangulartypescripthighchartssunburst-diagram

Show only three levels of highchart Sunburst always on each click in Angular8


I want to create a sunburst highchart that always show three levels at a time. FOr example it has 5 levels, but don't want to show all of it's levels at first. For example show three first levels at first. On click of level 3 show level 2, 3 and 4. on click of level 4 show level 3,4,5.

Here is the code I implemented in AngularV8:

.html file

<div class="kt-widget14__chart" style="min-height:300px;">
   <div class="chartjs-size-monitor-new">
       <highcharts-chart [Highcharts]="highcharts" [options]="company360SunburstOptions"
           style="width: 100%; height: 100%; display: block;">
       </highcharts-chart>
   </div>
</div>

.ts file


this.myService.getData().subscribe(result => {
            this.chartOptions.series[0].data = result;
            this.chartOptions.series[0].point = {
                events: {
                    click: (function (event) {
                        that.poinClicked(data);
                    })
                }
            };
        });

    chartOptions = {
        chart: {
            type: "sunburst",
            height: '100%',
        },
        credits: {
            enabled: false
        },
        exporting: { enabled: false },
        title: {
            text: ''
        },
        subtitle: {
            text: ''
        },
        plotOptions: {
            series: { // or general options: "series: { ... }"
                dataLabels: {
                    format: '{point.name}',
                    filter: {
                        property: 'innerArcLength',
                        operator: '>',
                        value: 16
                    },
                    enabled: true,
                    style: {
                        textShadow: false
                    }
                },
                animation: false
            }
        },
        series: [{
            type: "sunburst",
            data: [],
            allowDrillToNode: true,
            cursor: 'pointer',
            point: {},
            levels: [{
                level: 1,
                levelSize: {
                    units: 'percentage',
                },
                hidden: false,
                dataLabels: {
                    enabled: true
                }
            }, {
                level: 2,
                hidden: false,
                dataLabels: {
                    enabled: true
                }
            }, {
                level: 3,
                hidden: true,
                dataLabels: {
                    enabled: false
                },
                levelSize: {
                    value: 0
                }
            }, {
                level: 4,
                hidden: true,
                dataLabels: {
                    enabled: false
                },
                levelSize: {
                    value: 0
                }
            }]
        }],
        tooltip: {
            enabled: false
        }
    };

pointClicked() {

   let level = data['Level'];
   level = level + 1;
   if (level == 1) {
      this.sunburstShowLevels([1,2])
   } else if (level == 2) {
       this.sunburstShowLevels([1,2,3])
   } else if (level == 3) {
       this.sunburstShowLevels([2,3,4])
   }
}

sunburstShowLevels(levelIdList) {
   // Will get a list of levelIds that should be display
   let levels = this.chartOptions.series[0].levels; // Whole levels object
   let newLevels = [];
   for (var index=0; index<levels.length; index++) {
       var level = levels[index];
       if (levelIdList.includes(level['level'])) {
          // Then show it
          level.hidden = false; // set flag
          level['levelSize'] = {value: 1};
          level['dataLabels'] = {enabled: true}
       } else {
           level.hidden = true; // set flag
          level['levelSize'] = {value: 0};
          level['dataLabels'] = {enabled: false}
       }
       newLevels.push(level);
    }
}

This code will show the levels I want based on level i am selecting. It is almost working. But the problem is that when I click on a level, I need to click twice to make it work.

For example when I click on the second level, It first show completely level 2, Then when i click again it will show the levels I wanted : 1,2 and 3.

Problem

To sum up, I want to only show specific levels on click of each level. This is the solution I implemented and the problem is it is not triggering right after I click on it. I Need to click twice to make it work. Any idea?

Or If anyone has a better solution, I would really appreciate any help on it.

thanks in advance.


Solution

  • I have another idea how to achieve it via using the series.update feature to update level options.

    Demo: https://jsfiddle.net/BlackLabel/zrt4m13g/

    Code:

    point: {
      events: {
        click(e) {
          let series = this.series,
            clickedLevel = this.node.level,
            currentOptions = series.userOptions.levels;
    
          for (let i of currentOptions) {
            if (clickedLevel !== 0 /*or 1 */ && clickedLevel !== currentOptions.length) {
              if (i.level === clickedLevel || i.level === clickedLevel + 1 || i.level === clickedLevel - 1) {
                i.levelSize = {
                  value: 1
                }
              } else {
                i.levelSize = {
                  value: 0
                }
              }
              i.dataLabels = {
                  rotationMode: 'parallel',
                  filter: {
                    property: 'outerArcLength',
                    operator: '>',
                    value: 64
                  },
                  enabled: true
              }
            }
          }
    
          series.update({
            levels: currentOptions
          })
        }
      }
    },
    

    API: https://api.highcharts.com/highcharts/series.sunburst.levelSize

    API: https://api.highcharts.com/class-reference/Highcharts.Series#update