score:1
It's now built into CHart.js 3:
https://www.chartjs.org/docs/latest/samples/line/segments.html
score:4
You can extend the chart to redraw the segment of your choice with the different color.
Preview
Script
Chart.types.Line.extend({
name: "LineAlt",
draw: function () {
Chart.types.Line.prototype.draw.apply(this, arguments);
var index = 1;
var datasetIndex = 0;
var hasValue = function(item){
return item.value !== null;
},
previousPoint = function (point, collection, index) {
return Chart.helpers.findPreviousWhere(collection, hasValue, index) || point;
};
var ctx = this.chart.ctx;
var dataset = this.datasets[datasetIndex];
var pointsWithValues = Chart.helpers.where(dataset.points, hasValue);
ctx.strokeStyle = 'red';
ctx.lineWidth = 3;
ctx.beginPath();
var point = dataset.points[index];
ctx.moveTo(point.x, point.y);
point = dataset.points[index + 1];
var previous = previousPoint(point, pointsWithValues, index + 1);
ctx.bezierCurveTo(
previous.controlPoints.outer.x,
previous.controlPoints.outer.y,
point.controlPoints.inner.x,
point.controlPoints.inner.y,
point.x,
point.y
);
ctx.stroke();
}
});
and
...
new Chart(ctx).LineAlt(data);
Fiddle - http://jsfiddle.net/021xvuhd/10/
score:4
Here's a working example to do this in Charts.js 2
https://jsfiddle.net/egamegadrive16/zjdwr4fh/
var ctx = document.getElementById('myChart').getContext('2d');
//adding custom chart type
Chart.defaults.multicolorLine = Chart.defaults.line;
Chart.controllers.multicolorLine = Chart.controllers.line.extend({
draw: function(ease) {
var
startIndex = 0,
meta = this.getMeta(),
points = meta.data || [],
colors = this.getDataset().colors,
area = this.chart.chartArea,
originalDatasets = meta.dataset._children
.filter(function(data) {
return !isNaN(data._view.y);
});
function _setColor(newColor, meta) {
meta.dataset._view.borderColor = newColor;
}
if (!colors) {
Chart.controllers.line.prototype.draw.call(this, ease);
return;
}
for (var i = 2; i <= colors.length; i++) {
if (colors[i-1] !== colors[i]) {
_setColor(colors[i-1], meta);
meta.dataset._children = originalDatasets.slice(startIndex, i);
meta.dataset.draw();
startIndex = i - 1;
}
}
meta.dataset._children = originalDatasets.slice(startIndex);
meta.dataset.draw();
meta.dataset._children = originalDatasets;
points.forEach(function(point) {
point.draw(area);
});
}
});
var chart = new Chart(ctx, {
// The type of chart we want to create
type: 'multicolorLine',
// The data for our dataset
data: {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [{
label: "My First dataset",
borderColor: 'rgb(255, 99, 132)',
data: [0, 10, 5, 2, 20, 30, 45],
//first color is not important
colors: ['', 'red', 'green', 'blue']
}]
},
// Configuration options go here
options: {}
});
source: https://github.com/chartjs/Chart.js/issues/4895#issuecomment-342747042
Source: stackoverflow.com
Related Query
- How to change line segment color of a line graph in Chart.js?
- How to change line segment color based on label value in chart.js?
- How to use segment property to color line / border color based on value in chart js?
- How to change background color of labels in line chart from chart.js?
- How to change bar color on a angular-chart and chart js bar graph with multiple datasets?
- Chart.js: How can a line series (out of many) change line color and thickness upon mouse hover?
- how to set chart.js grid color for line chart
- How to start the line graph from the left Y axis in a line/bar mixed chart (Chart.js)?
- how to change color of bar if its goes above avg score in mixed graph chart.js
- How to add area with break on line chart with fill color
- Change Axis Line color in Chart created using chart.js
- How to change the label and grid line position on a timeseries chart in Chart.js 3?
- change stroke line color in chart according to datasets in react native
- How to change color of column in chart js
- How to change the chart line or area colors according to the user need?
- How to change color by clicking on the chart bar?
- Chart JS Line Graph multitooltipkey background color issue
- How can I change the cursor on pie chart segment hover in ChartJS 3?
- ChartJS - How to change color of some data points in graph
- How to change line color based on data - chartist or chart.js
- how to change color of dots in graph using chart.js
- How to draw multiple color bars in a bar chart along a Horizontal Line using chart.js
- how to set the color of all points in a line graph to the same in chart.js?
- How to fill a graph by a color till a vertical line using chart.js
- How to Add X axis Padding in chart js Line Graph
- How to change React line chart tooltip title font family in chart.js
- How do I change pointBackgroundColor in Line segment on ChartJS?
- How can i change the every legend label font color using chart.js version 2.8.0 (spacial line chart)?
- Line chart Change background color of shaded region on hover (Chartjs)
- How to change label color of ng2 chart in angular
More Query from same tag
- Why doesn't parsing option work in chartjs?
- Right way to Plot Dynamic Grouped Bar chart.js
- Is It Possible To Generate a ChartJS In C# Web API And Save Image?
- How to scale label size radar chart chart.js
- unable to pass data from views to js file having chart js codes, shows unexpected syntax error
- Line Chart x-Axis datapoints with Strings
- ChartJS plugin with live label update
- How do I use ChartJS with a background color in the space between two line charts?
- Class declaration merging for an existing type
- How to change width of the chart using Chart.js
- How to show bar labels in legend in Chart.js 2.1.6?
- how to filter year from datefield in django
- Chart.js Passing an array starting with 1 instead of 0 problem
- Chart Js clickable bar
- How can i display my data in a chart using chart js
- Change X and Y axis font color
- Chartjs How to render a custom horizon line under X-Axis
- PrimeNg Pie Chart - By default show all tooltip?
- Background color of the graph affects the other graphs too
- ChartJS: Custom legend not showing Labels for Multi-Pie chart
- Create multiple pie chart simultaneously using chart.js
- Automatic label updates
- How to save Chart JS charts as image without black background using blobs and filesaver?
- Django - object is not subscriptable django charts.js" or nothing displays at all?
- chart.js line chart update once every 5 seconds?
- How to set different colors in each stacked bar chart cell?
- Mobile page loading in "zoomed" state
- How to hide the legend display for a specific chart?
- How to show text as a series on a chart in Javascript?
- How do you use user input as data values in chart.js?