2.0.0-beta.7 • Published 6 months ago

react-charts v2.0.0-beta.7

Weekly downloads
6,998
License
MIT
Repository
github
Last release
6 months ago

React Charts

Simple, immersive & interactive charts for React

Features

  • Line, Bar, Bubble, Area.
  • Hyper Responsive
  • Powered by D3
  • React Hooks Ready
  • Flexible data model

Demos

Intro

React Charts is currently in beta! This means:

  • The existing API is mostly stable. Expect only subtle changes/additions as use-cases become polished.
  • It's safe for most production sites, as long as you lock in the alpha version.

Table of Contents

Installation

$ yarn add react-charts
# or
$ npm i react-charts --save

Quick Start

React

This will render a very basic line chart:

import React from 'react'
import { Chart } from 'react-charts'

function MyChart() {
  const data = React.useMemo(
    () => [
      {
        label: 'Series 1',
        data: [[0, 1], [1, 2], [2, 4], [3, 2], [4, 7]]
      },
      {
        label: 'Series 2',
        data: [[0, 3], [1, 1], [2, 5], [3, 6], [4, 4]]
      }
    ],
    []
  )

  const axes = React.useMemo(
    () => [
      { primary: true, type: 'linear', position: 'bottom' },
      { type: 'linear', position: 'left' }
    ],
    []
  )

  const lineChart = (
    // A react-chart hyper-responsively and continuusly fills the available
    // space of its parent element automatically
    <div
      style={{
        width: '400px',
        height: '300px'
      }}
    >
      <Chart data={data} axes={axes} />
    </div>
  )
}

Documentation

Complete documentation is coming soon. The most detailed usage examples are visible by browsing the website's examples.

Any sparse documentation available in this Readme is being progressively improved as the API evolves.

Memoize your Props!

As you'll see in every example, the React Charts <Chart> component expects all props and options to be memoized using either React.useMemo or React.useCallback. While passing an unmemoized option/prop to the <Chart> component won't severly break any visible functionality, your charts will be severly non-performant. Internally, React Charts uses the immutable nature of thes options/props to detect changes to the configuration and update accordingly.

While this may feel heavy at first, it gives you, the dev, full control over when you want to update your charts. To trigger and update, simply trigger one of your React.useMemo or React.useCallback hooks on the part of the config that you would like to update!

Data Model

React Charts uses a common and very flexible data model based on arrays of series and arrays of datums. You can either use the model defaults directly, or use data accessors to materialize this structure.

Typical visualization data can come in practically any shape and size. The following examples show data structures that are all reasonably equivalent at some level since they each contain an array of series[] and datums[]. They also show how to parse that data.

In the following example, there is no need to use any accessors. The default accessors are able to easily understand this format:

function MyChart() {
  const data = React.useMemo(
    () => [
      {
        label: 'Series 1',
        data: [{ x: 1, y: 10 }, { x: 2, y: 10 }, { x: 3, y: 10 }]
      },
      {
        label: 'Series 2',
        data: [{ x: 1, y: 10 }, { x: 2, y: 10 }, { x: 3, y: 10 }]
      },
      {
        label: 'Series 3',
        data: [{ x: 1, y: 10 }, { x: 2, y: 10 }, { x: 3, y: 10 }]
      }
    ],
    []
  )

  const axes = React.useMemo(
    () => [
      { primary: true, type: 'linear', position: 'bottom' },
      { type: 'linear', position: 'left' }
    ],
    []
  )

  return (
    <div
      style={{
        width: '400px',
        height: '300px'
      }}
    >
      <Chart data={data} axes={axes} />
    </div>
  )
}

In the following example, there is no need to use any accessors. The default accessors are able to easily understand this format, but please note that this format limits you from passing any meta data about your series and datums.

function MyChart() {
  const data = React.useMemo(
    () => [
      [[1, 10], [2, 10], [3, 10]],
      [[1, 10], [2, 10], [3, 10]],
      [[1, 10], [2, 10], [3, 10]]
    ],
    []
  )

  const axes = React.useMemo(
    () => [
      { primary: true, type: 'linear', position: 'bottom' },
      { type: 'linear', position: 'left' }
    ],
    []
  )

  return (
    <div
      style={{
        width: '400px',
        height: '300px'
      }}
    >
      <Chart data={data} axes={axes} />
    </div>
  )
}

Data Accessors

When data isn't in a convenient format for React Charts, your first instinct will be to transform your data into the above formats. Don't do that! There is an easier way 🎉 We can use the Chart components' accessor props to point things in the right direction. Accessor props pass the original data and the series/datums you return down the line to form a new data model. See the <Chart> component for all available accessors.

In the following example, the data is in a very funky format, but at it's core is the same as the previous examples.

