admin管理员组文章数量:1424902
I am using chart.js for drawing doughnut chart. Using 'fillText' I am adding text at the middle part of doughnut chart.But how i can give a background color for the middle
here is my code
javascript
<script>
var doughnutData = [
{
value: 300,
color:"#F7464A",
highlight: "#FF5A5E",
label: "Red"
},
{
value: 50,
color: "#46BFBD",
highlight: "#5AD3D1",
label: "Green"
},
{
value: 100,
color: "#FDB45C",
highlight: "#FFC870",
label: "Yellow"
},
{
value: 40,
color: "#949FB1",
highlight: "#A8B3C5",
label: "Grey"
},
{
value: 120,
color: "#4D5360",
highlight: "#616774",
label: "Dark Grey"
}
];
window.onload = function(){
var ctx = document.getElementById("chart-area").getContext("2d");
var option =
{
//prevents the text vanishing on redraw (when tooltip shows on hover)
showTooltips: false,
//nicer than default bouncing
animationEasing: "easeOut",
//bit smoother with less steps
animationSteps: 40,
//do once on pletion rather than every frame/draw cycle
onAnimationComplete: function () {
//setup the font and center it's position
this.chart.ctx.font = 'Normal 18px Ariel';
this.chart.ctx.textAlign = 'center';
this.chart.ctx.textBaseline = 'middle';
//put the pabel together based on the given 'skilled' percentage
var valueLabel = this.segments[0].value + '%';
//find the center point
var x = this.chart.canvas.clientWidth / 2;
var y = this.chart.canvas.clientHeight / 2;
//hack to center different fonts
var x_fix = 0;
var y_fix = 2;
//render the text
this.chart.ctx.fillText("Text", x + x_fix, y + y_fix);
this.chart.ctx.fillStyle("red");
//this.chart.ctx.fill();
}
};
window.myDoughnut = new Chart(ctx).Doughnut(doughnutData,option, {responsive : true});
};
</script>
HTML
<div id="canvas-holder">
<canvas id="chart-area" width="300" height="300"/>
</div>
chart.js has to include
only for the middle I have to add background color. (for 'text' i have to add background color)
I have tried with this.chart.ctx.fillStyle("red"); and this.chart.ctx.fillStyle("red", x + x_fix, y + y_fix); but both not working
thank you
I am using chart.js for drawing doughnut chart. Using 'fillText' I am adding text at the middle part of doughnut chart.But how i can give a background color for the middle
here is my code
javascript
<script>
var doughnutData = [
{
value: 300,
color:"#F7464A",
highlight: "#FF5A5E",
label: "Red"
},
{
value: 50,
color: "#46BFBD",
highlight: "#5AD3D1",
label: "Green"
},
{
value: 100,
color: "#FDB45C",
highlight: "#FFC870",
label: "Yellow"
},
{
value: 40,
color: "#949FB1",
highlight: "#A8B3C5",
label: "Grey"
},
{
value: 120,
color: "#4D5360",
highlight: "#616774",
label: "Dark Grey"
}
];
window.onload = function(){
var ctx = document.getElementById("chart-area").getContext("2d");
var option =
{
//prevents the text vanishing on redraw (when tooltip shows on hover)
showTooltips: false,
//nicer than default bouncing
animationEasing: "easeOut",
//bit smoother with less steps
animationSteps: 40,
//do once on pletion rather than every frame/draw cycle
onAnimationComplete: function () {
//setup the font and center it's position
this.chart.ctx.font = 'Normal 18px Ariel';
this.chart.ctx.textAlign = 'center';
this.chart.ctx.textBaseline = 'middle';
//put the pabel together based on the given 'skilled' percentage
var valueLabel = this.segments[0].value + '%';
//find the center point
var x = this.chart.canvas.clientWidth / 2;
var y = this.chart.canvas.clientHeight / 2;
//hack to center different fonts
var x_fix = 0;
var y_fix = 2;
//render the text
this.chart.ctx.fillText("Text", x + x_fix, y + y_fix);
this.chart.ctx.fillStyle("red");
//this.chart.ctx.fill();
}
};
window.myDoughnut = new Chart(ctx).Doughnut(doughnutData,option, {responsive : true});
};
</script>
HTML
<div id="canvas-holder">
<canvas id="chart-area" width="300" height="300"/>
</div>
chart.js has to include
only for the middle I have to add background color. (for 'text' i have to add background color)
I have tried with this.chart.ctx.fillStyle("red"); and this.chart.ctx.fillStyle("red", x + x_fix, y + y_fix); but both not working
thank you
Share Improve this question edited Aug 26, 2015 at 6:27 user1187 asked Aug 26, 2015 at 6:13 user1187user1187 2,2188 gold badges45 silver badges78 bronze badges 01 Answer
Reset to default 5After 2 hours I got the answer
add the circle code before adding the text
this.chart.ctx.beginPath();
this.chart.ctx.arc(x,y,80,0,2*Math.PI);
this.chart.ctx.fillStyle = '#8AC007';
this.chart.ctx.fill();
this.chart.ctx.lineWidth = 5;
this.chart.ctx.strokeStyle = '#003300';
this.chart.ctx.stroke();
this.chart.ctx.fillStyle = 'blue';
this.chart.ctx.fillText("Text", x + x_fix, y + y_fix);
result will be like above image
本文标签: javascriptHow to add background color for doughnut mid using chartjsStack Overflow
版权声明:本文标题:javascript - How to add background color for doughnut mid using chart,js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745439070a2658363.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论