0.0.1 • Published 5 years ago

factful-bubble-chart v0.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

What is Factful Bubble Chart?

Factful Bubble Chart is the bubble chart with 5 dimensions, 1. X axis 1. Y axis 1. Time 1. Size 1. Group

which is inspired by Hans Rosling's analysis method and his book titled "Factfulness".

See demo page: https://Tokky0425.github.io/factful-bubble-chart/

Installation

$ npm install --save factful-bubble-chart

Basic usage

1. Get your data ready

data.json

// Should be like...
[
  {
    "name": "Afghanistan",
    "group": "Asia",
    "content": {
      "1950": {
        "size": 7752.0,
        "x": 7.45,
        "y": 28.61
      },
      "1951": {
        "size": 7840.0,
        "x": 7.45,
        "y": 29.11
      }
    }
  },
  {
    "name": "Albania",
    "group": "Europe",
    "content": {
      "1950": {
        "size": 1263.0,
        "x": 6.23,
        "y": 55.26
      },
      "1951": {
        "size": 1287.0,
        "x": 6.29,
        "y": 56.07
      }
    }
  }
]

Fields

NameTypeDescription
namestringSet unique name of the item. (e.g. country name)
groupstringSet value if you want to identify certain groups by coloring. (e.g. area name)Not Required.
contentobjectAdd statistical data here.Each child object needs to have same number of data with same keys as other set of items.
sizenumber, floatSize of plot. (e.g. population)
xnumber, floatPosition of plot on x axis. (e.g. fertility rate)
ynumber, floatPosition of plot on y axis. (e.g. life expectancy)

2. Create the component with the data

index.js

import React from 'react';
import {render} from 'react-dom';
import FactfulBubbleChart from 'factful-bubble-chart';

fetch('data.json')
  .then(response => response.json())
  .then(rawData => {
    const config = {
      x: {
        min: 0,
        max: 9,
        interval: 1,
      },
      y: {
        min: 20,
        max: 90,
        interval: 10,
      },
    };

    const App = () => {
      const timeKey = 1950; // Note that timeKey should be controllable in the real usage.
      return (
        <FactfulBubbleChart config={config} rawData={rawData} timeKey={timeKey}/>
      )
    };
    
    render(<App/>, document.getElementById('root'));
  });

Config

NameTypeDefault valueDescriptionRequired
x, yobject-Configuration for x and y axisTrue
x.item, y.itemstring-Axis titleFalse
x.min, y.minnumber, float-Minimum number of axisTrue
x.max, y.maxnumber, float-Maximum number of axisTrue
x.interval, y.intervalnumber, float-Interval number between each division on axisTrue
x.unit, y.unitstring-Unit of axisFalse
normalizeTypestring-Try to set min-max if the sizes of the bubbles are not well balancedFalse
chartWidthnumber760max-width of chartFalse
chartHeightnumber480height of chartFalse
maxPlotSizenumber, float40Maximum size of the largest plot of allFalse
minPlotSizenumber, float1.5Minimum size of the largest plot of allFalse
chartFrameStyleobject-Custom style for chart frameFalse
chartTimeVisiblebooleantrueSet false to hide time key behind plotsFalse
chartTimeStyleobject-Custom style for time key behind plotsFalse
axisColorstring#333Color code for axisFalse
groupColorobject-Object that has children that have corresponding name as its key to the group field of the raw dataFalse
groupColor[$goupName].fillstring-Plot's color of given groupFalse
groupColor[$goupName].strokestring-Plot's border color of given groupFalse
onChartDidMountfunction-Callback called after the first mountFalse
onChartDidUpdatefunction-Callback called every time chart and plots are updatedFalse
onPlotMouseEnterfunction-Callback called when mouse pointer is hovered on plotFalse
onPlotMouseLeavefunction-Callback called when mouse pointer is left from plotFalse
onPlotClickfunction-Callback called when plot is clickedFalse

Callback arguments

CallbackArguments
onChartDidMount,onChartDidUpdateref: DOM of chart elementtimeKey: Current time key
onPlotMouseEnter,onPlotMouseLeave,onPlotClickref: DOM of plot elementdata: Object that has fields belowdata.name: Plot's identifiable namedata.group: Plot's groupdata.x: Plot's value of xdata.y: Plot's value of ydata.size: Plot's value of sizedata.positionX: Plot's position in 'px'data.positionY: Plot's position in 'px'

3. Add your controller

Probably you want to add a controller that can update timeKey field to move the plots.

In the example below, Slider from Material-UI is used.

index.js

import React, {Fragment, useState} from 'react';
import {render} from 'react-dom';
import FactfulBubbleChart from 'factful-bubble-chart';
import Slider from '@material-ui/lab/Slider'; // Add this as your controller

fetch('data.json')
  .then(response => response.json())
  .then(rawData => {
    const config = {
      x: {
        min: 0,
        max: 9,
        interval: 1,
      },
      y: {
        min: 20,
        max: 90,
        interval: 10,
      },
    };

    const App = () => {
      const timeMin = 1950;
      const timeMax = 2015;
      const [timeKey, setTimeKey] = useState(timeMin);
      
      return (
        <Fragment>
          <FactfulBubbleChart config={config} rawData={rawData} timeKey={Math.round(timeKey)}/>
          <Slider min={timeMin} max={timeMax} value={timeKey} onChange={(e, val) => setTimeKey(val)}/>
        </Fragment>
      )
    };
    
    render(<App/>, document.getElementById('root'));
  });

3. That's it!

Enjoy Seeing the facts of the world.

Examples can be found in the demo page.

License

This project is licensed under the MIT License - see the LICENSE file for details