score:5
as you want to give different color for each bar in graph, you are passing those value in array. but the thing is chart.js
doesn't support such type of feature.
reason
the color which we are able to see in bar area is nothing but fillcolor
option, if you look at the source code of chart.js line
code
helpers.each(dataset.data, function(datapoint, index) {
//add a new point for each piece of data, passing any required data to draw.
datasetobject.bars.push(new this.barclass({
value: datapoint,
label: data.labels[index],
datasetlabel: dataset.label,
strokecolor: dataset.strokecolor,
fillcolor: dataset.fillcolor, //<--this line responsible for filling color
highlightfill: dataset.highlightfill || dataset.fillcolor,
highlightstroke: dataset.highlightstroke || dataset.strokecolor
}));
}, this);
above code clearly says that fillcolor
required string. in anyway the you can pass only one color in that string. and only one color will get applied to bar series. if we want to make you requirement workable then we need to make changes in chart.js
.
changes to be made in chart.js
we need to make change in this line from fillcolor: dataset.fillcolor,
to fillcolor: dataset.fillcolor[index],
so that we can pass color array which needs to be applied on each bar of chart. index
variable of .each
will ensure the color would be applied on bar in order way which we had given in array. our color would be changed to $scope.medals_colours = [{fillcolor:['#087d76', '#b3bc3a', '#38b665', '#8b4a9d']}];
.
changed code
helpers.each(dataset.data, function(datapoint, index) {
//add a new point for each piece of data, passing any required data to draw.
datasetobject.bars.push(new this.barclass({
value: datapoint,
label: data.labels[index],
datasetlabel: dataset.label,
strokecolor: dataset.strokecolor,
fillcolor: dataset.fillcolor[index] || dataset.fillcolor, //<--getting color using index
highlightfill: dataset.highlightfill || dataset.fillcolor,
highlightstroke: dataset.highlightstroke || dataset.strokecolor
}));
}, this);
markup
<div class="graph-display" ng-controller="jsonserverbox">
<canvas id="bar" class="chart chart-bar" data="medal_data" labels="medal_ticks"
colours="medals_colours"></canvas>
</div>
controller
app.controller('jsonserverbox', function($scope) {
$scope.medal_ticks = ['gold', 'silver', 'bronze', 'not passed'];
$scope.series = ['medaljer'];
$scope.medals_colours = [{fillcolor:['#087d76', '#b3bc3a', '#38b665', '#8b4a9d']}];
$scope.medal_data = [['1', '5', '2', '0']];
});
i know it does look hacky but we have to make it.
kudos goes to @tpie this answer
Source: stackoverflow.com
Related Query
- Different color for each column in angular-chartjs bar chart
- Angular 4: Different color for bar in bar chart dynamically using ChartJS
- Different color for each bar in a bar chart; ChartJS
- Set different color for each bars using ChartJS in Angular 4
- Different color for each bar in a bar graph in ChartJS and VueJS
- How to set a full length background color for each bar in chartjs bar
- Gradient color for each bar in a bar graph using ChartJS
- chart js - Apply different color for each x-axes label
- How to set single color on each bar in angular chart js
- How to use set the color for each bar in a bar chart using chartjs?
- Set fixed label size for grouped bar chart in angular Chartjs
- Issue with chartjs linear gradient for the mixed bar chart in ReactJS is not calculated for each individual Bars
- How do I get a different label for each bar in a bar chart in ChartJS?
- ChartJS vertical bar chart - measure percentage of the data in each column based on a specified max
- ChartJS Adding Different Lines for each column
- subcategories for each bar in multibar chart using chartjs
- how to create bar chart with group and sam color for each group using chart.js?
- Chartjs random colors for each part of pie chart with data dynamically from database
- chart.js Line chart with different background colors for each section
- ChartJS bar chart with legend which corresponds to each bar
- Border radius for the bar chart in ChartJS
- Can I specify a different font size for each row of text in Chart Title?
- Chartjs - data format for bar chart with multi-level x-axes
- Grouping the object by key for Chartjs bar chart
- Different color for line segments in ChartJS
- Angular 8 & ChartJs change color in pie chart
- ChartJS bar chart fixed width for dynamic data sets
- Vue ChartKick - different colours for bar chart
- chartjs custom y axis values, different text for each one
- how to set color for each item in tooltip with ChartJS
More Query from same tag
- How to sum array value in chart.js inside loop function?
- Chart.js TypeError: context is null
- Chart.js plot live data
- How can I get chart.js to automatically add colours for dynamic labels?
- ChartJs: Different color fill between points makes lines straight...need curves (lineTension)
- Risers or bars of Dynamically built ChartJs from json not aligning to bottom of y axis
- Chartjs: Is it possible to add phases in line chart?
- Chart.js custom tooltip events
- How do you set up a chart.js scatter line chart through angular-chart.js?
- How do I run Chart.js with Angular 10 SSR / universal?
- React render Chart.js based on api response
- setInterval on my Doughnut.js
- Javascript and Chart.js not taking user input assuming it is a variable scope issue
- ChartJS 3 vertical annotations
- chartjs chart.update() function not rendering lines in foreground
- ChartJS/ ChartJS-plugin annotation set height of vertical line
- Chart JS - Use time for xAxes
- chart js same label, multi data
- Using Javascript with MPDF
- custom tooltip opacity always 1
- opening json file to create chart in chart.js
- MYSQL Query and Chart.js and PHP
- How to plot chart from external JSON and format X-AXIS to show time using Chart.JS?
- Creating charts (charts.js) values coming from SQLite database using AngularJS
- angular-chart.js not rendering any chart
- change legend in chart in report builder
- Customizing Chart.js troubleshoot
- How can i add a calculated value on top of the bars in Chart.js?
- converting int to List<double?>, ChartJS Core
- Show nested objects in Chart.js