Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Rechart barchart with rounded edges

enter image description here

I'm trying to create a barchart like this, where the bar is X length, and each segment of the bar is proportional in length to the % of the data it contains. I've gotten pretty close by creating a vertical layout stacked barchart, but can't figure out how to get rounded edges. I've tried border={x} on the Bar components, but that rounds both sides of the bar, and I just want the left side of the first, and right side of the second bar rounded. Any advice? Or am I possibly going about this using the wrong components to begin with?

Here's what I have so far, and a screenshot of the result.

const {BarChart, Bar, XAxis, YAxis, CartesianGrid, Tooltip, Legend} = Recharts;
const data = [
      {name: '1', a: 50, b: 20, c: 10},
];
const StackedBarChart = React.createClass({
  render () {
  	return (
    	<BarChart layout="vertical" width={600} height={50} data={data}
            margin={{top: 20, right: 30, left: 20, bottom: 5}}>
       <XAxis hide  type="number"/>
       <YAxis hide dataKey="name" type="category"/>
       <Tooltip />
       <Bar dataKey="a" stackId="a" fill="#8884d8" radius={20} />
       <Bar dataKey="b" stackId="a" fill="#8884d8" />
       <Bar dataKey="c" stackId="a" fill="black" radius={20}/>
      </BarChart>
    );
  }
})

ReactDOM.render(
  <StackedBarChart />,
  document.getElementById('container')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.6.2/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/15.6.2/react-dom.min.js"></script>
<script src="https://unpkg.com/recharts/umd/Recharts.min.js"></script>
<div id="container">
  <!-- This element's contents will be replaced with your component. -->
</div>

Not sure why that snippet isn't working, so here's the JSFiddle https://jsfiddle.net/nur2t39y/

enter image description here

like image 470
Weston Avatar asked Mar 21 '19 16:03

Weston


1 Answers

I found a Github issue that showed an example of using an array for the radius prop, allowing you to give the radius for each corner of the bar. https://github.com/recharts/recharts/issues/793

Here's the new, working code, and a screenshot of the result

const {BarChart, Bar, XAxis, YAxis, CartesianGrid, Tooltip, Legend} = Recharts;
const data = [
      {name: '1', a: 50, b: 20, c: 10},
];
const StackedBarChart = React.createClass({
	render () {
  	return (
    	<BarChart layout="vertical" width={600} height={50} data={data}
            margin={{top: 20, right: 30, left: 20, bottom: 5}}>
       <XAxis hide  type="number"/>
       <YAxis hide dataKey="name" type="category"/>
       <Tooltip />
       <Bar dataKey="a" stackId="a" fill="#8884d8" radius={[10, 0, 0, 10]}/>
       <Bar dataKey="b" stackId="a" fill="#82ca9d" />
       <Bar dataKey="c" stackId="a" fill="black" radius={[0, 10, 10, 0]}/>
      </BarChart>
    );
  }
})

ReactDOM.render(
  <StackedBarChart />,
  document.getElementById('container')
);

enter image description here

like image 72
Weston Avatar answered Nov 02 '22 11:11

Weston