简体   繁体   English

Recharts - 标准化堆积条形图

[英]Recharts - Normalised Stacked Bar Charts

I'm pretty new to both React and Recharts and I'm sitting with a bit of a predicament.我对 React 和 Recharts 都很陌生,而且我正面临着一些困境。 I did quite a bit of reading up but I can't seem to find what I'm looking for, so I am hoping I can get some help here.我做了很多阅读,但似乎找不到我要找的东西,所以我希望我能在这里得到一些帮助。

I have a dataset that includes a list of processes with completed, failed and in progress statuses and I would like to show a stacked Bar Chart but normalise them - ie they all need to be the same width.我有一个数据集,其中包含具有已完成、失败和进行中状态的进程列表,我想显示堆叠条形图但将它们标准化 - 即它们都需要具有相同的宽度。 I managed to get most of it working but displaying the value on the bars is proving to be a pain.我设法让它大部分工作,但在条形图上显示值被证明是一种痛苦。

Below is my code:下面是我的代码:

export default class DashboardView extends React.Component<IDashboardView, {}>{
render() {
    const { dashboard, onDashboardItemClick } = this.props;

    const data = [
        {name: 'NE Send', completed: 230, failed: 335, inprogress: 453},
        {name: 'NE Resend', completed: 335, failed: 330, inprogress: 345},
        {name: 'Miles Orchestrator', completed: 537, failed: 243, inprogress: 2110},
        {name: 'Commissions Payment Orch', completed: 132, failed: 328, inprogress: 540},
        {name: 'Business Integrators', completed: 530, failed: 145, inprogress: 335},
        {name: 'SmartTrack', completed: 538, failed: 312, inprogress: 110}
    ];

    const CustomizedLabel = React.createClass({
        render () {
            const {x, y, value, dataKey} = this.props;                
            const fullValue =  value; //(value[1] - value[0]);
            return <text x={x-20}  y={y+5}  dy={0} fontSize='12' fill="#FFFFFF" fontWeight="Bold" textAnchor="start">{fullValue}</text>
        }
    });
    
    return (
        <div className="content c-white">
            <h1>Dashboard</h1>
            <ResponsiveContainer height={250} width={'100%'}>
                <BarChart layout="vertical" data={data} margin={{left: 50, right: 50}} stackOffset="expand">
                    <XAxis hide type="number"/>
                    <YAxis type="category" dataKey="name" stroke="#FFFFFF" fontSize="12" />
                    <Tooltip/>
                    <Bar dataKey="failed" fill="#dd7876" stackId="a" label={<CustomizedLabel />} />
                    <Bar dataKey="completed" fill="#82ba7f" stackId="a" label={<CustomizedLabel/>} />
                    <Bar dataKey="inprogress" fill="#76a8dd" stackId="a" label={<CustomizedLabel/>} />
                </BarChart>
            </ResponsiveContainer>
        </div>
    );
}
}

Which results in this:结果如下: 带有堆积条形图的仪表板屏幕截图 As you can see, the numbers are... well... odd and this only happens when I add the stackOffset="expand" attribute.正如你所看到的,这些数字是......好吧......奇怪,这只会在我添加 stackOffset="expand" 属性时发生。

How can I get the actual value of the section to my label instead of the calculated value based on the stackOffset?如何将部分的实际值而不是基于 stackOffset 的计算值获取到我的标签? The value that I am displaying is an array of two values and I tried some manipulation of those values without success.我显示的值是一个包含两个值的数组,我尝试对这些值进行一些操作但没有成功。

Any help will be much appreciated.任何帮助都感激不尽。

I know this is an old question, but since it has 3k views and no answer, I will try to answer我知道这是一个老问题,但由于它有 3k 次观看并且没有答案,我会尝试回答

I think the author wanted to get this result: https://codesandbox.io/s/vigilant-lehmann-82dzz我想作者想得到这个结果: https : //codesandbox.io/s/vigilant-lehmann-82dzz

import React from "react";
import {
  BarChart,
  Bar,
  XAxis,
  YAxis,
  ResponsiveContainer,
  Tooltip,
  Label,
  LabelList
} from "recharts";

const renderCustomizedLabel = (props) => {
  const { content, ...rest } = props;

  return <Label {...rest} fontSize="12" fill="#FFFFFF" fontWeight="Bold" />;
};

export class DashboardView extends React.Component {
  render() {
    const data = [
      { name: "NE Send", completed: 230, failed: 335, inprogress: 453 },
      { name: "NE Resend", completed: 335, failed: 330, inprogress: 345 },
      {
        name: "Miles Orchestrator",
        completed: 537,
        failed: 243,
        inprogress: 2110
      },
      {
        name: "Commissions Payment Orch",
        completed: 132,
        failed: 328,
        inprogress: 540
      },
      {
        name: "Business Integrators",
        completed: 530,
        failed: 145,
        inprogress: 335
      },
      { name: "SmartTrack", completed: 538, failed: 312, inprogress: 110 }
    ];

    return (
      <div className="content c-white">
        <h1>Dashboard</h1>
        <ResponsiveContainer height={250} width={"100%"}>
          <BarChart
            layout="vertical"
            data={data}
            margin={{ left: 50, right: 50 }}
            stackOffset="expand"
          >
            <XAxis hide type="number" />
            <YAxis
              type="category"
              dataKey="name"
              stroke="#FFFFFF"
              fontSize="12"
            />
            <Tooltip />
            <Bar dataKey="failed" fill="#dd7876" stackId="a">
              <LabelList
                dataKey="failed"
                position="center"
                content={renderCustomizedLabel}
              />
            </Bar>
            <Bar dataKey="completed" fill="#82ba7f" stackId="a">
              <LabelList
                dataKey="completed"
                position="center"
                content={renderCustomizedLabel}
              />
            </Bar>
            <Bar dataKey="inprogress" fill="#76a8dd" stackId="a">
              <LabelList
                dataKey="inprogress"
                position="center"
                content={renderCustomizedLabel}
              />
            </Bar>
          </BarChart>
        </ResponsiveContainer>
      </div>
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM