reactjs - 如何使用 recharts 实现条形图的内部曲线
问题描述
我想使用recharts库实现类似的图表有什么可以实现类似的图表吗?
这是我的片段
<BarChart
data={[
{
"value1": 200,
"value2": 300,
type: '1',
},
...
]}
layout="vertical" >
<CartesianGrid
vertical={true}
horizontal={false}
stroke="#D2E7FB"
fillOpacity={0}
/>
<XAxis type="number" />
<YAxis width={80} type="category" dataKey="type" offset={10} />
<Tooltip />
<Bar
radius={[0, 0, 0, 0]}
dataKey="value1"
fill="#54C6F0"
stackId="a"
/>
<Bar
stackId="a"
radius={[10, 10, 10, 10]}
dataKey="value2"
fill="#F7B315"
/>
</BarChart>
电流输出
我缺少什么你能帮帮我吗?
解决方案
没有图表的 React 示例:
const data = [
{
value1: 200,
value2: 300,
},
{
value1: 120,
value2: 250,
},
{
value1: 100,
value2: 200,
},
];
const SPACING = 3;
const firstPath = value1 => `M 0,-10 H ${value1} A 10,10 1 1 1 ${value1},10 H 0 Z`;
const secondPath = (value1, value2) => `M ${value1 + SPACING},-10 H ${value2} A 10,10 1 1 1 ${value2},10 H ${value1 + SPACING} A 10,10 0 0 0 ${value1 + SPACING},-10 Z`;
const GraphItem = ({data, index}) => {
const {value1, value2} = data;
return (
<g transform={`translate(10, ${index * 50 + 20})`}>
<path d={firstPath(value1)} fill="#F7B315" />
<path d={secondPath(value1, value2)} fill="#54C6F0" />
</g>
);
}
const Graph = ({data}) => {
return (
<svg width="400" height="170">
{data.map((item, index) =>
<GraphItem data={item} index={index} key={index} />)
}
</svg>
);
}
ReactDOM.render(
<Graph data={data} />,
document.getElementById('container')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="container">
</div>
推荐阅读
- java - 如何在方法之间调用字符串?
- django - Django Graphene - 将 info.context 传递给 Query 或 Mutation 的装饰器
- python - pandas 序列的唯一 ID
- json - 抓取 AJAX 生成的表格以批量下载 PDF
- bokeh - Bokeh Sphinx 扩展——如何包含来自 CSV 文件的数据?
- kiwi-tcms - 调用 TestRun.update() 时出现 xmlrpc.client.Fault
- java - 回文测试器难度
- java - 使用 sdk 对 AWS S3 存储桶中的对象进行计数时计数不正确
- java - 高效地从 Firebase 数据库获取用户数据
- openssl - 如何使用 openssl 以文本形式从 CA 打印证书