简体   繁体   中英

How to use vanilla javascript inside Reactjs component?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM