5

I am using chartist.js and I am using the chartist within reactjs component. I am referring this http://gionkunz.github.io/chartist-js/examples.html#simple-pie-chart

chartist.js:

var Chartist = {
    version:'0.9.5' 
}

(function (window, document, Chartist) {

var options = {
  labelInterpolationFnc: function(value) {
    return value[0]
  }
};

var responsiveOptions = [
  ['screen and (min-width: 640px)', {
    chartPadding: 30,
    labelOffset: 100,
    labelDirection: 'explode',
    labelInterpolationFnc: function(value) {
      return value;
    }
  }],
  ['screen and (min-width: 1024px)', {
    labelOffset: 80,
    chartPadding: 20
  }]
];

})();

Reactjs component:

import React, { Component } from 'react';

var data = {
  labels: ['Bananas', 'Apples', 'Grapes'],
  series: [20, 15, 40]
};

showPieChart(data){
     new Chartist.Pie('.ct-chart', data, options, responsiveOptions);
}

class Chart extends Component {

  render(){
    return(
      <div>
          <div className="center">
              {showPieChart}
          </div>
      </div>

    )}

}

export default Chart;

Nothing is displayed on web page. How can I access vanilla javascript inside react component.

4
  • 1
    Consider calling the function with data instead of passing a reference to the function Commented Jun 12, 2018 at 7:50
  • @baao How can I access functions/methods from .js file into my react component. Commented Jun 12, 2018 at 7:59
  • good explanations here -> reactjs.org/docs/integrating-with-other-libraries.html with basic examples. Commented Jun 12, 2018 at 9:26
  • @stonerock I have the same issue you had, the wrapper missing features and decided to go with vanilla integration inside ReactJs. Can you help with stackoverflow.com/questions/64160680/… Commented Oct 2, 2020 at 8:22

1 Answer 1

3

Your question is a little bit misleading, and can be interpreted in two ways.

#1. If you're asking how to integrate Chartist library with React, here's how you can do it:

There's a wrapper library, that already did it for us: https://www.npmjs.com/package/react-chartist

You can use it as follow (example taken from their repo):

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

class Pie 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(<Pie />, document.body)

#2. If you generally asking how to integrate other libraries into React, then I recommend you to check the official React docs, because there's a really good tutorial about the topic - Integrating with Other Libraries

So, if you don't want to use the wrapper library (react-chartist), then you can check its main component too. It's a great starting point (that follows React recommendations) to understand how to create your own wrapper: https://github.com/fraserxu/react-chartist/blob/master/index.js

Sign up to request clarification or add additional context in comments.

3 Comments

Yes I know we can make use of react-chartist but it is just wrapper around chartist.js also react-chartist have many features missing.
I want to make use of chartist.js which is vanilla javascript inside my react component. Can I make .js file and how can I access those functions inside react component i.e .jsx file.
So, if you don't want to use the library (the wrapper library), you can check its main component. It's a great starting point (that follows React recommendations) to understand how to create your own wrapper: github.com/fraserxu/react-chartist/blob/master/index.js

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.