Can’t understand how to change my ‘react-native-svg-charts’ x and y axis labels. Can’t see the labels
import React,{useState} from 'react';
import {View, StyleSheet, Text, Button, Dimensions, TouchableOpacity} from 'react-native';
// import {LineChart} from "react-native-chart-kit";
import { LineChart, XAxis, YAxis } from 'react-native-svg-charts';
let {height, width} = Dimensions.get("window");//(Below) make it as width as the screen
const GraphComponent_1 = (props) => {
const {pinnedMeasurements, Labelss} = props;
const data = [ 50, 10, 40, 95, -4, -24, 85, 91, 35, 53, -53, 24 ]
const xLabels = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
const yLabels = [ 'Poor', 'Fair', 'Good', 'Very Good', 'Excellent' ]
return (
<View style={{ height: 200, padding: 20 }}>
<LineChart
style={{ flex: 1 }}
data={data}
svg={{ stroke: 'rgb(134, 65, 244)' }}
contentInset={{ top: 20, bottom: 20 }}
>
<XAxis
style={{ marginTop: 10 }}
data={data}
formatLabel={(value, index) => xLabels[index]}
style={{
axisLabel: {
color: 'red', // changes x axis text color to red
},
}}
/>
<YAxis
style={{ marginRight: 10 }}
data={data}
contentInset={{ top: 20, bottom: 20 }}
min={-60}
max={100}
numberOfTicks={yLabels.length}
formatLabel={(value, index) => yLabels[index]}
/>
</LineChart>
</View>
)
}
const styles = StyleSheet.create({
});
Best I’ve done is this(below) with this error(below):
2
Answers
Error is solved
The error is happening because the
index
gets out of range. ThexLabels
has a length of 12 while the actualdata
has a length of 15.You can either add extra labels for the x-axis or reduce the data back to 12.
The YAxis has the same issue. I see you want to "group" them with some labels. You can use the
numberOfTicks
prop with theyLabels.length
. Then when formatting the label you can use theindex
.EDIT
I found something with the
min
andmax
of theYAxis
. If they’re provided the error is gone.For the layout: