npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@chartshq/react-muze

v1.0.1

Published

React-Muze is a ReactJs wrapper over the core MuzeJs library which provides ReactJs bindings for MuzeJs making it easy to create charts for the ReactJs ecosystem.

Downloads

110

Readme

License NPM version Contributors

React-Muze

React-Muze is a React wrapper over the core Muze library. It provides React bindings for Muze and makes it easier to create charts using Muze for your React applications.

What is Muze?

Muze is a free library for creating exploratory data visualizations in the browser that is powered by WebAssembly. It is ideal for use in visual analytics dashboards & applications to create highly performant, interactive, multi-dimensional, and composable visualizations with the Grammar of Graphics approach. More about Muze here: https://muzejs.org/docs/wa/latest/introduction

Installation & Usage

Installation

To use React-Muze in your React project, you need to install the muze and react-muze package from NPM.

npm install @chartshq/muze @chartshq/react-muze

Next, as Muze is built on top of WebAssembly, we need to copy some WebAssembly assets to our build directory. To accomplish that we are going to use the copy-webpack-plugin NPM package in our build config.

npm install [email protected] -D

For a project created using Create-React-App

Since applications built with Create-React-App does not expose webpack config until ejected, we need to use the react-app-rewired package, to add the custom webpack config. How it works here: react-app-rewired

npm install react-app-rewired

Next, we need to create a file named config-overrides.js at the root of the project and add the following code in it

const CopyWebpackPlugin = require('copy-webpack-plugin');
const path = require("path");
module.exports = function override(config, env) {
  //add webpack copy plugin
  const copyPlugin = new CopyWebpackPlugin([
    {
      from: path.resolve("node_modules", "@chartshq/muze/dist"),
      to: '.',
    }
  ]);
  if (!config.plugins) {
    config.plugins = [];
  }
  config.plugins.push(copyPlugin);
  return config;
}

And finally, replace old start and build commands in your package.json with the following ones, and you are ready to go

{
  "scripts": {
    "start": "react-app-rewired start",
    "build": "react-app-rewired build"
  }
}

For a custom React project

In a custom setup, since we have direct access to webpack config, we can simply add copy-webpack-plugin configuration directly inside out webpack config. Just add the following config in the plugins section of your webpack.config.js file

{
  plugins: [
    new CopyWebpackPlugin([
      {
        from: path.resolve("node_modules", "@chartshq/muze/dist"),
        to: '.',
      }
    ])
  ]
}

Creating your first Chart

For this illustration, we will be using the following data and schema.

const data = [
  {
    Name: "chevrolet chevelle malibu",
    Acceleration: 12,
  },
  {
    Name: "buick skylark 320",
    Acceleration: 11.5,
  },
  {
    Name: "plymouth satellite",
    Acceleration: 11,
  },
  {
    Name: "amc rebel sst",
    Acceleration: 12,
  },
];
const schema = [
  {
    name: "Name",
    type: "dimension",
  },
  {
    name: "Acceleration",
    type: "measure",
    defAggFn: "avg",
  },
];

Step 1 - Import Muze, Canvas, DataModel as follows

import Muze, { Canvas } from "@chartshq/react-muze/components";

Step 2 - Create a DataModel Instance from the data

async function createDataModel() {
  const DataModelClass = await Muze.DataModel.onReady();
  const formattedData = await DataModelClass.loadData(data, schema);
  return new DataModelClass(formattedData);
}

Step 3 - Rendering Muze

In the render() method of you react component, we need to put the following

render() {
  // carsDm is the a dataModel instance
  // created from `data` and `schema`,
  // and saved on state
  const { carsDm } = this.state;

  return (
    <Muze data={carsDm}>
      <Canvas rows={["Acceleration"]} columns={["Name"]} />
    </Muze>
  );
}

Full Code of the example

import React from "react";
import Muze, { Canvas } from "@chartshq/react-muze/components";

const data = [
  {
    Name: "chevrolet chevelle malibu",
    Acceleration: 12,
  },
  {
    Name: "buick skylark 320",
    Acceleration: 11.5,
  },
  {
    Name: "plymouth satellite",
    Acceleration: 11,
  },
  {
    Name: "amc rebel sst",
    Acceleration: 12,
  },
];
const schema = [
  {
    name: "Name",
    type: "dimension",
  },
  {
    name: "Acceleration",
    type: "measure",
    defAggFn: "avg",
  },
];

async function createDataModel() {
  const DataModelClass = await Muze.DataModel.onReady();
  const formattedData = await DataModelClass.loadData(data, schema);
  return new DataModelClass(formattedData);
}

class Chart extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      carsDm: null,
    };
  }

  componentDidMount() {
    createDataModel().then((carsDm) => {
      this.setState({ carsDm });
    });
  }

  render() {
    const { carsDm } = this.state;

    return (
      <Muze data={carsDm}>
        <Canvas rows={["Acceleration"]} columns={["Name"]} />
      </Muze>
    );
  }
}

export default Chart;

Examples

In the example directory, you will find a react application that has many examples as individual components.

How to run the examples

Setup the project in your local environment

yarn install
yarn build
cd dist && yarn link / npm link --only=production
yarn watch-build

Go to the examples directory and run the following commands

yarn install
yarn link @chartshq/react-muze
yarn start

To try out all the other examples, inside the examples/src/index.js file import an example component and render on jsx. For example,

// import BoxPlot from './Examples/Composability/BoxPlot';
import SimplePieChart from './Examples/Pie/SimplePie';

ReactDOM.render(
  <React.StrictMode>
    <SimplePieChart />
  </React.StrictMode>,
  document.getElementById("root")
);

Contributing

Your PRs and stars are always welcome :). Checkout the Contributing guides.

Roadmap

Please contribute to our public wishlist or upvote an existing feature at Muze Public Wishlist & Roadmap.

License

MIT