score:8
Accepted answer
The option dataset.order
has similar effect as the z-index.
- Datasets with higher
order
are drawn first - Datasets with no or lower order are drawn last, hence appear on top
Therefore, adding order: 1
to your line datasets should solve the problem.
var newDataLine = {
...
order: 1
};
score:1
Instead of defining multiple datasets, you could proceed as follows:
- First convert your line chart into a scatter chart.
- Then draw the lines directly on the canvas using the Plugin Core API. The API offers a range of hooks that may be used for performing custom code. You can use the
beforeDraw
hook to draw connection lines of different colors between data points and to the open end of the chart.
Note that you have to define
xAxes.ticks.max
in order to obtain the open end line at the right of the chart.
Please take a look at below runnable code snippet and see how it works.
new Chart('line-chart', {
type: "scatter",
plugins: [{
beforeDraw: chart => {
var ctx = chart.chart.ctx;
ctx.save();
var xAxis = chart.scales['x-axis-1'];
var yAxis = chart.scales['y-axis-1'];
var dataset = chart.data.datasets[0];
var y = yAxis.getPixelForValue(0);
dataset.data.forEach((value, index) => {
var xFrom = xAxis.getPixelForValue(value.x);
var xTo;
if (index + 1 < dataset.data.size) {
xTo = xAxis.getPixelForValue(dataset.data[index + 1].x);
} else {
xTo = xAxis.right;
}
ctx.strokeStyle = dataset.backgroundColor[index];
ctx.lineWidth = 4;
ctx.beginPath();
ctx.moveTo(xFrom, y);
ctx.lineTo(xTo, y);
ctx.stroke();
});
ctx.restore();
}
}],
data: {
datasets: [{
data: [
{ x: 0, y: 0 },
{ x: 1, y: 0 },
{ x: 2, y: 0 }
],
backgroundColor: ['red', 'blue', 'green'],
borderColor: ['red', 'blue', 'green'],
pointRadius: 8,
pointHoverRadius: 8,
}],
},
options: {
layout: {
padding: {
left: 10,
right: 10
}
},
legend: {
display: false
},
tooltips: {
enabled: false
},
scales: {
yAxes: [{
ticks: {
display: false
},
gridLines: {
display: false,
}
}],
xAxes: [{
ticks: {
display: false,
max: 3
},
gridLines: {
display: false
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js"></script>
<canvas id="line-chart" height="30"></canvas>
Source: stackoverflow.com
Related Query
- Chartjs Datasets overlapping and z-index
- Chartjs different length of labels and datasets
- How to 1. pass two datasets and 2.have permanent label on charts in Chartjs
- ChartJS Tooltips with time axis and multiple datasets
- ChartJs How to display horizontal and vertical lines through the datasets points with their values on axes?
- Chartjs v2 - format tooltip for multiple data sets (bar and line)
- ChartJS canvas not displaying rgba colors in IE, Safari and Firefox
- Chartjs cannot read property datasets of undefined
- Chartjs 2 - Stacked bar and unstacked line on same chart with same y axis
- chartjs datalabels change font and color of text displaying inside pie chart
- Django and ChartJS
- Change point size and color on hover in chartjs
- ChartJS and jsPDF - why the background is black?
- how to change background in chartjs and remove background lines?
- chartjs - top and bottom padding of a chart area
- How to change the color of legend in chartjs and be able to add one more legend?
- ChartJS - Line Chart with different size datasets
- Usage of ChartJS v3 with TypeScript and Webpack
- Category scale on Y-axis and time on x-axis in bubble chart in Chartjs
- How to remove gridlines and grid labels in Chartjs Radar?
- Chartjs - show elements in all datasets on hover using bar chart
- How to show data values or index labels in ChartJs (Latest Version)
- How to feed hour and minute data into chartJS
- Create a rounded bar graph with Angular and chartJS
- Chartjs : data labels getting overlapped in smaller datasets using chartjs-plugin-datalabels
- Displaying mixed types of legends (bar and lines) with Chartjs
- Chartjs not rendering chart and no error thrown
- ChartJS Line chart cut off at the top and bottom
- How to get onClick Event for a Label in ChartJS and React?
- Update charts in chartjs and angular
More Query from same tag
- chartjs - no smooth rendering on time series plot
- automatic Y-axis scale: max is always as 120% of highest value in ChartJS
- How to add label square to Bar Chart using Chart.js
- Chart.js Dropdown to select 1 day, yesterday and 7 days
- Chart.js add padding to scales
- How to Add X axis Padding in chart js Line Graph
- build dynamic array for charts.js
- Chart.js How should I do to make vertical dotted line
- chart.js how to configure the lable for both axis
- Chart.js Line Chart x-Axes label rounding
- Dynamically change type with react-chartjs-2 React
- connect filter to multiple charts asp.net core mvc
- Chartjs Stacked bar have a stack id per x value
- Chart.js dynamically-created charts do not obey chart options
- Ng2-charts Time xAxes : unwanted high zoom on click
- Add border to Chart.js tooltips
- How to apply gradient color in chart.js?
- Define backgroundColor from data in chartJS
- How to remove percentage calculation on top of bar in ng2-chart bar chart
- react-chartjs state update error
- Different labels in tooltip with chartjs
- How can I control the placement of my Chart.JS pie chart's legend, as well as its appearance?
- Failing to render chart – "can't acquire context from the given item"
- Legend option destroys pie chart in Chart.js
- ChartJs: Different color fill between points makes lines straight...need curves (lineTension)
- Ionic 3 Angular Chart.js update data
- Chart.js 2.x - How to get handle of only clicked datapoint?
- ChartJS - TypeError: Cannot read property '_model' of null - Angular
- Chart from chart.js to pdf
- How to Remove axis Lines from chart in chart js