【问题标题】:Morris.Donut graph disappears after calling setData()Morris.Donut 图在调用 setData() 后消失
【发布时间】:2020-05-29 10:24:40
【问题描述】:

我尝试通过调用 setData() 来更新 Morris.Donut 图。但是在 setData() 方法被执行之后,图表就消失了。我在网上搜索过,但没有找到解决我的问题的方法。

我在这里所做的是:

    1) Initialize a Morris.Donut object
    2) Make a button. When I click on the button, I will update the graph with new data.

当我运行我的代码时,图表会显示为here。但是在点击按钮更新图表后,图表消失了。

我的代码

<div id="donut-chart" style="height: 200px;" data-width="100"></div>
<button type='button' class='update-chart'>Update chart</button>

<script type="text/javascript">

    $(document).ready(function () {

        // Initialize a Morris.Donut object
        donut_chart = Morris.Donut({
            element: 'donut-chart',
            data: [
                {'label': 'Yes', 'value': 1},
                {'label': 'No', 'value': 0},
            ]
        });

        // Update the Morris.Donut object
        $('.update-chart').on('click', function(){
            line = {
                element: 'donut-chart',
                data: [
                    {'label': 'Yes', 'value': 2},
                    {'label': 'No', 'value': 0},
                ],
            };

            donut_chart.setData(line);  // This code makes the graph disapears
        });
    });

</script>

【问题讨论】:

    标签: morris.js


    【解决方案1】:

    你应该写donut_chart.setData(line.data);

    您不需要使用 setData 传递元素。

    【讨论】:

    • 嗨皮埃尔。感谢您的时间。您的解决方案非常完美。它解决了我的问题。
    猜你喜欢
    • 1970-01-01
    • 2011-02-11
    • 2014-09-29
    • 1970-01-01
    • 2011-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多