javascript - Chart.js : Controller for Scatter chart doesn't work for draw function -
following link extend scatter type draw() function print "no data found" in center y axis scale.
here's code:
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 } });
i getting y axis scale don't see no data found text in center of chart. doesn't seem work.
looked @ this , this stack-overflow answers come solution.
note: here doesn't have built in type scatter. reason?
any other approach or other appreciated.
the reason why it's not working because, haven't initialized data
property of chart. in order use extended dataset controller, must initialize / set data.datasets
property while constructing chart, :
... chart = new chart(ctx, { type: "derivedscatter", data: { datasets: [{}] }, options: { ...
note: important part initialize datasets
property , doesn't have contain data.
ᴡᴏʀᴋɪɴɢ ᴇ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>
Comments
Post a Comment