Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Tim Cooke
Campbell Ritchie
paul wheaton
Ron McLeod
Devaka Cooray
Sheriffs:
Jeanne Boyarsky
Liutauras Vilda
Paul Clapham
Saloon Keepers:
Tim Holloway
Carey Brown
Piet Souris
Bartenders:
Forum:
Swing / AWT / SWT
Can you help me plzzz with a graph?
dorjee gyaltsen
Greenhorn
Posts: 4
posted 20 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
hi!
can anyone give me the basic
java
code to create a simple line graph for output of my java program.
thanks a lot in advance.
cheers!
Craig Wood
Ranch Hand
Posts: 1535
posted 20 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
This is a modification to some older code and should run okay in j2se 1.4
If using j2se 1.5 try compiling to an earlier version with the source flag, eg,
>javac -source 1.4 simplelinegraph.java
import java.awt.*; import java.awt.font.*; import java.awt.geom.*; import java.util.*; import java.util.List; import javax.swing.*; public class SimpleLineGraph { public SimpleLineGraph() { float[] data = { 100f, 220f, 12.9f, 65f, 47.3f, 175f, 190f, -18f }; Plotter plotter = new Plotter(); for(int j = 0; j < data.length; j++) plotter.plot(data[j]); JFrame f = new JFrame("Plot +/- Values"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(plotter); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } public static void main(String[] args) { new SimpleLineGraph(); } } class Plotter extends JPanel { final double PAD = 30.0; Font font; List dataList; public Plotter() { font = new Font("dialog", Font.PLAIN, 14); dataList = new ArrayList(); setBackground(Color.white); setPreferredSize(new Dimension(600,400)); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setFont(font); double width = getWidth(); double height = getHeight(); double xStep = (width - 2*PAD)/(dataList.size() - 1); // scale data double max = ((Double)Collections.max(dataList)).doubleValue(); double min = ((Double)Collections.min(dataList)).doubleValue(); double vertSpace = height - 2*PAD; double yOffset = height - PAD; double yDataOffset = (min >= 0 ? min : max > 0 ? 0 : max); double scale = vertSpace/(max - min); double yOrigin = yOffset + (min > 0 ? 0 : max > 0 ? scale * min : - vertSpace); // draw ordinate g2.draw(new Line2D.Double(PAD, PAD, PAD, yOffset)); // label ordinate limits g2.drawString(String.valueOf(max), 10, (float)(PAD - 10)); g2.drawString(String.valueOf(min), 10, (float)(yOffset + PAD*2/3)); // draw abcissa g2.draw(new Line2D.Double(PAD, yOrigin, width - PAD, yOrigin)); // abcissa tick marks FontRenderContext frc = g2.getFontRenderContext(); double textWidth = font.getStringBounds("8", frc).getWidth(); LineMetrics lm = font.getLineMetrics("8", frc); double textHeight = lm.getHeight(); double x = PAD + xStep; double y = yOrigin; for(int j = 1; j < dataList.size(); j++) { g2.draw(new Line2D.Double(x, y, x, y + 3)); x += xStep; } // label abcissa String s; double sx = PAD + xStep - textWidth/2; float sy = (float)(yOrigin + textHeight); for(int j = 1; j < dataList.size(); j++) { s = String.valueOf(j + 1); g2.drawString(s, (float)sx, sy); sx += xStep; } // plot data x = PAD; double lastX = 0, lastY = 0, value; for(int j = 0; j < dataList.size(); j++) { value = ((Double)dataList.get(j)).doubleValue(); y = yOrigin - scale * (value - yDataOffset); g2.setPaint(Color.red); g2.draw(new Ellipse2D.Double(x - 2, y - 2, 4, 4)); if( j > 0) { g2.setPaint(Color.black); g2.draw(new Line2D.Double(lastX, lastY, x, y)); } lastX = x; lastY = y; x += xStep; } } protected void plot(double input) { dataList.add(new Double(input)); repaint(); } }
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
How do I create a scrolling line graph applet?
Graph
help regarding pie graph
Graphs in Java Standalone Project
graph class
More...