Highcharts-每个切片的饼图颜色

问题描述 投票:0回答:1

我有一个带有高位图表的角度应用程序,对于饼图,我有一个要在每个切片上使用的颜色列表。我限制派只有10片。我需要在切片上按从大到小的顺序排列一些颜色。

这是饼图配置:

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
     },
    }]
 };

colors数组应按从最大片到最小片的顺序排列。问题是我还有一个折线图,当鼠标悬停时,该线会更新饼图,因此大小会动态变化。

这是饼图的数据设置:

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 };
  }

这是图表的示例:

enter image description here

在此示例中,橙色切片应为"#205493",与颜色数组上的第一项一样。

关于如何实现这一目标的任何想法?

angular typescript charts highcharts
1个回答
0
投票

您可以直接在一个点上设置颜色并动态更新它。

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

实时示例: http://jsfiddle.net/BlackLabel/6m4e8x0y/4899/

API参考:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.