Flot charts - changing the y axis on the fly

强颜欢笑 提交于 2019-12-23 17:30:29

问题


I am new to flot, but managed to set my time graph pretty quickly. Here is my time based plot:

    $.plot("#placeholder", [d],
    {
        xaxis: {
            mode: "time",
            minTickSize: [1, "month"],
            min: (new Date(2008, 05, 20)).getTime(),
            max: (new Date(2014, 05, 27)).getTime()
        }
    });

when the user clicks a button I want to change the plot to show only a part of [d] - for instance, if [d] contains data for 2013 and 2014, I want to show only 2013. They have such example in flot examples (http://www.flotcharts.org/flot/examples/axes-time/index.html). So, in my button click I pretty much did what the example showed, something like this:

           $("#myBtn").click(function () {
                $.plot("#placeholder", [d], {
                    xaxis: {
                        mode: "time",
                        min: (new Date(2013, 06, 30)).getTime(),
                        max: (new Date(2013, 12, 30)).getTime()
                    }
                });
            });

and it worked fine - except the Y axis ticks do not get regenerated according the the min and max values of 2013. This was ugly, so I looked up a solution and tried this code:

     $("#myBtn").click(function () {

        plot.getOptions().yaxes[0].min = minVal;
        plot.getOptions().yaxes[0].max = maxVal;
        plot.setupGrid(); //only necessary if your new data will change the axes or grid
        plot.draw();
    });

But when I run this, the y axis doesn't change.

Can anyone help me figure out what I am doing wrong?


回答1:


I discovered that I needed to set the tick size property for this to work:

    plot.getOptions().yaxes[0].min = v1;
    plot.getOptions().yaxes[0].max = v2;
    plot.getOptions().yaxes[0].tickSize = 5;
    plot.setupGrid(); //only necessary if your new data will change the axes or grid
    plot.draw();


来源:https://stackoverflow.com/questions/23906523/flot-charts-changing-the-y-axis-on-the-fly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!