Giter Site home page Giter Site logo

react-chartist's Introduction

react-chartist

NPM version Downloads

React component for Chartist.js

Installation

$ npm install react-chartist --save

Chartist is a peer dependency to react chartist. You need to install it if you do not have it installed already.

$ npm install chartist --save

Usage

import React from 'react';
import ReactDOM from 'react-dom';
import ChartistGraph from 'react-chartist';

class Bar extends React.Component {
  render() {

    var data = {
      labels: ['W1', 'W2', 'W3', 'W4', 'W5', 'W6', 'W7', 'W8', 'W9', 'W10'],
      series: [
        [1, 2, 4, 8, 6, -2, -1, -4, -6, -2]
      ]
    };

    var options = {
      high: 10,
      low: -10,
      axisX: {
        labelInterpolationFnc: function(value, index) {
          return index % 2 === 0 ? value : null;
        }
      }
    };

    var type = 'Bar'

    return (
      <div>
        <ChartistGraph data={data} options={options} type={type} />
      </div>
    )
  }
}

ReactDOM.render(<Bar />, document.body)

Options

Please check out Chartist.js API documentation for more details of the options.

  • data - chart data (required)
  • type - chart type (required)
  • style - inline css styles (optional)
  • options - chart options (optional)
  • responsive-options - chart responsive options (optional)

To add support for aspect ratio

<ChartistGraph className={'ct-octave'} data={data} options={options} type={type} />

Note

This module does not include the css files for Chartist. If you want to add it, include their CDN in your html file

<link rel="stylesheet" href="//cdn.jsdelivr.net/chartist.js/latest/chartist.min.css">
<script src="//cdn.jsdelivr.net/chartist.js/latest/chartist.min.js"></script>

Or use bower or npm to install Chartist and include it in your build process.

$ npm install chartist

Or

$ bower install chartist

Development

$ npm install

To build run npm run build

Changelog

If you want to support react version under v0.13, use npm install [email protected]

License

MIT

react-chartist's People

Contributors

amwam avatar cmmartin avatar danielmschmidt avatar debugpai avatar diedsmiling avatar eyepulp avatar fraserxu avatar hsbiti avatar jiucheng-front avatar joshgerdes avatar julien-f avatar lawrencekgrant avatar mrvicadai avatar nerdcowboy avatar opudalo avatar osdiab avatar riccardo-ditosto-gellify avatar robinsoncol avatar rojerua avatar sajinshrestha avatar sergei-zelinsky avatar tanapoln avatar tlambrou avatar toldyaonce avatar vladmiller avatar wmira avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-chartist's Issues

Chartist v1

Hi 👋. I’m Dan from the Chartist team.

Thank you for your work on react-chartist!

Recently we’ve released Chartist v1.0.0, fully rewritten to TypeScript and updated to the modern tech. What do you think about adding Chartist v1 support to react-chartist? I’ll be happy to help with it if needed. Also, I can offer help with fully renovating this project, as I did with Chartist.

Error when rendering the chart

I get an error when first rendering the chart:
ReactMount.js:730 - Uncaught TypeError: Cannot read property 'firstChild' of undefined

It passes the getInitialState and then goes to the first render, but it doesn't seem to work there.

React version: '0.13.2',
React-Chartist version: '0.5.0',
Node version: '0.10.33',

Chartist[type] is not a constructor

I am using this on next.js but it shows TypeError: Chartist[type] is not a constructor.

<ChartistGraph
    data={{
        label: timeline.map(item => dayjs(item[0]).format('MM YYYY')),
        series: [timeline.map(item => item[1])]
    }}
    options={{
        fullWidth: true,
        axisX: {
            showGrid: true
        },
    }}
    type='Line'
/>

I tried <NoSsr /> of MUI but to no avail. Any Solutions?

Data refresh performance lacking

Love the library, but the fast data refresh performance seems to be lacking. I'm trying to re-plot 1200 data points every 30ms and it just isn't able to keep up. Victory and Chart.js can both render this smoothly but I'm really pushing for chartist.

Is this something that can be addressed through the react library, or though the main chartist library...if at all? Any tips would be greatly appreciated.

@types/chartist need to be runtime dependency

009cdb7

