Skip to content

Instantly share code, notes, and snippets.

@mrtimp
Created June 5, 2013 07:20
Show Gist options
  • Save mrtimp/5712156 to your computer and use it in GitHub Desktop.
Save mrtimp/5712156 to your computer and use it in GitHub Desktop.
Auto scale the yAxis with Flot and the selectable plugin
$("#placeholder").bind("plotselected", function (event, ranges) {
var ymin, ymax;
var plotdata = plot.getData();
$.each(plotdata, function (e, val) {
$.each(val.data, function (e1, val1) {
if ((val1[0] >= ranges.xaxis.from) && (val1[0] <= ranges.xaxis.to)) {
if (ymax == null || val1[1] > ymax) ymax = val1[1];
if (ymin == null || val1[1] < ymin) ymin = val1[1];
}
})
});
$.plot("#placeholder", plotdata, $.extend(true, {}, options, {
xaxis: {
min: ranges.xaxis.from,
max: ranges.xaxis.to
},
yaxis: {
min: ymin - 10,
max: ymax + 10
}
}));
overview.setSelection(ranges, true);
});
@tpimentelms
Copy link

A few minor sugestion:
I would sugest rescaling the y axis as a percentage, instead of a constant. And setting the ymin to 0 if it's positive.

            ymin = ( ymin > 0 ) ? 0 : ymin;
            extraY = ( ymax - ymin ) * 0.07;
            ymin = ( ymin == 0 ) ? 0 : ymin - extraY;
            ymax = ymax + extraY;

            $.plot(graph_holder, plotdata, $.extend(true, {}, options, {
                    xaxis: {
                            min: ranges.xaxis.from,
                            max: ranges.xaxis.to
                    },
                    yaxis: {
                            min: ymin,
                            max: ymax
                    }
            }));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment