I am working on Recat.js and using rechart library for chart implementation. I want to give string value on the y axis as label and workout on some numbers included in the json data in x axis. here Iam giving my code I don’t why its not working properly. the value key pair has to gone through x axis and label values on the y axis. but its not working . please help me to solve this issue. iam giving my code
JavaScript
x
66
66
1
"timestamp": 1645727400000,
2
"value": 1,
3
"label":"connection_recovered"
4
},
5
{
6
"timestamp": 1645790232297,
7
"value": 2,
8
"label":"up"
9
},
10
{
11
"timestamp": 1645790232297,
12
"value": -2,
13
"label":"down"
14
},
15
{
16
"timestamp": 1645790232297,
17
"value": 2,
18
"label":"up"
19
},
20
{
21
"timestamp": 1645790232297,
22
"value": -2,
23
"label":"down"
24
}]
25
26
return (
27
<Container lg={6}>
28
<ResponsiveContainer aspect="3">
29
<AreaChart
30
width={500}
31
height={400}
32
data={data}
33
margin={{
34
top: 10,
35
right: 30,
36
left: 0,
37
bottom: 0,
38
}}
39
>
40
<CartesianGrid strokeDasharray="3 3" />
41
42
<XAxis style={{fontSize:"12px"}} dataKey="timestamp" />
43
44
45
<YAxis dataKey="label" type="category" />
46
<Tooltip />
47
<defs>
48
<linearGradient id="splitColor" x1="0" y1="0" x2="0" y2="1">
49
<stop offset={off} stopColor="green" stopOpacity={1} />
50
<stop offset={off} stopColor="red" stopOpacity={1} />
51
</linearGradient>
52
</defs>
53
<Area style={{fontSize:"12px"}}
54
type="monotone"
55
dataKey="value"
56
stroke="#000"
57
fill="url(#splitColor)"
58
/>
59
60
</AreaChart>
61
</ResponsiveContainer>
62
</Container>
63
64
);
65
}
66
Advertisement
Answer
In your data array, I assumed that the values with
2
will always have the"up"
label,1
the"connection_recovered"
label and-2
the"down"
label.
In order to have the label on a fixed tick in your graph on the YAxis, you can use the tickFormatter prop, where you can pass a function, which could return the label you want according to the value.
It would give the following:
JavaScript
1
14
14
1
// Depending on the value of the tick, you'll have a different label
2
function formatYAxis(value: number) {
3
switch(value) {
4
case 2:
5
return "up";
6
case -2:
7
return "down";
8
case 1:
9
return "connection_recovered";
10
default:
11
return ""
12
}
13
}
14
Used in the YAxis just like so:
JavaScript
1
2
1
<YAxis tickFormatter={formatYAxis} />
2