function MyChart() {
  // Use any data object you want
  const data = React.useMemo(
    () => ({
      axis: [1, 2, 3],
      lines: [
        { data: [{ value: 10 }, { value: 10 }, { value: 10 }] },
        { data: [{ value: 10 }, { value: 10 }, { value: 10 }] },
        { data: [{ value: 10 }, { value: 10 }, { value: 10 }] }
      ]
    }),
    []
  )

  // Use data.lines to represent the different series
  const getSeries = React.useCallback(data => data.lines, [])

  // Use data.lines[n].data to represent the different datums for each series
  const getDatums = React.useCallback(series => series.data, [])

  // Use the original data object and the datum index to reference the datum's primary value.
  const getPrimary = React.useCallback(
    (datum, i, series, seriesIndex, data) => data.axis[i],
    []
  )

  // Use data.lines[n].data[n].value as each datums secondary value
  const getSecondary = React.useCallback(datum => datum.value, [])

  return (
    <div
      style={{
        width: '400px',
        height: '300px'
      }}
    >
      <Chart
        data={data}
        getSeries={getSeries}
        getDatums={getDatums}
        getPrimary={getPrimary}
        getSecondary={getSecondary}
      />
    </div>
  )
}

Series Labels

Multiple series are often useless without labels. By default, React Charts looks for the label value on the series object you pass it. If not found, it will simply label your series as Series [n], where [n] is the zero-based index of the series, plus 1.

If the default label accessor doesn't suit your needs, then you can use the <Chart> component's getLabel accessor prop:

function MyChart() {
  const data = React.useMemo(
    () => [
      {
        specialLabel: 'Hello World!',
        data: [
          //...
        ]
      }
    ],
    []
  )

  const getLabel = React.useCallback(series => series.specialLabel, [])

  return (
    <div
      style={{
        width: '400px',
        height: '300px'
      }}
    >
      <Chart data={data} getLabel={getLabel} />
    </div>
  )
}

Axes & Scales

React Charts supports an axes prop that handles both the underlying scale and visual rendering. These axes can be combined and configured to plot data in many ways. To date, we have the following scale types available:

  • Cartesian
    • linear - A continuous axis used for plotting numerical data on an evenly distributed scale. Works well both as a primary and secondary axis.
    • ordinal - A banded axis commonly used to plot categories or ordinal information. Works well as the primary axis for bar charts.
    • time - A continuous axis used for plotting localized times and dates on an evenly distributed scale. Works well as a primary axis.
    • utc - Similar to the time scale, but supports UTC datetimes instead of localized datetimes. Works well as a primary axis.
    • log - A continuous axis used for plotting numerical data on a logarithmically distributed scale. Works well as a secondary axis
    • pie - A standalone numerical axis used for plotting arc lengths on a pie chart. Use this as the only axis when plotting a Pie chart. -->

Axes are a required component of a React Chart and can used like so:

import { Chart } from 'react-charts'

function MyChart() {
  const axes = React.useMemo(
    () => [
      { primary: true, type: 'time', position: 'bottom' },
      { type: 'linear', position: 'left' }
    ],
    []
  )

  return (
    <div
      style={{
        width: '400px',
        height: '300px'
      }}
    >
      <Chart axes={axes} />
    </div>
  )
}

For more information on usage and API, see the axes prop

Series Types

  • Cartesian
    • line
    • area
    • bar
    • bubble
  • Radial
    • pie

Example

function MyChart() {
  const series = React.useMemo(() => ({ curve: 'cardinal' }), [])

  return <Chart series={series} />
}

Advanced API

<Chart /> Props

  • getSeries() - Responsible for returning an array of series.
    • Default - () => null
    • Arguments:
      • data - The original
    • Returns an Object
  • getLabel()
    • Default - () => null
    • Arguments:
      • Thing
    • Returns an Object
  • getSeriesID()
    • Default - () => null
    • Arguments:
      • Thing
    • Returns an Object
  • getDatums()
    • Default - () => null
    • Arguments:
      • Thing
    • Returns an Object
  • getPrimary()
    • Default - () => null
    • Arguments:
      • Thing
    • Returns an Object
  • getSecondary()
    • Default - () => null
    • Arguments:
      • Thing
    • Returns an Object

Curve Types

All series types that support lines or curves can be configured to use any curve function from d3-shape by passing one of the following strings as the curve prop to a series component. You may also pass your own curve function directly from d3 or if you're feeling powerful, even create your own!

Note the following string correspond to their respective d3 curve functions but with the curve prefix removed.

  • basisClosed
  • basisOpen
  • basis
  • bundle
  • cardinalClosed
  • cardinalOpen
  • cardinal
  • catmullRomClosed
  • catmullRomOpen
  • catmullRom
  • linearClosed
  • linear
  • monotoneX (default)
  • monotoneY
  • natural
  • step
  • stepAfter
  • stepBefore
