gpt4 book ai didi

reactjs - 使用react-chartjs-2显示每个切片的饼图数据值

转载 作者:行者123 更新时间:2023-12-02 10:10:54 30 4
gpt4 key购买 nike

我正在制作一个饼图,并且正在努力显示每个切片的饼图数据值。由于我的应用程序是用 React.js 编写的,因此我使用react-chartjs-2。

我为chart.js找到了这个解决方案并尝试实现,但它不适用于react-chartjs-2。

How to display pie chart data values of each slice in chart.js

如何为每个切片增加值(value)?任何帮助表示赞赏。

这是饼图的当前 View 。

enter image description here

根据建议,我实现了chart.piecelabel.js。目前还无法正常工作..

我的代码

import {Pie} from 'react-chartjs-2';
import {pieceLabel} from 'chart.piecelabel.js';

export default class Categories extends React.Component{
constructor(props){
super(props);
this.state = {
slideOpen : false,
piData : piData
}

this.handleClick = this.handleClick.bind(this);
this.update = this.update.bind(this);
this.doParentToggle = this.doParentToggle.bind(this);
}

doParentToggle(){


this.setState({
piData : piData
})
console.log('-------')
console.log('parentToggle' + piData )
console.log('parentToggle' + piData2 )
console.log('parentToggle' + piData3 )
console.log('-------')
this.update();
}

handleClick(){
this.setState({
slideOpen : !this.state.slideOpen
})
}

update() {
var piData;
this.setState({
piData : piData
})
console.log('-------')
console.log('parentToggle' + piData )
console.log('parentToggle' + piData2 )
console.log('parentToggle' + piData3 )
console.log('-------')
}

render(){
const CategoriesPanel = this.state.slideOpen? "slideOpen" : "";
const { length } = this.props


var totalData = piData + piData2 + piData3 + piData4 + piData5;

let newpiData = function() {
return parseFloat((piData / totalData ) * 100 ).toFixed(2) };

let newpiData2 = function() {
return parseFloat((piData2 / totalData ) * 100).toFixed(2) };

let newpiData3 = function() {
return parseFloat((piData3 / totalData ) * 100).toFixed(2) };

let newpiData4 = function() {
return parseFloat((piData4 / totalData ) * 100).toFixed(2) };

let newpiData5 = function() {
return parseFloat((piData5 / totalData ) * 100).toFixed(2) };

const data = {
datasets: [{
data: [ newpiData() , newpiData2(), newpiData3(), newpiData4(), newpiData5()],
backgroundColor: [
'orange',
'blue',
'red',
'purple',
'green'
],
hoverBackgroundColor: [
'orange',
'blue',
'red',
'purple',
'green'
],
borderColor: [ 'orange',
'blue',
'red',
'purple',
'green'
]
}],
options : [{
pieceLabel: { render: 'value' }
}]};

var pieOptions = {
events: false,
animation: {
duration: 500,
easing: "easeOutQuart",
onComplete: function () {
var ctx = this.chart.ctx;
ctx.font = Chart.helpers.fontString(Chart.defaults.global.defaultFontFamily, 'normal', Chart.defaults.global.defaultFontFamily);
ctx.textAlign = 'center';
ctx.textBaseline = 'bottom';

this.data.datasets.forEach(function (dataset) {

for (var i = 0; i < dataset.data.length; i++) {
var model = dataset._meta[Object.keys(dataset._meta)[0]].data[i]._model,
total = dataset._meta[Object.keys(dataset._meta)[0]].total,
mid_radius = model.innerRadius + (model.outerRadius - model.innerRadius)/2,
start_angle = model.startAngle,
end_angle = model.endAngle,
mid_angle = start_angle + (end_angle - start_angle)/2;

var x = mid_radius * Math.cos(mid_angle);
var y = mid_radius * Math.sin(mid_angle);

ctx.fillStyle = '#fff';
if (i == 3){ // Darker text color for lighter background
ctx.fillStyle = '#444';
}
var percent = String(Math.round(dataset.data[i]/total*100)) + "%";
ctx.fillText(dataset.data[i], model.x + x, model.y + y);
// Display percent in another line, line break doesn't work for fillText
ctx.fillText(percent, model.x + x, model.y + y + 15);
}
});
}
}
};
return(
<div>
<div id="chart" className={CategoriesPanel}>
<Pie style={{"fontSize" : "20px" }}data={data}/>
<div className="categoriesSlide" onClick={this.handleClick}>{this.state.slideOpen? <img src={Arrowup} alt="arrowup" className="arrowup" /> : <img src={Arrowdown} alt="arrowdown" className="arrowdown"/>}</div>
<button onClick={this.update} className="chartButton">Update Information</button></div>

<div className="clear">
<List parentToggle={this.doParentToggle} />
<ListSecond parentToggle={this.doParentToggle} />
<ListThird parentToggle={this.doParentToggle} />
<ListFourth parentToggle={this.doParentToggle} />
<ListFifth parentToggle={this.doParentToggle} />
</div>
</div>
)
}
}

最佳答案

您可以使用此 ChartJS 插件 - Chart.PieceLabel.js

首先,通过 npm 安装它:

npm install chart.piecelabel.js --save

然后,将其导入您的组件中:

import 'chart.piecelabel.js';

最后,在图表选项中添加以下内容:

pieceLabel: {
render: 'value'
}

注意:这是显示该值所需设置的最小选项,您可以找到更多选项 here .

关于reactjs - 使用react-chartjs-2显示每个切片的饼图数据值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46447098/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com