Jquery 浮点数和周数
我制作了一个页面来按周数绘制平均雪深。现在我们进入了 2010 年的第 1 周,我的图表拉伸了 x 轴(年周)上的间隔。 我想要的是 x 轴上的周数(51,52,1,2 等)和固定的间隔宽度。 有人知道该怎么做吗?
<script id="source" language="javascript" type="text/javascript">
$(function () {
var d1 = [
[201001,118],[200952,112],[200951,102],[200950,97],[200949,93],
[200948,41],[200947,10],[200947,0]];
var d2 = [
[201001,33],[200952,31],[200951,31],[200950,25],[200949,23],
[200948,12],[200947,0],[200947,0]];
$.plot($("#placeholder"),
[{data:d1,lines:{show: true},
points:{show: true},label:"Mountain"},
{data:d2,lines:{show: true},points:{show: true},label:"Valley"}],
{xaxis: {tickSize:1, tickDecimals:0 }}
);
});
</script>
I made a page to plot average snowdepth by week number. Now that we entered 2010, week 1 my graph stretches the interval on the x-axis (yearweek).
What I want is the weeknumber on the x-axis (51,52,1,2 etc), and a fixed intervalwidth.
Anyone know how to do that?
<script id="source" language="javascript" type="text/javascript">
$(function () {
var d1 = [
[201001,118],[200952,112],[200951,102],[200950,97],[200949,93],
[200948,41],[200947,10],[200947,0]];
var d2 = [
[201001,33],[200952,31],[200951,31],[200950,25],[200949,23],
[200948,12],[200947,0],[200947,0]];
$.plot($("#placeholder"),
[{data:d1,lines:{show: true},
points:{show: true},label:"Mountain"},
{data:d2,lines:{show: true},points:{show: true},label:"Valley"}],
{xaxis: {tickSize:1, tickDecimals:0 }}
);
});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以为 x 轴定义一组“刻度”,并在那里映射您的周数
V2 - 似乎可行,请参阅 流程图
看来“200947”在您的数据集中出现了两次。
you could define a set of 'ticks' for the x-axis, and map your week numbers there
V2 - seems to work, see the flot chart
It seems '200947' is in your data set twice.
尝试在tickformatter中编写一个函数,将直集转换为周数。
例如:
我没有测试过这段代码,可能存在类型转换的一些问题。它应该采用您的集合 (0, 1, 2, ...),并将其转换为新集合 (49, 50, 51, 52, 1, 2, ...),因此,当您绘制您的周,在您的数据中,将第 49 周称为 0,它将显示为一周。为了进一步提高灵活性,您可以将返回值替换为 return '"week" + ((val.toFixed(axis.tickDecimals) + 49) % 52);'这样您的刻度线就会显示“第 1 周”、“第 2 周”等。
有关详细信息,请参阅“自定义轴”下的 flot api。
try writing a function in tickformatter that will convert the straight set into week numbers.
for example:
I have not tested this code, there may be some problems with type conversion. It should take your set of (0, 1, 2, ...), and convert it to the new set (49, 50, 51, 52, 1, 2, ...), and so, when you plot your weeks, in your data, call week 49 0, and it will show up with a week. For further flexibility, you can replace the return value with return '"week" + ((val.toFixed(axis.tickDecimals) + 49) % 52);' so that your ticks display 'week 1', 'week 2', etc.
See the flot api under 'Customizing the axes' for more info.