如何在chart.js/ng 2-charts中给极坐标图背景中的单个圆环着色?

kuarbcqp  于 2022-11-06  发布在  Chart.js
关注(0)|答案(2)|浏览(144)

我试图使用chart.js版本和ng 2-charts在Angular中更改单个极坐标图环的颜色,但在特定版本的文档中,我没有找到任何与之相关的内容,也没有在Web上搜索解决方案。

"chart.js": "^2.8.0",
"ng2-charts": "^2.3.0",

代码:

public polarAreaChartLabels: Label[] = [];

  public polarAreaChartData: SingleDataSet = [];

  public polarAreaLegend = true;

  myColors = [{ backgroundColor: ["#cb4b4b", "#edc240", "#afd8f8"] }];

  public polarAreaChartType: ChartType = "polarArea";

  public polarAreaChartOptions: ChartOptions = {
    plugins: {
      datalabels: {
         color: '#000000',
         anchor: 'end',
         align: 'end',
         padding: 50,
         display: true,
         font: {
           weight: 'bolder'
         },
         formatter: function(value, ctx) {
          return `${ctx.chart.data.labels[ctx.dataIndex]} - ${value} %`;
       },
      },
    },
    scale: {
      ticks: {
          beginAtZero: true,
          max: 100,
          min: 0,
          stepSize: 10
      }
    }
  };

  public ChartPlugins = [pluginDataLabels];

HTML格式:

<canvas id="polar-chart" baseChart height="40vh" width="120vw" 
                [data]="polarAreaChartData" 
                [labels]="polarAreaChartLabels"
                [legend]="polarAreaLegend"
                [plugins]="ChartPlugins"
                [options]="polarAreaChartOptions"
                [chartType]="polarAreaChartType" 
                [colors]="myColors">        
            </canvas>

电流输出

所需输出

有没有任何插件或解决方案可用于此?任何帮助将非常感谢。

vc6uscn9

vc6uscn91#

这可以通过将scale.gridLines.color选项定义为颜色数组来实现。

options: {
  scale: {
    gridLines: {
      color: [...]
    }
  }
}

请看一下下面的可运行代码,看看它是如何工作的。
第一个

vhmi4jdf

vhmi4jdf2#

这并不是一个特定的Angular 解决方案,但如果您只想突出显示秤上的特定环,这段视频会非常有用,我想我可以带着同样的问题与其他人分享:https://www.youtube.com/watch?v=hPGuSNdwOC4
我对它做了一些修改,在图表上的100和110处放置线(分别代表预算的100%和110%),而不像视频的解决方案那样提前知道刻度的数量。刻度可能会根据图表数字的值、图表大小等而变化。我使用r.ticks集合来获取刻度的数量,以便正确计算圆半径。

const budgetLines = {
    id: "budgetLines",
    afterDatasetsDraw(chart, args, options) {
        var budgetTickCount = 0;
        var overBudgetTickCount = 0;
        const { ctx, chartArea: { top, bottom, left, right, width, height }, scales: { r } } = chart;
        const angle = Math.PI / 180;
        const trueHeight = r.yCenter - r.top;

        r.ticks.forEach(function (tick) {
            if (tick.value < options.budgetValue) {
                budgetTickCount = budgetTickCount + 1;
            }

            if (tick.value < options.budgetValue) {
                overBudgetTickCount = overBudgetTickCount + 1;
            }
        });

        const budgetRadius = ((trueHeight / r.end) * options.budgetValue) - (budgetTickCount + 1);
        const overBudgetRadius = ((trueHeight / r.end) * options.overBudgetValue) - (overBudgetTickCount + 1);

        ctx.save();
        ctx.beginPath();
        ctx.lineWidth = 1;
        ctx.strokeStyle = options.budgetColor;
        ctx.arc(r.xCenter, r.yCenter, budgetRadius, angle * 0, angle * 360, false);
        ctx.stroke();
        ctx.closePath();

        ctx.beginPath();
        ctx.lineWidth = 1;
        ctx.strokeStyle = options.overBudgetColor;
        ctx.arc(r.xCenter, r.yCenter, overBudgetRadius, angle * 0, angle * 360, false);
        ctx.stroke();
        ctx.closePath();
        ctx.restore();
    }
}

var ctx = document.getElementById("summaryChart");
new Chart(ctx, {
    type: "polarArea",
    data: chartData,
    options: {
        plugins: {
            budgetLines: {
                budgetValue: 100,
                overBudgetValue: 110,
                budgetColor: "rgba(0, 255, 0, 1)",
                overBudgetColor: "rgba(0, 0, 255, 1)"
            },
            legend: {
                display: false
            }
        },
        scales: {
            r: {
                suggestedMax: 100
            }
        }
    },
    plugins: [budgetLines]
});

相关问题