[英]REACT: How to build a half donut
我是 react-google-charts 的新手,我正在開發一個半餡餅甜甜圈,但盡管成功了,但我遇到了一個問題,這個問題是可見半餡餅的百分比總和僅為 50%,因為其他 50% 被隱藏。
我怎樣才能使那個可見的部分是 100%?
這里你有圖表的代碼:
import React, { Component } from "react";
import { Chart } from "react-google-charts";
class PieChart extends Component {
constructor(props) {
super(props);
this.props = props;
this.state = {
loaded: false
};
}
componentDidMount() {
this.setState(
{
loaded: true
},
() => {
this.setState({
width: document.querySelector("#container").getBoundingClientRect()
.width
});
}
);
}
render() {
return (
<div>
{this.state.loaded ? (
<div id="container">
<Chart
width={this.state.width}
height={"400px"}
chartType="PieChart"
loader={<div>Loading Chart</div>}
data={this.props.data}
options={{
backgroundColor: "transparent",
title: "Distribución de posesiones",
// Just add this option
pieHole: 0.4,
pieStartAngle: 270,
slices: {
4: {
color: "transparent"
}
}
}}
/>
</div>
) : (
"Cargando datos"
)}
</div>
);
}
}
module.exports.PieChart = PieChart;
如您所見,可見部分的總和僅為 50% 而不是 100%。
那么,我怎樣才能使可見部分占總數的 100%?
您可以在此處查看所有代碼:
我們可以使用 react-google-chart 的回調 function 來更改標簽值,如下所示。 在回調 function 中我們可以訪問餅圖的svg 。
<Chart
chartEvents={[
{
eventName: "ready",
callback: ({ chartWrapper, google }) => {
// const chart = chartWrapper.getChart();
let svg = document.querySelector("svg");
console.log("svg childNodes are ",svg.childNodes)
// YOU CAN WRITE THIS CODE USING FOR EACH BUT FOR EASY UNDERSTANDING I WROTE LIKE THIS
// first 2 children in svg are heading & label colors(sidenav)
for(var i=3;i<svg.childNodes.length-1;i++) {
var temp = svg.childNodes[i].childNodes[1].innerHTML;
if(temp.length>3) {
temp = temp.substring(0,3)
}
else {
temp = temp.substring(0,2)
}
// doubling to make them as 100% summary
temp = parseFloat(temp)*2;
svg.childNodes[i].childNodes[1].innerHTML = temp+'%';
}
}
}
]}
width={this.state.width}
height={"400px"}
chartType="PieChart"
loader={<div>Loading Chart</div>}
data={this.props.data}
options={{
backgroundColor: "transparent",
title: "Distribución de posesiones",
pieHole: 0.4,
pieStartAngle: 270,
slices: {
4: {
color: "transparent"
}
}
}}
/>
注意如果您有 static 數據,那就沒問題了。 如果您的數據值發生變化,那么每次我們都沒有得到甜甜圈形狀。 圖表不是這樣工作的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.