- November 24, 2020
- saptrxuy_learnit
- 0 Comments
- 1856 Views
- 1 Likes
- Java
Plotting with Gral
1. Introduction
2. Main class
3. Call method for Simple Bar Chart
4. Create an object of GralSimpleBarPlot class
5. Extend this class to parent class that contains common things like JFrame, color, size etc
6. Extend this class to JPanel
7. Add version for serialization
8. Constructor of MainGralPanel
9. Render Graph
10. Set frame properties
11. Call render method from the parent class
12. Create constructor of this class
13. Create data for bar chart
14. Create new bar plot
15. Create bar renderer
16. Set plot dimensions
17. Configure bar renderer
18. Change graph title
19. Method for Scatter Plot
20. Create an object of GralSimpleBarPlot class
21. Create constructor
22. Generate data points
23. Create a new xy-plot
24. Add description as title
1.Introduction▲
Every chart has three components
1. Main class that has main method and calls the chart class
2. The chart class like GralSimpleBarPlot or ScatterPlot calls inherits common Gral properties and functions from parent class MainGralPanel
3. MainGralPanel extends to JPanel. It provides frame and basic formatting and also rendering.
2.Main class▲
2.1.Code▲
10:package xv.training.java.ml.plotting.gral.examples; 20: 30:public class Main { 40: 50: // 200 main method 60: public static void main(String[] args) { 70: 80: // 200 90: System.out.println("Program started\n"); 160: 170: // 200 180: System.out.println("\nProgram closed"); 190: 200: } 210: 360: 540: 550:} 560: 570:
2.2.Output▲
Program started Program closed
3.Call method for Simple Bar Chart▲
3.1.Code▲
100: 110: //300 Call method for Simple Bar Chart 120: simpleBarChartExample(); 370: 380: //300 method for Simple Bar Chart 390: private static void simpleBarChartExample() { 450: 520: 530: }
4.create an object of GralSimpleBarPlot class▲
4.1.Code▲
400: 410: // 400 create an object of GralSimpleBarPlot class 420: //this class contains code for bar plot 430: System.out.println("Creating instance of GralSimpleBarPlot class"); 440: GralSimpleBarPlot graph = new GralSimpleBarPlot(); 580: 590://400 600:package xv.training.java.ml.plotting.gral.examples; 610: 620:import java.awt.BasicStroke; 630:import java.awt.Color; 640:import java.awt.Font; 650:import java.awt.LinearGradientPaint; 770: 780:// 400 create class contains code for the plot we want 790:public class GralSimpleBarPlot 840:{ 850: 1300: 1670: 1680: 1690: 1700: 1710: 1720: 1830: 1840: 2000: 2010: 2020: 2030: 2040: 2050:}
4.2.Output▲
Creating instance of GralSimpleBarPlot class
5.extend this class to parent class that contains common things like JFrame, color, size etc▲
5.1.Code▲
800: 810: // 500 extend this class to parent class that contains 820: // common things like JFrame, color, size etc 830: extends MainGralPanel 2060: 2070://500 2080:package xv.training.java.ml.plotting.gral.examples; 2210: 2220: 2230: 2240:// 500 this class to parent class that contains 2250:// common things like JFrame, color, size etc 2260:public class MainGralPanel 2290: { 2750: 2760: 2770:} 2780:
6.extend this class to JPanel▲
6.1.Code▲
2090: 2100://600 2110:import javax.swing.JPanel; 2270: // 600 extend this class to JPanel 2280: extends JPanel
7.add version for serialization▲
7.1.Code▲
2300: 2310: // 700 add version for serialization 2320: private static final long serialVersionUID = 7457745757745774456L;
8.constructor of MainGralPanel▲
8.1.Code▲
2120: 2130: 2140://800 2150:import java.awt.BorderLayout; 2160:import java.awt.Color; 2170:import java.awt.Dimension; 2330: 2340: // 800 constructor of MainGralPanel 2350: // also set layout, preferred size and background color 2360: public MainGralPanel() { 2370: super(new BorderLayout()); 2380: this.setPreferredSize(new Dimension(800, 600)); 2390: this.setBackground(Color.WHITE); 2400: }
9.Render Graph▲
9.1.Code▲
2180: 2190://900 2200:import javax.swing.JFrame; 2410: 2420: // 900 Render Graph 2430: // this will be called from main mathod 2440: public JFrame render() { 2450: 2460: // 900.1 set title of the graph 2470: System.out.println("Intializing JFrame and Setting its title"); 2480: JFrame frame = new JFrame(getTitle()); 2560: 2570: 2580: //900.2 set frame properties 2590: frame.setVisible(true); 2600: 2610: 2620: 2630: //900.3 2640: System.out.println("Returning JFrame"); 2650: return frame; 2660: } 2670: 2680: 2690: // 900.2 get title of the graph 2700: // overrride this method for the graph you create in inherited class 2710: public String getTitle() { 2720: return "Gral Graph Example"; 2730: } 2740:
10.set frame properties▲
10.1.Code▲
2490: 2500: //1000 set frame properties 2510: frame.getContentPane().add(this, BorderLayout.CENTER); 2520: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 2530: 2540: //this method getPreferredSize() is defined in JComponent class 2550: frame.setSize(getPreferredSize());
11.call render method from the parent class▲
This step will render a simple graph panel
11.1.Code▲
460: 470: // 1100 call render method from the parent class 480: // the render method displays the frame 490: // in the begining it will show a blank panel 500: System.out.println("Rendering Graph"); 510: graph.render();
12.create constructor of this class▲
12.1.Code▲
900: 910: //1200 create constructor of this class 920: public GralSimpleBarPlot() { 930: super(); 940: 1270: 1280: 1290: }
13.create data for bar chart▲
13.1.Code▲
660: 670://1300 680:import de.erichseifert.gral.data.DataTable; 690:import de.erichseifert.gral.graphics.Insets2D; 700:import de.erichseifert.gral.graphics.Location; 950: 960: // 1300 create data for bar chart 970: System.out.println("\nCreating data"); 980: DataTable dataTable = this.createBarChartData(); 1850: 1860: 1870: 1880: // 1300 create data for bar chart 1890: private DataTable createBarChartData() { 1900: // Create example data 1910: DataTable dataTable = new DataTable(Double.class, Integer.class, String.class); 1920: dataTable.add(0.1, 10, "A"); 1930: dataTable.add(0.2, 50, "B"); 1940: dataTable.add(0.3, -10, "C"); 1950: dataTable.add(0.4, -20, "D"); 1960: 1970: 1980: return dataTable; 1990: }
14.Create new bar plot▲
At this point, we get a chart with black panel
14.1.Code▲
710://1400 720:import de.erichseifert.gral.plots.BarPlot; 730:import de.erichseifert.gral.ui.InteractivePanel; 740:import de.erichseifert.gral.util.GraphicsUtils; 990: 1000: 1010: //1400 Create new bar plot 1020: System.out.println("\nCreating new bar plot"); 1030: BarPlot plot = new BarPlot(dataTable); 1220: 1230: //1400 Add plot to Swing component 1240: //at this point, dark background plot shows 1250: System.out.println("\nAdd plot to Swing component"); 1260: this.add(new InteractivePanel(plot));
14.2.Output▲
Program started Creating instance of GralSimpleBarPlot class Creating data Creating new bar plot Add plot to Swing component Rendering Graph Intializing JFrame and Setting its title Returning JFrame Program closed
15.Create bar renderer▲
15.1.Code▲
750://1500 760:import de.erichseifert.gral.plots.BarPlot.BarRenderer; 1090: 1100: 1110: 1120: //1500 Create bar renderer 1130: System.out.println("\nFormat bars"); 1140: BarRenderer barRenderer = (BarRenderer) plot.getPointRenderers(dataTable).get(0); 1180: 1190: //1500 show bar renderer 1200: barRenderer.setValueVisible(true); 1210:
16.Set plot dimensions▲
At this point, we get proper chart with black bars
16.1.Code▲
1040: 1050: 1060: //1600 Set plot dimensions 1070: //at this point we see the chart 1080: setPlotDimensions(plot); 1730: 1740: 1750: //1600 Set plot dimensions 1760: //setting inset and bar width 1770: private void setPlotDimensions(BarPlot plot) { 1780: System.out.println("\nFormat plot setting inset and bar width"); 1790: plot.setInsets(new Insets2D.Double(40.0, 40.0, 40.0, 40.0)); 1800: plot.setBarWidth(0.075); 1810: } 1820:
17.configure bar renderer▲
We get proper bar chart
17.1.Code▲
860: 870: //1700 configure 880: public static final Color COLOR1 = new Color( 55, 170, 200); 890: 1150: 1160: //1700 configure bar renderer 1170: this.configureBarRenderer(barRenderer); 1400: 1410: //1700 configure bar renderer 1420: private void configureBarRenderer(BarRenderer barRenderer) { 1430: barRenderer.setColor( 1440: new LinearGradientPaint(0f,0f, 0f,1f, 1450: new float[] { 0.0f, 1.0f }, 1460: new Color[] { COLOR1, GraphicsUtils.deriveBrighter(COLOR1) } 1470: ) 1480: ); 1490: 1500: barRenderer.setBorderStroke(new BasicStroke(3f)); 1510: barRenderer.setBorderColor( 1520: new LinearGradientPaint(0f,0f, 0f,1f, 1530: new float[] { 0.0f, 1.0f }, 1540: new Color[] { GraphicsUtils.deriveBrighter(COLOR1), COLOR1 } 1550: ) 1560: ); 1570: 1580: barRenderer.setValueColumn(2); 1590: barRenderer.setValueLocation(Location.CENTER); 1600: barRenderer.setValueColor(GraphicsUtils.deriveDarker(COLOR1)); 1610: barRenderer.setValueFont(Font.decode(null).deriveFont(Font.BOLD)); 1620: 1630: 1640: } 1650: 1660:
18.change graph title▲
Change bar graph title
18.1.Code▲
1310: 1320: //1800 change graph title 1330: @Override 1340: public String getTitle() { 1350: return "Gral Bar plot"; 1360: } 1370: 1380: 1390:
18.2.How to run▲
19.method for Scatter Plot▲
19.1.Code▲
130: 140: //1900 Call method for Scatter Plot 150: scatterPlotExample(); 220: 230: //1900 method for Scatter Plot 240: private static void scatterPlotExample() { 340: 350: }
20.create an object of GralSimpleBarPlot class▲
20.1.Code▲
120: //simpleBarChartExample(); 250: 260: // 2000 create an object of GralSimpleBarPlot class 270: System.out.println("Creating instance of ScatterPlot class"); 280: ScatterPlot graph = new ScatterPlot(); 290: 300: 310: //2000 call render method from the parent class 320: System.out.println("Rendering Graph"); 330: graph.render(); 2790: 2800://2000 2810:package xv.training.java.ml.plotting.gral.examples; 2820: 2840:import java.awt.Color; 2910: 2920: 2930:// 2000 Class for Scatter Plot 2940:public class ScatterPlot extends MainGralPanel { 2950: 2960: //2000 serialization id 2970: private static final long serialVersionUID = -123456734567678345L; 2980: public static final Color COLOR1 = new Color( 55, 170, 200); 2990: 3060: 3070: 3360: 3370: 3380: //2000 Override getTitle() 3390: @Override 3400: public String getTitle() { 3410: return "Gral Scatter plot"; 3420: } 3490: 3500:} 3510: 3520:
21.create constructor▲
21.1.Code▲
3080: 3090: //2100 create constructor 3100: public ScatterPlot() { 3110: 3170: 3280: 3350: }
22.Generate data points▲
22.1.Code▲
2830:import java.awt.BorderLayout; 2850:import java.util.Random; 2860: 2870:import de.erichseifert.gral.data.DataTable; 3000: 3010: //2200 Generate data points max count 3020: private static final int MAX_COUNT = 100000; 3030: 3040: //2200 instance to generate random values 3050: private static final Random random = new Random(); 3120: //2200 Generate data points 3130: DataTable data = new DataTable(Double.class, Double.class); 3140: for (int i = 0; i <= MAX_COUNT; i++) { 3150: data.add(random.nextGaussian()*2.0, random.nextGaussian()*2.0); 3160: }
23.Create a new xy-plot▲
We get scatter plot
23.1.Code▲
2880:import de.erichseifert.gral.plots.XYPlot; 2890:import de.erichseifert.gral.ui.InteractivePanel; 2900:import de.erichseifert.gral.graphics.Insets2D; 3180: 3190: //2300 Create a new xy-plot 3200: //create instance of XYPlot 3210: XYPlot plot = new XYPlot(data); 3220: 3230: //format plot 3240: plot.setInsets(new Insets2D.Double(20.0, 40.0, 40.0, 40.0)); 3250: 3260: // Format color of points 3270: plot.getPointRenderers(data).get(0).setColor(COLOR1); 3320: 3330: //2300 Add plot to Swing component 3340: add(new InteractivePanel(plot), BorderLayout.CENTER);
24.add description as title▲
24.1.Code▲
3290: 3300: //2400 add description as title 3310: plot.getTitle().setText(getDescription()); 3430: 3440: //2400 add description as title 3450: //@Override 3460: public String getDescription() { 3470: return String.format("Scatter plot with %d data points", MAX_COUNT); 3480: }
Leave a Comment