Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error in the values of the Rechart Stacked chart

I’m trying to build a Stacked Bar Chart using the Pchart library. I attach the code below.

function StackedBarChart({...props}){
    const {dataChart:{data,keys}} = props;

    const renderBars = () =>{
        return keys.map((item,index)=>(
            <Bar
                key={index}
                dataKey={item}
                stackId='a'
                fill={index%2?'blue':'red'}
                label
            />
        ))
    }

    return(
        <ResponsiveContainer width='100%' height={400}>
            <BarChart
                data={data}
                stackOffset='expand'
                >
                <XAxis dataKey="name" />
                <YAxis />
                {renderBars()}
            </BarChart>
        </ResponsiveContainer>
    )
}

When I output a value to each Bar I get incorrect signatures.enter image description here . Why is 2 Bar subscribed with the value 1 and not the remaining percentage. What is my mistake ?

my data

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const dataChart = {
  data: [{
      name: 'Page A',
      count: 4000,
      price: 2400,
    },
    {
      name: 'Page B',
      count: 3000,
      price: 1398,
    },
    {
      name: 'Page C',
      count: 2000,
      price: 9800,
    },

  ],
  keys: ['price', 'count']
};

>Solution :

The reason of this behavior, is that upper bar shown his top value, which is 1, so this is the sum of all bars stacked.

To avoid this situation I used valueAccessor and some formatting to get value of bar and count its percentage.

Here is a link to working codesandbox where you can see final result

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading