【问题标题】:Using different symbols for plotting data not working使用不同的符号绘制数据不起作用
【发布时间】:2013-03-27 15:25:37
【问题描述】:

我试图在我的浮动图上为我的一个数据系列使用不同的符号,因为它的规模比其他数据大得多。我使用这个例子作为参考:http://www.flotcharts.org/flot/examples/symbols/index.html

这是我目前所拥有的:

我的包括:

<script  type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.flot.js"></script>
<script type="text/javascript" src="jquery.flot.symbol.js"></script>

然后我像这样配置我的浮动选项:

var options = {
        grid: {hoverable : true},
        xaxis: { mode: "time", timeformat: "%H:%M", tickLength: 1},
        series: { points : { show: true } }
        };

然后我实际绘制数据:

 $.plot('#placeholder', [{
               data: my_data,
               lines: { show : true},
               points: { symbol: "square"},
               color: '#CB4B4B',
               label: 'My Data'
               }], options);
        }

但是,flot 仍将点绘制为默认圆。我在 firebug 中没有收到任何错误消息,我什至尝试在 jquery.flot.symbol.js 库本身中添加一条日志消息,以查看“square”处理程序是否被这样调用:

var handlers = {
            square: function (ctx, x, y, radius, shadow) {
                console.log("Square handler was called");
                // pi * r^2 = (2s)^2  =>  s = r * sqrt(pi)/2
                var size = radius * Math.sqrt(Math.PI) / 2;
                ctx.rect(x - size, y - size, size + size, size + size);
            },

我没有收到控制台消息,所以我假设处理程序没有被正确调用。我在这里遗漏了什么吗?

编辑:

我要绘制的数据示例:

var d1 = [
[1364342400000, 208],
[1364346000000, 107],
[1364353200000, 42],
[1364371200000, 1680],
[1364360400000, 52],
[1364349600000, 67],
[1364385600000, 1118],
[1364367600000, 163],
[1364382000000, 1359],
[1364378400000, 1468],
[1364389200000, 1023],
[1364356800000, 63],
[1364374800000, 1601],
[1364392800000, 556],
[1364364000000, 84],
],
d2 = [
[1364342400000, 86],
[1364346000000, 42],
[1364353200000, 13],
[1364371200000, 458],
[1364360400000, 10],
[1364349600000, 22],
[1364385600000, 453],
[1364367600000, 45],
[1364382000000, 369],
[1364378400000, 379],
[1364389200000, 358],
[1364356800000, 17],
[1364374800000, 471],
[1364392800000, 147],
[1364364000000, 16],
],
d3 = [
[1364342400000, 7],
[1364346000000, 5],
[1364382000000, 11709],
[1364371200000, 58336],
[1364360400000, 1],
[1364349600000, 1],
[1364367600000, 2],
[1364389200000, 1191],
[1364378400000, 9085],
[1364385600000, 4688],
[1364374800000, 9386],
[1364392800000, 1140],
[1364364000000, 1],
];

我还编辑了我的options 参数以及绘图函数的调用方式:

var options = {
grid: {hoverable : true},
xaxis: { mode: "time", timeformat: "%H:%M", tickLength: 1}
}; 

 $.plot("#placeholder", [{
data: d1,
lines: { show : true },
points: { show: true},
color: '#EDC240',
label: "d1"
}, {
data: d2,
lines: { show : true },
points: { show : true},
color: '#AFD8F8',
label: "d2"
}, {
data: d3,
yaxis: 2,
lines: { show : true},
points: { show: true, symbol: "square"},
color: '#CB4B4B',
label: "d3"
}], options);

我还确定 symbol 库已包含在内,因为我已将一些日志记录本身添加到库中,并且显示良好。

【问题讨论】:

    标签: javascript jquery flot


    【解决方案1】:

    我认为你所做的没有问题。我在这里做了一个快速的工作示例:http://jsfiddle.net/ryleyb/shLtU/1/

    我猜你没有包含符号库(即使你说你有)。

    或者显示您的数据,可能存在某种问题(值得怀疑?)。

    这是我运行的完整的flot 代码以创建一个工作示例(加上包括 flot 和符号库):

    $.plot('#placeholder', [{
        data: [
            [1, 1],
            [2, 3],
            [4, 4],
            [5, 9]
        ],
        lines: {
            show: true
        },
        points: {
            show: true,
            symbol: "square"
        },
        color: '#CB4B4B',
        label: 'My Data'
    }]);
    

    【讨论】:

    • 感谢您迄今为止的帮助。我已经编辑了我的问题以包含更多信息,包括我试图绘制的数据。我很困惑为什么它不起作用,因为我的代码与您上面的代码非常相似。
    • 确实......即使添加了这些细节,仍然不清楚问题是什么 - jsfiddle.net/ryleyb/shLtU/2 显示了您的所有更改,而且看起来仍然很好......
    • 我应该检查一下我运行的是什么版本的flot。我使用的版本是0.6,一旦我升级到0.7,方形符号就可以完美运行。您的回答使我走上了正确的道路。感谢您的帮助!
    猜你喜欢
    • 2018-03-13
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-20
    相关资源
    最近更新 更多