score:8
breaking (broken) lines 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 fillcolors = [];
var strokecolors = [];
data.datasets.foreach(function (dataset, i) {
if (dataset.data.indexof(null) !== -1) {
fillcolors.push(dataset.fillcolor);
strokecolors.push(dataset.strokecolor);
dataset.fillcolor = "rgba(0,0,0,0)"
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.data.indexof(null) !== -1) {
self.datasets[i]._saved = {
fillcolor: fillcolors.shift(),
strokecolor: strokecolors.shift()
}
}
})
},
draw: function () {
chart.types.line.prototype.draw.apply(this, arguments);
// from chart.js library code
var hasvalue = function (point) {
return point.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._saved) {
ctx.linewidth = self.options.datasetstrokewidth;
ctx.strokestyle = dataset._saved.strokecolor;
ctx.fillstyle = dataset._saved.fillcolor;
// adapted from chart.js library code
var pointswithvalues = chart.helpers.where(dataset.points, hasvalue);
dataset.points.foreach(function (point, index) {
if (index === 0 || (hasvalue(point) && !hasvalue(dataset.points[index - 1])))
point.start = true;
});
var currentstartpoint = undefined;
chart.helpers.each(pointswithvalues, function (point, index) {
if (point.start) {
if (currentstartpoint) {
ctx.lineto(pointswithvalues[index - 1].x, self.scale.endpoint);
ctx.lineto(currentstartpoint.x, self.scale.endpoint);
ctx.closepath();
ctx.fill();
}
currentstartpoint = point;
ctx.beginpath();
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.lineto(pointswithvalues[pointswithvalues.length - 1].x, self.scale.endpoint);
ctx.lineto(currentstartpoint.x, self.scale.endpoint);
ctx.closepath();
ctx.fill();
}
})
ctx.restore();
}
});
and then
var data = {
...
datasets: [
{
...
data: [65, 59, null, 81, 52, 62, null, 56, 40],
}
],
};
...
new chart(ctx).linealt(data);
fiddle - https://jsfiddle.net/hbrhz2q4/
score:1
i'm not sure if chartjs itself can natively do this.
however one way i did something similar was to make the line graph out of multiple datasets. one dataset would end when a null value is reached, and the next dataset would start. this would of course requires parsing all this data before passing it into var linechartdata
you can make all the datasets have the same colors and highlights and the graph should render with spaces where null data would have been found.
score:12
i know this is pretty old, but i think there is a better solution now. replace all your null values with number.nan
http://www.chartjs.org/docs/#line-chart-chart-options
below is a data point i replaced with nan on the red line
score:34
in case you still reach this page, new version supports skipping missing data. . if you want the lines to be connected by skipping missing data, you can set spangaps: true in the options.
then if you have null or nan for missing data, it will skip it and connect to the next point.
.....
showtooltips: true,
options: {
spangaps: true,
......
Source: stackoverflow.com
Related Query
- Not drawing null values using 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
- Display ellipsis for null or empty values in the bar chart using Chart.js
- null values for instead of date causes the browser to crash while using chart js
- Uncaught TypeError: Chart is not a constructor when using Chart.js
- Adding custom text to Bar Chart label values using Chart.js
- Show point values in Radar Chart using chart.js
- Chart js logarithmic line chart showing NaN values instead of null
- Chartjs - Donut Chart label for small values not visible
- chart is not getting updated from the values it received from Jquery
- Chart JS chart not rendering in ArcGIS popup when using navigation arrows
- Force ChartJS to show Doughnut chart with null values
- Bar Chart Not Stacking When Using ChartJs
- ChartJS chart not scaling after adding values
- Why is this bar chart using chart.js not rendering in react.js?
- Datalabels of Chart JS can not display full values
- I am using chart.js I sent my time values throw my api as timestamp know i need to show them in the chart as date format
- Using Chart.js onClick function, can you execute code from an undefined or null result?
- Using number/text input field to set the data values in ChartJs stops the chart from being displayed
- My chart is not shown on browser screen using chart.js in meteor
- Display values in Pareto chart using Chart.js 2.0.2
- How to display the values inside the pie chart of PrimeNG (chart) using JavaScript or Angular
- Bar Chart not displaying when using chart.js
- Using Chart.js 2.0, display line chart values
- ChartJS Bar Chart not respecting disabled legend when using cdn
- (Chart.js) Is there a way to compare one chart with another so as not to have this inconsistent effect of small values being as big as big values?
- Trying to create a custom tooltip in a doughnut chart using chartsjs, but it is not working
- chart does not change the values when the variable changes the value
- Not able to see values on bar chart
- Can not update bar chart values in Chart.js 2.0.0 alpha3
More Query from same tag
- How can I show chartjs datalabels only last bar?
- Chart JS 2.x: How to show a tooltip in a timeline chart?
- On hover bar is overlapped by label chartjs
- Chart.js 3.3.0 - Draw text on top of chart
- Show labels on each sector to polar chart using angular js chart
- how to use vuechartkick in nuxt js
- How get values on legend label withn Chart js on Angular
- Remove only y-axis line from chat drawn using chart.js 1.0.2
- Position tooltip based on mouse position
- Graph Chart.js dropdown menu - chart rendering
- Sending 2 fetched result into AJAX
- Uncaught ReferenceError: require is not defined using react-chartjs.min.js
- How to change background color of labels in line chart from chart.js?
- Chart.JS change Text Color
- In chart.js how can I change the x axis on my line \chart from January-December to October-September? Basically, fiscal year instead of calendar year
- Reactive Vue Chart.js component using Typescript
- is it possible to sum up the properties of an array of objects and to filter it according to another property?
- generator-angular-fullstack cannot add angular-chart.js
- How do I use Chart.js and Bootstrap at same time? (various jQuery versions)
- Passing data from a controller to ChartJS - Laravel
- how can i add value on bar in the charts in chart.js vs 3.x
- How to sum/divide array values in chart.js?
- Accessing data in an associative array in CodeIgniter 3
- Rendering a chart.js component twice in a page
- Keep two Pie Charts Side by Side
- ChartJS: Highlight dataset in a stacked bar chart when hovering over the legend?
- chartjs and making an onClick action for a specific element?
- chartJS, how to disable user from clicking on legend names and changing the graph?
- Chartjs random colors for each part of pie chart with data dynamically from database
- Charts in Ionic 2