score:1
The Plugin Core API offers a range of hooks that may be used for performing custom code. You can use the beforeDraw
hook to draw lines of different style between different datapoints using text directly on the canvas using CanvasRenderingContext2D
.
Please take a look at the runnable code snippet below:
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'line',
plugins: [{
beforeDraw: chart => {
var ctx = chart.chart.ctx;
ctx.save();
let xAxis = chart.scales['x-axis-0'];
let yAxis = chart.scales['y-axis-0'];
let dataset = chart.data.datasets[0];
var valueFrom = null;
var valueFromIndex = 0;
var xFrom = null;
let yFrom = null;
ctx.strokeStyle = dataset.borderColor;
dataset.data.forEach((value, index) => {
if (value != null) {
var x = xAxis.getPixelForTick(index);
var y = yAxis.getPixelForValue(value);
if (valueFrom != null) {
ctx.lineWidth = dataset.borderWidth;
if (index - valueFromIndex > 1) {
ctx.setLineDash([5, 5]);
} else {
ctx.setLineDash([]);
}
ctx.beginPath();
ctx.moveTo(xFrom, yFrom);
ctx.lineTo(x, y);
ctx.stroke();
}
valueFrom = value;
valueFromIndex = index;
xFrom = x;
yFrom = y;
}
});
ctx.restore();
}
}],
data: {
labels: ["A", "B", "C", "D", "E", "F", "G"],
datasets: [{
label: 'My Dataset',
data: [12, 19, null, 3, 6, null, 8],
backgroundColor: 'rgba(255, 99, 132, 0.6)',
borderColor: 'rgb(255, 99, 132)',
borderWidth: 3,
showLine: false,
}]
},
options: {
animation: {
duration: 0
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<canvas id="myChart" height="90"></canvas>
Source: stackoverflow.com
Related Query
- Dashed line for missing data in Chart.JS (spanGaps style)
- Fetch more data for line chart onZoom/onPan issues
- Vue Chart.js -- can I give a line chart a default value for missing data?
- Show label for every data point in line chart
- getting additional value fields from data source for dx.chartjs doughnut chart
- Line chart plotting multiple points for duplicate data on x and y axis using chart.js
- Chart Js Change Label orientation on x-Axis for Line Charts
- Chartjs random colors for each part of pie chart with data dynamically from database
- line chart with {x, y} point data displays only 2 values
- Chart.js how to show cursor pointer for labels & legends in line chart
- show label in tooltip but not in x axis for chartjs line chart
- How to add second Y-axis for Bar and Line chart in Chart.js?
- chart.js Line chart with different background colors for each section
- Chart.js Timeseries chart - formatting and missing data values
- Chart JS show multiple data points for the same label
- Chart.js: Line chart with partial dashed line
- dump missing date in data for chartjs
- Show data dynamically in line chart - ChartJS
- Chartjs - data format for bar chart with multi-level x-axes
- ChartJS: Draw vertical line at data point on chart on mouseover
- How to hide the y axis and x axis line and label in my bar chart for chart.js
- ChartJs line chart - display permanent icon above some data points with text on hover
- How to use 'time' (data from database, data type: timestamp ) for plotting graph in Chart JS
- how to set chart.js grid color for line chart
- Mouse over on line chart data active other data-set in Chart.js
- Chart js: Update line chart having two data sets
- Show "No Data" message for Pie chart with no data
- Multiple line labels for chart js
- How to show data values in top of bar chart and line chart in chart.js 3
- ChartJS bar chart fixed width for dynamic data sets
More Query from same tag
- angular-chart.js custom color based on chart label values
- Creating a real time line chart using chartjs and firebase realtime db
- how to programmatically make a line chart point active/highlighted
- 'barradius' for Barchart in ChartJS is not working
- Chart JS grouped sub labels
- ng2 charts on Angular 9 not responsive
- Chart js scatter - display label on hover
- ChartJS tooltip when having multiple lines on a time graph
- Links in bar charts (charts.js)
- Single point on multiple line linear graph with Chart.js
- vue-chart.js doesn't show data
- chart.js not getting linked with online database and php
- Trying to get _index of clicked bar in ChartJS
- How change chart height in chart.js
- Angular Chart.js Multiple Canvas
- Chartjs: Is it possible to hide the data labels on the axis but show up on the graph on hover?
- Chart JS Y-axis labeling
- Change size of bubble radius without changing r value
- Change List<Object> into List<List<int>> using LINQ
- ChartJS set yAxes "ticks"
- how to use vuechartkick in nuxt js
- Angular: chart.js chart is not displaying
- Can I make Y axis begin at specific value in ChartJS?
- Mixed bubble chart with line chart in vue-chartjs4
- Chartsjs multi-axis, make the scale appear/disappear when the dataset is activated/deactivated
- Getting Error regarding :Cannot read property 'length' of undefined
- Why wont the json_encoded data show
- How to use async data for use with chart.js in ionic
- Ionic + Angular Charts: Uncaught Error: Chart.js library needs to included
- chart.js - Supplied parameters do not match any signature of call target (angular2)