I've looked at How to change colours for Angular-Chart.js, but it relates to colours for an entire (dataset), not a specific bar.
What I'm looking for is a way to apply Different color for each bar in a bar chart; ChartJS to Angular.
So, I've got a bar chart:
<canvas id="locationBar" class="chart chart-bar" data="chartParams.votes" labels="chartParams.listOfLocations" series="chartParams.series" colours="chartParams.colours" options="chartParams.options"></canvas>
With the following angular code (in a controller of course)
$scope.chartParams = {
listOfLocations: ['Trenzalore','Earth','Caprica','Sol','Tau Ceti'],
votes: [[5,10,6,7,2]],
series: ["Nice Places"],
colours: [{fillColor:getRandomColour()}],
options: {barShowStroke : false}
};
where getRandomColour()
would return a random colour.
Right now, the colours
field applies this colour to all the bars:
when I actually want a different colour for each bar:
Using the latest version of angular-chart.js, here's an example that changes the colours based on a condition, without modifying the library itself. Instead, it uses the
chart-dataset-override
feature.The trick is to use a series with only one data set.
HTML
JavaScript
Add the following code to the controller:
Output
Produces:
Related Links
Plunker Demo
The easiest thing to do was to modify the chart.js source file to accept an array of colors for fillColor rather than a single color string.
If you could get one of the other proposed solutions working, I think it would end up having to be done in a very un-angular way.
Some people may not like tweaking the source, but for how simple and easy it was to do, and it achieves the desired result, and it isn't any less 'angular' in it's solution...let's just call it an improvement to chart.js.
Note that I only modified the "bar" chart to accept an array of colors, because it seems chart.js references the fillColor separately in each chart type. So you should be able to make this modification to work for any of the charts types.
The place you need to modify:
This block of code can be found in the Chart.type.extend function for the bar chart. Just look for this:
and look further down inside that function for the place where it pushes the fillColor to the datasetObject.bars.
Now just set up your chart like before, except feed it an array for fillColor:
One of the bigger problems with Chart libraries is that sooner or later you hit a wall, which can only be breached by hacking or extending the library. Knowing you hit that wall and what to could be the answer to this question.
Even though you can create a custom chart.js graph you could also solve it with d3.js. I've taken one of the basic angular d3.js barchart examples and added the behavior you want:
http://plnkr.co/edit/9RCYAPCEj9iRsxkkYPaV?p=preview
Customizing a chart in any way, like changing fill, stroke or anything else is within reach. It is of cause still though a lot more code, but if you want to customize beyond library support, it is always a lot more code.