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

react-highcharts-wrapper

v1.1.0

Published

A React wrapper for building Highcharts components, that provides a singular reference to the Highcharts global.

Downloads

187

Readme

React Highcharts Wrapper

npm version

Build Status

codecov

A React wrapper for building Highcharts components.

  1. A helper Component to wrap chart instantiation

  2. A Provider that hosts a singular reference to the Highcharts global

TLDR

app.js

render() (
  <HighchartsProvider executeFuncs={[]}>  // [optional] array of functions to decorate window.Highcharts
    <App />
  </HighchartsProvider>
)

chart.js

render() (
  <AbstractChart config={{  // any Highcharts config object here
    chart: {
      type: 'pie'
    }
  }} />
)

Why

  1. You want to be productive with Highcharts in React.

  2. You want to build Highcharts components using the Highcharts Config API.

  3. You want a tiny library.

  4. You want a singular reference to the Highcharts Primitive, to do things like apply a Custom Theme or extend Highcharts for your whole application.

Demo

Try it:

Open this repo in Codesandbox

Examples:

https://react-highcharts-wrapper-demos.firebaseapp.com

Pitch site:

https://react-highcharts-wrapper.firebaseapp.com

The problem

  1. Highcharts does not translate to the ES6+ ecosystem, as the Highcharts constructor is global you can not apply application wide configuration to Highcharts without attaching it to window.

  2. Highcharts depends on the DOM existing to render.

  3. Rehydrating Highcharts in the React view re-rendering paradigm is not native for Highcharts.

The solution

  1. Wraps your application with Highcharts using HighchartsProvider. And then consumes the Highcharts constructor internally with a higher order component withHighcharts

2.3. Render charts and manage their lifecycle with a helper component AbstractChart, passing in normal Highcharts config as props.

Getting started

1. Add the module to your React app:

npm install react-highcharts-wrapper

2. Wrap your app with HighchartsProvider.

app.js

import {HighchartsProvider} from "react-highcharts-wrapper";

const App = () => {
  return (
    <HighchartsProvider>
      <div>
        <p>Welcome to my amazing app</p>
        <MyPage />
      </div>
    </HighchartsProvider>
  )
};

This will make Highcharts available to context.

3. Build the chart components you need with AbstractChart passing in a standard Highcharts configuration object.

components/myPieChart.js

import {AbstractChart} from 'react-highcharts-wrapper';

const MyPieChart = () => {
  return (
    <div>
      <h1>My Pie Chart</h1>
      <AbstractChart config={{
        chart: {
          type: 'pie'
        },
        series: [{
          name: 'Brands',
          colorByPoint: true,
          data: [
            {name: 'Microsoft Internet Explorer', y: 56.33}, 
            {name: 'Chrome', y: 24.03, sliced: true, selected: true}, 
            {name: 'Firefox', y: 10.38}, 
            {name: 'Safari', y: 4.77}, 
            {name: 'Opera', y: 0.91},
            {name: 'Proprietary or Undetectable', y: 0.2}
            ]
        }],
      }} />
    </div>
  )
};

4. Consume the charts like any other component

components/myPage.js

import MyPieChart from './myPieChart';

const MyPage = () => {
  return (
    <div>
      <h1>My page</h1>
      <MyPieChart />
    </div>
  )
};

Done! 🏁

Optional: How can I extend the Highcharts primitive?

You can pass an array of executable functions to HighchartsProvider like this:

<HighchartsProvider executeFuncs={[
  (Highcharts) => {
    console.log(Highcharts)
    return Highcharts;
  }
]}>

This can be useful for setting a default theme for example for Highcharts:

const HIGHCHARTS_THEME = {
  chart: {
    style: {
      fontFamily: 'Open Sans,sans-serif',
    },
  },
}

<HighchartsProvider executeFuncs={[
  (Highcharts) => {
    Highcharts.setOptions({
      ...HIGHCHARTS_THEME
    });
    return Highcharts;
  },
  (Highcharts) => {
    Highcharts.loremIpsum = 'boo ya!';
    return Highcharts;
  }
]}>