项目作者: chartshq

项目描述 :
React SDK for Muze charting Library
高级语言: TypeScript
项目地址: git://github.com/chartshq/react-muze.git
创建时间: 2020-10-08T13:13:13Z
项目社区:https://github.com/chartshq/react-muze

开源协议:MIT License

下载








react-muze







License
@chartshq/react-muze"">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.

  1. 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.

  1. npm install copy-webpack-plugin@5.1.1 -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

  1. 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

  1. const CopyWebpackPlugin = require('copy-webpack-plugin');
  2. const path = require("path");
  3. module.exports = function override(config, env) {
  4. //add webpack copy plugin
  5. const copyPlugin = new CopyWebpackPlugin([
  6. {
  7. from: path.resolve("node_modules", "@chartshq/muze/dist"),
  8. to: '.',
  9. }
  10. ]);
  11. if (!config.plugins) {
  12. config.plugins = [];
  13. }
  14. config.plugins.push(copyPlugin);
  15. return config;
  16. }

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

  1. {
  2. "scripts": {
  3. "start": "react-app-rewired start",
  4. "build": "react-app-rewired build"
  5. }
  6. }

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

  1. {
  2. plugins: [
  3. new CopyWebpackPlugin([
  4. {
  5. from: path.resolve("node_modules", "@chartshq/muze/dist"),
  6. to: '.',
  7. }
  8. ])
  9. ]
  10. }

Creating your first Chart

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

  1. const data = [
  2. {
  3. Name: "chevrolet chevelle malibu",
  4. Acceleration: 12,
  5. },
  6. {
  7. Name: "buick skylark 320",
  8. Acceleration: 11.5,
  9. },
  10. {
  11. Name: "plymouth satellite",
  12. Acceleration: 11,
  13. },
  14. {
  15. Name: "amc rebel sst",
  16. Acceleration: 12,
  17. },
  18. ];
  19. const schema = [
  20. {
  21. name: "Name",
  22. type: "dimension",
  23. },
  24. {
  25. name: "Acceleration",
  26. type: "measure",
  27. defAggFn: "avg",
  28. },
  29. ];

Step 1 - Import Muze, Canvas, DataModel as follows

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

Step 2 - Create a DataModel Instance from the data

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

Step 3 - Rendering Muze

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

  1. render() {
  2. // carsDm is the a dataModel instance
  3. // created from `data` and `schema`,
  4. // and saved on state
  5. const { carsDm } = this.state;
  6. return (
  7. <Muze data={carsDm}>
  8. <Canvas rows={["Acceleration"]} columns={["Name"]} ></Canvas>
  9. </Muze>
  10. );
  11. }

Full Code of the example

  1. import React from "react";
  2. import Muze, { Canvas } from "@chartshq/react-muze/components";
  3. const data = [
  4. {
  5. Name: "chevrolet chevelle malibu",
  6. Acceleration: 12,
  7. },
  8. {
  9. Name: "buick skylark 320",
  10. Acceleration: 11.5,
  11. },
  12. {
  13. Name: "plymouth satellite",
  14. Acceleration: 11,
  15. },
  16. {
  17. Name: "amc rebel sst",
  18. Acceleration: 12,
  19. },
  20. ];
  21. const schema = [
  22. {
  23. name: "Name",
  24. type: "dimension",
  25. },
  26. {
  27. name: "Acceleration",
  28. type: "measure",
  29. defAggFn: "avg",
  30. },
  31. ];
  32. async function createDataModel() {
  33. const DataModelClass = await Muze.DataModel.onReady();
  34. const formattedData = await DataModelClass.loadData(data, schema);
  35. return new DataModelClass(formattedData);
  36. }
  37. class Chart extends React.Component {
  38. constructor(props) {
  39. super(props);
  40. this.state = {
  41. carsDm: null,
  42. };
  43. }
  44. componentDidMount() {
  45. createDataModel().then((carsDm) => {
  46. this.setState({ carsDm });
  47. });
  48. }
  49. render() {
  50. const { carsDm } = this.state;
  51. return (
  52. <Muze data={carsDm}>
  53. <Canvas rows={["Acceleration"]} columns={["Name"]} ></Canvas>
  54. </Muze>
  55. );
  56. }
  57. }
  58. 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

  1. yarn install
  2. yarn build
  3. cd dist && yarn link / npm link --only=production
  4. yarn watch-build

Go to the examples directory and run the following commands

  1. yarn install
  2. yarn link @chartshq/react-muze
  3. 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,

  1. // import BoxPlot from './Examples/Composability/BoxPlot';
  2. import SimplePieChart from './Examples/Pie/SimplePie';
  3. ReactDOM.render(
  4. <React.StrictMode>
  5. <SimplePieChart ></SimplePieChart>
  6. </React.StrictMode>,
  7. document.getElementById("root")
  8. );

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