# Thread: scaling of negative values in graph

1. ## 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:
Java 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

Java 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

Java 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
Last edited by noobplus; 04-03-2012 at 05:30 PM.  Reply With Quote

2. ## Re: scaling of negative values in graph

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.  Reply With Quote

3. ## Re: scaling of negative values in graph 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
Last edited by noobplus; 04-03-2012 at 06:16 PM.  Reply With Quote

4. ## 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  Reply With Quote

5. ## Re: scaling of negative values in graph

What have you tried to do with the JToolTip class?  Reply With Quote

6. ## Re: scaling of negative values in graph 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  Reply With Quote

7. ## Re: scaling of negative values in graph

Have you tried it?  Reply With Quote

8. ## Re: scaling of negative values in graph Originally Posted by Norm Have you tried it?
yep, I tried..
Java 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  Reply With Quote

9. ## 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?  Reply With Quote

10. ## Re: scaling of negative values in graph 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  Reply With Quote

11. ## 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.  Reply With Quote

12. ## Re: scaling of negative values in graph 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  Reply With Quote

13. ## Re: scaling of negative values in graph

a way to scale the graph
Use arithmetic operations like - / * and +  Reply With Quote

14. ## Re: scaling of negative values in graph 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..
Java 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()
Java 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
Java Code:
`double y1 =  h-PAD - scale*((this.fftofyArray[i]));`
Java 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  Reply With Quote

15. ## 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  Reply With Quote

16. ## Re: scaling of negative values in graph 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  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•