0

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

编辑: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"));
});

有没有办法重新调整这些条?

4

1 回答 1

1

我总是在最后一刻发布我的问题,并且总是在最后一刻找到解决方案。

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

基本上,图形库的浮点线在其日期时间计算中存在一个完整的错误。它们都遭受了与图表上时间轴相关的严重浮点舍入误差,这导致条形图在其预期绘图位置的左侧摆动。

下面是一个 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
        }
});
于 2014-11-20T15:34:41.707 回答