0
votes

I have an angular app with highcharts, in the case of the pie chart, I have a list of colors to use on each slice. I restricted the pie to have only 10 slices. I need to put some colors on the slices in order from the biggest slice to the smallest.

Here's the pie chart configuration:

pieChartOptions = {
  chart: {
    type: "pie"
 },
 title: {
  enabled: true,
  text: this.xAxis.name + " by Daypart",
  verticalAlign: "top",
  align: "left"
},
credits: {
  enabled: false
},
    xAxis: {
      title: {
        text: this.xAxis.name
      }
    },
    yAxis: {
      title: {
          text: "Reach"
      }
    },
    series: [{
      data: null,
      colors: ["#205493", "#BE5873", "#81CACF", "#E98841", "#E3D830", "#A6C46F",
       "#894C7B", "#BA9765", "#7F7F7F", "#C3C3C3"],
      dataLabels: {
        enabled: false
     },
    }]
 };

The colors array should be in order from the biggest slice to the smallest. The problem is that I also have a line chart that when on hover the line updates the pie chart, so the sizes are dymamically changing.

Here's the data setup for the pie chart:

loadCharts() {
    let presentationResults: OptimizationAllocationGroupByTrps[] | OptimizationAllocationGroupByCost[] = [];

    this.xAxis.name = this.selectedOption == null ? "TRP" : this.selectedOption;

    if (this.selectedOption === "Budget") {
      presentationResults = [...this.projectResults.OptimizationAllocationGroupByCost];
    } else {
      presentationResults = [...this.projectResults.OptimizationAllocationGroupByTRPs];
    }

    this.chartOptions.series[0].name = this.xAxis.name;
    this.chartOptions.series[0].data = [];
    this.chartOptions.series[0].data = [... this.projectResults.Curves.filter(x => x.Type === this.xAxis.name)[0].Points];

    presentationResults.forEach((oa: OptimizationAllocationGroupByTrps | OptimizationAllocationGroupByCost) => {
      if (oa.name.indexOf("%") === -1) {
        oa.y = (Math.round(oa.y * 100) / 100);
        oa.name = oa.name + ": " + oa.y + "%";
      }
    });

    if (presentationResults.length > 10) {
      let accumPercentage: number = 100;


      presentationResults.sort((v1: any, v2: any) => v1.y > v2.y ? -1 : 1).length = 9;

      presentationResults.forEach(updateResult => {
        accumPercentage -= updateResult.y;
      });

      presentationResults.push({name: "Others: " + Math.round(((accumPercentage))) + "%", y: accumPercentage});
    }


    this.pieChartOptions.series[0].data = presentationResults;
    this.pieChartOptionsBig.series[0].data = presentationResults;
    console.log(this.pieChartOptions.series[0].data);

    this.chartOptions.xAxis.title.text = this.xAxis.name;
    this.chartOptions.title.text = "Reach +1/" + this.xAxis.name;
    this.pieChartOptions.title.text = this.xAxis.name + " by Daypart";
    this.pieChartOptionsBig.title.text = this.xAxis.name + " by Daypart";

    this.chartOptions = {... this.chartOptions };
    this.pieChartOptions = {... this.pieChartOptions };
    this.pieChartOptionsBig = {... this.pieChartOptionsBig };
  }

And here's an example of the charts:

enter image description here

On this example the orange slice should be "#205493" As is the first item on the colors array.

Any ideas on how to achieve this?

1
It's a pleasure to see a well formatted post! In order to help you can you give a stackblitz with you app please? It'll be by far easier to help you!Maxime Lafarie
sure, let me trypedrodotnet
the structure is too complex to create a stablitz, I'm not being able to replicatepedrodotnet
I think you can sort the presentationResults: this.pieChartOptions.series[0].data = presentationResults.sort((a,b)=>a-b)Eliseo
Hi @pedrodotnet, You can set a color directly on a point and update it dynamically. For example: jsfiddle.net/BlackLabel/6m4e8x0y/4899ppotaczek

1 Answers

1
votes

You can set a color directly on a point and update it dynamically.

series: [{
  type: 'pie',
  data: [
    { y: 20, color: 'red'},
    { y: 20, color: 'blue'},
    { y: 20, color: 'orange'}
  ]
}]

Live example: http://jsfiddle.net/BlackLabel/6m4e8x0y/4899/

API Reference:

https://api.highcharts.com/highcharts/series.pie.data.color

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