遵循此link扩展Scatter类型并使用draw()函数在中心打印“No Data Found”,但使用Y轴刻度。
以下是代码:
Chart.defaults.derivedScatter = Chart.defaults.scatter;
var ctx = canvas.getContext("2d");
var custom = Chart.controllers.scatter.extend({
draw: function() {
Chart.controllers.scatter.prototype.draw.call(this);
this.chart.chart.ctx.textAlign = "center";
this.chart.chart.ctx.font = "11px Arial";
this.chart.chart.ctx.fillText("No data found", 45, 100);
}
});
Chart.controllers.derivedScatter = custom;
chart = new Chart(ctx, {
type: "derivedScatter",
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true,
max:0.10,
stepSize:0.001,
callback: function(label, index, labels) {
var n = parseFloat(label);
return (Math.round(n*1000)/10).toFixed(1);
}
},
gridLines: {
display: false,
drawBorder: false
}
}],
xAxes: [{
display: false,
gridLines: {
display: false
}
}]
},
legend: {
display:false
},
responsive: false
}
});
我正在获得Y轴刻度,但我没有看到在图表中心找不到任何数据作为文本。它似乎不起作用。
注意:Here它没有说他们有Scatter的内置类型。那是什么原因吗?
任何其他方法或任何其他帮助将不胜感激。
答案 0 :(得分:1)
无法正常工作的原因是,您尚未初始化图表的data
属性。为了使用扩展数据集控制器,您必须在构建图表时初始化/设置data.datasets
属性,如下所示:
...
chart = new Chart(ctx, {
type: "derivedScatter",
data: {
datasets: [{}]
},
options: {
...
注意: 重要的部分是初始化datasets
属性,它不一定要包含任何数据。
ᴡᴏʀᴋɪɴɢᴡᴏʀᴋɪɴɢxᴀᴍᴘʟᴇᴀᴍᴘʟᴇ
var ctx = canvas.getContext("2d");
Chart.defaults.derivedScatter = Chart.defaults.scatter;
var custom = Chart.controllers.scatter.extend({
draw: function() {
Chart.controllers.scatter.prototype.draw.call(this);
this.chart.chart.ctx.textAlign = "center";
this.chart.chart.ctx.font = "11px Arial";
this.chart.chart.ctx.fillText("No data found", 80, 80);
}
});
Chart.controllers.derivedScatter = custom;
chart = new Chart(ctx, {
type: "derivedScatter",
data: {
datasets: [{}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
max: 0.10,
stepSize: 0.001,
callback: function(label, index, labels) {
var n = parseFloat(label);
return (Math.round(n * 1000) / 10).toFixed(1);
}
},
gridLines: {
display: false,
drawBorder: false
}
}],
xAxes: [{
display: false,
gridLines: {
display: false
}
}]
},
legend: {
display: false
},
responsive: false
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.0/Chart.min.js"></script>
<canvas id="canvas"></canvas>