Code Monkey home page Code Monkey logo

Comments (2)

leineveber avatar leineveber commented on September 22, 2024 1

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.

Maximization avatar Maximization commented on September 22, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.