Recharts Bar Size not calculated automatically when scale is point - reactjs

I am using recharts to display a composite chart.
const Chart = ({ responseAverage }) => {
responseAverage = responseAverage.sort((a, b) => a.label - b.label);
return (
<ResponsiveContainer width={700} height={400}>
<ComposedChart
data={responseAverage}
>
<CartesianGrid horizontal={false} strokeDasharray="4 4" />
<XAxis scale="point" dataKey="label" />
<YAxis label={{ value: 'No.Of Employees', angle: -90, position: 'insideLeft' }} tick={false} height={20} />
{/* <Tooltip cursor={false} content={<CustomTooltip teamData={teamData} />} /> */}
<Bar dataKey="count" barSize={40} fill="#AAE5F9" />
<Line connectNulls={true} strokeWidth={3} dot={false} type="monotone" dataKey="count" stroke="#3080ED" />
{/* <LabelList dataKey="name" position="insideTop" /> */}
</ComposedChart>
</ResponsiveContainer>
);
};
In this I am specifying the barSize. But it dosent work in all cases. I want the Bar size to calculate automatically based on the number of values
Is there any workaround for this problem ?
Any help would be really thankfull

If you want to remove the gaps between the bars, you need to remove the barSize parameter from the Bar and add the barCategoryGap={0} parameter to the ComposedChart component.
const Chart = ({ responseAverage }) => {
responseAverage = responseAverage.sort((a, b) => a.label - b.label);
return (
<ResponsiveContainer width={700} height={400}>
<ComposedChart
barCategoryGap={0}
data={responseAverage}
>
<CartesianGrid horizontal={false} strokeDasharray="4 4" />
<XAxis scale="point" dataKey="label" />
<YAxis label={{ value: 'No.Of Employees', angle: -90, position: 'insideLeft' }} tick={false} height={20} />
{/* <Tooltip cursor={false} content={<CustomTooltip teamData={teamData} />} /> */}
<Bar dataKey="count" fill="#AAE5F9" />
<Line connectNulls={true} strokeWidth={3} dot={false} type="monotone" dataKey="count" stroke="#3080ED" />
{/* <LabelList dataKey="name" position="insideTop" /> */}
</ComposedChart>
</ResponsiveContainer>
);
};
If as a result, thin lines remain between the bars, then you can do this:
<Bar dataKey="count" fill="#AAE5F9" stroke="#AAE5F9" strokeWidth={2} />

Related

recharts synchronize x-axis for two datasets

I'm trying to set up my graph so that it will display barcharts (dataset 1) and linecharts (dataset 2) simultaneously.
However, when I set this up, the x-axis doesn't really sync up together. Is there a way to get the x-axis to sync up for the two datasets? For example, 350 from dataset2 matches on top of 350 from dataset1? The length of dataset2 is about ~200 and the length of dataset1 is about ~2000.
Here is the graph from recharts:
Here is my code:
const barChart = chartData[0] ? (
<ResponsiveContainer width="100%" aspect={2}>
<ComposedChart data={chartData} barGap={0}>
<XAxis dataKey="strike_price" xAxisId={0} />
<XAxis dataKey="strike_price" xAxisId={1} hide />
<XAxis dataKey="strike_price" xAxisId={2} hide />
<XAxis dataKey="strike_price" xAxisId={3} axisLine={false} />
<YAxis tickFormatter={formatNumbers} yAxisId={0} />
<YAxis tickFormatter={formatNumbers} yAxisId={1} orientation="right" />
<Tooltip />
<Legend />
<Bar
data={chartData}
dataKey="c_notion_expo"
fill="green"
xAxisId={0}
barSize={100}
fillOpacity={0.9}
/>
<Bar
data={chartData}
dataKey="p_notion_expo"
fill="red"
xAxisId={1}
barSize={100}
fillOpacity={0.9}
/>
<Bar
data={chartData}
dataKey="total_notional_exposure"
fill="black"
barSize={50}
xAxisId={2}
fillOpacity={0.9}
/>
<Line
data={theoData}
type="monotone"
dataKey="total_gamma"
xAxisId={3}
yAxisId={1}
/>
</ComposedChart>
</ResponsiveContainer>
) : null;

Is it possible create something like this using Recharts? #DesignChallenge

