so basically i have this data:
const bodyWeightAndFatData = [
{
name: 'salim',
style:'#57c0e8',
category:'Body Weight',
data: [
{ day: 23, value: 100 },
{ day: 24, value: 101 },
{ day: 25, value: 104 },
{ day: 26, value: 107 },
{ day: 27, value: 108 },
{ day: 28, value: 105 },
{ day: 29, value: 106 },
{ day: 30, value: 107 },
{ day: 31, value: 107 },
],
},
{
name: 'salim',
style:'pink',
category:'Fat Percentage',
data: [
{ day: 23, value: 134 },
{ day: 24, value: 135 },
{ day: 25, value: 131 },
{ day: 26, value: 133 },
{ day: 27, value: 137 },
{ day: 28, value: 131 },
{ day: 29, value: 130 },
{ day: 30, value: 139 },
{ day: 31, value: 138 },
],
},
];
coming from one component. and i'm drawing a rechart from another component like this:
<div className={styles.outer}>
<div className={styles.inner}>
<p className={styles.title}>Average Measurments</p>
<ResponsiveContainer width="95%" height={300}>
<LineChart >
<CartesianGrid strokeDasharray="5 0" vertical={false} tickSize={10} padding={{ left: 20 }}/>
<XAxis yAxisId="right" tickLine={false} axisLine={false} dataKey="day" allowDuplicatedCategory={false} />
<YAxis tickCount={5} axisLine={false} dx={-15} dataKey="value"/>
<Tooltip/>
<Legend
layout="horizontal" verticalAlign="top" align="center"
payload={
props.data.map(
item => ({
type: "circle",
id: item.name,
color: item.style,
value: `${item.category}`
})
)
}/>
{props.data.map(s => (
<Line dataKey="value" data={s.data} name={s.name} key={s.category} stroke={s.style}/>
))}
</LineChart>
</ResponsiveContainer>
</div>
</div>
the result is working great its drawing the data and everything, but what i'm stuck on is that i need two Y axis to be shown instead of just one which is present. i have looked it up but i don't know how to create it using .map can anyone help?
Here is how I did it.
const bodyWeightAndFatData = [
{
name: 'salim',
style:'#57c0e8',
category:'Body Weight',
data: [
{ day: 23, value: 100 },
{ day: 24, value: 101 },
{ day: 25, value: 104 },
{ day: 26, value: 107 },
{ day: 27, value: 108 },
{ day: 28, value: 105 },
{ day: 29, value: 106 },
{ day: 30, value: 107 },
{ day: 31, value: 107 },
],
},
{
name: 'salim',
style:'pink',
category:'Fat Percentage',
data: [
{ day: 23, value: 134 },
{ day: 24, value: 135 },
{ day: 25, value: 131 },
{ day: 26, value: 133 },
{ day: 27, value: 137 },
{ day: 28, value: 131 },
{ day: 29, value: 130 },
{ day: 30, value: 139 },
{ day: 31, value: 138 },
],
},
];
export default function App() {
return (
<div >
<div >
<p>Average Measurments</p>
<ResponsiveContainer width="95%" height={300}>
<LineChart >
<CartesianGrid strokeDasharray="5 0" vertical={false} tickSize={10} padding={{ left: 20 }}/>
<XAxis tickLine={false} axisLine={true} dataKey="day" allowDuplicatedCategory={false} />
<YAxis yAxisId="left" tickCount={5} dx={-15} dataKey="value"/>
<YAxis yAxisId="right" orientation="right" tickCount={5} dx={-15} dataKey="value"/>
<Tooltip/>
<Legend
layout="horizontal" verticalAlign="top" align="center"
payload={
bodyWeightAndFatData.map(
item => ({
type: "circle",
id: item.name,
color: item.style,
value: `${item.category}`
})
)
}/>
{bodyWeightAndFatData.map(s => (
<Line yAxisId="left" dataKey="value" data={s.data} name={s.name} key={s.category} stroke={s.style}/>
))}
{bodyWeightAndFatData.map(s => (
<Line yAxisId="right" dataKey="value" data={s.data} name={s.name} key={s.category} stroke={s.style}/>
))}
</LineChart>
</ResponsiveContainer>
</div>
</div>
);
}
Two axis one on left and one on right.
Related
I want to set the domain for the giving data set. So you can only see 5 objects at once. When doing this using ids it works well. However the x-axis uses dates. Is there a way to decouple the x-axis labels from the domain?
const initialData = [
{ id: 1, date: "05/16/2022", value: 4456 },
{ id: 2, date: "05/17/2022", value: 2789 },
{ id: 3, date: "05/18/2022", value: 7891 },
{ id: 4, date: "05/19/2022", value: 4561 },
{ id: 5, date: "05/20/2022", value: 5561 },
{ id: 6, date: "05/21/2022", value: 6561 },
{ id: 7, date: "05/22/2022", value: 4561 },
{ id: 8, date: "05/23/2022", value: 4561 },
{ id: 9, date: "05/24/2022", value: 4861 },
{ id: 10, date: "05/25/2022", value: 8561 },
{ id: 11, date: "05/26/2022", value: 6561 },
{ id: 12, date: "05/27/2022", value: 4561 }
];
const initialState = {
data: initialData,
left: initialData[0].date,
right: initialData[5].date,
refAreaLeft: undefined,
refAreaRight: undefined,
top: "dataMax+1000",
bottom: "dataMin-1000",
animation: true
};
<ResponsiveContainer width="100%" height={400}>
<LineChart
width={800}
height={400}
data={data}
>
<CartesianGrid strokeDasharray="3 3" />
<XAxis
allowDataOverflow
dataKey="date"
domain={[left, right]}
type="category"
/>
<YAxis allowDataOverflow domain={[bottom, top]} type="number" />
<Tooltip />
<Line
type="natural"
dataKey="value"
stroke="#8884d8"
animationDuration={300}
/>
</LineChart>
</ResponsiveContainer>
I am just getting started learning React and React Native for Web these days. I tried to make a LineChart by using Recharts Library. I expected that the all XAxis name should be shown in the graph; 10시, 11시 ... 24시.
This is my LineData. name is XAsis label name and UV is its value.
let lineData = [
{ name: "10시", uv: 44 },
{ name: "11시", uv: 32 },
{ name: "12시", uv: 14 },
{ name: "13시", uv: 14 },
{ name: "14시", uv: 14 },
{ name: "15시", uv: 34 },
{ name: "16시", uv: 4 },
{ name: "17시", uv: 54 },
{ name: "18시", uv: 14 },
{ name: "19시", uv: 0 },
{ name: "20시", uv: 34 },
{ name: "21시", uv: 54 },
{ name: "22시", uv: 14 },
{ name: "23시", uv: 24 },
{ name: "24시", uv: 34 },
];
In the Linechart I am using ResponsiveContainer, XAsis, and YAxis for customizing the graph.
This is my code
<ResponsiveContainer width="100%" height={250}>
<LineChart
data={lineData}
margin={{ top: 20, right: 0, left: -15, bottom: 5 }}
>
<Line type="monotone" dataKey="uv" stroke="red" />
<XAxis
dataKey="name"
tick={{ fontSize: 10 }}
// width="-100"
// ticks={[
// 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
// ]}
// domain={[10, 24]}
/>
<YAxis tick={{ fontSize: 10 }} ticks={[0, 10, 20, 30, 40]} />
</LineChart>
</ResponsiveContainer>
What I mentioned above, the XAsis name should be shown in the graph all of them but part of name only show like this.
10시, 12시, 14시, 16시, 18시, 20시, 22시, 23시 are disapeared.
Could you help me with what part do I missing or wrong?
I would be really appreciated your help!
I think because you set the width 100% for ResponsiveContainer and you are rendering this graph in narrow screen, Recharts automatically cut some x-axis values.
Have you tried adding interval props for XAxis?
If you set interval={0} in XAxis component, you can see all the x-axis values in the screen even it is not wide enough.
<XAxis
dataKey="name"
tick={{ fontSize: 10 }}
interval={0}
/>
I want to create a custom bar chart with Victory like this:
How can I add a horizontal line in the graph every 10%?
This is my sample code:
const data = [
{ day: 1, data: 0 },
{ day: 2, data: 1 },
{ day: 3, data: 2 },
{ day: 4, data: 2 },
{ day: 5, data: 0 },
];
const BarChart = () => {
return (
<VictoryChart domainPadding={20} theme={VictoryTheme.material}>
<VictoryAxis
tickFormat={(x) => `${x.toFixed(0)}`}
style={{
grid: { stroke: "none" },
ticks: { size: 0 },
}}
/>
<VictoryAxis
dependentAxis
tickFormat={(x) => `${x}`}
style={{
grid: { stroke: "none" },
ticks: { size: 0 },
}}
/>
<VictoryStack>
<VictoryBar data={data} style={{ data: { fill: '#379F4B' } }} x="day" y="data" />
</VictoryStack>
</VictoryChart>
);
};
Have you tried to use tickValues? Example:
<VictoryAxis dependentAxis
tickValues={[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]}
...other stuff...
/>
I have a sample chart here:
https://codesandbox.io/s/rechart-stackoverflow-forked-ev1tt?file=/src/BarGraph.js
It is a dynamically created line chart (dynamic number of lines) but the tooltip is only showing the data for one of the lines.
Am wondering how to get the other dynamically created lines' data in the tooltip...
Code:
import React, { Component } from "react";
import {
BarChart,
Bar,
XAxis,
YAxis,
CartesianGrid,
Tooltip,
Legend,
LineChart,
Line
} from "recharts";
import _ from "underscore";
class BarGraph extends Component {
state = {
channels: [],
data: []
};
componentDidMount() {
let data = {
channels: ["219180924-2", "219180924-1"],
data: [
{ period: 10, "219180924-2": 0 },
{ period: 11, "219180924-2": 0 },
{ period: 12, "219180924-2": 0 },
{ period: 13, "219180924-2": 0 },
{ period: 14, "219180924-2": 0 },
{ period: 15, "219180924-2": 0 },
{ period: 16, "219180924-2": 0.01 },
{ period: 17, "219180924-2": 0.33 },
{ period: 18, "219180924-2": 0.14 },
{ period: 19, "219180924-2": 0.22 },
{ period: 20, "219180924-2": 0.69 },
{ period: 10, "219180924-1": 0.12 },
{ period: 11, "219180924-1": 0.19 },
{ period: 12, "219180924-1": 0.2 },
{ period: 13, "219180924-1": 0.22 },
{ period: 14, "219180924-1": 0.29 },
{ period: 15, "219180924-1": 1.51 },
{ period: 16, "219180924-1": 0.03 },
{ period: 17, "219180924-1": 0 },
{ period: 18, "219180924-1": 0 },
{ period: 19, "219180924-1": 0 },
{ period: 20, "219180924-1": 0 }
]
};
this.setState({
channels: data.channels,
data: data.data
});
}
render() {
return (
<LineChart
width={600}
height={300}
data={this.state.data}
margin={{ top: 5, right: 30, left: 20, bottom: 5 }}
>
<CartesianGrid strokeDasharray="1 " />
<XAxis dataKey={"period"} allowDuplicatedCategory={false} />
<YAxis />
<Tooltip />
<Legend />
{this.state.channels.map((channel) => (
<Line dataKey={channel} name={channel} strokeWidth={2} />
))}
</LineChart>
);
}
}
export default BarGraph;
so i have this data:
const systolicAndDiastolicAndPulseAverage = [
{
name:'Systolic Average',
style:'#FFDA83',
id:'right',
category:'Systolic Average',
data: [
{ day: '23/05/2020 04:50', value: 76 },
{ day: '24/05/2020 04:50', value: 98 },
{ day: '25/05/2020 04:50', value: 63 },
{ day: '26/05/2020 04:50', value: 112 },
{ day: '27/05/2020 04:50', value: 81 },
{ day: '28/05/2020 04:50', value: 81 },
{ day: '29/05/2020 04:50', value: 72 },
{ day: '30/05/2020 04:50', value: 74 },
{ day: '31/05/2020 04:50', value: 124 },
]
},
{
name:'Diastolic Average',
style:'#EA1D75',
id:'left',
category:'Diastolic Average',
data: [
{ day: '23/05/2020 04:50', value: 61 },
{ day: '24/05/2020 04:50', value: 65 },
{ day: '25/05/2020 04:50', value: 82 },
{ day: '26/05/2020 04:50', value: 74 },
{ day: '27/05/2020 04:50', value: 69 },
{ day: '28/05/2020 04:50', value: 59 },
{ day: '29/05/2020 04:50', value: 67 },
{ day: '30/05/2020 04:50', value: 71 },
{ day: '31/05/2020 04:50', value: 74 },
]
},
{
name:'Pulse Average',
style:'#5FE3A1',
category:'Pulse Average',
id:'right',
data: [
{ day: '23/05/2020 04:50', value: 80 },
{ day: '24/05/2020 04:50', value: 83 },
{ day: '25/05/2020 04:50', value: 65 },
{ day: '26/05/2020 04:50', value: 72 },
{ day: '27/05/2020 04:50', value: 79 },
{ day: '28/05/2020 04:50', value: 93 },
{ day: '29/05/2020 04:50', value: 96 },
{ day: '30/05/2020 04:50', value: 91 },
{ day: '31/05/2020 04:50', value: 46 },
]
}
]
i have successfully created a bar chart in recharts using this data and everything is great and working fine but my problem is that i want to take only the day and month out of the day and give it to the xaxis, i have looked up so many different documentations and github chats but nothing worked( for example on the xaxis it will only contain 30/05 31/05 ....) can anyone please help?
code:
<BarChart
width={1000}
height={300}
data={systolicAndDiastolicAndPulseAverage}
margin={{
top: 5, right: 30, left: 20, bottom: 5,
}}
>
<CartesianGrid strokeDasharray="5 0" tickSize={15} />
<XAxis dataKey="day" type="number" tickFormatter={tickFormatter} scale='time'
domain = {['dataMin','dataMax']}
tickLine={false} axisLine={false} dx={-15} allowDuplicatedCategory={false}/>
<YAxis yAxisId="left" orientation="left" dataKey="value" type="number" tickLine={false} axisLine={false} allowDuplicatedCategory={false}/>
<YAxis yAxisId="right" orientation="right" dataKey="value" type="number" tickLine={false} axisLine={false} allowDuplicatedCategory={false}/>
<Tooltip />
<Legend
layout="horizontal" verticalAlign="top" align="center"
payload={
systolicAndDiastolicAndPulseAverage.map(
item => ({
type: "circle",
id: item.name,
color: item.style,
value: `${item.category}`
})
)
}/>
{systolicAndDiastolicAndPulseAverage.map(s => (
<Bar yAxisId={s.id} barSize={10} dataKey="value" data={s.data} key={s.name} name={s.name} fill={s.style}/>
))}
</BarChart>