How To Draw Multiple Graphs In Java Swing
For a Java user sometimes there is a data task to visualize how a certain variable changes in time. For that y'all can employ JFreeChart. And if there are many variables, no problem there is JFreeChart. JFreeChart is an open source library and it is free to use. In this example we visualize a series of lines that stand for how variables change on fourth dimension.
A very self explanatory lawmaking for the above job is also the following one :
The code we will :
one - generate an interval of values to use it for each line
2 - brand the methods for representing the multiple lines graph itself
The Java lawmaking for the multiple lines graphic is ready to use and you tin can copy paste information technology in your Eclipse IDE after importing the jfreechart.jar file in your project
public form MultipleLinesChart extends JFrame { // the class extends the JFrame class
} And this is the effect we have later on running the code . With JFreeChart it is pretty piece of cake to brand even more than data visualizations. There are many other guides in DataTreeMap and if you liked this guide please follow .
public MultipleLinesChart() { // the constructor volition contain the panel of a certain size and the close operations super("XY Line Nautical chart Example with JFreechart"); // calls the super class constructor JPanel chartPanel = createChartPanel(); add(chartPanel, BorderLayout.CENTER); setSize(640, 480); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); } private JPanel createChartPanel() { // this method will create the nautical chart panel containin the graph String chartTitle = "Objects Move Chart"; String xAxisLabel = "Ten"; String yAxisLabel = "Y"; XYDataset dataset = createDataset(); JFreeChart chart = ChartFactory.createXYLineChart(chartTitle, xAxisLabel, yAxisLabel, dataset); customizeChart(chart); // saves the nautical chart as an image files File imageFile = new File("XYLineChart.png"); int width = 640; int superlative = 480; try { ChartUtilities.saveChartAsPNG(imageFile, chart, width, meridian); } catch (IOException ex) { Organization.err.println(ex); } return new ChartPanel(nautical chart); } private XYDataset createDataset() { // this method creates the information every bit fourth dimension seris XYSeriesCollection dataset = new XYSeriesCollection(); XYSeries series1 = new XYSeries("Object 1"); XYSeries series2 = new XYSeries("Object 2"); XYSeries series3 = new XYSeries("Object 3"); series1.add(1.0, ii.0); series1.add(two.0, iii.0); series1.add(3.0, ii.v); series1.add(iii.5, 2.8); series1.add(4.2, 6.0); series2.add(two.0, i.0); series2.add(2.five, 2.4); series2.add together(3.ii, 1.ii); series2.add(3.9, 2.8); series2.add together(four.6, iii.0); series3.add(1.2, 4.0); series3.add(2.v, iv.4); series3.add(3.8, 4.2); series3.add(4.3, 3.8); series3.add(4.5, 4.0); dataset.addSeries(series1); dataset.addSeries(series2); dataset.addSeries(series3); return dataset; } private void customizeChart(JFreeChart chart) { // here nosotros make some customization XYPlot plot = chart.getXYPlot(); XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(); // sets paint colour for each series renderer.setSeriesPaint(0, Color.Ruddy); renderer.setSeriesPaint(1, Color.Greenish); renderer.setSeriesPaint(ii, Colour.YELLOW); // sets thickness for serial (using strokes) renderer.setSeriesStroke(0, new BasicStroke(4.0f)); renderer.setSeriesStroke(1, new BasicStroke(iii.0f)); renderer.setSeriesStroke(2, new BasicStroke(2.0f)); // sets paint color for plot outlines plot.setOutlinePaint(Colour.BLUE); plot.setOutlineStroke(new BasicStroke(2.0f)); // sets renderer for lines plot.setRenderer(renderer); // sets plot background plot.setBackgroundPaint(Colour.DARK_GRAY); // sets paint color for the grid lines plot.setRangeGridlinesVisible(truthful); plot.setRangeGridlinePaint(Color.BLACK); plot.setDomainGridlinesVisible(true); plot.setDomainGridlinePaint(Colour.BLACK); } public static void principal(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new MultipleLinesChart().setVisible(truthful); } }); }
See you next time with more than interesting guides on data visualization.
Source: https://steemit.com/visualization/@datatreemap/visualize-a-multiple-lines-graph-with-jfreechart-in-java
Posted by: johnsonequallown.blogspot.com

0 Response to "How To Draw Multiple Graphs In Java Swing"
Post a Comment