我正在使用Chart.js绘制图表.我的数据集有一些空值,实际上chart.js以这种方式在空值的前一个点和后一个点之间绘制一条线:
红色箭头表示空值的位置
我使用的配置很简单:
var lineChartData = {
"datasets": [{
"label": "defi score",
"data": data,
"pointStrokeColor": "#fff",
"fillColor": "rgba(220,220,220,0.5)",
"pointColor": "rgba(220,220,220,1)",
"strokeColor": "rgba(220,220,220,1)",
}],
"labels": labels
};
var ctx = document.getElementById("chart_per_week").getContext("2d");
var myLine = new Chart(ctx).Line(lineChartData, {
responsive: true,
scaleFontColor: "#FF5972",
bezierCurve: false
});
Run Code Online (Sandbox Code Playgroud)
谢谢您的帮助
Sel*_*lay 21
如果您仍然访问此页面,新版本支持跳过丢失的数据..如果希望通过跳过丢失的数据来连接线路,可以在选项中设置spanGaps:true.
然后,如果您有缺失数据的null或NaN,它将跳过它并连接到下一个点.
.....
showTooltips: true,
options: {
spanGaps: true,
......
Run Code Online (Sandbox Code Playgroud)
我知道这已经很老了,但我认为现在有一个更好的解决方案.用Number.NaN替换所有空值
http://www.chartjs.org/docs/#line-chart-chart-options
下面是我在红线上用NaN替换的数据点
您可以扩展折线图类型来执行此操作
预习

脚本
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();
}
});
Run Code Online (Sandbox Code Playgroud)
然后
var data = {
...
datasets: [
{
...
data: [65, 59, null, 81, 52, 62, null, 56, 40],
}
],
};
...
new Chart(ctx).LineAlt(data);
Run Code Online (Sandbox Code Playgroud)
小提琴-https: //jsfiddle.net/hbrhz2q4/
| 归档时间: |
|
| 查看次数: |
11557 次 |
| 最近记录: |