简体   繁体   English

Morris.js - 有没有Setter方法?

[英]Morris.js - Is There any Setter Method?

I there any setter function for morris.js ? 我有morris.js的任何setter函数吗? I've been through their documentation and it seems there is only one example of changing values of the data points using setData() . 我一直在阅读他们的文档,似乎只有一个使用setData()更改数据点值的示例。

But when you change data you might need to change labels on the x/y axis or change other options too. 但是当您更改数据时,您可能需要更改x / y轴上的标签或更改其他选项。 So, is there any general setter method on morris.js? 那么, morris.js上有没有通用的setter方法?

After inspecting the script and setData() function I've found there isn't any setter function. 检查脚本setData()函数后,我发现没有任何setter函数。 But options can be reset and redraw like below. 但是可以重置选项并重绘如下所示。

Initiate the Chart on js file (Hard Coded) 在js文件上启动图表(硬编码)

var options = {
            element : 'lead-summery',
            data : xChart.leads,
            xkey: 'date',
            ykeys: ['total', 'bananas', 'apples', 'oranges'],
            labels:['Total', 'Bananas', 'Apples', 'Oranges'],
            lineColors:['#E67A77', '#D9DD81', '#79D1CF'],
            fillOpacity: 0.2,
            behaveLikeLine: true,
            lineWidth: 1
        };

        // you must save the cart in a global variable
        window.myAreaChart = Morris.Area(options); 

Dynamically Update The Chart Options 动态更新图表选项

Now say we want to Add some new data via AJAX and want to change xLabelFormat function and xLabelAngle for labels. 现在说我们想通过AJAX添加一些新数据,并希望更改标签的xLabelFormat函数和xLabelAngle

var data = {}; // we get this from ajax call or whatever

// now we update the options

// xLabelFormat callback function
window.myAreaChart.options.xLabelFormat = function(date){
    return formatDate(date);
}

// xLabelAngle
window.myAreaChart.xLabelAngle = 60;

// add data and update the chart
window.myAreaChart.setData(data);

It is very important to understand that in order to update the chart we need to call redraw() method for example window.myAreaChart.redraw() . 了解为了更新图表,我们需要调用redraw()方法,例如window.myAreaChart.redraw() 这一点非常重要 Otherwise the chart won't update. 否则图表将不会更新。 We did not call redraw() on the example above because the setData() method calls it so no need for call it twice. 我们没有在上面的例子中调用redraw() ,因为setData()方法调用它所以不需要调用它两次。 But if you do not use setData() you will need to call redraw() in order to update the chart visually. 但是如果你不使用setData()你需要调用redraw()才能直观地更新图表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM