Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Wiki
Search Coderanch
Advance search
Google search
Register / Login
Win a copy of
DevSecOps Adventures: A Game-Changing Approach with Chocolate, LEGO, and Coaching Games
this week in the
Agile and Other Processes
forum!
Bookmark Topic
Watch 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:
Campbell Ritchie
Ron McLeod
Paul Clapham
Devaka Cooray
Tim Cooke
Sheriffs:
Rob Spoor
Liutauras Vilda
paul wheaton
Saloon Keepers:
Tim Holloway
Tim Moores
Mikalai Zaikin
Carey Brown
Piet Souris
Bartenders:
Stephan van Hulst
Forum:
Wiki
Plot Test
posted 8 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
This code sample shows how to draw a simple X/Y graph. It is based on code
posted here
by Craig Wood. Optionally, either the data points themselves can be shown, or a line connecting them, or both.
import java.awt.*; import javax.swing.*; public class PlotTest extends JPanel { int PAD = 20; boolean drawLine = true; boolean drawDots = true; int dotRadius = 3; // the y coordinates of the points to be drawn; the x coordinates are evenly spaced int[] data = { 25, 60, 42, 75 }; 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(); g2.drawLine(PAD, PAD, PAD, h-PAD); g2.drawLine(PAD, h-PAD, w-PAD, h-PAD); double xScale = (w - 2*PAD) / (data.length + 1); double maxValue = 100.0; double yScale = (h - 2*PAD) / maxValue; // The origin location int x0 = PAD; int y0 = h-PAD; // draw connecting line if (drawLine) { for (int j = 0; j < data.length-1; j++) { int x1 = x0 + (int)(xScale * (j+1)); int y1 = y0 - (int)(yScale * data[j]); int x2 = x0 + (int)(xScale * (j+2)); int y2 = y0 - (int)(yScale * data[j+1]); g2.drawLine(x1, y1, x2, y2); } } // draw the points as little circles in red if (drawDots) { g2.setPaint(Color.red); for (int j = 0; j < data.length; j++) { int x = x0 + (int)(xScale * (j+1)); int y = y0 - (int)(yScale * data[j]); g2.fillOval(x-dotRadius, y-dotRadius, 2*dotRadius, 2*dotRadius); } } } public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(new PlotTest()); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } }
CategoryCodeSamples
CodeBarn
them good ole boys were drinking whiskey and rye singin' this'll be the day that I die. Drink tiny ad.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
drawing arrows
Drawing graphs
label in coordinates
How to draw a chart with multiple y-axises
Problem in reloading graphics object.
More...