【问题标题】:Using Morris.js with reactjs将 Morris.js 与 reactjs 一起使用
【发布时间】:2017-08-06 09:24:43
【问题描述】:

如何使用 reactjs 创建 morris.js 图表? 我正在使用带有 react.js 库的 Asp.net MVC5 项目。这是我的第一个 react 项目,我想在单击某个按钮时更改 morris.js 图表。 我不想在 react js 脚本中使用其他 react 库,只是 morris js librayr

【问题讨论】:

    标签: asp.net-mvc reactjs morris.js


    【解决方案1】:

    简单的解决方案

    componentDidMount() 中绘制图表,在我的示例中是一个甜甜圈:

    yourChart = Morris.Donut({ element: 'elementId', data: data, // ...other options });

    yourChart 在类之外声明,或者您可以在constructor() 中声明this.yourChart

    如果你想更新/重绘你的图表,你可以在点击按钮时调用yourChart.setData(newData)

    【讨论】:

    • 感谢您的回答我在互联网上找到了这个框架:recharts.org,它对我很有效
    • 现在我收到了TypeError: morris_js_morris_js__WEBPACK_IMPORTED_MODULE_3___default.a.Area is not a function
    【解决方案2】:

    这是让 Morris.js 与 React 一起工作的方法。

    1. 安装 Morris.js 及其依赖项 - Raphael.js:
    npm install --save-dev morris.js
    npm install --save-dev raphael
    1. 在您的组件中:
    import Raphael from 'raphael';
    import 'morris.js/morris.css';
    import 'morris.js/morris.js';
    1. 莫里斯在全球范围内寻找拉斐尔
    constructor() {
        super();
        window.Raphael = Raphael;
    }

    重要提示

    1. 如果您收到morris.css 的编译错误,请阅读this

    2. 如果你写import Morris from 'morris.js/morris.js',那就不行了。

    3. 还有另一种方法可以让 Raphael 在全球范围内。

    const webpack = require('webpack');
    module.exports = {
        plugins: [
            new webpack.ProvidePlugin({
                Raphael: 'raphael'
            })
        ],
        ...
    }

    如果您更喜欢此变体,则不需要:

    import Raphael from 'raphael';
    window.Raphael = Raphael;

    【讨论】:

    • 试过这个解决方案,但我得到一个“ReferenceError: jQuery is not defined”,我不知道为什么。我已经安装了 jQuery,因为我也有 boostrap。
    • 我会确保在 Raphael 和 Morris 脚本之前调用 jQuery。
    • 完成。但是在尝试像@novasaint 提供的答案那样使用它之后,我得到了一个错误
    猜你喜欢
    • 2016-12-03
    • 2015-03-13
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2015-07-24
    • 2018-06-03
    • 2022-07-23
    • 2018-04-08
    相关资源
    最近更新 更多