score:14
The borderDash
property (scroll to Line Configuration) is the key to your problem.
The thing is, the full chart is drawn with a border dash, you cannot choose where it starts and where it ends.
A simple workaround is to create two identical datasets. One dotted and one with a plain line. Then you remvoe the last data of your plain one, and they both will be displayed as how you want it.
You can see the full code in this jsFiddle, and here is its result :
Note :
- Since there are two datasets now, the legend will display both of them. Setting the
display
to false fixes it (more or less). - The declaration order doesn't matter since the plain line will always overwrite the dotted one.
- Having a bezier curve (
tension
property > 0) can create a display problem since the data is not the same in both datasets.
score:2
You can create a scatter
chart and draw the lines directly on the canvas using the Plugin Core API. The API offers a range of hooks that can be used for performing custom code. The advantage of this approach is that you can customize the style of every single connection line (width, color, dash pattern etc.).
const labels = [1, 2, 3, 4, 5, 6];
const values = [12, 19, 3, 5, 2, 3];
const data = labels.map((label, index) => ({ x: label, y: values[index]}));
var lineChart = new Chart(document.getElementById("chart"), {
type: "scatter",
plugins: [{
afterDraw: chart => {
var ctx = chart.chart.ctx;
var xAxis = chart.scales['x-axis-1'];
var yAxis = chart.scales['y-axis-1'];
chart.config.data.datasets[0].data.forEach((value, index) => {
if (index > 0) {
var valueFrom = data[index - 1];
var xFrom = xAxis.getPixelForValue(valueFrom.x);
var yFrom = yAxis.getPixelForValue(valueFrom.y);
var xTo = xAxis.getPixelForValue(value.x);
var yTo = yAxis.getPixelForValue(value.y);
ctx.save();
ctx.strokeStyle = '#922893';
ctx.lineWidth = 2;
if (index + 1 == data.length) {
ctx.setLineDash([5, 10]);
}
ctx.beginPath();
ctx.moveTo(xFrom, yFrom);
ctx.lineTo(xTo, yTo);
ctx.stroke();
ctx.restore();
}
});
}
}],
data: {
datasets: [{
label: "My Dataset",
data: data,
borderColor: '#922893',
pointBackgroundColor: "transparent"
}]
},
options: {
legend: {
display: false
},
scales: {
xAxes: [{
ticks: {
stepSize: 1
}
}],
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<canvas id="chart" height="90"></canvas>
Source: stackoverflow.com
Related Query
- In ChartJS is it possible to change the line style between different points?
- Chartjs Line Color Between Two Points
- How to change cursor style to pointer on hovering over points of the chart?
- Change the style of the border in ChartJS
- How to reduce the number of points shown on line chartjs chart with a large dataset?
- chartjs line graph points with different color
- How do I change the grid line style on the Y axis in Chart.js?
- How to fix the distance between horizontal points (x-axis) in chartJS
- Chartjs - Line between two dots on the Y axis
- How do I use ChartJS with a background color in the space between two line charts?
- Is it possible to change chartjs line value in one column ? No transition
- Chartjs change grid line color
- Change Chartjs Legend Icon Style
- How to change the color of Chart.js points depending on the label
- Chart.js is it possible to style the labels?
- How to change the color of legend in chartjs and be able to add one more legend?
- ChartJS - Line Chart with different size datasets
- How to change the cursor to a pointer when I hover over a bar in a ChartJS bar chart?
- Chartjs change the specific label color in x axis in callback function
- ChartJs line chart - display permanent icon above some data points with text on hover
- how to change Y axis value dynamically based on user input in Chartjs for Line chart?
- Find intersection between the chart lines in chartjs
- ChartJS Line chart cut off at the top and bottom
- Different color for line segments in ChartJS
- How to show only the data points that have a change in Chartjs?
- Line ChartJS empty / null values doesn't break the line
- ChartJs: Different Fill Colour Between Data Points
- Chart.js Tooltip over the line not only over points
- ChartJS - handling of overlapping points in line chart
- How to Change the Label Strike-Through with light gray on a ChartJS Doughnut?
More Query from same tag
- Problem adding name to X-axis for a chart
- Data label over stacked bar chart
- Chart.js -> Displaying % on pie chart
- Ionic Capacitor chart.js showing blank on ios device
- Angular2 + Chart.js and ng2-charts. Data binding not working as expected
- charts.js Stacked Bar Graphs - selecting row and dataset selected
- ChartJS click on bar and change it's background color
- React chartjs-2 - Increase spacing between legend and chart
- Chart.js different time datasets
- Chart.js what is the new syntax for extending?
- charts.js fixed y label to 8 decimals
- Why is ChartJS combining two charts?
- Remove y and x axis lines and change axis font style in Chartjs
- How to make the whole <canvas> element in Chart.js V3.7.0 display cursor as pointer on hover?
- How to use icon as legend in Chart.js?
- ChartJS Create New chart from variable
- How scale sector in doughnut chartjs on hover?
- Stacked horizontal chart. How to trigger individual hover in the stacked element?
- node js mysql query result rendered to chart.js labels in pug page
- Chart JS version 3+. How to get a tick's label in a tick callback function?
- Laravel & chartjs blank canvas
- Chart.js Multiple dataset
- Snapshot testing ChartJS components with Jest
- Chartjs x axis scaling
- How to add an extra legend item in chart.js?
- Setting ChartJS plugin on vue-chartjs not working
- ChartJs Scatter with Bars
- how can i modify scale labels in angular chart js?
- How to pass Arrays from backing bean to JavaScript to draw a chart using Chart.js in an Ajax call
- ChartJS 3 get height of stacked vertical bar