@carbonplan/charts

3.1.1 • Public • Published

CarbonPlan monogram.

carbonplan / charts

responsive reactive themeable charts

CI GitHub deployments NPM Version License: MIT

There are a lot of charting libraries for the web. Somehow, this one fills a niche that we think still exists. If nothing else, we find it useful, and will be using it ourselves! It's still super early in development, so expect lots of changes.

We're designing @carbonplan/charts with a few goals in mind:

  • built for modern react ↗️
  • fully responsive ↔️
  • modular and composable ♻️
  • easy to theme via theme-ui
  • plays great with MDX 🎉

It's also super light weight, depending only on react and theme-ui and some d3 utility packages. If there's interest we could probably factor out the theme-ui dependency so it's optional.

background

This package is most directly inspired by pancake, an experimental package build for svelte. The concept motivating pancake is that responsive charts that adapt fluidly to their containers without the need for redrawing can be built by carefully combining HTML for the chart and axes and SVG with viewBox for the graphical elements. It's like an SVG sandwich or pancake. Pick your favorite food metaphor. Rich Harris described the concept super clearly in a blog post. Without knowing about pancake, we started doing almost exactly the same thing a few months ago for our charts at CarbonPlan. There must be something to it.

However, to our knowledge, none of the existing charting libraries for JS help streamline this pattern. And pancake is built for svelte, whereas we're mainly using react. That's how we got here.

Also worth mentioning that we've used vega and vega-lite and d3 a lot. We love them, we're taking inspiration all over the place, and even using some d3 utilities under the hood. Incredibly grateful to the developers of those tools.

usage

Here's a super simple scatter chart just to give you the idea.

import {
  Chart,
  Grid,
  Ticks,
  TickLabels,
  AxisLabel,
  Plot,
  Scatter,
} from '@carbonplan/charts'

const ScatterChart = () => {
  return (
    <div style={{ width: '500px', height: '500px', padding: '64px' }}>
      <Chart x={[0, 100]} y={[0, 100]}>
        <Grid vertical horizontal />
        <Ticks left bottom />
        <TickLabels left bottom />
        <AxisLabel left>Variable one</AxisLabel>
        <AxisLabel bottom>Variable two</AxisLabel>
        <Plot>
          <Scatter
            data={[
              [10, 10],
              [50, 50],
              [90, 90],
            ]}
          />
        </Plot>
      </Chart>
    </div>
  )
}

This creates the following chart, shown with the light and dark themes of @carbonplan/theme.

Screen Shot 2021-05-16 at 10 11 50 AM

Screen Shot 2021-05-16 at 10 18 51 AM

Many more examples and documentation coming soon.

development

To update a component and publish a new version, first make your changes, then follow these steps

  • Increase the version number in package.json
  • npm run build
  • npm publish

license

All the code in this repository is MIT-licensed, but we request that you please provide attribution if reusing any of our digital content (graphics, logo, articles, etc.).

about us

CarbonPlan is a nonprofit organization that uses data and science for climate action. We aim to improve the transparency and scientific integrity of climate solutions with open data and tools. Find out more at carbonplan.org or get in touch by opening an issue or sending us an email.

Package Sidebar

Install

npm i @carbonplan/charts

Weekly Downloads

46

Version

3.1.1

License

MIT

Unpacked Size

428 kB

Total Files

9

Last publish

Collaborators

  • shane98c
  • freeman-lab
  • katamartin