Jquery 浮点数和周数

发布于 2024-08-16 19:52:45 字数 799 浏览 6 评论 0原文

我制作了一个页面来按周数绘制平均雪深。现在我们进入了 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?

example

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼泪都笑了 2024-08-23 19:52:45

您可以为 x 轴定义一组“刻度”,并在那里映射您的周数

{xaxis: 
    {
        ticks: [[201001,'1'],[200952,'52'],[200951,'51'],....]
        tickSize:1, 
        tickDecimals:0
    }
}

V2 - 似乎可行,请参阅 流程图

<div id="placeholder" style="width:600px;height:300px;"></div> 
<script id="source" language="javascript" type="text/javascript">
$(function () {

var d1 = [
[200952,112],[200951,102],[200950,97],[200949,93],[200948,41],[200947,10],[200946,0]];

var d2 = [
[200952,31],[200951,31],[200950,25],[200949,23],[200948,12],[200947,0],[200946,0]];

$.plot($("#placeholder"),
   [{data:d1,lines:{show: true},
     points:{show: true},label:"Mountain"},
    {data:d2,lines:{show: true},points:{show: true},label:"Valley"}],
    {xaxis: {
        ticks: [[200952,'52'],[200951,'51'],[200950,'50'],[200949,'49'],[200948,'48'],[200947,'47'],[200946,'46']],
        tickSize:1, tickDecimals:1 }}
);

});
</script>

看来“200947”在您的数据集中出现了两次。

you could define a set of 'ticks' for the x-axis, and map your week numbers there

{xaxis: 
    {
        ticks: [[201001,'1'],[200952,'52'],[200951,'51'],....]
        tickSize:1, 
        tickDecimals:0
    }
}

V2 - seems to work, see the flot chart

<div id="placeholder" style="width:600px;height:300px;"></div> 
<script id="source" language="javascript" type="text/javascript">
$(function () {

var d1 = [
[200952,112],[200951,102],[200950,97],[200949,93],[200948,41],[200947,10],[200946,0]];

var d2 = [
[200952,31],[200951,31],[200950,25],[200949,23],[200948,12],[200947,0],[200946,0]];

$.plot($("#placeholder"),
   [{data:d1,lines:{show: true},
     points:{show: true},label:"Mountain"},
    {data:d2,lines:{show: true},points:{show: true},label:"Valley"}],
    {xaxis: {
        ticks: [[200952,'52'],[200951,'51'],[200950,'50'],[200949,'49'],[200948,'48'],[200947,'47'],[200946,'46']],
        tickSize:1, tickDecimals:1 }}
);

});
</script>

It seems '200947' is in your data set twice.

失退 2024-08-23 19:52:45

尝试在tickformatter中编写一个函数,将直集转换为周数。

例如:

xaxis: {        
    tickFormatter: function suffixFormatter(val, axis) {                
        return (val.toFixed(axis.tickDecimals) + 49) % 52;
    }
}

我没有测试过这段代码,可能存在类型转换的一些问题。它应该采用您的集合 (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:

xaxis: {        
    tickFormatter: function suffixFormatter(val, axis) {                
        return (val.toFixed(axis.tickDecimals) + 49) % 52;
    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文