Skip to main content
Version: 1.2.0

Bubble Chart

A bubble chart is a data visualization that displays multiple circles (bubbles) in a two-dimensional plot. It is built with d3. Here's a preview:

Live Editor

Usage

1 . Import the BubbleChart component after installation.

import { BubbleChart } from "@k2/d3-viz";

2 . Add data or URL to the BubbleChart component.

<BubbleChart data={BubbleChartData} />
// or
<BubbleChart url="server/api/data" />

Examples

Bubble Chart with animation: Demo

<BubbleChart
data={[
{
color: "#ffb964",
name: "Android",
value: 10,
},
{
color: "#a26be9",
name: "A.Tablet",
value: 10,
},
{
color: "#6098f3",
name: "iPhone",
value: 65,
},
{
color: "#71af4f",
name: "iPad",
value: 15,
},
]}
height={400}
width={500}
/>

Bubble Chart with Legends: Demo

<BubbleChart
data={[
{
color: "#ffb964",
name: "Android",
value: 10,
},
{
color: "#a26be9",
name: "A.Tablet",
value: 10,
},
{
color: "#6098f3",
name: "iPhone",
value: 65,
},
{
color: "#71af4f",
name: "iPad",
value: 15,
},
]}
height={400}
width={500}
/>

Bubble Chart with Classes: Demo

<BubbleChart
classes={{ root: "myClassName" }}
data={[
{
color: "#ffb964",
name: "Android",
value: 10,
},
{
color: "#a26be9",
name: "A.Tablet",
value: 10,
},
{
color: "#6098f3",
name: "iPhone",
value: 65,
},
{
color: "#71af4f",
name: "iPad",
value: 15,
},
]}
height={400}
width={500}
/>

Bubble Chart with data from API endpoint:

<BubbleChart url="server/api/data" />

Props

PropertyTypeRequiredDefault valueDescription
dataBubble[]yesData for the chart.
titlestring | JSX.ElementnoNoneTitle of the chart.
animatebooleannotrueEnable the animation.
labelboolean | LabelComponentnotrueEnable or customize the labels.
legendsboolean | LegendsPropsnotrueEnable or customize the legends.
tooltipboolean | TooltipPropsnotrueEnable or customize the tooltip.
colors{[theme-mode: e.g "light" or "dark"] : string[] }noNoneColors of visual in different theme modes
onClick(event: {data:Bubble) ⇒ voidnoNoneCallback for bubble click.
widthnumberno400Width of the chart.
heightnumberno300Height of the chart.
styleCSSPropertiesnoNoneStyles applied to the root element of chart.
classNamestringnoNoneClass applied to the root element of chart.
classesClassesnotrueClasses of chart.

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

Custom Types

BubbleChartData

Here is the format of the data accepted by the BubbleChart component.

type Bubble = {
name: string | number,
color?: string,
labelColor?: string,
value: number,
};

LabelComponent

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

type LabelComponent = (obj: { data: Bubble, radius: number }) => JSX.Element;

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.
bubblestringnoNoneApplied to bubbles of chart.
labelstringnoNoneApplied to labels of chart.

Please see Class Specificity for correctly defining it.