If you are going to export types as part of the repo, you need to cause package consumers to import transitive dependencies. In this case, you need to cause ts users to import @types/chartist by moving it to dependencies in the package.json.

This has a zero impact on bundle size.

Unable to launch the application: Unexpected token

Hi, I am using these versions:
react 16.4.1
react-chartist: 0.13.3
chartist: 0.11.3

When I launch my application, it fails with the following error:

`./node_modules/react-chartist/index.js
SyntaxError: .\node_modules\react-chartist\index.js: Unexpected token (6:13)

4 | class ChartistGraph extends Component {
5 |

6 | displayName: 'ChartistGraph'
| ^
7 |
8 | componentWillReceiveProps(newProps) {
9 | this.updateChart(newProps);
`

Do I need any other dependencies?
I think perhaps it fails because of types?

I cleared my node-modules, and json-package, and did a fresh npm install, I tried previous versions as well, but nothing seemed to help.

Any suggestions where else I should look at?

Please add graph output to your demo page

Hiya-
Someone submitted this to react.rocks. It looks good (the chartist examples are amazing!), but unfortunately the page at http://fraserxu.me/react-chartist/ has code without really much visual to screenshot. Will add a screenshot of the code for now, hopefully can get something more visual though. Sorry to complain - thanks for putting this out!

Can it support animation

First. I use this component very long. it's amazing.
However. I wish we use it can show a animation with chart.
So. How do i do it.
Thx .

String ref causing error in React 16.2

I hope that I'm just doing something wrong, but I'm getting a pesky ref-related error and I think it's due to the distributed output containing a string ref:

In react-chartist/dist/index.js:
screen shot 2018-04-05 at 4 28 34 pm

The error I'm seeing in the console:
screen shot 2018-04-05 at 4 40 36 pm

Upon digging in, it looks like maybe the build step wasn't run again after the bump to 13.0? To confirm, I ran your build script locally and sure enough the transpiled output is what I would have expected:
screen shot 2018-04-05 at 4 46 57 pm

I'm happy to help any way I can, but I hope it's as simple as publishing a new build.

Thanks for your work on this package - I've used it a lot and it's great!

Resizing charts, specifically pie charts

Thanks for the component! It's awesome.

Anyway, I know I'm missing something here, but I'm stumped. I haven't been using React very long. I'm using the pie charts, and for the life of me cannot resize them. They seem to be limited to a predefined size, because when the labeloffset is a big value the label gets cut off. Help will be very much appreciated.

Edit: Discovered the width and height for options slaps forehead, but still very tricky for me

NPM install error - unmet peer dependency

Hi there,

I'm having some issues installing this package (v0.10.1). Keep getting the unmet peer dependency issues complaining about React and React-dom, even though both packages are already installed (v15.5.4). Both my colleague and I have tried to install it, in our current project with React v15.5.4 already installed, and in a new empty project with nothing installed. We also cleared NPM cache, but still getting the same unmet peer dependency error each time. Will you be able to help, please? We'd love to add Chartist component to our application since it's such a nice solution, but at the moment we can't pass the installation stage. Thanks in advance!

Regards,

Convert to es5

I also love new cool es6 syntax. But in my apps i really need to exclude node_modules from babel parse. Parse node_modules decrease speed of build.
Can you convert this to es5? Or if I do, will you merge PR?

Problem after updating React to 0.13:

Hi,

just updated React to 0.13 and now, when React-Chartist runs the updateChart() function, specifically the

this.chartist = new Chartist[type](this.getDOMNode(), data, options, responsiveOptions);

React bombs with

Cannot read property 'firstChild' of undefined

Has anyone else tried updating?

Cheers,
mg

Window resize causes data display problem

Great work on the project!

I have noticed that the data on the chart gets stacked on top of each other when the browser windows changes size.
For example, the full width is this:
image

When I put in mobile mode, it becomes this:
image

Would there be any fix or guidelines on how to fix it?

Thank you!

horizontal bars doesn't start at 0

When I use bar chart on horizontal mode, the zero is not at the start.
(for example, the red bar is 30 but it only shows 25):
image001

It doesn't happen when horizontalBars=false.

this is my code:

<ChartistGraph
    data={{
        labels: ['Yes', 'No', 'Maybe'],
        series: [30, 10, 5]
    }}
    type="Bar"
    options={{
        istributeSeries: true,
        horizontalBars: true,
    }}
