flot标记功能,未定义轴

flot marking function, axes is not defined?

本文关键字:未定义 功能 flot      更新时间:2023-09-26

当我用以下选项创建一个图形时,flot会正确绘制图形:

var options = {
            colors: trendcolors,
            series: {
                points: {
                    show: true
                },
                lines: {
                    show: true
                }
            },
            xaxis: {
                mode: "time",
                axisLabel: "Date/Time",
                tickLength: 0
            },
            yaxis: {
                axisLabel: "Duration (Sec)"
            },
            selection: {
                mode: "x"
            },
            grid: {
                hoverable: true,
                clickable: true,
                markings: function (axes) {
                    var markings = [];
                    var date = new Date(axes.xaxis.min);
                    date.setUTCSeconds(0);
                    date.setUTCMinutes(0);
                    date.setUTCHours(0);
                    var i = date.getTime();
                    do {
                        markings.push({xaxis:{from: i, to: i + (24 * 60 * 60 * 1000) }, color: colormarking } );
                        i += ((24 * 60 * 60 * 1000) * 2);
                    } while (i < axes.xaxis.max);
                    return markings;
                }
            },
            legend: {
                labelFormatter: function(label, series) { 
                            return label + " (" + series.data.length + ")";
                        }
            }
        };

但是,当我将标记匿名函数更改为标准函数时,会发生错误,flot无法绘制图形,因为fMarkings线上没有定义"轴"。为什么会这样?有什么不同?

    var options = {
        colors: trendcolors,
        series: {
            points: {
                show: true
            },
            lines: {
                show: true
            }
        },
        xaxis: {
            mode: "time",
            axisLabel: "Date/Time",
            tickLength: 0
        },
        yaxis: {
            axisLabel: "Duration (Sec)"
        },
        selection: {
            mode: "x"
        },
        grid: {
            hoverable: true,
            clickable: true,
            markings: fMarkings(axes)
        },
        legend: {
            labelFormatter: function(label, series) { 
                        return label + " (" + series.data.length + ")";
                    }
        }
    };

顺便说一下,fMarkings是在另一个js块中全局定义的。

markings参数需要一个函数或数组。不过,您所做的是在定义选项对象时调用函数。当你调用它时,axes变量并不存在。你需要的只是:

    grid: {
        hoverable: true,
        clickable: true,
        markings: fMarkings
    },

Where’s fMarkings是一个类似于的函数

fMarkings = function(axes){
   return arrayOfMarkings
}