Morris图表无法处理动态数据,即使在以Json格式发送数据之后也是如此

Morris charts are not working with dynamic data, even after sending data in Json format

本文关键字:数据 格式 Json 之后 处理 动态 Morris      更新时间:2023-09-26

我在我的网站上使用Morris图,但当我以JSON格式动态发送数据时,它就不起作用了。但同时,如果我手动推送数据,它就会开始工作。请提出一些建议。

这是代码

function generateUserGraph() {  
            // some logic and below object is return  
            var toreturn = "{ Y: '10000001', A: '-1' }, { Y: '10000045', A: '1' }";
            return toreturn;
        }
        Morris.Bar({
            element: 'NLcurrentProgressBar',
            resize: true,
            data: [generateUserGraph()],
            barColors: function (row, series, type) {
                if (row.y == 0) {
                    return ['#6D5858'];
                }
                else if (row.y < 0) {
                    return ['#E71717'];
                }
                else {
                    return ['#00a65a'];
                }
            },
            xkey: ['Y'],
            ykeys: ['A'],
            labels: ['<a href="www.google.com">Question</a>'],
            hideHover: 'auto'
        });
<div id = "NLcurrentProgressBar"></div>

请对以上代码提出建议。如果我直接传递上面的字符串,那么它可以工作,但当我使用函数传递它时,没有结果呈现

like : below code is working :
data: [{ Y: '10000001', A: '-1' }, { Y: '10000045', A: '1' }],

but this is not working :
data: [generateUserGraph()]

必须提供对象数组,但函数返回字符串!

    function generateUserGraph() {  
        // some logic and below object is return  
        var toreturn = [{ Y: '10000001', A: '-1' }, { Y: '10000045', A: '1' }];
        return toreturn;
    }
    Morris.Bar({
        element: 'NLcurrentProgressBar',
        resize: true,
        data: generateUserGraph(),
        barColors: function (row, series, type) {
            if (row.y == 0) {
                return ['#6D5858'];
            }
            else if (row.y < 0) {
                return ['#E71717'];
            }
            else {
                return ['#00a65a'];
            }
        },
        xkey: ['Y'],
        ykeys: ['A'],
        labels: ['<a href="www.google.com">Question</a>'],
        hideHover: 'auto'
    });

我发送的字符串不是对象,而是为了解决这个问题。我们需要以对象格式推送数据。

工作链接