javascript - Adding text inside 2 different Doughuts chartjs -


i have 2 charts, each 1 diplay data month title shows witch month chart showing. shows title both months in both charts, on eachother. wonder if did manage display wanted text in middle of right chart. demo

<canvas id="mychart"></canvas> <canvas id="mychart1"></canvas>  <script type="javascript">     var data = {         labels: [             "white",             "blue",             "yellow"         ],         datasets: [{             data: [300, 50, 100],             backgroundcolor: ["#fff", "#36a2eb", "#ffce56"],             hoverbackgroundcolor: ["#fff", "#36a2eb", "#ffce56"]         }]     };      var deliverychart = new chart(document.getelementbyid('mychart'), {         type: 'doughnut',         elementbyid: ('mychart'),         data: data,         options: {responsive: true, legend: {display: false}}     });      chart.pluginservice.register({         beforedraw: function (chart) {             var width = chart.chart.width, height = chart.chart.height, ctx = chart.chart.ctx;             ctx.restore();              var fontsize = (height / 114).tofixed(2);             ctx.font = fontsize + "em sans-serif";             ctx.textbaseline = "middle";              var text = "oktober",                 textx = math.round((width - ctx.measuretext(text).width) / 2),                 texty = height / 2;             ctx.filltext(text, textx, texty);             ctx.save();         }     });      var data = {         labels: ["red", "blue", "yellow"],         datasets: [{             data: [300, 50, 100],             backgroundcolor: ["#ff6384", "#36a2eb", "#ffce56"],             hoverbackgroundcolor: ["#ff6384", "#36a2eb", "#ffce56"]         }]     };      var promiseddeliverychart = new chart(document.getelementbyid('mychart1'), {         type: 'doughnut',         elementbyid: ['mychart1'],         data: data,         options: {responsive: true, legend: {display: false}}     });      chart.pluginservice.register({         beforedraw: function (chart) {             var width = chart.chart.width,                 height = chart.chart.height,                 ctx = chart.chart.ctx;             ctx.restore();              var fontsize = (height / 114).tofixed(2);             ctx.font = fontsize + "em sans-serif";             ctx.textbaseline = "middle";              var text = "november",                 textx = math.round((width - ctx.measuretext(text).width) / 2),                 texty = height / 2;             ctx.filltext(text, textx, texty);             ctx.save();         }     }); </script> 

i think because you're registering 2 beforedraw functions. these not bound particular chart. if want text refer data chart, you're going have update use data in chart object.

to this, i've added title options pass in (this has disadvantage of displaying title on each doughnut, can work around that). ondraw function uses title text display in centre of doughnut.

var promiseddeliverychart = new chart(document.getelementbyid('mychart1'), {   type: 'doughnut',   elementbyid:['mychart1'],   data: data2,   options: {     responsive: true,     legend: {       display: false     },     title: {             display: true,             text: 'november'         }   } });     chart.pluginservice.register({   beforedraw: function(chart) {     var width = chart.chart.width,         height = chart.chart.height,         ctx = chart.chart.ctx;      ctx.restore();     var fontsize = (height / 114).tofixed(2);     ctx.font = fontsize + "em sans-serif";     ctx.textbaseline = "middle";     console.log (chart.options.title.text);     var text = chart.options.title.text;         textx = math.round((width - ctx.measuretext(text).width) / 2),         texty = height / 2;      ctx.filltext(text, textx, texty);     ctx.save();   } }); 

i've updated fiddle.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -