Necesito implementar este código exactamente usándolo. Aquí está mi ejemplo de código:
barChartOptions: ChartOptions = { responsive: true, }; barChartColors: Color[] = [ { backgroundColor: 'forestgreen', borderColor: 'white', borderWidth: 3 }, { backgroundColor: 'red', borderColor: 'white', borderWidth: 3 }]; barChartLabels: Label[] = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31']; barChartType: ChartType = 'bar'; barChartLegend = false; barChartPlugins = []; barChartData: ChartDataSets[] = [ { data: [11, 12, 12, 14, 14, 19, 22, 28, 29, -30, 31, 31, 35, 35, 35, -37, 37, 40, 40, 41, 45, 45, 48, 51, 57, 62, 65, 69, 69, 71, 75] } ];Este es mi lado HTML del código pero no funciona:
<div class="chart-wrapper" style="width: 100%"> <canvas baseChart [colors]="barChartData>0 ? barChartColors[0] : barChartColors[1]" [datasets]="barChartData" [labels]="barChartLabels" [options]="barChartOptions" [plugins]="barChartPlugins" [legend]="barChartLegend" [chartType]="barChartType"> </canvas> </div>[colors]="barChartData>0 ? barChartColors[0] : barChartColors[1]" esta fila me dio este error:
Además, el compilador me da esto:
El operador '>' no se puede aplicar a los tipos 'ChartDataSets[]' y 'number'.[colors]="barChartData>0 ? barChartColors['forestgreen'] : barChartColors['red']"
¿Me podrían ayudar con este problema?
barChartData es una matriz de ChartDataSets que contiene un solo elemento. Por lo tanto, para que funcione, podría cambiar su código de la siguiente manera:
[colors]="barChartData[0].data.map(v => v > 0 ? barChartColors[0] : barChartColors[1])"o más compacto...
[colors]="barChartData[0].data.map(v => barChartColors[v > 0 ? 0 : 1])"Este código usa
Array.map(), en la matriz dedatadentroChartDataSets. Devuelve una nuevaarraycon el color apropiado para cada valor.