# scaling of negative values in graph

• 04-03-2012, 05:26 PM
noobplus
scaling of negative values in graph
hi,

my project submission date is tomorrow, so please reply as soon as possible (thanks for reading this line)

I've drawn the graph, and I've given a scroll-bar for x-axis values.
this is because, I know how many x-values are there, so I've set the graph resolution to the number of x-values.
now my y-value need to be scaled
my y-value maximum value is 0.05299999999917244 and minimum value is -2.147483647E9 (obtained using print statement inside my coding)
How do I scale them?
(without scaling, my graph doesn't show accurate all the time)
I tried this scaling:
Code:

```private double getMax()             {               double max = -Integer.MAX_VALUE;                               for(int i = 0; i < this.yArray.length; i++) {                     if(this.fftofyArray[i] > max)                         max = this.fftofyArray[i];                 }                 return (max);             }```
//returns the maximum of the y-values

Code:

```final int PAD=30; int h = getHeight(); double scale = (double)(h - 2*PAD)/getMax();```
//scales the positive values according to the height of the graph

Code:

```for(int i = 1; i < this.yArray.length-1; i++) { double x1 = PAD + i;             double y1 =  h - PAD - scale*((this.fftofyArray[i]));                         double x2 = PAD + (i+1);             double y2 =  h - PAD - scale*((this.fftofyArray[i+1]));                         g2.draw(new Line2D.Double(x1, y1, x2, y2));                     }```
//draw graph with lines y-values.

x-values are working fine, but y-values negative values are hidden in the graph

Could you suggest me a different scaling for showing negative values

Edited to add: fftofyArray[] consists of the y-values
regards
dhilip
• 04-03-2012, 06:06 PM
Norm
Re: scaling of negative values in graph
Quote:

How do I scale them?
Use a piece of paper and write down the range of the data values and the range of the graphing numbers.
Then use division and addition to map from one range into the other.
lowest in one to 0 in the other, max in one to max in the other.
• 04-03-2012, 06:12 PM
noobplus
Re: scaling of negative values in graph
Quote:

Originally Posted by Norm
Use a piece of paper and write down the range of the data values and the range of the graphing numbers.
Then use division and addition to map from one range into the other.
lowest in one to 0 in the other, max in one to max in the other.

thanks, and is it possible to add a tooltip to the graph, so it makes easy for me..
I'm using paintComponent() of JPanel to draw the graph. Could we add a tooltip to each point on the graph?
edited to add: I mean, I've 63,375 values, so difficult to plot on paper , that's what, thanks
regards
dhilip
• 04-03-2012, 06:21 PM
noobplus
Re: scaling of negative values in graph
hi,
but I know my maximum and minimum values . maximum value is 0.05299999999917244 and minimum value is -2.147483647E9. I got it by using getMax() and getMin() methods.

regards
dhilip
• 04-03-2012, 06:22 PM
Norm
Re: scaling of negative values in graph
What have you tried to do with the JToolTip class?
• 04-03-2012, 06:26 PM
noobplus
Re: scaling of negative values in graph
Quote:

Originally Posted by Norm
What have you tried to do with the JToolTip class?

the tooltip displays the y-value of the corresponding point, So I can try to scale them.
not sure,whether JToolTip can be used in JPanel

regards
dhilip
• 04-03-2012, 07:00 PM
Norm
Re: scaling of negative values in graph
Have you tried it?
• 04-03-2012, 07:09 PM
noobplus
Re: scaling of negative values in graph
Quote:

Originally Posted by Norm
Have you tried it?

yep, I tried..
Code:

```for(int i = 1; i < this.yArray.length-1; i++) {             double x1 = PAD + i/**xInc*/;             double y1 =  PAD + 500*((this.fftofyArray[i]));                         double x2 = PAD + (i+1)/**xInc*/;             double y2 =  PAD + 500*((this.fftofyArray[i+1]));                         g2.draw(new Line2D.Double(x1, 9*y1, x2, 9*y2));             setToolTipText(Double.toString(this.fftofyArray[i]));         }ToolTipManager.sharedInstance().registerComponent( this);         ToolTipManager.sharedInstance().setInitialDelay(0) ;```
this is the line-drawing loop of my paintComponent()
but the tooltip i got in output is only the final y-value as a tooltip everywhere

I need some way to know, which point on the graph has what value

regards
dhilip
• 04-03-2012, 07:14 PM
Norm
Re: scaling of negative values in graph
The call to a method in a loop will continually replace the last value with the current value. At the end of the loop only the last value will be there. The other, earlier values will all have been replaced.

Does the tooltip class have a way to get the mouse click location and set the text based on that?
• 04-03-2012, 07:18 PM
noobplus
Re: scaling of negative values in graph
Quote:

Originally Posted by Norm
The call to a method in a loop will continually replace the last value with the current value. At the end of the loop only the last value will be there. The other, earlier values will all have been replaced.

Does the tooltip class have a way to get the mouse click location and set the text based on that?

nop, all I have is that line-drawing loop, and don't know to get the mouse click location(the location should be on the line drawn which is done by my line-drawing-loop).

regards
dhilip
• 04-03-2012, 07:21 PM
Norm
Re: scaling of negative values in graph
Then you may have to use a mouse listener and draw your own label at the mouse click location.
• 04-03-2012, 07:24 PM
noobplus
Re: scaling of negative values in graph
Quote:

Originally Posted by Norm
Then you may have to use a mouse listener and draw your own label at the mouse click location.

but Is there a way to scale the graph using the maximum and minimum values of my y-axis-array?
I got the maximum and minimum values of my y-axis-array using "getMax()" and getMin() and they are maximum is 0.05299999999917244 and minimum is -2.147483647E9

regards
dhilip
• 04-03-2012, 07:28 PM
Norm
Re: scaling of negative values in graph
Quote:

a way to scale the graph
Use arithmetic operations like - / * and +
• 04-03-2012, 07:46 PM
noobplus
Re: scaling of negative values in graph
Quote:

Originally Posted by Norm
Use arithmetic operations like - / * and +

hi,
i've calculated the maximum value of positive values, and minimum of negative value, and added the modulus of two values, and got a new max-value..
Code:

```private double getMax() {               double max = -Integer.MAX_VALUE;                               for(int i = 0; i < this.yArray.length; i++) {                     if(this.fftofyArray[i] > max)                         max = this.fftofyArray[i];                 }                 double min=Integer.MAX_VALUE;                 for(int i = 0; i < this.yArray.length; i++) {                     if(this.fftofyArray[i] < min)                         min = this.fftofyArray[i];                                     }                                                 return (max-min);             }```
now i've divided the graph height by the answer of getMax()
Code:

`double scale = (double)(h - 2*PAD)/getMax();//int h = getHeight();//h is the height of the graph, and PAD is initialized to 20 for bordering`
now I've plotted y as
Code:

`double y1 =  h-PAD - scale*((this.fftofyArray[i]));`
Code:

`double y2 =  h-PAD - scale*((this.fftofyArray[i+1]));`
and then I drew the line,
I don't get negative values yet on the graph, Could you tell me what's wrong with my scaling

regards
dhilip
• 04-03-2012, 07:51 PM
Norm
Re: scaling of negative values in graph
An example:
data range: -1000 to 1000 (2000 items)
graph range: 0 to 99 (100 items)
Plot -1000 at 0 (-1000 - (-1000) = 0)
plot 1000 at 99
plot 0 at 50
• 04-07-2012, 06:37 AM
noobplus
Re: scaling of negative values in graph
Quote:

Originally Posted by Norm
An example:
data range: -1000 to 1000 (2000 items)
graph range: 0 to 99 (100 items)
Plot -1000 at 0 (-1000 - (-1000) = 0)
plot 1000 at 99
plot 0 at 50

Hi,
sorry there was power cut that day, there was no power for past four days.. thank you very much, the scaling worked

regards
dhilip