label mpchart 饼图_Chart.js在饼图上显示标签

I recently updated my charts.js library to the most updated version (2.5.0).

This version doesn't show the labels on the chart.

I have an example of working one on fiddler: http://jsfiddle.net/g6fajwg8 .

However, I defined my chart exactly as in the example but still can not see the labels on the chart.

Note: There are a lot of questions like this on Google and Stackoverflow but most of them are about previous versions which is working well on them.

var config = {

type: 'pie',

data: {

datasets: [{

data: [

1200,

1112,

533,

202,

105,

],

backgroundColor: [

"#F7464A",

"#46BFBD",

"#FDB45C",

"#949FB1",

"#4D5360",

],

label: 'Dataset 1'

}],

labels: [

"Red",

"Green",

"Yellow",

"Grey",

"Dark Grey"

]

},

options: {

responsive: true,

legend: {

position: 'top',

},

title: {

display: true,

text: 'Chart.js Doughnut Chart'

},

animation: {

animateScale: true,

animateRotate: true

}

}

};

window.pPercentage = new Chart(ChartContext, config);

解决方案

It seems like there is no such build in option.

However, there is special library for this option, it calls: "Chart PieceLabel".

Here is their demo.

After you add their script to your project, you might want to add another option, called: "pieceLabel", and define the properties values as you like:

pieceLabel: {

// mode 'label', 'value' or 'percentage', default is 'percentage'

mode: (!mode) ? 'value' : mode,

// precision for percentage, default is 0

precision: 0,

// font size, default is defaultFontSize

fontSize: 18,

// font color, default is '#fff'

fontColor: '#fff',

// font style, default is defaultFontStyle

fontStyle: 'bold',

// font family, default is defaultFontFamily

fontFamily: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif"

}

如果饼图标签元素重叠了,可以尝试以下两种方法来解决: 1. 调整饼图大小 可以尝试调整饼图的大小,使其更大或更小,以便标签元素能够适当地分散开来。可以通过修改 Chart.js 中的 options 中的 responsive 和 maintainAspectRatio 参数来实现: ```javascript var myPieChart = new Chart(ctx, { type: 'pie', data: data, options: { responsive: true, // 设置为 true 可以自适应大小 maintainAspectRatio: false, // 设置为 false 可以不保持纵横比 // 其他选项... } }); ``` 2. 调整标签位置和样式 可以尝试调整标签的位置和样式,使其更容易辨认。可以通过修改 Chart.js 中的 options 中的 legend 和 tooltips 参数来实现: ```javascript var myPieChart = new Chart(ctx, { type: 'pie', data: data, options: { legend: { position: 'bottom', // 将 legend 放在 bottom labels: { fontSize: 12, // 修改标签字体大小 padding: 10 // 修改标签内边距 } }, tooltips: { callbacks: { label: function(tooltipItem, data) { var dataset = data.datasets[tooltipItem.datasetIndex]; var label = dataset.labels[tooltipItem.index] || ''; var value = dataset.data[tooltipItem.index] || ''; return label + ': ' + value; // 修改 tooltip 的显示内容 } } }, // 其他选项... } }); ``` 以下是一个完整的使用 Flask 和 Chart.js 生成饼图的示例代码: ```python from flask import Flask, render_template import json app = Flask(__name__) @app.route('/') def index(): # 饼图数据 data = { 'labels': ['A', 'B', 'C', 'D', 'E'], 'datasets': [{ 'data': [12, 19, 3, 5, 2], 'backgroundColor': ['#FF6384', '#36A2EB', '#FFCE56', '#008000', '#800080'] }] } # 将数据转换为 JSON 格式 json_data = json.dumps(data) return render_template('index.html', data=json_data) if __name__ == '__main__': app.run() ``` ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Chart.js Pie Chart</title> <script src="https://cdn.jsdelivr.net/npm/chart.js"></script> <style> canvas { display: block; margin: 0 auto; } </style> </head> <body> <canvas id="myPieChart"></canvas> <script> var ctx = document.getElementById('myPieChart').getContext('2d'); var data = {{ data|safe }}; var myPieChart = new Chart(ctx, { type: 'pie', data: data, options: { responsive: true, maintainAspectRatio: false, legend: { position: 'bottom', labels: { fontSize: 12, padding: 10 } }, tooltips: { callbacks: { label: function(tooltipItem, data) { var dataset = data.datasets[tooltipItem.datasetIndex]; var label = dataset.labels[tooltipItem.index] || ''; var value = dataset.data[tooltipItem.index] || ''; return label + ': ' + value; } } } } }); </script> </body> </html> ``` 以上是一个简单的 Flask 和 Chart.js 生成饼图的示例,你可以根据自己的需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值