/>

Have any idea how to fix it?

Thanks

Release 0.5.1

Right now the 0.5.0 release is broken because of 828360d and 0.5.1 hasn't been pushed to npm.

Zoom Feature

HI,
I am unable to find zoom feature for my Pie chart.
Is there any solution in react-chartist with zoom feature.

Thanks,

Ashwini Kumar

updateChart logic appears to be not allowing re-render

I think I found a bug here in index.js

if (this.chartist) {
      this.chartist.update(data, options, responsiveOptions);
    } else {
      this.chartist = new Chartist[type](this.chart, data, options, responsiveOptions);
...

This bit of code doesn't allow the chart type to change if the prop actually changes. I spent awhile trying to figure out why if I change the type the chart doesn't re-render. I removed the this.chartist.update(data, options, responsiveOptions); part along with the logic and it fixed it.

chart does not update on class change

I'm rewriting a vanilla HTML/js project in react, in which charts are hidden at time of creation and shown using a toggle button which adds a class to the parent of a <ChartistGraph/> ocmponent.

I'm having trouble forcing an update on the chart component when the display value of the component container changes from none to block.

in vanilla JS, I could trigger this update as follows:

  // trigger chartist update event on tab -- mostly for first click, otherwise it doesn't
  // know its size (b/c display:none on start. )
  document.querySelector(`#${tabName} figure`).__chartist__.update()

in React, I don't seem to be able to accomplish this. If I create the chart hwen its parent is displayed, it renders properly:
image
I can then hide it:
image
But when I go to redisplay it, the chart has been resized to width=height=0:
image

So, I guess the chart size is updated when I hide the component, but not when I re-show it. The parent component (code here) is clearly re-rendering when the display class changes. But I do not seem to be able to trigger an appropriate update of the chart myself.

I've tried adding a ref to the <ChartistGraph /> component, and then triggering chartist''s own update() function in the render:

    if (this.chartRef.current) {
      console.log('CHARTREF2', this.chartRef.current.chart.__chartist__.update)
      this.chartRef.current.chart.__chartist__.update(series)
    }

But this does not fix the problem for me as it did in the vanilla JS version.

I see there are a couple of possibly-related issues, e.g. #73, but I think this one is distinct and I would love to get some guidance. Thanks!

Chart doesn't shpw up

For some reason, hopefully someone here can find out what my, chart is not showing up. I suspect it has something to do with TypeScript though I have not been able to prove it. Here is what I would post as a sandbox example (I don't seem to be able to save a sandbox)

import * as React from "react";
import ChartistGraph from "react-chartist";
import "./styles.css";

export default function App() {
  const chartLabels:string[] = [
    "Milwaukee", "Dane", "Brown", "Waukesha", "Racine", "Kenosha", "Rock", "Walworth", "Outagamie", "Winnebago", "Washington", "La Crosse", "Dodge", "Sheboygan", "Ozaukee", "Fond du Lac", "Marathon", "Jefferson", "Eau Claire", "St. Croix", "Waupaca", "Sauk", "Portage", "Marinette", "Grant", "Trempealeau", "Manitowoc", "Calumet", "Barron", "Wood", "Columbia", "Monroe", "Oconto", "Chippewa", "Pierce", "Shawano", "Clark", "Douglas", "Green", "Juneau", "Polk", "Kewaunee", "Lafayette", "Oneida", "Dunn", "Waushara", "Door", "Adams", "Iowa", "Marquette", "Iron", "Crawford", "Taylor", "Lincoln", "Langlade", "Vernon", "Sawyer", "Forest", "Jackson", "Green Lake", "Vilas", "Washburn", "Buffalo", "Pepin", "Richland", "Price", "Ashland", "Bayfield", "Menominee", "Burnett", "Rusk", "Florence" 
  ];
  const chartSeries:number[] = [
    20920,4492,4240,4188,3499,2659,1425,1314,1236,1164,1039,896,808,742,676,635,629,624,574,485,442,433,405,387,352,336,332,313,291,286,245,242,237,227,209,190,183,172,158,137,131,130,130,124,122,115,103,85,79,79,74,74,69,68,63,62,59,59,58,55,53,45,43,42,37,33,24,23,22,22,20,8
  ];
  const chartOptions =  
  {
    data: {
      labels: chartLabels,
      series: [chartSeries]
    },
    options: {
      axisX: {
        showGrid: false
      },
      low: Math.min(...chartSeries),
      high: Math.max(...chartSeries),
      chartPadding: {
        top: 0,
        right: 5,
        bottom: 0,
        left: 0
      }    
    }
  };
return (
    <ChartistGraph
    className="ct-chart"
    data={chartOptions.data}
    type="Bar"
    options={chartOptions.options}
  />

  );
}

As can be see the chart labels show up (all be it jumbled) but there are no bars?

Module not found

Hi,

I cannot use

Module not found: Can't resolve 'chartist ' 

Do you have any idea how I can fix it?

Cheers

adding a class to the component

Hi,

I have try to add a class because I want to change the color of the bar in the barchart.

<ChartistGraph className="ct-chart" data={lineChartData} options={lineChartOptions} type={'Bar'} />

the Chartist documentation told me to do this

var chart = new Chartist.Bar('.ct-chart', {
  labels: ['2005', '2006', '2007', '2008'],
  series: [[
    { meta: 'category1', value: 1000 },
    { meta: 'category2', value: 1500 },
    { meta: 'category3', value: 1900 },
    { meta: 'category4', value: 700 }
  ]]
});

any idea, how i can do this

var chart = new Chartist.Bar('.ct-chart', {

with your component? Thanks

Configure Chartist as a peer dependency to prevent duplicate imports

When using easing functions all available on the Chartist library itself, Chartist needs to be imported into whichever module the ChartistGraph component is being used, too.

Looking at the compiled js, this results in a duplicate Chartist import.

Moving Chartist from dependencies to peerDependencies should fix this.

As a workaround in the meantime, Chartist can be aliased in the Webpack config:

// webpack.config.js
...
  resolve: {
    alias: {
      ...
      chartist: path.resolve(__dirname, 'node_modules/chartist'),
      ...
    },
  },
...

@fraserxu would you like me to submit a quick PR for this?

Using plugins

Is it possible to use plugins with this? So far I have not been able to see how, although I am far from a Javascript expert. In particular, I would really like to be able to use the axis title plugin.

React 18 compatibility concern

Hi Chartist Team,

There is no new release in last 2 years.
Wondering if there will be any risks using react-chartist with latest version of React(18).
I am about to start new development for my project and thinking about using react-chartist for charts.

Thanks

Can't seem to find a way to use an auto scale axis

I looked at the API docs for regular version of chartist, and the method of selecting an autoscale x axis doesn't appear to be available in the wrapper. Inspecting the source code, it seems that a chart is created by default with a numeric y axis and a stepped x axis, with no means of overriding that.

Am I missing something here? If not, at there plans to expose the different axis types via the react wrapper?

updateChart does not properly handle responsiveOptions

This is really an issue with the chartist library itself.

In the updateChart method if an already existing instance of this.chartist is found, its update method is called, passing the responsiveOptions into the 3rd argument.

react-chartist_index_js_at_master_ _fraserxu_react-chartist

The problem is that the 3rd argument to the Chartist.Base class update method is

{Boolean} [override] If set to true, the passed options will be used to extend the options that have been configured already. Otherwise the chart default options will be used as the base

The result is that changing charts on the same page (as my app does) does not handle changes to the responsiveOptions.

I was able to work around this by creating a ChartistGraph child class and override the updateChart method using two alternatives..

//Method 1 - Undefined ChartistGraph's chartist reference immediately before calling its updateChart.

class ChartistGraphExt extends ChartistGraph {
    updateChart(config) {
        this.chartist = undefined;
        return super.updateChart(config);
    }
}

///Method 2 - set the this.chartist.responsiveOptions property directly

class ChartistGraphExt extends ChartistGraph {
    updateChart(config) {
        if (this.chartist) {
            this.chartist.responsiveOptions = config.responsiveOptions || [];
        }
        return super.updateChart(config);
    }

}

Both work, but each obviously has its drawbacks. I believe the proper fix for this would require changes to the chartist library and this wrapper. However if there's a consensus on the best way to fix in this library, I'd be happy to implement and put up a PR.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.