Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Dygraph in React with redux?

I've been having a lot of trouble implementing Dygraph in React (I'm using redux): http://dygraphs.com/. The Dygraph wrapper packages on NPM don't seem to work.

Also I can't simply use:

<div id="graph"></div>. 

I believe this is because in react your working in a state instead of an actual index.html file.

So the method I'm currently trying to use is to create the graph component:

import React, { Component } from 'react';
import Dygraph from 'dygraphs';
import myData from '../../mockdata/sample-data.json';
import 'dygraphs/dist/dygraph.min.css'
import './graphComponent.css';

class DyGraph extends Component {

    constructor(props) {
        super(props);
        // mock json data for graph
        const messages = myData;

        var data = "";
        messages.forEach((response) => {
            data += response[0] + ',' + response[1] + "\n";
        });

        new Dygraph('graphContainer', data, {
            title: 'Pressure Transient(s)',
            titleHeight: 32,
            ylabel: 'Pressure (meters)',
            xlabel: 'Time',
            gridLineWidth: '0.1',
            width: 700,
            height: 300,
            connectSeparatedPoints: true,
            axes: { "x": { "axisLabelFontSize": 9 }, "y": { "axisLabelFontSize": 9 } },
            labels: ['Date', 'Tampines Ave10 (Stn 40)'],

        });
    }

    render() {
        return <div></div>
    }
}
export default DyGraph;

and then import it into:

import React, { Component } from 'react';
import DyGraph from './components/graph/graphComponent';
import './App.css';
class DeviceDetails extends Component {

    render() {
        return (
                <div >
                    <DyGraph />
                </div> 
        ); 
    }
}
export default DeviceDetails;

And there is a Display state that if you click something it will go to:

import React, { PropTypes } from 'react'
import { connect } from 'react-redux'

import WarningView from '../warning/warningView'
import DirectoryView from '../directory/directoryView'
import DeviceDetailView from '../devicedetails/devicedetails'


export const Display = ({ currentPage }) => {

    switch(currentPage) {
        case 'WARNING_PAGE':
            return <WarningView/>;
        case 'DIRECTORY_PAGE':
            return <DirectoryView/>;
        case 'SENSOR_PAGE':
            return <DeviceDetailView/>;
        default:
            return <WarningView/>;
    }
};

Display.propTypes = {
    currentPage: PropTypes.string.isRequired,
};

export default connect(
    (state) => ({ currentPage: state.currentPage }),
    (dispatch) => ({ })
)(Display)

When I build and run this locally I get an error in the console (when I try to see the graph):

Uncaught (in promise) Error: Constructing dygraph with a non-existent div!
    at Dygraph.__init__ (dygraph.js:217)
    at new Dygraph (dygraph.js:162)
    at new DyGraph (graphComponent.js:19)
    at ReactCompositeComponent.js:295
    at measureLifeCyclePerf (ReactCompositeComponent.js:75)
    at ReactCompositeComponentWrapper._constructComponentWithoutOwner (ReactCompositeComponent.js:294)
    at ReactCompositeComponentWrapper._constructComponent (ReactCompositeComponent.js:280)
    at ReactCompositeComponentWrapper.mountComponent (ReactCompositeComponent.js:188)
    at Object.mountComponent (ReactReconciler.js:46)
    at ReactDOMComponent.mountChildren (ReactMultiChild.js:238)

If anyone can figure out what's going on or even give me a hint that'd be SOOO APPRECIATED. I specifically want to use dygraph instead of google charts or other react charts (which I've gotten working very easily), however, there is little information about dygraph implementation in React and I don't really understand why it won't work.

like image 363
foerever Avatar asked Dec 18 '22 06:12

foerever


1 Answers

The problem is that this line:

new Dygraph('graphContainer', data, { ... })

Tries to create a Dygraph in the element with ID graphContainer. But there is no element with that ID, hence the failure.

You need to wait until React creates a div in the DOM to create the dygraph. You'll want to instantiate the Dygraph in componentDidMount:

class Dygraph extends Component {
    render() {
        return <div ref="chart"></div>;
    }


    componentDidMount() {
        const messages = myData;

        var data = "";
        messages.forEach((response) => {
            data += response[0] + ',' + response[1] + "\n";
        });

        new Dygraph(this.refs.chart, data, {
            /* options */
        });
    }
}
like image 189
danvk Avatar answered Dec 24 '22 00:12

danvk