在React中使用Dygraph和redux?

foe*_*ver 4 javascript dygraphs reactjs react-router react-redux

我在React中实现Dygraph遇到了很多麻烦(我使用的是redux):http://dygraphs.com/ .NPM上的Dygraph包装程序包似乎不起作用.

我也不能简单地使用:

<div id="graph"></div>. 
Run Code Online (Sandbox Code Playgroud)

我相信这是因为在你的状态下工作而不是实际的index.html文件.

所以我目前正在尝试使用的方法是创建图形组件:

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;
Run Code Online (Sandbox Code Playgroud)

然后将其导入:

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;
Run Code Online (Sandbox Code Playgroud)

并且有一个显示状态,如果你点击它会转到:

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)
Run Code Online (Sandbox Code Playgroud)

当我在本地构建并运行时,我在控制台中出现错误(当我尝试查看图形时):

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)
Run Code Online (Sandbox Code Playgroud)

如果有人能弄明白正在发生什么,甚至给我一个暗示,那就是赞美.我特别想使用dygraph而不是谷歌图表或其他反应图表(我已经非常容易地工作),但是,关于React中的dygraph实现的信息很少,我真的不明白为什么它不起作用.

dan*_*nvk 10

问题是这一行:

new Dygraph('graphContainer', data, { ... })
Run Code Online (Sandbox Code Playgroud)

尝试在具有ID的元素中创建Dygraph graphContainer.但是没有带有该ID的元素,因此失败了.

你需要等到React在DOM中创建一个div来创建dygraph.您将要在以下位置实例化Dygraph 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 */
        });
    }
}
Run Code Online (Sandbox Code Playgroud)