Skip to main content
Version: 1.2.0

Bar Chart

A bar chart is a data visualization where each category is represented by a rectangle, with the width of the rectangle being proportional to the values being plotted. Idea is to develop a bar chart in react-vis. React-vis is a charting library that provides chart components. In other words, the chart is composed of multiple sub-components like XYPlot, xAxis, yAxis.

Live Editor

Usage

1 . Import BarChart after installation

import { BarChart } from "@k2/rv-viz";

2 . Add data or URL to BarChart component.

<BarChart data={BarChartData} />
// or
<BarChart url="server/api/data" />

Examples

Basic BarChart With X and Y Axis: Demo

<BarChart
data={[
{
color: "#5579ae",
data: [
{
labelColor: "#777777",
x: 20.5,
y: "Critical",
},
{
labelColor: "#777777",
x: 34,
y: "High",
},
{
labelColor: "#777777",
x: 22,
y: "Mid",
},
{
labelColor: "#777777",
x: 80.55,
y: "Low",
},
],
name: "Series 1",
},
]}
title={<span>Heading</span>}
xAxis={{
style: {
strokeWidth: 0.5,
},
tickFormat: function noRefCheck() {},
tickSize: 0,
tickTotal: 100,
tickValues: [0, 20, 40, 60, 80, 100],
}}
xyPlot={{
margin: {
left: 50,
right: 30,
},
xDomain: [0.5, 100],
}}
yAxis={{
hideLine: true,
tickSize: 0,
}}
/>

Basic BarChart with Labels: Demo

<BarChart
data={[
{
data: [
{
color: "#e77355",
labelColor: "#777777",
x: 20.5,
y: "Critical",
},
{
color: "#f29d56",
labelColor: "#777777",
x: 34,
y: "High",
},
{
color: "#f1bf43",
labelColor: "#777777",
x: 22,
y: "Mid",
},
{
color: "#6ab04c",
labelColor: "#777777",
x: 80.55,
y: "Low",
},
],
name: "Series 1",
},
]}
label={({ data }: { data: HorizontalBarSeriesPoint }) => {
return <div>{data.x}%</div>;
}}
title={<span>Heading</span>}
xyPlot={{
margin: {
left: 50,
right: 30,
},
xDomain: [0.5, 100],
}}
/>

Stacked BarChart With Legends: Demo

<BarChart
xyPlot={{
stackBy: "x",
}}
data={[
{
data: [
{
x: 100,
y: "Jun 1",
},
{
x: 200,
y: "Jun 2",
},
{
x: 30,
y: "Jun 3",
},
{
x: 200,
y: "Jun 4",
},
{
x: 50,
y: "Jun 5",
},
],
name: "External Links",
},
{
data: [
{
x: 200,
y: "Jun 1",
},
{
x: 60,
y: "Jun 2",
},
{
x: 80,
y: "Jun 3",
},
{
x: 200,
y: "Jun 4",
},
{
x: 80,
y: "Jun 5",
},
],
name: "Inside Links",
},
{
data: [
{
x: 50,
y: "Jun 1",
},
{
x: 140,
y: "Jun 2",
},
{
x: 200,
y: "Jun 3",
},
{
x: 100,
y: "Jun 4",
},
{
x: 100,
y: "Jun 5",
},
],
name: "Outside Links",
},
]}
/>

Grouped BarChart With Legends: Demo

<BarChart
data={[
{
data: [
{
x: 100,
y: "Jun 1",
},
{
x: 200,
y: "Jun 2",
},
{
x: 30,
y: "Jun 3",
},
],
name: "External Links",
},
{
data: [
{
x: 200,
y: "Jun 1",
},
{
x: 60,
y: "Jun 2",
},
{
x: 80,
y: "Jun 3",
},
],
name: "Inside Links",
},
{
data: [
{
x: 130,
y: "Jun 1",
},
{
x: 40,
y: "Jun 2",
},
{
x: 90,
y: "Jun 3",
},
],
name: "Other Links",
},
]}
/>

Props

Note: External props are mostly derived from react-vis & K2. All external props are linked. You can click highlighted props to get more details from official sites.

PropertyTypeRequiredDefault valueDescription
dataBarChartData[]yesData for the chart.
xyPlotXYPlotPropsnoNoneCustomize XYPlot. Inherited from "react-vis".
xAxisboolean | XAxisPropsnotrueEnable or customize XAxis. Inherited from "react-vis".
yAxisboolean | YAxisPropsnotrueEnable or customize YAxis. Inherited from "react-vis".
verticalGridLinesboolean | VerticalGridLinesPropsnotrueEnable or customize VerticalGridLines. Inherited from "react-vis".
horizontalGridLinesboolean | HorizontalGridLinesPropsnotrueEnable or customize HorizontalGridLines. Inherited from "react-vis".
barWidthnumberno0.3Inherited from "react-vis" BarSeries
titlestring | JSX.ElementnoNoneTitle of the chart.
animatebooleannotrueEnable the animation.
colors{[theme-name: e.g "light" or "dark"] : string[] | GradientColor[]no{ dark: ["#5579ae", "#e5c238", "#CF7373", "#85BE50"], light: ["#5579ae", "#e5c238", "#CF7373", "#85BE50"], }Colors of visual in different theme modes
labelboolean | LabelComponentnotrueEnable or customize the labels(Note: Labels appear only in case of single series).
tooltipboolean | TooltipPropsnotrueEnable or customize the tooltip.
legendsboolean | LegendsPropsnofalseEnable or customize the legends.
onClickonClick?: (event: { data: HorizontalBarSeriesPoint }) => void;noNoneCallback for bar click.
styleCSSPropertiesnoNoneStyles applied to the root element of chart.
classNamestringnoNoneClass applied to the root element of chart.
classesClassesnoNoneClasses of chart.

BarChart is wrapped with fetchData HoC, to perform data handling tasks, which adds support for some additional props.

Custom Types

BarChartData

Here is the format of the data accepted by BarChart.

interface BarChartData extends Omit<HorizontalBarSeriesProps, "data"> {
name: string;
data: HorizontalBarSeriesPoint[];
}

HorizontalBarSeriesProps and HorizontalBarSeriesPoint are derived from react-vis

LabelComponent

This is a type of custom label component. You can create a custom label component by this.

type LabelComponent =
| boolean
| ((props: { data: HorizontalBarSeriesPoint }) => JSX.Element);

HorizontalBarSeriesPoint is derived from react-vis

GradientColor

Type of GradientColor object.

type GradientColor = {
name: string,
startingColor: string,
endingColor: string,
startingColorStopOpacity?: number,
endingColorStopOpacity?: number,
x1?: string,
x2?: string,
y1?: string,
y2?: string,
};

Classes

Classes enable customization and styling of chart elements through their classes.

ClassNameTypeRequiredDefault valueDescription
rootstringnoNoneApplied to root element of chart.
headerstringnoNoneApplied to header of chart.
titlestringnoNoneApplied to title of chart.
legendsstringnoNoneApplied to legends of chart.
tooltipstringnoNoneApplied to tooltip of chart.
chartstringnoNoneApplied to svg component of chart.
barSeriesstringnoNoneApplied to bar series of chart.
xAxisstringnoNoneApplied to x axis of chart.
yAxisstringnoNoneApplied to y axis of chart.
labelstringnoNoneApplied to labels of chart.

Please see Class Specificity for correctly defining it.