首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带条形图日期的Flotr2错误

带条形图日期的Flotr2错误
EN

Stack Overflow用户
提问于 2014-11-20 15:03:07
回答 1查看 306关注 0票数 0

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

编辑:Chrome用户:请注意,这个花招似乎不适合我使用铬,不知道为什么

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

我的问题是,我的酒吧与它们所代表的日期不一致。

代码语言:javascript
复制
$(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"));
});

有什么办法可以重新调整这些条子吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-20 15:34:41

我总是在最后时刻发表我的问题,在最后时刻我总是找到解决办法。

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

基本上,图形库的flot行在日期时间计算中有一个完整的错误。它们都在图上的时间轴上遇到严重的浮点四舍五入误差,这使得条形图在其预期的地块位置的左边抖动。

下面是一个getTimeScale函数的示例,该函数将日期作为从纪元时间(从2014年1/1/1开始的2200开始)的周数返回:

代码语言:javascript
复制
function getTimeScale(date){
    return (new Date(date).getTime() / 1000 / 60 / 60 / 24 / 7);
}

此函数应用于数据系列中的日期参数,返回一个不按数十万计的正规化数字:

代码语言:javascript
复制
$(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"));
});

为了再次将滴答显示为日期时间,您必须显式地指定滴答:

代码语言:javascript
复制
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
        }
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27042756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档