React, ApexCharts, Radial Bar series value from data

2.1k views Asked by At

I'm new to React and trying to learn. My personal project is homebrewing display site. I want to show some of the details in radial bar. I followed tutorial and made array data file, and index file where details show. Now I'm stuck getting single values to display in radial bar. Also values need to be calculated from percent to numeric. I got that working. I tried some push functions but did not get it to work way I wanted. My coding skills are beginner level.

display example

data.js

import product1 from '../../images/product-1.png';
import product2 from '../../images/product-1.png';

export const productData = [
  {
    id: 1,
    img: product1,
    alt: 'Beer',
    name: 'Lager',
    desc: 'Saaz',
    ibu: '35',
    ebc: '40',
    abv: '8.5',
  },

  {
    id: 2,
    img: product2,
    alt: 'Beer',
    name: 'IPA',
    desc: 'Mango, Citrus',
    ibu: '85',
    ebc: '25',
    abv: '5.5',
  },
];

index.js

import React, { Component } from 'react';
import Chart from 'react-apexcharts';
import {
  ProductsContainer,
  ProductWrapper,
  ProductsHeading,
  ProductTitle,
  ProductCard,
  ProductImg,
  ProductInfo,
  ProductDesc,
  ProductIbu,
  ProductEbc,
  ProductAbv,
} from './ProductsElements';

const max = 119;
function valueToPercent(value) {
  return (value * 100) / max;
}

class IBU extends Component {
  constructor(props) {
    super(props);

    this.state = {
      series: [valueToPercent(15)],

      options: {
        plotOptions: {
          radialBar: {
            startAngle: -90,
            endAngle: 90,
            hollow: {
              margin: 10,
              size: '70%',
              background: '#222222',
              image: undefined,
              imageOffsetX: 0,
              imageOffsetY: 0,
              position: 'front',
              dropShadow: {
                enabled: true,
                top: 5,
                left: 0,
                blur: 4,
                opacity: 0.9,
              },
            },
            track: {
              background: '#d42d2d',
              strokeWidth: '67%',
              margin: 0, // margin is in pixels
              dropShadow: {
                enabled: true,
                top: 0,
                left: 0,
                blur: 4,
                color: '#000',
                opacity: 0.6,
              },
            },

            dataLabels: {
              show: true,
              name: {
                offsetY: -10,
                show: true,
                color: '#fff',
                fontSize: '17px',
              },
              value: {
                formatter: function (value) {
                  return (parseFloat(value) * max) / 100;
                },
                color: '#dadada',
                fontSize: '36px',
                show: true,
              },
            },
          },
        },
        fill: {
          colors: [
            function ({ value, seriesIndex, w }) {
              if (value < 55) {
                return '#7E36AF';
              } else if (value >= 55 && value < 80) {
                return '#164666';
              } else {
                return '#D9534F';
              }
            },
          ],
        },
        stroke: {
          lineCap: 'round',
        },
        labels: ['IBU'],
      },
    };
  }

  render() {
    return <Chart options={this.state.options} series={this.state.series} type="radialBar" height={250} />;
  }
}

const Products = ({ data }) => {
  return (
    <ProductsContainer>
      <ProductsHeading>heading</ProductsHeading>
      <ProductWrapper>
        {data.map((product, index) => {
          return (
            <ProductCard key={index}>
              <ProductImg src={product.img} alt={product.alt} />
              <ProductInfo>
                <ProductTitle>{product.name}</ProductTitle>
                <ProductDesc>{product.desc}</ProductDesc>
                <ProductIbu>{product.ibu}</ProductIbu>

                <IBU></IBU>

                <ProductEbc>{product.ebc}</ProductEbc>
                <ProductAbv>{product.abv}</ProductAbv>
              </ProductInfo>
            </ProductCard>
          );
        })}
      </ProductWrapper>
    </ProductsContainer>
  );
};
export default Products;
0

There are 0 answers