score:14
HI Michael Hurley I think you should use:
interpolation: http://www.chartjs.org/samples/latest/charts/line/interpolation-modes.html
or
Multi-axis: http://www.chartjs.org/samples/latest/charts/line/multi-axis.html
My idea is we have 3 datasets with multi-color, End of dataset1 is first of dataset2.
Here my Example:
window.chartColors = { red: 'rgb(255, 99, 132)', orange: 'rgb(255, 159, 64)', yellow: 'rgb(255, 205, 86)', green: 'rgb(75, 192, 192)', blue: 'rgb(54, 162, 235)', purple: 'rgb(153, 102, 255)', grey: 'rgb(201, 203, 207)' };
var randomScalingFactor = function() {
return Math.round(Math.random() * 100);
};
var config = {
type: 'line',
data: {
labels: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'],
datasets: [{
label: 'Cubic interpolation (monotone)',
data: [0, 20, 20, 60, 60, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN],
borderColor: window.chartColors.red,
backgroundColor: 'rgba(0, 0, 0, 0)',
fill: false,
cubicInterpolationMode: 'monotone'
}, {
label: 'Cubic interpolation (default)',
data: [NaN, NaN, NaN, NaN, 60, 120, 140, 180, 120, NaN, NaN, NaN, NaN],
borderColor: window.chartColors.blue,
backgroundColor: 'rgba(0, 0, 0, 0)',
fill: false,
}, {
label: 'Linear interpolation',
data: [NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 120, 125, 105, 110, 170],
borderColor: window.chartColors.green,
backgroundColor: 'rgba(0, 0, 0, 0)',
fill: false,
lineTension: 0
}]
},
options: {
responsive: true,
title: {
display: true,
text: 'Chart.js Line Chart - Cubic interpolation mode'
},
tooltips: {
mode: 'index'
},
scales: {
xAxes: [{
display: true,
scaleLabel: {
display: true
}
}],
yAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'Value'
},
ticks: {
suggestedMin: -10,
suggestedMax: 200,
}
}]
}
}
};
var ctx = document.getElementById('canvas').getContext('2d');
window.myLine = new Chart(ctx, config);
canvas {
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="width:75%;">
<canvas id="canvas"></canvas>
</div>
score:1
I've been wanting to let everyone know a way for react chart.
import React from "react";
import { LineController } from 'chart.js';
import Chart from 'chart.js/auto';
class MultiLineController extends LineController {
draw() {
const ctx = this.chart.ctx;
const meta = this.getMeta();
const points = meta.data || [];
const colors = this.getDataset().colors || [];
const area = this.chart.chartArea;
colors.forEach((color, idx) => {
meta.dataset.options.borderColor = color;
meta.dataset.draw(ctx, area, idx, 2);
});
meta.dataset.draw(ctx, area, colors.length, points.length - colors.length);
}
}
MultiLineController.id = "multicolorLine";
MultiLineController.defaults = LineController.defaults;
Chart.register(MultiLineController);
export default function App() {
React.useEffect(() => {
const ctx = document.getElementById("line-chart").getContext("2d");
window.lineChart = new Chart(ctx, {
type: 'multicolorLine',
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],
colors: ['red', 'green', 'blue', 'yellow']
}]
},
options: {}
});
return () => window.lineChart.destroy();
}, []);
return (
<div style={{width: '100%', height: 300}}>
<canvas id="line-chart" />
</div>
);
}
Here is screenshot of this chart. React Chart Component implemented by chart.js
score:1
In V3 you can make use of the segment
option in the dataset to style specific line parts:
new Chart('myChart', {
type: 'line',
data: {
labels: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
datasets: [{
label: 'My Dataset',
data: [101, 122, 103, 115, 95, 94, 100, 108, 112, 115, 119, 120, 109, 108, 105, 116, 117, 108, 109, 114],
segment: {
borderColor: (ctx) => {
const xVal = ctx.p1.parsed.x;
if (xVal <= 7) {
return 'gray';
} else if (xVal <= 15) {
return 'red'
} else {
return 'blue'
}
},
},
}]
},
options: {}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.1/chart.js"></script>
<canvas id="myChart" height="70"></canvas>
score:3
The Plugin Core API offers a range of hooks that may be used for performing custom code. You can use the afterLayout
hook to create a linear gradient through CanvasRenderingContext2D.createLinearGradient()
.
In the following example, the linear gradient is created from the colors defined in data.dataset.colors
.
new Chart('myChart', {
type: 'line',
plugins: [{
afterLayout: chart => {
var ctx = chart.chart.ctx;
var xAxis = chart.scales['x-axis-0'];
var gradientStroke = ctx.createLinearGradient(xAxis.left, 0, xAxis.right, 0);
var dataset = chart.data.datasets[0];
dataset.colors.forEach((c, i) => {
var stop = 1 / (dataset.colors.length - 1) * i;
gradientStroke.addColorStop(stop, dataset.colors[i]);
});
dataset.backgroundColor = gradientStroke;
dataset.borderColor = gradientStroke;
dataset.pointBorderColor = gradientStroke;
dataset.pointBackgroundColor = gradientStroke;
dataset.pointHoverBorderColor = gradientStroke;
dataset.pointHoverBackgroundColor = gradientStroke;
}
}],
data: {
labels: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
datasets: [{
label: 'My Dataset',
data: [101, 122, 103, 115, 95, 94, 100, 108, 112, 115, 119, 120, 109, 108, 105, 116, 117, 108, 109, 114],
fill: false,
colors: ['gray', 'gray', 'gray', 'gray','gray', 'gray', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue']
}]
},
options: {
scales: {
yAxes: [{
ticks: {
min: 0
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.min.js"></script>
<canvas id="myChart" height="70"></canvas>
score:4
Latest versions of ChartJS allow you to customize line segments individually, which can be used to change the color and also the style (dashed etc) of a specific segment.
const config = {
type: 'line',
data: {
labels: Utils.months({count: 7}),
datasets: [{
label: 'My First Dataset',
data: [65, 59, NaN, 48, 56, 57, 40],
borderColor: 'rgb(75, 192, 192)',
segment: {
borderColor: ctx => skipped(ctx, 'rgb(0,0,0,0.2)') || down(ctx, 'rgb(192,75,75)'),
borderDash: ctx => skipped(ctx, [6, 6]),
}
}]
},
options: genericOptions
};
See https://www.chartjs.org/docs/master/samples/line/segments.html for more info.
Source: stackoverflow.com
Related Query
- Chartjs Line Color Between Two Points
- How do I use ChartJS with a background color in the space between two line charts?
- How to add background color between two lines in yAxis Chartjs
- Chart.js drawing line between two points
- How to add background color between two specific lines in Chartjs 3.1
- chartjs line graph points with different color
- ChartJs: remove line between two points
- ChartJS: Draw line between two data points on hover
- ChartJs, How can I get different color fills between my two datasets in a line graph?
- Chartjs - Line between two dots on the Y axis
- ChartJS set color of Line chart points depending on Y Axis
- ChartJS Line Charts - remove color underneath lines
- Chartjs change grid line color
- In ChartJS is it possible to change the line style between different points?
- ChartJs line chart - display permanent icon above some data points with text on hover
- Different color for line segments in ChartJS
- ChartJS - handling of overlapping points in line chart
- Display two line graphs with data starting at different points
- Chart.js - Line charts: draw points between grid lines
- ChartJS getting an unwanted line between first data point and last data point
- Remove background color in chartjs line chart
- How to hide some points inside my line graphic in React ChartJS 2?
- How to reduce the number of points shown on line chartjs chart with a large dataset?
- Mixed Chart calculating difference between two bars - ChartJS
- ChartJS - How to change color of some data points in graph
- Chart.js Fill color at null line and null points Tooltip
- ChartJs - set background color of the space between ticks
- How to move the x-axis values and line points to the middle of two x-axis lines using chartjs?
- Stacked Bar Chart, Two on Same Line w/ Same Color
- Chartjs 2 Line Graph Single Stroke Between Datasets
More Query from same tag
- Error with Chart.js : TypeError: t is undefined
- how to show information of hidden bar graphs also in a Tooltip using chart.js in angular?
- Chart.js updating data
- Referencing locally declared variable into an array for data for chartjs
- How can I force the ".0" portion of a value to explicitly display (Chart.JS)?
- Charts Js Stacked Bar Graph displays no values?
- Chartjs not rendering in navigation stack
- How to use chart.js for financial with dynamic CSV data?
- Chart works uncorrectly chart.js
- How to change Y axis labels
- Hover event in graph Chart.js
- JavaScript doughnut chart with centered hover label
- React-Redux and Chart.js props Do not rerender
- ChartJS: Limit label's length on an axis and show a tooltip on hover?
- Chart.js is not working on my Browser
- Avoiding Empty Curly Braces in MongoDB Aggregation
- zoom and pan on charts in angular
- chartjs & asp.net: Cannot read property 'labels' of undefined
- Creating Chart.js using API data in react
- Fix ChartJS Values not Well Displayed
- Have text displayed instead of numerical values on y axis
- Chat js with respose ajax
- Am using Chart.js and am trying to list the attendances for the last 7 days
- then function not returning values Angular js
- Setting ticks of Chart.js in html.erb file
- Change Step Area Color Based on Value
- How do I correctly include Moment.js and Chart.js with RequireJS when I want to create charts with time scales?
- Chart.js 3+, Firefox 68 and Angular: "ReferenceError: "ResizeObserver is not defined"
- ChartJS Doughnout Chart Pie Offset on Hover
- Chart.js data not initially loaded