Comments (2)
Solved.
For anyone, who needs the code:
import React, {useState} from 'react';
import {BarChart, barDataItem} from 'react-native-gifted-charts';
import {View} from 'react-native-ui-lib';
import ChartLabel from './ChartLabel';
const data = [
{
values: [5, 40, 77, 81, 43],
label: 'Hemoglobin',
color: '#7CDE86',
},
{
values: [40, 5, 50, 23, 79],
label: 'White blood cell',
color: '#7CD8DE',
},
{
values: [10, 55, 35, 90, 82],
label: 'Mean corpuscular volume',
color: '#7C9DDE',
},
{
values: [22, 45, 50, 60, 30],
label: 'Hematocrit',
color: '#A17CDE',
},
{
values: [30, 25, 10, 60, 15],
label: 'Platelet',
color: '#DC7CDE',
},
];
const xAxisLabels = ['01.02', '11.02', '19.02', '12.03', '13.03'];
const getData = (barSpace: number, labelWidth: number, groupWidth: number) => {
const res: barDataItem[] = [];
for (let i = 0; i < data.length; i++) {
data.forEach((item, j, array) => {
const obj = {
value: item.values[i],
frontColor: item.color,
};
const label = {
labelWidth,
labelComponent: () => (
<ChartLabel
label={xAxisLabels[i]}
labelWidth={labelWidth}
groupWidth={groupWidth}
/>
),
};
const spacing = barSpace;
if (j === 0) {
res.push({...obj, ...label, spacing});
} else if (j === array.length - 1) {
res.push({...obj});
} else {
res.push({...obj, spacing});
}
});
}
return res;
};
export default function Chart() {
const [width, setWidth] = useState(0);
const groupSpace = (width * 0.2) / (xAxisLabels.length - 1);
const groupWidth = (width * 0.8) / xAxisLabels.length;
const barSpace = (groupWidth * 0.2) / (xAxisLabels.length - 1);
const barWidth = (groupWidth * 0.8) / xAxisLabels.length;
const labelWidth = 40;
return (
<View
flex
onLayout={({
nativeEvent: {
layout: {width: calculatedWidth},
},
}) => setWidth(calculatedWidth - labelWidth)}>
<BarChart
disablePress
disableScroll
isAnimated
data={getData(barSpace, labelWidth, groupWidth)}
barWidth={barWidth}
spacing={groupSpace}
roundedTop
roundedBottom
noOfSections={3}
xAxisThickness={0}
yAxisThickness={0}
dashWidth={20}
dashGap={12}
initialSpacing={0}
endSpacing={0}
/>
</View>
);
}
IMO, these calculations should be done by the library, so, probably, take it into account
Also, please, take in mind, that I had to use transform: [{translateX: (groupWidth - labelWidth) / 2}]
for the label component so it can be in the middle.
And one more thing, when you click on the first bar in the group, opacity applies to label too. Only if you click on the first bar, not others, so it leads one more bug. Had to disable press
from react-native-gifted-charts.
@leineveber Thanks for posting your solution. It helped me. I saw you're using labelWidth (xAxis) to get the chart width, don't you mean to use yAxisLabelWidth (yAxis) instead?
from react-native-gifted-charts.
Related Issues (20)
- Line Charts: line segments doesn't work with CurveType = {CurveType.QUADRATIC} HOT 1
- Can we acheive yAxis addtional label? HOT 1
- Pie Chart label text overflowing HOT 1
- Issue - OnPress is not working in pie chart HOT 1
- BarChart: topLabel component not displayed above bars when isAnimated is enabled HOT 5
- How to create group bar chart (Left Y-Axis) and 2 line chart (Right Y-axis) with negative values HOT 18
- Image Capture for charts HOT 6
- Stop interpolating after the last not null datapoint HOT 7
- Inconsistent graph scaling with similar datas HOT 4
- Bug: Cannot read property 'isSecondary' of null HOT 5
- (PieChart) onPress event is not returning data properly HOT 1
- Animation is not working anymore HOT 2
- Bar alignment issue in recent update HOT 12
- Facing alignment isseu with topLabelComponent HOT 4
- X-Axis alignmetn when isAnimated={true} & extra space at the bottom of the graph when using xAxisLabelsVerticalShift HOT 2
- How to move xAxisLabel upwards in Horizontal bar chart? HOT 1
- Dont see line in the chart on android device if I use hideDataPoints HOT 1
- Area Line Chart render animation is jerky HOT 4
- yAxisSide="right" in Line Chart this property is not working and also if m implementing positive and negative then xAxisLabelText is not showing properly
- Gradient line and area blinking with pointerConfig HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-native-gifted-charts.