chartformers

Percentage Bar Chart
Copy component with data

To use the percentage bar chart, import these on top:

import { PercentageBarChart } from 'chartformers';
import 'chartformers/dist/chartformers.css';

Minimum call:

The chart must be called in a container with width and height set. It will then fill the available space of this container.

<div className="w-full h-60 md:h-96">
 <PercentageBarChart data={[]} />
</div>

The component requires a mandatory data property. This data is an array of object which:

  • has a "label" property which value is a string
  • has key(string)-value(number) pair as property(es)

The 'keys' which will determines the layers are all the properties of the first member of the data other than the 'label' property. Each member after the first member can has from one to all keys as properties.
Sample Data

const kidsFruits = [
    { label: "Jeremy", apple: 3, orange: 7, mango: 4 },
    { label: "Greg", apple: 6 }
]

For this example, the 'keys' are ['apple', 'orange', 'mango']. Under the hood the data will be formatted into:

const kidsFruits = [
    { label: "Jeremy", apple: 3, orange: 7, mango: 4 },
    { label: "Greg", apple: 6, orange: 0, mango: 0 }
]

Incorrect Data Format

const kidsFruits = [
    { label: "Jeremy", apple: 3, orange: 7, mango: 4 },
    { label: "Greg", grapes:3 }
]

Incorrect because Greg has grapes which Jeremy - the first member - does not have. To fix this, change into:

const kidsFruits = [
    { label: "Jeremy", apple: 3, orange: 7, mango: 4, grapes:0 },
    { label: "Greg", grapes:3 }
]

Now, after formatted under the hood, all the members will have the same layers of data.

#Property Table

propertydescriptionmandatorydefault value
dataArray of objects, each with key(string) - value(number property), and a mandatory label(string) which has string valueYes[]
colorIdxIndex number between 0 - 45 which controls the coloring of the chartNo0
orientationpercentage bar chart orientation, either "vertical" (default) or "horizontal"Novertical
tooltipFormatsee belowNo{}
About tooltipFormat prop

For most chart types you can pass the optional tooltipFormat prop. This prop has 3 properties: formatType, prefix and suffix, all are optional.

formatTypeprefixsuffix
either 'long' or 'short', default is 'long'. If given a number value of 1500, 'long' will format the value into '1,500', while 'short' will format into '1,5k'. Optional property.prefix for the given number value, optional property. eg: 'US$'suffix for the given number value, optional property. eg: 'KWh'

Example use:

if given number value of 1500:

  • <PercentageBarChart data={[...]} tooltipFormat={{ prefix:"US$ " }} />

    with default 'formatType' as 'long', will render 'US$ 1,500' at the tooltip component.

    Hint: In this example the US$ prefix has a single space at the end. If you want to put space between prefix and the value, do so by manually typing the space at the end of the prefix.

  • <PercentageBarChart data={[...]} tooltipFormat={{ formatType: "short", suffix:" KWh" }} />

    with the 'formatType' as 'short', will render '1,5k KWh' at the tooltip component.

    Hint: In this example the KWh suffix has a single space at the start. If you want to put space between suffix and the value, do so by manually typing the space at the start of the suffix.