我想在我的ReactJS应用程序中使用D3绘制图表。我看了一些youtube视频并在网络上阅读了一些教程,但是它们都没有帮助。每次出现错误或我看不到结果时。这是我最新的代码。这段代码没有错误,但是我所能看到的只是一些没有我试图绘制的甜甜圈图的数字。
import React from 'react';
import {withStyles} from '@material-ui/core/styles';
import { arc as d3Arc, pie as d3Pie } from 'd3-shape';
import { csvParse } from 'd3-dsv';
import * as d3 from 'd3';
import dataCsv from '../../static/book1.csv';
const styles = theme =>({
arc:{
background:'red',
fill:'blue',
color:'red',
}
});
const width = 500,
height = 500,
radius = Math.min(width, height) / 2;
const color = d3.scaleOrdinal().range([
'#98abc5',
'#d0743c',
]);
const arc = d3.arc()
.outerRadius(radius - 10)
.innerRadius(radius - 70);
const pie = d3Pie().sort(null).value(function(d) {
return d.population;
});
const data = pie(
csvParse(dataCsv, d => {
d.population = +d.population;
return d;
})
);
class DonutChart extends React.Component{
constructor(){
super();
}
render(){
const {classes} = this.props;
return(
<svg width={width} height={height}>
<g transform={`translate(${width / 2}, ${height / 2})`}>
{data.map(d =>(
<g className={classes.arc} key={`a${d.type}`}>
<path d={arc(d)} fill={color(d.color)} />
<text transform={`translate(${arc.centroid(d)})`} dy=".35em">
{d.value}
</text>
</g>
))}
</g>
</svg>
);
}
}
export default withStyles(styles)(DonutChart);