3.0.0-beta.56

7 months ago

3.0.0-beta.57

6 months ago

3.0.0-beta.55

10 months ago

3.0.0-beta.52

1 year ago

3.0.0-beta.53

1 year ago

3.0.0-beta.54

1 year ago

3.0.0-beta.51

1 year ago

3.0.0-beta.50

2 years ago

3.0.0-beta.49

2 years ago

3.0.0-beta.40

2 years ago

3.0.0-beta.41

2 years ago

3.0.0-beta.42

2 years ago

3.0.0-beta.43

2 years ago

3.0.0-beta.44

2 years ago

3.0.0-beta.45

2 years ago

3.0.0-beta.46

2 years ago

3.0.0-beta.47

2 years ago

3.0.0-beta.48

2 years ago

3.0.0-beta.37

2 years ago

3.0.0-beta.38

2 years ago

3.0.0-beta.39

2 years ago

3.0.0-beta.36

2 years ago

3.0.0-beta.35

2 years ago

3.0.0-beta.34

2 years ago

3.0.0-beta.33

3 years ago

3.0.0-beta.32

3 years ago

3.0.0-beta.31

3 years ago

3.0.0-beta.30

3 years ago

3.0.0-beta.27

3 years ago

3.0.0-beta.28

3 years ago

3.0.0-beta.29

3 years ago

3.0.0-beta.24

3 years ago

3.0.0-beta.25

3 years ago

3.0.0-beta.26

3 years ago

3.0.0-beta.22

3 years ago

3.0.0-beta.23

3 years ago

3.0.0-beta.20

3 years ago

3.0.0-beta.21

3 years ago

3.0.0-beta.19

3 years ago

3.0.0-beta.16

3 years ago

3.0.0-beta.17

3 years ago

3.0.0-beta.18

3 years ago

3.0.0-beta.15

3 years ago

3.0.0-beta.10

3 years ago

3.0.0-beta.11

3 years ago

3.0.0-beta.12

3 years ago

3.0.0-beta.13

3 years ago

3.0.0-beta.14

3 years ago

3.0.0-beta.7

3 years ago

3.0.0-beta.9

3 years ago

3.0.0-beta.8

3 years ago

3.0.0-beta.6

3 years ago

3.0.0-beta.3

3 years ago

3.0.0-beta.5

3 years ago

3.0.0-beta.4

3 years ago

3.0.0-beta.1

3 years ago

3.0.0-beta.2

3 years ago

2.1.0

3 years ago

2.1.0-beta.1

3 years ago

2.1.0-next.2

3 years ago

2.1.0-next.1

3 years ago

2.0.3-next.7

3 years ago

2.0.3-next.6

3 years ago

2.0.3-next.5

3 years ago

2.0.3-next.4

3 years ago

2.0.3-next.3

3 years ago

2.0.2

3 years ago

2.0.3-next.2

3 years ago

2.0.3-next.1

3 years ago

2.0.2-next.22

3 years ago

2.0.2-next.21

4 years ago

2.0.2-next.20

4 years ago

2.0.2-next.19

4 years ago

2.0.2-next.18

4 years ago

2.0.2-next.17

4 years ago

2.0.2-next.16

4 years ago

2.0.2-next.15

4 years ago

2.0.2-next.14

4 years ago

2.0.2-next.13

4 years ago

2.0.2-next.12

4 years ago

2.0.2-next.11

4 years ago

2.0.2-next.10

4 years ago

2.0.2-next.6

4 years ago

2.0.2-next.5

4 years ago

2.0.2-next.4

4 years ago

2.0.2-next.3

4 years ago

2.0.2-next.2

4 years ago

2.0.0-beta.7

4 years ago

2.0.1

4 years ago

2.0.2-next.1

4 years ago

0.0.10

4 years ago

2.0.0-beta.6

4 years ago

2.0.0-beta.5

5 years ago

2.0.0-beta.4

5 years ago

2.0.0-beta.3

5 years ago

2.0.0-beta.2

5 years ago

2.0.0-beta.1

5 years ago

2.0.0-beta.0

5 years ago

2.0.0-alpha.12

5 years ago

2.0.0-alpha.11

5 years ago

2.0.0-alpha.10

5 years ago

2.0.0-alpha.9

5 years ago

2.0.0-alpha.8

5 years ago

2.0.0-alpha.7

5 years ago

2.0.0-alpha.6

5 years ago

2.0.0-alpha.5

5 years ago

2.0.0-alpha.4

5 years ago

2.0.0-alpha.3

5 years ago

2.0.0-alpha.2

5 years ago

2.0.0-alpha.1

5 years ago

2.0.0-alpha.0

5 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

0.1.0

7 years ago

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

10 years ago