Hie*_*915 11 javascript state graph bar-chart reactjs
我的 React 页面上有一个条形图。它从另一个页面传递的 props 获取数据。我有一个显示三个不同数据的条形图。一个显示“正确”,另一个显示“不正确”,最后一个显示“总计”。我想让每个条形的颜色都不同。我尝试过使用单元格功能,但无法使其工作。我也尝试更改每条数据的名称,但没有成功。不幸的是,Recharts 的文档并不多。有人有什么想法吗?
import React from 'react';
import {
ResponsiveContainer, BarChart, Bar, Cell, XAxis, YAxis, CartesianGrid, Tooltip, Legend,
} from 'recharts';
export default class GameChart extends React.Component {
constructor(props) {
super(props);
this.state = {
axes: [
{ primary: true, type: 'ordinal', position: 'left' },
{ position: 'bottom', type: 'linear', stacked: true }
],
series: {
type: 'bar'
},
chartData: [
{
name: 'Correct',
total: 0,
},
{
name: 'Incorrect',
total: 0,
},
{
name: 'Total',
total: 0,
},
],
chartLayout: {
title: 'Math Game Results',
yaxis: {
showticklabels: false
},
}
}
}
componentDidUpdate(prevProps) {
if (prevProps.chartData.totalCounter !== this.state.chartData[2].total) {
let tempState = this.state;
tempState.chartData = [
{
name: 'Correct',
total: this.props.chartData.correctCounter,
},
{
name: 'Incorrect',
total: this.props.chartData.incorrectCounter,
},
{
name: 'Total',
total: this.props.chartData.totalCounter,
},
];
this.setState(tempState);
}
}
render () {
return (
<div>
{
(this.state.chartData[2].total > 0) ?
(<ResponsiveContainer width="95%" height={225}>
<BarChart
data={this.state.chartData.slice()}
layout="vertical" barCategoryGap={5}
margin={{top: 5, right: 30, left: 20, bottom: 5,}}
>
<XAxis
type="number"
stroke="#000000"
/>
<YAxis
type="category"
stroke="#000000"
dataKey="name"
/>
<Tooltip
wrapperStyle={{ width: 100, backgroundColor: '#ccc' }}
formatter={function(name) {return `${name}`}}
/>
<Bar
dataKey="total"
fill="#00a0fc"
stroke="#000000"
strokeWidth={1}
/>
</BarChart>
</ResponsiveContainer>
):
(null)
}
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
Hie*_*915 23
好的,多亏了@c0m1t,我才成功。我所要做的就是列出一个高于国家级的颜色列表!
import React from 'react';
import {
BarChart, Bar, XAxis, Cell, YAxis, Tooltip, ResponsiveContainer,
} from 'recharts';
const barColors = ["#1f77b4", "#ff7f0e", "#2ca02c"]
export default class Example extends React.Component {
constructor(props) {
super(props);
this.state = {
chartData: [
{
name: "Correct",
total: this.props.chartData.correctCounter
},
{
name: "Incorrect",
total: this.props.chartData.incorrectCounter
},
{
name: "Total",
total: this.props.chartData.totalCounter
}
]
}
}
render() {
return (
<ResponsiveContainer width="95%" height={450}>
<BarChart
data={this.state.chartData.slice()}
margin={{ top: 20, right: 20, left: 20, bottom: 5, }}
data={this.state.chartData}
>
<XAxis
dataKey="name"
stroke="#000000"
/>
<YAxis
stroke="#000000"
/>
<Tooltip
wrapperStyle={{ width: 100, backgroundColor: '#ccc' }}
formatter={function(total) {return `${total}`}}
/>
<Bar
dataKey="total"
fill="#00a0fc"
stroke="#000000"
strokeWidth={1}
>
{
this.state.chartData.map((entry, index) => (
<Cell key={`cell-${index}`} fill={barColors[index % 20]} />
))
}
</Bar>
</BarChart>
</ResponsiveContainer>
);
}
}
Run Code Online (Sandbox Code Playgroud)
Ben*_*els 13
chartData您可以使用 键直接添加填充颜色fill:
[
{
name: 'Correct',
total: 0,
fill: '#1f77b4',
},
{
name: 'Incorrect',
total: 0,
fill: '#ff7f0e',
},
{
name: 'Total',
total: 0,
fill: '#2ca02c',
},
]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23435 次 |
| 最近记录: |