54

GitHub - hshoff/vx: ?react + d3 = vx | visualization components

 5 years ago
source link: https://github.com/hshoff/vx
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md

Screen%20Shot%202017-05-05%20at%206.55.56%20AM.png

68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f4076782f64656d6f2e7376673f7374796c653d666c61742d737175617265 68747470733a2f2f7472617669732d63692e6f72672f6873686f66662f76782e7376673f6272616e63683d6d6173746572 Coverage Status 68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f646d2f4076782f73686170652e7376673f7374796c653d666c61742d737175617265 68747470733a2f2f77697468737065637472756d2e6769746875622e696f2f62616467652f62616467652e737667 68747470733a2f2f6170702e666f7373612e696f2f6170692f70726f6a656374732f67697425324268747470732533412532462532466769746875622e636f6d2532466873686f666625324676782e7376673f747970653d736869656c64 68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6d61696e7461696e6564253230776974682d6c65726e612d6363303066662e737667

vx

vx is a collection of reusable low-level visualization components. vx combines the power of d3 to generate your visualization with the benefits of react for updating the DOM.


Docs Gallery Blog Slack #vx Changelog Getting started tutorial

vx-gallery.png

Usage

Remix on Glitch

Let's make a simple bar graph.

First we'll install the relevant packages:

$ npm install --save @vx/mock-data @vx/group @vx/shape @vx/scale

simplebar.png

import React from 'react';
import { letterFrequency } from '@vx/mock-data';
import { Group } from '@vx/group';
import { Bar } from '@vx/shape';
import { scaleLinear, scaleBand } from '@vx/scale';

// We'll use some mock data from `@vx/mock-data` for this.
const data = letterFrequency;

// Define the graph dimensions and margins
const width = 500;
const height = 500;
const margin = { top: 20, bottom: 20, left: 20, right: 20 };

// Then we'll create some bounds
const xMax = width - margin.left - margin.right;
const yMax = height - margin.top - margin.bottom;

// We'll make some helpers to get at the data we want
const x = d => d.letter;
const y = d => +d.frequency * 100;

// And then scale the graph by our data
const xScale = scaleBand({
  rangeRound: [0, xMax],
  domain: data.map(x),
  padding: 0.4,
});
const yScale = scaleLinear({
  rangeRound: [yMax, 0],
  domain: [0, Math.max(...data.map(y))],
});

// Compose together the scale and accessor functions to get point functions
const compose = (scale, accessor) => (data) => scale(accessor(data));
const xPoint = compose(xScale, x);
const yPoint = compose(yScale, y);

// Finally we'll embed it all in an SVG
function BarGraph(props) {
  return (
    <svg width={width} height={height}>
      {data.map((d, i) => {
        const barHeight = yMax - yPoint(d);
        return (
          <Group key={`bar-${i}`}>
            <Bar
              x={xPoint(d)}
              y={yMax - barHeight}
              height={barHeight}
              width={xScale.bandwidth()}
              fill='#fc2e1c'
            />
          </Group>
        );
      })}
    </svg>
  );
}

// ... somewhere else, render it ...
// <BarGraph />

For more examples using vx, check out the gallery.

Motivation

Goal

The goal is to create a library of components you can use to make both your own reusable chart library or your slick custom one-off chart. vx is largely unopinionated and is meant to be build on top of. Keep your bundle sizes down and use only the packages you need.

How?

Under the hood, vx is using d3 for the calculations and math. If you're creating your own awesome chart library on top of vx, it's easy to create a component api that hides d3 entirely. Meaning your team could create charts as easily as using reusable react components.

But why?

Mixing two mental models for updating the DOM is never a good time. Copy and pasting d3 code into componentDidMount() is just that. This collection of components lets you easily build your own reusable visualization charts or library without having to learn d3. No more selections or enter()/exit()/update().

Status

Beta We're still in pre v1. Need to add interactions. No breaking changes planned right now read more. Check out the road to v1.

If you're a curious coder, feel free to install and play around with the packages. I recommend using --save-exact when you npm install.

Roadmap

Lots coming soon, check out the roadmap.

In the wild

FAQ

  1. What does vx stand for?

    vx stands for visualization components.

  2. Do you plan on supporting animation/transitions?

    A common criticism of vx is it doesn't have animation baked in, but this was a concious choice. It's a powerful feature to not bake it in.

    Imagine your app already bundles react-motion, adding a hypothetical @vx/animation is bloat. Since vx is react, it already supports all react animation libs.

    Charting libraries are like style guides. Each org or app will eventually want full control over their own implementation.

    vx makes this easier for everyone. No need to reinvent the wheel each time.

    more info: https://github.com/hshoff/vx/issues/6

    examples:

  3. Do I have to use every package to make a chart?

    nope! pick and choose the packages you need.

  4. Can I use this to create my own library of charts for my team?

    Please do.

  5. Does vx work with preact?

    yup! need to alias react + react-dom and use preact-compat. Here's a quick demo: https://vx-preact.now.sh/. more info

  6. I like using d3.

    Me too.

Development

lerna is used to manage versions and dependencies between packages in the umbrella vx repo.

vx/
  lerna.json
  package.json
  packages/
    vx-package-1/
      src/
      test/
      build/
      package.json
      ...
    vx-package-2/
      ...
    ...

For easiest development clone or fork vx, install the root dependencies including lerna, then have lerna install package dependencies and manage the symlinking between packages for you by using the lerna bootstrap command:

git clone [email protected]:hshoff/vx.git # or your fork
cd vx
npm install # installs root vx deps
./node_modules/.bin/lerna bootstrap # installs all package deps, sym-links within-vx deps

Upon modification of a given package you can run npm run build from that package's folder to re-build the package with your changes. You can use the local dev server within packages/vx-demo to view and iterate on your changes in the gallery. From the packages/vx-demo folder run npm run dev to start the next server which (if correctly sym-linked with lerna) will also watch for changes you make to other packages.

✌️

MIT@hshoff

FOSSA Status


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK