Convert Figma logo to code with AI

airbnb logovisx

🐯 visx | visualization components

19,429
710
19,429
150

Top Related Projects

23,884

Redefined chart library built with React and D3

10,987

A collection of composable React components for building interactive data visualizations

108,427

Bring data to life with SVG, Canvas and HTML. :bar_chart::chart_with_upwards_trend::tada:

13,142

nivo provides a rich set of dataviz components, built on top of the awesome d3 and React libraries

64,568

Simple HTML5 Charts using the <canvas> tag

Quick Overview

visx is a collection of reusable low-level visualization components for React. It aims to provide the building blocks for creating custom charts and data visualizations while leveraging the power of D3.js for calculations and React for rendering. visx allows developers to create complex visualizations without sacrificing the flexibility and control of lower-level components.

Pros

  • Highly customizable and flexible, allowing for creation of unique visualizations
  • Leverages the strengths of both D3.js and React
  • Modular design allows for using only the components needed, reducing bundle size
  • Strong TypeScript support for improved developer experience

Cons

  • Steeper learning curve compared to high-level charting libraries
  • Requires more code to create basic charts compared to other libraries
  • Less suitable for quick prototyping or simple visualizations
  • Documentation can be overwhelming due to the large number of components

Code Examples

Creating a simple bar chart:

import { Bar } from '@visx/shape';
import { Group } from '@visx/group';
import { scaleLinear, scaleBand } from '@visx/scale';

const data = [
  { city: 'New York', population: 8419000 },
  { city: 'Los Angeles', population: 3898000 },
  { city: 'Chicago', population: 2746000 },
];

const width = 500;
const height = 300;

const xScale = scaleBand({
  range: [0, width],
  domain: data.map(d => d.city),
  padding: 0.2,
});

const yScale = scaleLinear({
  range: [height, 0],
  domain: [0, Math.max(...data.map(d => d.population))],
});

const BarChart = () => (
  <svg width={width} height={height}>
    <Group>
      {data.map(d => (
        <Bar
          key={d.city}
          x={xScale(d.city)}
          y={yScale(d.population)}
          height={height - yScale(d.population)}
          width={xScale.bandwidth()}
          fill="#3182CE"
        />
      ))}
    </Group>
  </svg>
);

Creating a line chart with an axis:

import { LinePath } from '@visx/shape';
import { scaleTime, scaleLinear } from '@visx/scale';
import { AxisLeft, AxisBottom } from '@visx/axis';
import { curveMonotoneX } from '@visx/curve';

const data = [
  { date: new Date('2021-01-01'), value: 10 },
  { date: new Date('2021-02-01'), value: 20 },
  { date: new Date('2021-03-01'), value: 15 },
];

const width = 500;
const height = 300;
const margin = { top: 20, right: 20, bottom: 40, left: 40 };

const xScale = scaleTime({
  range: [margin.left, width - margin.right],
  domain: [Math.min(...data.map(d => d.date)), Math.max(...data.map(d => d.date))],
});

const yScale = scaleLinear({
  range: [height - margin.bottom, margin.top],
  domain: [0, Math.max(...data.map(d => d.value))],
});

const LineChart = () => (
  <svg width={width} height={height}>
    <LinePath
      data={data}
      x={d => xScale(d.date)}
      y={d => yScale(d.value)}
      stroke="#3182CE"
      strokeWidth={2}
      curve={curveMonotoneX}
    />
    <AxisLeft scale={yScale} left={margin.left} />
    <AxisBottom scale={xScale} top={height - margin.bottom} />
  </svg>
);

Getting Started

To start using visx, install the necessary packages:

npm install @visx/group @visx/shape @

Competitor Comparisons

23,884

Redefined chart library built with React and D3

Pros of Recharts

  • More comprehensive and ready-to-use chart components
  • Easier to get started with for beginners
  • Better documentation and examples

Cons of Recharts

  • Less flexibility for custom visualizations
  • Larger bundle size due to more built-in components
  • Limited animation capabilities compared to Visx

Code Comparison

Recharts example:

import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend } from 'recharts';

<LineChart width={600} height={300} data={data}>
  <Line type="monotone" dataKey="uv" stroke="#8884d8" />
  <CartesianGrid stroke="#ccc" />
  <XAxis dataKey="name" />
  <YAxis />
</LineChart>

Visx example:

import { LinePath } from '@visx/shape';
import { scaleLinear, scaleTime } from '@visx/scale';

<svg width={600} height={300}>
  <LinePath
    data={data}
    x={d => xScale(d.date)}
    y={d => yScale(d.value)}
    stroke="#8884d8"
  />
</svg>

Both libraries offer powerful charting capabilities, but Recharts provides more out-of-the-box components, while Visx offers greater flexibility for custom visualizations. Recharts is generally easier for beginners, while Visx gives more control to experienced developers who want to create unique chart designs.

10,987

A collection of composable React components for building interactive data visualizations

Pros of Victory

  • More comprehensive and opinionated charting library
  • Easier to get started with for beginners
  • Better documentation and examples

Cons of Victory

  • Less flexible and customizable than visx
  • Larger bundle size due to higher-level abstractions
  • May have performance issues with large datasets

Code Comparison

Victory:

<VictoryChart>
  <VictoryBar data={data} x="quarter" y="earnings" />
</VictoryChart>

visx:

<svg width={400} height={300}>
  <BarGroup data={data} x0={d => d.quarter} y0={d => d.earnings} />
</svg>

Summary

Victory is a more complete charting solution that's easier for beginners to use, while visx offers greater flexibility and control over chart elements. Victory provides higher-level components, resulting in simpler code but larger bundle sizes. visx requires more manual setup but allows for finer-grained customization and potentially better performance with large datasets. The choice between the two depends on the specific needs of the project and the developer's preference for abstraction versus control.

108,427

Bring data to life with SVG, Canvas and HTML. :bar_chart::chart_with_upwards_trend::tada:

Pros of D3

  • Highly flexible and powerful, allowing for complex, custom visualizations
  • Extensive documentation and large community support
  • Direct manipulation of the DOM for fine-grained control

Cons of D3

  • Steeper learning curve, especially for those new to data visualization
  • Requires more boilerplate code for common chart types
  • Less integration with React ecosystem

Code Comparison

D3:

const svg = d3.select("body").append("svg")
    .attr("width", 400)
    .attr("height", 300);

svg.selectAll("rect")
    .data(data)
    .enter()
    .append("rect")
    .attr("x", (d, i) => i * 40)
    .attr("y", d => 300 - d * 10)
    .attr("width", 35)
    .attr("height", d => d * 10);

Visx:

<XYChart height={300} width={400} xScale={xScale} yScale={yScale}>
  <BarGroup data={data}>
    {(barGroups) =>
      barGroups.map((barGroup) => (
        <Bar
          key={`bar-${barGroup.index}`}
          x={barGroup.x}
          y={barGroup.y}
          width={barGroup.width}
          height={barGroup.height}
        />
      ))
    }
  </BarGroup>
</XYChart>

Visx provides a more React-friendly approach with declarative components, while D3 offers lower-level control over SVG elements. Visx simplifies common chart creation, but D3 allows for more customization and complex visualizations.

13,142

nivo provides a rich set of dataviz components, built on top of the awesome d3 and React libraries

Pros of nivo

  • Offers a wide range of pre-built chart components
  • Provides server-side rendering capabilities
  • Includes theming and customization options out of the box

Cons of nivo

  • Less flexible for creating custom visualizations
  • Larger bundle size due to comprehensive feature set
  • Steeper learning curve for advanced customizations

Code Comparison

nivo example:

import { ResponsiveLine } from '@nivo/line'

const MyChart = ({ data }) => (
  <ResponsiveLine
    data={data}
    margin={{ top: 50, right: 110, bottom: 50, left: 60 }}
    xScale={{ type: 'point' }}
    yScale={{ type: 'linear', min: 'auto', max: 'auto', stacked: true, reverse: false }}
  />
)

visx example:

import { LinePath } from '@visx/shape'
import { scaleLinear } from '@visx/scale'

const MyChart = ({ data }) => {
  const xScale = scaleLinear({ domain: [0, data.length - 1], range: [0, width] })
  const yScale = scaleLinear({ domain: [0, Math.max(...data)], range: [height, 0] })
  return <LinePath data={data} x={(d, i) => xScale(i)} y={d => yScale(d)} />
}

visx provides lower-level components, offering more flexibility but requiring more setup. nivo offers higher-level components with built-in responsiveness and styling, but less granular control over individual elements.

64,568

Simple HTML5 Charts using the <canvas> tag

Pros of Chart.js

  • Easier to get started with, especially for beginners
  • More comprehensive documentation and examples
  • Wider browser compatibility, including older versions

Cons of Chart.js

  • Less flexible for creating custom or complex visualizations
  • Limited to canvas-based rendering, which may impact performance for large datasets
  • Fewer options for advanced interactivity and animations

Code Comparison

Chart.js example:

new Chart(ctx, {
    type: 'bar',
    data: {
        labels: ['Red', 'Blue', 'Yellow'],
        datasets: [{
            label: 'My Dataset',
            data: [12, 19, 3]
        }]
    }
});

visx example:

<XYChart height={300} xScale={{ type: 'band' }} yScale={{ type: 'linear' }}>
  <Bar dataKey="value" data={data} />
  <Axis orientation="bottom" />
  <Axis orientation="left" />
</XYChart>

Summary

Chart.js is more beginner-friendly and widely supported, making it a good choice for simple charts and graphs. visx, on the other hand, offers greater flexibility and customization options, leveraging React and D3 for more complex visualizations. Chart.js uses canvas rendering, while visx uses SVG, which can affect performance and styling capabilities. The choice between the two depends on the project's requirements, the developer's familiarity with React, and the desired level of customization.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Coverage Status

visx

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


Docs Gallery Blog Discussions Changelog Getting started tutorial

Usage

Remix on Glitch

Let's make a simple bar graph.

First we'll install the relevant packages:

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

// We'll use some mock data from `@visx/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({
  range: [0, xMax],
  round: true,
  domain: data.map(x),
  padding: 0.4,
});
const yScale = scaleLinear({
  range: [yMax, 0],
  round: true,
  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 visx, 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. visx is largely unopinionated and is meant to be built upon. Keep your bundle sizes down and use only the packages you need.

How?

Under the hood, visx is using d3 for the calculations and math. If you're creating your own awesome chart library on top of visx, 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().

In the wild

Have a project that's using visx? Open a pull request and we'll add it to the list.

FAQ

  1. What does visx stand for?

    visx stands for visualization components.

  2. Do you plan on supporting animation/transitions?

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

    Imagine your app already bundles react-motion, adding a hypothetical @visx/animation is bloat. Since visx 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.

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

    more info: https://github.com/airbnb/visx/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 visx work with preact?

    yup! need to alias react + react-dom and use preact-compat.

  6. I like using d3.

    Me too.

Development

Please see CONTRIBUTING.md

:v:

MIT

FOSSA Status

NPM DownloadsLast 30 Days