Results 1 to 12 of 12
- 05-06-2011, 10:26 AM #1
Member
- Join Date
- May 2011
- Posts
- 6
- Rep Power
- 0
How to plot graph in java with multiple lines for given samples (stored in arrays)?
I need to plot multiple lines on one graph axis in java, the data for each line is stored in an Integer Array. I found a useful example in a thread from a couple of years ago, code posted by 'hardwire'. This was the code-
Code:
This plots one line on the graph from the array 'data'. I have been trying to modify it so that you can add more lines from more arrays. I am new to java 2D and have been have some problems and was not sure how to do this. Could anyone help? Or give some hints/tips as to how it can be done?Java Code:import java.awt.*; import java.awt.font.*; import java.awt.geom.*; import javax.swing.*; public class GraphingData extends JPanel { int[] data = { 21, 14, 18, 03, 86, 88, 74, 87, 54, 77, 61, 55, 48, 60, 49, 36, 38, 27, 20, 18 }; final int PAD = 20; protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); int w = getWidth(); int h = getHeight(); // Draw ordinate. g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD)); // Draw abcissa. g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD)); // Draw labels. Font font = g2.getFont(); FontRenderContext frc = g2.getFontRenderContext(); LineMetrics lm = font.getLineMetrics("0", frc); float sh = lm.getAscent() + lm.getDescent(); // Ordinate label. String s = "data"; float sy = PAD + ((h - 2*PAD) - s.length()*sh)/2 + lm.getAscent(); for(int i = 0; i < s.length(); i++) { String letter = String.valueOf(s.charAt(i)); float sw = (float)font.getStringBounds(letter, frc).getWidth(); float sx = (PAD - sw)/2; g2.drawString(letter, sx, sy); sy += sh; } // Abcissa label. s = "x axis"; sy = h - PAD + (PAD - sh)/2 + lm.getAscent(); float sw = (float)font.getStringBounds(s, frc).getWidth(); float sx = (w - sw)/2; g2.drawString(s, sx, sy); // Draw lines. double xInc = (double)(w - 2*PAD)/(data.length-1); double scale = (double)(h - 2*PAD)/getMax(); g2.setPaint(Color.green.darker()); for(int i = 0; i < data.length-1; i++) { double x1 = PAD + i*xInc; double y1 = h - PAD - scale*data[i]; double x2 = PAD + (i+1)*xInc; double y2 = h - PAD - scale*data[i+1]; g2.draw(new Line2D.Double(x1, y1, x2, y2)); } // Mark data points. g2.setPaint(Color.red); for(int i = 0; i < data.length; i++) { double x = PAD + i*xInc; double y = h - PAD - scale*data[i]; g2.fill(new Ellipse2D.Double(x-2, y-2, 4, 4)); } } private int getMax() { int max = -Integer.MAX_VALUE; for(int i = 0; i < data.length; i++) { if(data[i] > max) max = data[i]; } return max; } public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new GraphingData()); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } }
Thanks.Last edited by Fubarable; 05-08-2011 at 05:06 PM. Reason: code tags added
- 05-06-2011, 10:36 AM #2
Homework?
Use code tags to post codes -- [code]CODE[/code] will display asdbJava Code:CODE
- 05-06-2011, 11:08 AM #3
Member
- Join Date
- May 2011
- Posts
- 6
- Rep Power
- 0
Ok thanks, I was wondering how to do that.
It is not a homework question, its a small feature I need to be able to do, as part of larger project I am working on.
- 05-06-2011, 11:47 AM #4
Ever heard of JFreeChart?
db
- 05-06-2011, 12:35 PM #5
Member
- Join Date
- May 2011
- Posts
- 6
- Rep Power
- 0
I have heard a little about it, but I would prefer not to use a 3rd party plugin if possible.
- 05-06-2011, 01:01 PM #6
Member
- Join Date
- May 2011
- Posts
- 6
- Rep Power
- 0
I have heard a little about it, but I would prefer not to use a 3rd party plugin if possible.
- 05-06-2011, 01:21 PM #7
Member
- Join Date
- May 2011
- Location
- Munich
- Posts
- 15
- Rep Power
- 0
use a double array (eg.int[][] data)
In this part..
<Code>
for (int i = 0; i < data.length - 1; i++) {
double x1 = PAD + i * xInc;
double y1 = h - PAD - scale * data[i];
double x2 = PAD + (i + 1) * xInc;
double y2 = h - PAD - scale * data[i + 1];
g2.draw(new Line2D.Double(x1, y1, x2, y2));
}
</Code>
Just contain that within another four loop, and iterate through you first data array. Set the graphic to a different colour and you should be ticketyboo.read my blog : www.blue-walrus.com
- 05-08-2011, 05:02 PM #8
Member
- Join Date
- May 2011
- Posts
- 6
- Rep Power
- 0
Thanks for the reply Oliver, am I right in thinking that that would only allow for 2 lines to be plotted though (using a double array)? I ideally need to have about 24 lines on one graph you see.
-
Shoot, Hardwire already showed how to plot lines:
You could use a 2-d array of data as suggested above and then enclose this for loop in another for loop that loops through each row of data.Java Code:g2.setPaint(Color.green.darker()); for(int i = 0; i < data.length-1; i++) { double x1 = PAD + i*xInc; double y1 = h - PAD - scale*data[i]; double x2 = PAD + (i+1)*xInc; double y2 = h - PAD - scale*data[i+1]; g2.draw(new Line2D.Double(x1, y1, x2, y2)); } // Mark data points. g2.setPaint(Color.red); for(int i = 0; i < data.length; i++) { double x = PAD + i*xInc; double y = h - PAD - scale*data[i]; g2.fill(new Ellipse2D.Double(x-2, y-2, 4, 4)); }
Also, I added code tags added to your first post above to help make it readable.
To do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.
Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:
Best of luckJava Code:[code[i][/i]] // your code goes here // notice how the top and bottom tags are different [/code[i][/i]]
Last edited by Fubarable; 05-08-2011 at 05:10 PM.
- 05-09-2011, 02:52 PM #10
Member
- Join Date
- May 2011
- Posts
- 6
- Rep Power
- 0
Thanks for your help Fubarable, I have done what I needed to do now. :)
- 01-14-2012, 06:39 PM #11
Member
- Join Date
- Jan 2012
- Posts
- 1
- Rep Power
- 0
Re: How to plot graph in java with multiple lines for given samples (stored in arrays
HI there , the code is really help full. I want to plot the values of the error at each run of the for loop and the number of runs are not predecided. I am saving the data in the vector data type. Help me to plot the data saved in the Vector.
-
Re: How to plot graph in java with multiple lines for given samples (stored in arrays
You'll want to ask this question in its own thread, not hijack an old question (which I'm going to lock). In your question, you'll want to provide more details on just where you're stuck, since your post above doesn't ask a truly answerable question. Please see the links in my signature below including the one on how to ask smart questions as it will tell you what information we need to make your question answerable.
Similar Threads
-
plot graph
By alaa in forum New To JavaReplies: 9Last Post: 04-15-2011, 09:13 PM -
How to plot graph in java for given samples
By annesteve31 in forum New To JavaReplies: 27Last Post: 11-29-2009, 09:27 PM -
i want plot realtime graph in java
By santhosh_el in forum New To JavaReplies: 3Last Post: 02-26-2009, 08:32 AM -
Plot 2D graph in Java from RS-232 data
By spratana in forum Java 2DReplies: 4Last Post: 02-11-2009, 06:49 PM -
How to plot a dot graph
By Manfizy in forum Java 2DReplies: 3Last Post: 01-28-2009, 02:57 PM


LinkBack URL
About LinkBacks


Bookmarks