带有条形图日期的 Flotr2 错误

Flotr2 bug with bar graph dates

本文关键字:Flotr2 错误 日期 条形图      更新时间:2023-09-26

我有一个flotr2图,应该在其中显示一些数据:

编辑:Chrome用户:请注意,这个jsfiddle似乎在chrome中不起作用,不知道为什么

http://jsfiddle.net/1jgb2k6r/7/

我的

问题是我的条形图在它们所代表的日期上没有居中对齐。

$(function () {
    (function color_gradients(container) {
        var myData = [];
        myData.push([new Date('1/1/2014').getTime(), Math.ceil(Math.random() * 10)]);
        myData.push([new Date('1/8/2014').getTime(), Math.ceil(Math.random() * 10)]);
        myData.push([new Date('1/15/2014').getTime(), Math.ceil(Math.random() * 10)]);
        myData.push([new Date('1/22/2014').getTime(), Math.ceil(Math.random() * 10)]);
        var
        bars = {
            data: myData,
            bars: {
                show: true,
                barWidth: 0.8,
                lineWidth: 20,
            }
        };
        graph = Flotr.draw(
        container, [bars], {
            yaxis: {
                min: 0,
                max: 11
            },
            xaxis: {
                mode: 'time',
                timeMode: 'local',
                min: (new Date('1/1/2014').getTime() - (7 * 24 * 60 * 60 * 1000)),
                max: (new Date('1/22/2014').getTime() + (7 * 24 * 60 * 60 * 1000))
            }
        });
    })(document.getElementById("editor-render-0"));
});

有没有办法重新对齐这些条形?

总是在第十一个小时内发布我的问题,并且总是在第十一个小时内找到解决方案。

http://jsfiddle.net/1jgb2k6r/10/

基本上,图形库的 flot 行在其日期时间计算中有一个不可或缺的错误。它们都遭受与图形上的时间轴相关的严重浮点舍入错误,这会导致条形在其预期绘图位置的左侧摆动。

下面是一个 getTimeScale 函数的示例,该函数从纪元时间(从 2014 年 1 月 1 日开始从 ~2200 开始)以周数的形式返回日期:

function getTimeScale(date){
    return (new Date(date).getTime() / 1000 / 60 / 60 / 24 / 7);
}

此函数应用于数据系列中的日期参数,返回一个非数十万量级的规范化数字:

$(function () {
    (function color_gradients(container) {
        var myData = [];

        myData.push([getTimeScale('1/1/2014'),Math.ceil(Math.random() * 10)]);
        myData.push([getTimeScale('1/8/2014'),Math.ceil(Math.random() * 10)]);
        myData.push([getTimeScale('1/15/2014'), Math.ceil(Math.random() * 10)]);
        myData.push([getTimeScale('1/22/2014'), Math.ceil(Math.random() * 10)]);
        var bars = {
            data: myData,
            bars: {
                show: true,
                barWidth: 0.8,
                lineWidth: 1,
            }
        };
        graph = Flotr.draw(
        container, [bars], {
            yaxis: {
                min: 0,
                max: 11
            },
            xaxis: {
                ticks: ticks,
                min: (getTimeScale('1/1/2014') - 1),
                max: (getTimeScale('1/22/2014') + 1)
            }
        });
    })(document.getElementById("editor-render-0"));
});

为了再次将刻度显示为日期时间,您必须明确指定刻度:

var ticks = [];
ticks.push([getTimeScale('1/1/2014'), '1/1/2014']);
ticks.push([getTimeScale('1/8/2014'),  '1/8/2014']);
ticks.push([getTimeScale('1/15/2014'), '1/15/2014']);
ticks.push([getTimeScale('1/22/2014'), '1/22/2014']);
graph = Flotr.draw(
    container, [bars], {
        yaxis: {
            min: 0,
            max: 11
        },
        xaxis: {
            ticks: ticks,
            min: (getTimeScale('1/1/2014') - 1), //gives a little padding room on the graph
            max: (getTimeScale('1/22/2014') + 1) //gives a little padding room on the graph
        }
});