score:9
For chartjs 2.0 see this following answer.
Original answer below.
Good question regarding ChartJS. I've been wanting to do a similar thing. i.e dynamically change the point colour to a different colour. Have you tried this below. I just tried it and it worked for me.
Try this:
myLineChart.datasets[0].points[4].fillColor = "rgba(000,111,111,55)" ;
Or Try this:
myLineChart.datasets[0].points[4].fillColor = "#FF0000";
Or even this:
myLineChart.datasets[0].points[4].fillColor = "lightgreen";
Then do this:
myLineChart.update();
I guess you could have something like;
if (myLineChart.datasets[0].points[4].value > 100) {
myLineChart.datasets[0].points[4].fillColor = "lightgreen";
myLineChart.update();
}
Give it a try anyway.
score:0
If you initialize the myChart in this manner,
var myChart = new Chart(ctx, {
type: 'line',
data: {
you have to change line color by this code
myChart.data.datasets[0].backgroundColor[0] ="#87CEFA";
If you initialize the myChart in this manner,
myBar = new Chart(ctx).Line(barChartData, {
you have to change line color by this code
myLineChart.datasets[0].points[4].fillColor = "#FF0000";
score:1
Just adding what worked for me in the new 2.0 version.
Instead of:
myLineChart.datasets[0].points[4].fillColor = "lightgreen";
I had to use:
myChart.config.data.datasets[0].backgroundColor[4] = "lightgreen";
Not sure if that's because of a change in 2.0 or because I'm using a bar chart and not a line chart.
score:12
Here's what worked for me (v 2.7.0), first I had to set pointBackgroundColor and pointBorderColor in the dataset to an array (you can fill this array with colours in the first place if you want):
var myChart = new Chart(ctx, {
type: 'line',
data: {
datasets: [
{
data: dataPoints,
pointBackgroundColor: [],
pointBorderColor: [],
}
]
}
});
Then you can monkey with the colours of the points directly:
myChart.data.datasets[0].pointBackgroundColor[4] = "#cc00cc";
myChart.data.datasets[0].pointBorderColor[4] = "#cc0000";
myChart.update();
Some other properties to play with to distinguish a point: pointStrokeColor (it apparently exists but I can't seem to get it to work), pointRadius & pointHoverRadius (integers), pointStyle ('triangle', 'rect', 'rectRot', 'cross', 'crossRot', 'star', 'line', and 'dash'), though I can't seem to figure out the defaults for pointRadius and pointStyle.
score:24
With recent versions of chart.js
I would recommend doing this with scriptable options.
Scriptable options give you an easy way to vary the style of a dataset property (e.g. line point colour) dynamically according to some function you provide. Your function is passed a 'context' object that tells it the index and value of the point etc. (see below).
Most chart properties can be scripted; the dataset properties for each chart type tell you the exact list (e.g. see here for line chart).
Here is how you might use scriptable options on a line chart (based on the example in the docs). On this chart negative data points are shown in red, and positive ones in alternating blue/green:
window.myChart = Chart.Line(ctx, {
data: {
labels: x_data,
datasets: [
{
data: y_data,
label: "Test Data",
borderColor: "#3e95cd",
fill: false,
pointBackgroundColor: function(context) {
var index = context.dataIndex;
var value = context.dataset.data[index];
return value < 0 ? 'red' : // draw negative values in red
index % 2 ? 'blue' : // else, alternate values in blue and green
'green';
}
}
],
}
});
The context
object passed to your function can have the following properties. Some of these won't be present for certain types of entity, so test before use.
- chart: the associated chart
- dataIndex: index of the current data
- dataset: dataset at index datasetIndex
- datasetIndex: index of the current dataset
- hover: true if hovered
score:45
In updating to version 2.2.2 of ChartJS, I found that the accepted answer no longer works. The datasets will take an array holding styling information for the properties. In this case:
var pointBackgroundColors = [];
var myChart = new Chart($('#myChart').get(0).getContext('2d'), {
type: 'line',
data: {
datasets: [
{
data: dataPoints,
pointBackgroundColor: pointBackgroundColors
}
]
}
});
for (i = 0; i < myChart.data.datasets[0].data.length; i++) {
if (myChart.data.datasets[0].data[i] > 100) {
pointBackgroundColors.push("#90cd8a");
} else {
pointBackgroundColors.push("#f58368");
}
}
myChart.update();
I found this looking through the samples for ChartJS, specifically this one: "Different Point Sizes Example"
Source: stackoverflow.com
Related Query
- ChartJS - Different color per data point
- Different color for each bar in a bar chart; ChartJS
- Chartjs hide data point labels
- Chart.js Line, different fill color for negative point
- Change point size and color on hover in chartjs
- Change point color on click using ChartJS
- Changing color of specific chartjs point
- Chartjs Radar - Change color of end point labels
- Chart.js data background color is overwriting point background color
- Different color for line segments in ChartJS
- ChartJS different background gradient depending on data (line graph)
- Chartjs tick color different at zero
- How can I load multiple Chartjs charts with different data on the same page?
- ChartJS getting an unwanted line between first data point and last data point
- Changing color of specific ChartJS - AngularChartJS point
- How to add new data point and remove leftmost data point dynamically in Chartjs
- How to reuse a Chartjs Chart component in with different Data and get past the **Canvas is already in use** error?
- ChartJS - How to change color of some data points in graph
- Change background point color at runtime for angular ng2-charts / chartjs
- Chartjs Different Fill Color For Overlap Area
- How to properly feed data to ChartJS with different number of x(labels) and y(data) points
- ChartJS have xAxes labels match data source
- chartjs line graph points with different color
- ChartJS different border color for legend
- ng2-charts doughnut chart with different data per series
- chartjs display data in one bar line with 3 different data sets in 3 different colors
- ChartJs different data for Tooltips
- Two data sets with different time instances on the same ChartJs chart
- Calling data from outside of Chartjs code
- ChartJS with ChartJS DataLabels: Change Color per Dataset for Value Labels
More Query from same tag
- Set height of chart in Chart.js
- Getting the HTML code of a chart created by chart.js
- How can I sort month by order?
- Chart.js remove gridline
- How to reduce number of multiple gridlines in stack bar graph chart.js
- Count the duplicates in a string array using React JS
- Legend on chart.js
- How can I make my backgroundColor in Chart.js match up with a reversed order y axis?
- Ionic 3 Angular component load listener
- How I can increase the number of colors for bar labels in ChartJS?
- How to rotate custom marker in chart.js?
- Cursor selects multiple points instead of just one - chart.js
- Adding additional data to chart
- Chartjs: Overwrite plot y-axis integer labels to some string value
- Chart.js barchart custom on hover method
- how to make scrollable the legends of pie chart ( chart js )
- Chart.js data/label scale
- Hide Legend in Chart.js V3.7.1
- How to change ChartJS font size with javascript?
- How to assign specific colors to data items in a doughnut chart in Angular Chart?
- Sum array of objects value based on month - ReactJS
- Updating chart.js with JSON from Razor page
- Chart.js Chart in Angular does not load until resizing browser window
- Prime NG Customise tool tip on Bar Chart
- PHP Date and Time, getting the 1st, 2nd, 3rd, and 4th week in a month
- Printing Chart using ngPrint and tc-chartjs
- Is there a way to align title and legend on the same line?
- How do you output the correct value for Chart.js Radar graph on label?
- Move y-Axis and hide in Chart.JS V3
- increase the gap between y-axis and first x-axis value chart.js