I was wondering If someone could help me design something like this using Recharts:
I have something simillar:
I tried to add more Axis, I modified the position of the bars, I put a padding but nothing works
Here is my code:
<BarChart
barGap={-55}
data={data}
height={300}
margin={{ top: 30, left: -15, right: 5, bottom: 20 }}
width={100}
>
<pattern height='8' id='pattern-stripe' patternTransform='rotate(45)' patternUnits='userSpaceOnUse' width='8'>
<rect fill='white' height='8' transform='translate(0,0)' width='4' />
</pattern>
<mask id='mask-stripe'>
<rect fill='url(#pattern-stripe)' height='100%' width='100%' x='0' y='0' />
</mask>
<CartesianGrid vertical={false} />
<XAxis dataKey='period' xAxisId={0} hide />
<XAxis allowDuplicatedCategory={false} dataKey='period' tickLine={false} xAxisId={1}>
{groupName && (
<ChartLabel position='insideBottomRight' style={{ transform: 'translateY(1rem)' }} value={groupName} />
)}
</XAxis>
<YAxis name={t('conceptionRate')} type='number'>
<ChartLabel
position='right'
style={{ transform: 'translateY(-45%)' }}
value={`${t('Rate')} (%)`}
/>
</YAxis>
<Tooltip content={<ReproductionTooltip />} cursor={{ fill: 'rgba(0, 0, 0, 0.1)' }} />
<ReferenceLine stroke={WUZZY} strokeDasharray='3 3' y={referenceValue} />
<Bar barSize={100} dataKey='bar' fill={GREEN} shape={<HashedBar />} xAxisId={1} />
<Bar barSize={100} dataKey='reference' fill={BLUE} shape={<HashedBar />} xAxisId={0} />
<Bar barSize={10} dataKey='labelList' fill={DARK_GRAY} xAxisId={0} />
</BarChart>

Recharts bar label inner padding from top

I'm using recharts and trying to use the position property of the label object in the Bar component to set the label at the top-inner position but I couldn't find the correct approach for it.
code:
<ResponsiveContainer width="100%" height="100%">
<BarChart
data={data}
>
<XAxis
type={"category"}
hide={false}
tickLine={false}
axisLine={false}
dataKey={"age"}
/>
<YAxis hide={true} dataKey={""} axisLine={false} tickLine={false} type={"number"} />
<Bar
dataKey="age"
background={{ fill: "theme.darkblue" }}
label={{ position: "top" }}
>
{data.bar.map((d: IData, index: number) => (
<Cell
key={index}
fill={"green"}
/>
))}
</Bar>
</BarChart>
</ResponsiveContainer>
current:
desirable:

How to display React Recharts dynamically?

I am using React Recharts and I want to display it dynamically.
https://recharts.org/en-US/examples/CustomActiveShapePieChart
<ResponsiveContainer width='99%' aspect={3.0 / 2.0}>
<LineChart width={500} height={300} data={this.state.android}>
{/* margin={{ top: 5, right: 0, left: -40, bottom: 5 }}> */}
<XAxis dataKey="Date"/>
<YAxis/>
<Tooltip />
<Legend />
<CartesianGrid stroke="#eee" strokeDasharray="5 5"/>
{/* "#AD7BEC"
"#F88E4E" */}
<Line name="android" type="monotone" dataKey="android" stroke="#ff883c" strokeWidth={2} />
<Line name="ios" type="monotone" dataKey="ios" stroke="#ffb847" strokeWidth={2} />
</LineChart>
</ResponsiveContainer>
This is my code for plotting line chart of recharts but I am doing this in hard code way... Now I have a data key array which is generated dynamically and now I want to pass that key to there
<Line name="android" type="monotone" dataKey="android" stroke="#ff883c" strokeWidth={2} />
And I want to pass element of array to datakey variable like this
dynamic_array=["android","ios","value"]
<Line name="android" type="monotone" dataKey=dynamic_array[0] stroke="#ff883c" strokeWidth={2} />
Can I apply map or forEach?
Indeed you can use the map method to create as many Line you need, just like this:
renderLines () {
dynamic_array = ["android", "ios", "value"];
const lines = dynamic_array.map((value) => (
<Line
key={value}
name={value}
type="monotone"
dataKey={value}
stroke"#ff883c"
strokeWidth={2}
/>
));
return lines;
}
And then use the result of the method within the LineChart component, like this:
<LineChart width={500} height={300} data={this.state.android}>
<XAxis dataKey="Date"/>
<YAxis/>
<Tooltip />
<Legend />
<CartesianGrid stroke="#eee" strokeDasharray="5 5"/>
{this.renderLines()}
</LineChart>

How to add a custom svg as the line/tooltip for recharts

I have a line chart with two lines.
For one of the lines, I want to use a custom svg to represent it in the chart. See screenshot.
How can I do this?
Here's my code:
<LineChart
// width={800}
onClick={() => {}}
height={300}
data={this.state.data}
// margin={{ top: 5, right: 30, left: 20, bottom: 5 }}
>
<XAxis dataKey="to_char" />
<YAxis
tickFormatter={value => {
let val = value / 1000000;
return `$${new Intl.NumberFormat("en").format(val)}m`;
}}
/>
<CartesianGrid strokeDasharray="3" vertical={false} />
<Tooltip />
<Legend />
<Line
dot={false}
type="monotone"
dataKey="predictedprice"
stroke="#1C85E8"
activeDot={{ r: 8 }}
name="True Home Estimateā„¢"
strokeWidth={3}
/>
<Line
type="monotone"
name="Sold Price"
dataKey="soldprice"
stroke="#82ca9d"
shape="star"
/>
</LineChart>
You should be able to add the shape property to ReferenceDot or ReferenceArea - I'm not sure if the shape properly can be used on regular properties, but here's a link I found to a GitHub conversation that talks more about this:
https://github.com/recharts/recharts/issues/922

Resources