Question

I use jqPlot to plot a line chart out of a .csv-file. I need to get the xmax and ymax values of the plot and use them for further processings.

How do I get this or any other values and write them inside my own variables?

EDIT

Let's say this is my plot: enter image description here

What I need is not the maximum x-value from the array (here 1380). I need to get the maximum value from the plot (here 2000). For further processing I would like to add rectangles inside the plot, see second picture: and calculate their height as a x-value and not as their pixel-value.

enter image description here

Therefore I need to access the jqplot variables, not the array variables I give over to jqplot.

Was it helpful?

Solution

So, at some point you have an array of values that you passed to jqPlot to draw the graph, for example:

var myData = [[1, 2],[3,5.12],[5,13.1],[7,33.6],[9,85.9],[11,219.9]];

If you want to find the maximum x and y values, you just need to loop through the array keeping track of the largest value you've found so far.

var maxX, maxY;
for (var i=0; i < myData.length; i++) {
    if (myData[i][0] > maxX || !maxX) {
        maxX = myData[i][0];
    }
    if (myData[i][1] > maxY || !maxY) {
        maxY = myData[i][1];
    }
}

Here's a simple demo: http://jsfiddle.net/LAbvj/

EDIT: Ok, so I think what you are now asking for is the maximum for each axis. In that case, this is simple:

var plot1 = $.jqplot('chart1', [
    [3, 7, 19, 1, 4, 6, 8, 2, 5]
]);
console.log(plot1.axes.xaxis.max);
console.log(plot1.axes.yaxis.max);

See demo: http://jsfiddle.net/KJTRF/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top