score:7
Accepted answer
Dotted Line using Chart.js
You can extend the line chart type to do this
Preview
Script
Chart.types.Line.extend({
name: "LineAlt",
initialize: function (data) {
var strokeColors = [];
data.datasets.forEach(function (dataset, i) {
if (dataset.dottedFromLabel) {
strokeColors.push(dataset.strokeColor);
dataset.strokeColor = "rgba(0,0,0,0)"
}
})
Chart.types.Line.prototype.initialize.apply(this, arguments);
var self = this;
data.datasets.forEach(function (dataset, i) {
if (dataset.dottedFromLabel) {
self.datasets[i].dottedFromIndex = data.labels.indexOf(dataset.dottedFromLabel) + 1;
self.datasets[i]._saved = {
strokeColor: strokeColors.shift()
}
}
})
},
draw: function () {
Chart.types.Line.prototype.draw.apply(this, arguments);
// from Chart.js library code
var hasValue = function (item) {
return item.value !== null;
},
nextPoint = function (point, collection, index) {
return Chart.helpers.findNextWhere(collection, hasValue, index) || point;
},
previousPoint = function (point, collection, index) {
return Chart.helpers.findPreviousWhere(collection, hasValue, index) || point;
};
var ctx = this.chart.ctx;
var self = this;
ctx.save();
this.datasets.forEach(function (dataset) {
if (dataset.dottedFromIndex) {
ctx.lineWidth = self.options.datasetStrokeWidth;
ctx.strokeStyle = dataset._saved.strokeColor;
// adapted from Chart.js library code
var pointsWithValues = Chart.helpers.where(dataset.points, hasValue);
Chart.helpers.each(pointsWithValues, function (point, index) {
if (index >= dataset.dottedFromIndex)
ctx.setLineDash([3, 3]);
else
ctx.setLineDash([]);
if (index === 0) {
ctx.moveTo(point.x, point.y);
}
else {
if (self.options.bezierCurve) {
var previous = previousPoint(point, pointsWithValues, index);
ctx.bezierCurveTo(
previous.controlPoints.outer.x,
previous.controlPoints.outer.y,
point.controlPoints.inner.x,
point.controlPoints.inner.y,
point.x,
point.y
);
}
else {
ctx.lineTo(point.x, point.y);
}
}
ctx.stroke();
}, this);
}
})
ctx.restore();
}
});
and then
var data = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [
{
...
dottedFromLabel: "April"
}
],
};
...
new Chart(ctx).LineAlt(data);
Fiddle - https://jsfiddle.net/3gxjfndm/3/
score:0
With chart.js V3 you can make use of segments to achieve this:
const options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderColor: 'orange',
segment: {
borderDash: ctx => ((ctx.chart.data.datasets[ctx.datasetIndex].data.length - 1) === ctx.p1DataIndex ? [2, 2] : undefined)
}
}]
},
options: {}
}
const ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.6.0/chart.js"></script>
</body>
Source: stackoverflow.com
Related Query
- Dotted lines using Chart.js
- Why is my line chart using multiple lines to connect random points? (Chart.js)
- I am using chart js to draw a chart. I did everything right but i don't know why the x axis and y axis label is not comming in chart. code below
- How to run Chart.js samples using source code
- How to add text inside the doughnut chart using Chart.js?
- Converting Chart.js canvas chart to image using .toDataUrl() results in blank image
- ChartJS New Lines '\n' in X axis Labels or Displaying More Information Around Chart or Tooltip with ChartJS V2
- Moving vertical line when hovering over the chart using chart.js
- create a multi line chart using Chart.js
- How can I make two of my lines in Chart JS thicker
- How to add an on click event to my Line chart using Chart.js
- Display line chart with connected dots using chartJS
- Dynamically update the options of a chart in chartjs using Javascript
- How to save Chart JS charts as image without black background using blobs and filesaver?
- How to Draw Gantt chart using chart js or other libraries
- Chart JS Fill Between two lines
- How can I draw dotted line using chartjs?
- Detecting hover events over parts of a chart using Chart.js
- Overlapping Bar Chart using Chart.js
- How to add text in centre of the doughnut chart using Chart.js?
- How to create stacked bar chart using react-chartjs-2?
- Display a limited number of labels only on X-Axis of Line Chart using Chart.js
- Chart.js - Add text/label to bubble chart elements without using tooltips?
- How to display data labels outside in pie chart with lines in ionic
- How to access labels array using chart plugin (Chart.pluginService.register) in Chartjs 2.x?
- Blank PNG image using Chart JS . toBase64Image() function
- Chartjs - show elements in all datasets on hover using bar chart
- How to display the labels in doughnut chart using ng2 charts?
- How to create single value Doughnut or Pie chart using Chart.js?
- ChartJS Radar Chart radar lines color?
More Query from same tag
- Django chart.js multi axis line chart
- Is it possible to update the width of a chart in chart.js?
- Dataset push in for loop only 1 time instead of 3
- Dynamically Setting Height of Chart
- How can i add an image as background in Chartjs?
- ChartJS canvas not displaying colors in edge browser
- Chart is disappearing during # link
- Trying to update background color on chart.js
- Chartjs displays numbers not time
- Graph is not displayed
- I want to draw a horizontal line
- 2 or more charts on same page?
- ChartJS: Full Date for xAxis Labels?
- ChartJS 2.9.4 can't overlay line data on Horizontal Bar chart
- Rotating a chart.js pie chart with two datapoints to align both sectors on horizontal axis
- chartjs-plugin-error-bars display error-bars without displaying label on a horizontalBar chart
- Chart.js Update to X Axis Not Updating
- Error in compilation for chart.js Expected method shorthand in object literal ('{label() {...}}'
- ChartJS - Line chart issue with only 1 point
- How to dynamically set backgroundColor and borderColor barchart unsing Chart.js
- How to change legend icon on hover Chartjs
- How to make bars different colours in Chart.js with csv data?
- Chart JS not showing On hover with small data
- Chartjs ignore values and draw chart
- How to change React line chart tooltip title font family in chart.js
- How to hide tooltip for selected datasets? - Chart.js v2.8
- PrimeNg chart label rotation with Angular2
- How to get the length (height) of the vertical bar in Chart.js
- Call ChartJS details in main Javascript file
- ChartJS creating var takes each character and outputs undefined