Recharts 折线图截掉顶线
Recharts line chart cut off top line
我正在使用 recharts 折线图,它工作得很好,但有时会切断线的顶部。
请检查 jsfiddle 中的代码段。
const {LineChart, Line, XAxis, YAxis, ReferenceLine, CartesianGrid, Tooltip, Legend} = Recharts;
const data = [
{"Date":"16 May","Download":"40717"},{"Date":"17 May","Download":"39640"},{"Date":"18 May","Download":"52122"},{"Date":"19 May","Download":"51800"},{"Date":"20 May","Download":"49605"},{"Date":"21 May","Download":"45601"},{"Date":"22 May","Download":"43798"},{"Date":"23 May","Download":"36113"},{"Date":"24 May","Download":"278531"},{"Date":"25 May","Download":"323521"},{"Date":"26 May","Download":"154527"},{"Date":"27 May","Download":"119990"},{"Date":"28 May","Download":"100524"},{"Date":"29 May","Download":"83121"},{"Date":"30 May","Download":"60536"},{"Date":"31 May","Download":"58208"},{"Date":"01 Jun","Download":"304154"},{"Date":"02 Jun","Download":"538870"},{"Date":"03 Jun","Download":"213244"},{"Date":"04 Jun","Download":"130246"},{"Date":"05 Jun","Download":"104131"},{"Date":"06 Jun","Download":"72564"},{"Date":"07 Jun","Download":"69012"},{"Date":"08 Jun","Download":"96166"},{"Date":"09 Jun","Download":"89748"},{"Date":"10 Jun","Download":"80895"},{"Date":"11 Jun","Download":"155434"},{"Date":"12 Jun","Download":"81735"},{"Date":"13 Jun","Download":"56838"}
];
const SimpleLineChart = React.createClass({
render () {
return (
<LineChart width={600} height={300} data={data}
margin={{top: 0, right: 50, left: 20, bottom: 5}}>
<CartesianGrid strokeDasharray="3 3"/>
<XAxis dataKey="Date"/>
<YAxis/>
<Tooltip/>
<Legend />
<Line type="monotone" dataKey="Download" stroke="#8884d8" />
</LineChart>
);
}
})
ReactDOM.render(
<SimpleLineChart />,
document.getElementById('container')
);
问题:
问题是由于 "Download":"40717"
解法:
将 download
-> string
转换为 int
// data={data.map(d => ({...d , "Download" : +d.Download }))
<LineChart
width={600}
height={300}
data={data.map(d => ({...d , "Download" : +d.Download }))} // <---- HERE
margin={{top: 0, right: 50, left: 20, bottom: 5}}>
我正在使用 recharts 折线图,它工作得很好,但有时会切断线的顶部。 请检查 jsfiddle 中的代码段。
const {LineChart, Line, XAxis, YAxis, ReferenceLine, CartesianGrid, Tooltip, Legend} = Recharts;
const data = [
{"Date":"16 May","Download":"40717"},{"Date":"17 May","Download":"39640"},{"Date":"18 May","Download":"52122"},{"Date":"19 May","Download":"51800"},{"Date":"20 May","Download":"49605"},{"Date":"21 May","Download":"45601"},{"Date":"22 May","Download":"43798"},{"Date":"23 May","Download":"36113"},{"Date":"24 May","Download":"278531"},{"Date":"25 May","Download":"323521"},{"Date":"26 May","Download":"154527"},{"Date":"27 May","Download":"119990"},{"Date":"28 May","Download":"100524"},{"Date":"29 May","Download":"83121"},{"Date":"30 May","Download":"60536"},{"Date":"31 May","Download":"58208"},{"Date":"01 Jun","Download":"304154"},{"Date":"02 Jun","Download":"538870"},{"Date":"03 Jun","Download":"213244"},{"Date":"04 Jun","Download":"130246"},{"Date":"05 Jun","Download":"104131"},{"Date":"06 Jun","Download":"72564"},{"Date":"07 Jun","Download":"69012"},{"Date":"08 Jun","Download":"96166"},{"Date":"09 Jun","Download":"89748"},{"Date":"10 Jun","Download":"80895"},{"Date":"11 Jun","Download":"155434"},{"Date":"12 Jun","Download":"81735"},{"Date":"13 Jun","Download":"56838"}
];
const SimpleLineChart = React.createClass({
render () {
return (
<LineChart width={600} height={300} data={data}
margin={{top: 0, right: 50, left: 20, bottom: 5}}>
<CartesianGrid strokeDasharray="3 3"/>
<XAxis dataKey="Date"/>
<YAxis/>
<Tooltip/>
<Legend />
<Line type="monotone" dataKey="Download" stroke="#8884d8" />
</LineChart>
);
}
})
ReactDOM.render(
<SimpleLineChart />,
document.getElementById('container')
);
问题:
问题是由于 "Download":"40717"
解法:
将 download
-> string
转换为 int
// data={data.map(d => ({...d , "Download" : +d.Download }))
<LineChart
width={600}
height={300}
data={data.map(d => ({...d , "Download" : +d.Download }))} // <---- HERE
margin={{top: 0, right: 50, left: 20, bottom: 5}}>