2018년 7월 16일 월요일

[Java] Build Project with Gradle


Downloadgradle-4.9-bin.zip gradle-4.9-all.zip

1. Create Project

- Make an empty project directory
- Create project skeleton with 'gradle init' command in the project directory.
1
2
3
4
5
6
7
$ gradle init --type <name>
  <name>
 - java-application
 - java-library
 - scala-library
 - groovy-library
 - basic


2. Add local jar files for offline build

- Add jar files to the dependencies category in the root build.gradle file.
1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
 plugins {
  id 'java'
  id 'application'
 }

 mainClassName = 'com.app.App1'
  
 dependencies {
  compile files('libs/commons-io-2.5.jar')
  compile files('libs/logback-classic-1.2.3.jar')
  compile files('libs/logback-core-1.2.3.jar')
  compile files('libs/slf4j-api-1.7.25.jar')
 }

 repositories {
  jcenter()
 }


3. Make multiple start script

- Add below command in the root build.gradle file

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
 applicationName = 'App1'
 applicationDefaultJvmArgs = ["-Xms512m", "-Xmx1024m"]

 task createApp2Script(type: CreateStartScripts) {
  mainClassName = "com.app.App2"
  classpath = startScripts.classpath
  outputDir = startScripts.outputDir
  applicationName = 'App2'
  defaultJvmOpts = ["-Xms1024m", "-Xmx2048m"]
 }

 applicationDistribution.into("bin") {
  duplicatesStrategy= DuplicatesStrategy.EXCLUDE
  from(createApp2Script)
  fileMode = 0755
 }
 distZip {
  archiveName 'AppRelease.zip'
 }



4. Make single combined jar files 

- Add below command in the root build.gradle file

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 task makeApp1Jar(type: Jar) {
  manifest {
   attributes 'Implementation-Title': 'AppRelease',  
    'Implementation-Version': '1.0.0',
    'Main-Class': 'com.app.App1'
  }
  baseName = project.name + '_App1'
  from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
  with jar
 }
 task makeApp2Jar(type: Jar) {
  manifest {
   attributes 'Implementation-Title': 'AppRelease',  
    'Implementation-Version': '1.0.0',
    'Main-Class': 'com.app.App2'
  }
  baseName = project.name + '_App2'
  from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
  with jar
 }
 applicationDistribution.into("bin") {
  duplicatesStrategy= DuplicatesStrategy.EXCLUDE
  from(makeApp1Jar)
  from(makeApp2Jar)
  from(createApp2Script)
  fileMode = 0755
 }








2018년 7월 15일 일요일

[Python] Get File List in Folder


1. Get File List using os.listdir
1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import os

def getFileList(folder):
    fileList = [ ]
    files = os.listdir(folder)
    for file in files:
        fullpath = os.path.join(folder,file)
        if os.path.isdir(fullpath):
            fileList = fileList + getFileList(fullpath)
        else:
            fileList.append(fullpath)
    return fileList

if __name__ == "__main__":
    files = getFileList("D:\Temp")
    for f in files:
        print(f)

Output:
D:\Temp\file1.txt
D:\Temp\file2.txt
D:\Temp\folder1\file3.txt
D:\Temp\folder1\file4.txt
D:\Temp\folder2\file5.txt
D:\Temp\folder2\file6.txt

2. Get File List using os.walk

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import os

def getFileListByWalk(folder):
    from os import walk
    f = []
    for (dirpath, dirnames, filenames) in walk(folder):
        for filename in filenames:
            f.append(os.path.join(dirpath,filename))
    return f

if __name__ == "__main__":
    files = getFileListByWalk("D:\Temp")  
    for f in files:
        print(f)  




[Java] Chart (Graph) Image Creation


Downloadjfreechart-1.5.0.jar


1. JFreeChart Wrapper Class

1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.CenterTextMode;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.RingPlot;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.AreaRenderer;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.renderer.category.StandardBarPainter;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.chart.ui.ApplicationFrame;
import org.jfree.chart.ui.RectangleEdge;
import org.jfree.chart.ui.VerticalAlignment;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import chart.jfree.ChartSupplier;

public class ChartTools {

    final String STYLE_LINE = "line";
    final String STYLE_DASH = "dash";
    final String STYLE_DOT = "dot";
    
    DefaultCategoryDataset categoryDataset;
    DefaultPieDataset pieDataset;
    XYSeriesCollection xySeriesDatasset;
    Color[] chartColors;
    Color[] pieColors;
    JFreeChart chart;
    
    Font font;
    Color background;
    boolean showLegend;
    String title;
    String xtitle;
    String ytitle;
    TextTitle subtitle;
    
    public ChartTools() {
        font = new Font("맑은 고딕", Font.BOLD+Font.ITALIC, 20);
        //font = new Font("TimesRoman", Font.BOLD+Font.ITALIC, 20);
        categoryDataset = new DefaultCategoryDataset();
        pieDataset = new DefaultPieDataset();
        xySeriesDatasset = new XYSeriesCollection();
        background = Color.WHITE;
        subtitle = null;
        chartColors = null;
        pieColors = null;
        chart = null;

    }

    //----------------------------------------------------------------------
    //1. Common Method
    //----------------------------------------------------------------------
    public void showLegend(boolean onoff) {
        showLegend = onoff;
    }
    
    public void setBackground(Color color) {
        background = color;
    }

    public void setTitles(String title, String xtitle, String ytitle) {
        this.title  = title;
        this.xtitle = xtitle;
        this.ytitle = ytitle;
    }

    public void setSubtitle(String description) {
        subtitle = new TextTitle(description);
        subtitle.setFont(new Font("SansSerif", Font.PLAIN, 12));
        subtitle.setPosition(RectangleEdge.TOP);
        //subtitle.setSpacer(new Spacer(Spacer.RELATIVE, 0.05, 0.05, 0.05, 0.05));
        subtitle.setVerticalAlignment(VerticalAlignment.BOTTOM);
    }
    

    public Plot getPlot() {
        return chart.getPlot();
    }

    public void setColors(Color[] colors) {
        chartColors = colors;
    }
    
    public CategoryItemRenderer getRenderer() {
        return chart.getCategoryPlot().getRenderer();
    }
    
    public XYItemRenderer getXYRenderer() {
        return chart.getXYPlot().getRenderer();
    }
    
    public BufferedImage getChartImage(int width, int height) {
        return chart.createBufferedImage(width, height);
    }

    public void saveChartToPng(String fileName, int width, int height) throws IOException {
        ChartUtils.saveChartAsPNG(new File(fileName), chart, width, height);
    }

    public void saveChartToJpg(String fileName, int width, int height) throws IOException {
        ChartUtils.saveChartAsJPEG(new File(fileName), chart, width, height);
    }
    
    public void showChartSimple(int width, int height) {
        ChartPanel panel = new ChartPanel(chart);
        panel.setPreferredSize(new Dimension(width, height));
        panel.setMouseZoomable(false);
        
        ApplicationFrame frame = new ApplicationFrame("Chart");
        frame.setContentPane(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
        
    public void showChart(int width, int height) {
        BufferedImage image = chart.createBufferedImage(width, height);
        image.flush();
        JPanel panel = new JPanel() {
            private static final long serialVersionUID = 1L;
            public void paintComponent(Graphics g) {
                g.drawImage(image, 0, 0, width, height, this);
            }
        };
        panel.setPreferredSize(new Dimension(width, height));
        panel.setOpaque(false);
        panel.prepareImage(image, panel);
        panel.repaint();
        JFrame frame = new JFrame();
        //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(panel);
        frame.pack();
        frame.setVisible(true);
    }
    
    //----------------------------------------------------------------------
    //2. Add Data Set
    //----------------------------------------------------------------------
    public void addCategoryData( String title, String[] axis, double[] values ) throws Exception {
        if( axis.length != values.length ) {
            throw new Exception("dataset");
        }
        for( int i = 0; i < axis.length; i++ ) {
            categoryDataset.addValue( values[i], title, axis[i]);
        }
        pieColors = null;
    }

    public void addPieData( String[] titles, double[] values, Color[] colors ) throws Exception {
        if( titles.length != values.length ) {
            throw new Exception("dataset");
        }
        for( int i = 0; i < titles.length; i++ ) {
            pieDataset.setValue( titles[i], values[i]);
        }
        pieColors = colors;
    }
    
    public void addPieData( String[] titles, double[] values ) throws Exception {
        addPieData( titles, values, null );
    }
    
    public void addXYData(XYSeries series) throws Exception {
        xySeriesDatasset.addSeries(series); 
    }

    public BasicStroke getLineStyle(String style, float width) {
        float dash[] = { 5.0f };
        float dot[] = { width };
        BasicStroke stroke = null;
        if (style.equalsIgnoreCase(STYLE_LINE)) {
            stroke = new BasicStroke(width);
        } else if (style.equalsIgnoreCase(STYLE_DASH)) {
            stroke = new BasicStroke(width, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f);
        } else if (style.equalsIgnoreCase(STYLE_DOT)) {
            stroke = new BasicStroke(width, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 2.0f, dot, 0.0f);
        }
        return stroke;
    }

    //----------------------------------------------------------------------
    //3. Decorate Chart
    //----------------------------------------------------------------------
    public void decoreatePlot(Plot plot) {
        plot.setForegroundAlpha(0.5f);
        plot.setBackgroundPaint(background);
        if( chartColors != null ) {
            plot.setDrawingSupplier(new ChartColor(chartColors));
        }
    }
    
    public void decorateCategoryPlot(CategoryPlot plot) {
        decoreatePlot(plot);
        //plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
        plot.setDomainGridlinesVisible(true);
        plot.setDomainGridlinePaint(Color.lightGray);
        plot.setRangeGridlinesVisible(true);
        plot.setRangeGridlinePaint(Color.lightGray);

    }
    
    public void decorateBarChart() {
        CategoryPlot plot = (CategoryPlot) getPlot();
        decorateCategoryPlot(plot);

        BarRenderer renderer = (BarRenderer) getRenderer();
          renderer.setBarPainter(new StandardBarPainter());
          renderer.setDrawBarOutline(true);
          renderer.setShadowVisible(true);
          renderer.setDefaultItemLabelsVisible(true);
          renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());
    }
    
    public void decorateAreaChart() {
        CategoryPlot plot = (CategoryPlot) getPlot();
        decorateCategoryPlot(plot);

        AreaRenderer renderer = (AreaRenderer) getRenderer();
          renderer.setDefaultItemLabelsVisible(true);
          renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());
    }
    
    public void decorateLineChart() {
        CategoryPlot plot = (CategoryPlot) getPlot();
        decorateCategoryPlot(plot);

        LineAndShapeRenderer renderer = (LineAndShapeRenderer) getRenderer();
          renderer.setDefaultItemLabelsVisible(true);
          renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());
          renderer.setSeriesStroke(0, getLineStyle(STYLE_LINE, 4.0f)); 
          renderer.setSeriesStroke(1, getLineStyle(STYLE_DASH, 4.0f)); 
    }
    
    public void decoratePieChart() {
        PiePlot plot = (PiePlot) getPlot();
        decoreatePlot(plot);

        plot.setSimpleLabels(true);
        plot.setLabelFont(font);
        plot.setLabelOutlinePaint(null);
        plot.setLabelBackgroundPaint(null);
        plot.setLabelShadowPaint(null);
        plot.setOutlinePaint(null);

        for( int i=0; pieColors != null && i < pieColors.length; i++ ) {
            System.out.println(pieDataset.getKey(i));
            plot.setSectionPaint(pieDataset.getKey(i), pieColors[i]);
        }
    }

    public void decorateRingChart() {
        decoratePieChart();

        RingPlot plot = (RingPlot) getPlot();
        plot.setSectionDepth(0.6);
        plot.setSeparatorsVisible(false);
        plot.setCenterTextFont(font);
        plot.setCenterText(title);
        plot.setCenterTextColor(Color.BLUE);
        plot.setCenterTextMode(CenterTextMode.FIXED);   
    }

    
    public void decorateScatterChart() {
        XYPlot plot = (XYPlot) getPlot();
        decoreatePlot(plot);

        //plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
        plot.setDomainGridlinesVisible(true);
        plot.setDomainGridlinePaint(Color.lightGray);
        plot.setRangeGridlinesVisible(true);
        plot.setRangeGridlinePaint(Color.lightGray);
        plot.setDrawingSupplier(new ChartColor());
        
        XYItemRenderer renderer = (XYItemRenderer) getXYRenderer();
          renderer.setDefaultItemLabelsVisible(true);
    }
    

    //----------------------------------------------------------------------
    //4. Create Chart
    //----------------------------------------------------------------------
    public void createBarChart() {
        chart = ChartFactory.createBarChart( title, xtitle, ytitle, 
            categoryDataset, PlotOrientation.VERTICAL, showLegend, true, false);
        
        if( subtitle != null) { chart.addSubtitle(subtitle); }
        decorateBarChart();
    }
    
    public void createStackedBarChart() {
        chart = ChartFactory.createStackedBarChart( title, xtitle, ytitle, 
                categoryDataset, PlotOrientation.VERTICAL, showLegend, true, false);

        if( subtitle != null) { chart.addSubtitle(subtitle); }
        decorateBarChart();
    }
    
    public void createAreaChart() {
        chart = ChartFactory.createAreaChart( title, xtitle, ytitle, 
                categoryDataset, PlotOrientation.VERTICAL, showLegend, true, false);

        if( subtitle != null) { chart.addSubtitle(subtitle); }
        decorateAreaChart();
    }
    
    public void createStackedAreaChart() {
        chart = ChartFactory.createStackedAreaChart( title, xtitle, ytitle, 
                categoryDataset, PlotOrientation.VERTICAL, showLegend, true, false);

        if( subtitle != null) { chart.addSubtitle(subtitle); }
        decorateAreaChart();
    }
    
    public void createLineChart() {
        this.chart = ChartFactory.createLineChart( title, xtitle, ytitle, 
                categoryDataset, PlotOrientation.VERTICAL, showLegend, true, false);

        if( subtitle != null) { chart.addSubtitle(subtitle); }
        decorateLineChart();
    }
    
    public void createPieChart() {
        chart = ChartFactory.createPieChart( title, 
                pieDataset, showLegend, true, false); 

        if( subtitle != null) { chart.addSubtitle(subtitle); }
        decoratePieChart();
    }

    public void createRingChart() {
        chart = ChartFactory.createRingChart( title, 
                pieDataset, showLegend, true, false); 

        if( subtitle != null) { chart.addSubtitle(subtitle); }
        decorateRingChart();
    }
    
    
    public void createScatterChart() {
        chart = ChartFactory.createScatterPlot( title, xtitle, ytitle, 
                xySeriesDatasset, PlotOrientation.VERTICAL, showLegend, true, false);

        if( subtitle != null) { chart.addSubtitle(subtitle); }
        decorateScatterChart();
    }
    
    //----------------------------------------------------------------------
    //5. Example
    //----------------------------------------------------------------------
    public static void showCategoryChart(ChartTools chart) throws Exception {
        String[] Axises1 = new String[] { "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008" };
        double[] Data1 = new double[] {1.0, 4.0, 3.0, 5.0, 5.0, 7.0, 7.0, 8.0};
        chart.addCategoryData( "Data1", Axises1, Data1);

        String[] Axises2 = new String[] { "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008" };
        double[] Data2 = new double[] {5.0, 7.0, 6.0, 8.0, 4.0, 4.0, 2.0, 1.0};
        chart.addCategoryData( "Data2", Axises2, Data2);
        
        chart.createBarChart();
        chart.showChart(800,600);
        chart.saveChartToPng( "D:/bar.png", 800, 600 );     

        chart.createStackedBarChart();
        chart.showChart(800,600);
        chart.saveChartToPng( "D:/stacked_bar.png", 800, 600 );     

        chart.createAreaChart();
        chart.showChart(800,600);
        chart.saveChartToPng( "D:/area.png", 800, 600 );        

        chart.createStackedAreaChart();
        chart.showChart(800,600);
        chart.saveChartToPng( "D:/stacked_area.png", 800, 600 );        

        chart.createLineChart();
        chart.showChart(800,600);
        chart.saveChartToPng( "D:/line.png", 800, 600 );        
    }
    
    public static void showPieChart(ChartTools chart) throws Exception {
        String[] Title1 = new String[] { "2001", "2002", "2003", "2004", "2005", "2006" };
        double[] PieData1 = new double[] { 43.2, 10.0, 27.5, 17.5, 11.0, 19.4 };
        Color[] pie_colors = new Color[] { Color.yellow, Color.gray, Color.cyan, Color.red, Color.green, Color.blue };      
        
        chart.addPieData( Title1, PieData1, pie_colors);   
        chart.createPieChart();
        chart.showChart(800,600);
        chart.saveChartToPng( "D:/pie.png", 800, 600 );     
        
        chart.addPieData( Title1, PieData1);
        chart.createRingChart();
        chart.showChart(800,600);
        chart.saveChartToPng( "D:/ring.png", 800, 600 );        
    }
    
    public static void showXYChart(ChartTools chart) throws Exception {    
        Random random = new Random();
        for( int s = 0; s < 2; s++ ) {
            XYSeries series = new XYSeries("Data" + s);
            for (int i = 0; i < 1000; i++) {
                series.add(random.nextGaussian() * 2.0,random.nextGaussian() * 2.0);
            } 
            chart.addXYData(series); 
        }
        chart.createScatterChart();
        chart.showChart(800,600);
        chart.saveChartToPng( "D:/scatter.png", 800, 600 ); 
    }
    
    public static void main(String[] args) throws Exception {
        String description = "An area chart demonstration.  We use this "
                + "subtitle as an example of what happens when you get a "
                + "really long title or subtitle.";
        ChartTools chart = new ChartTools();
        chart.setTitles( "Chart Demo", "Years", "Values");
        chart.showLegend(true);
        chart.setSubtitle(description);
        chart.setColors(new Color[] { Color.gray, Color.yellow, Color.cyan, Color.red, Color.green, Color.blue});
        
        showCategoryChart(chart);
        showPieChart(chart);
        showXYChart(chart);
    }
}

Output:

















2. Galleries

2.1. Create Scatter Chart
1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
public static void showChart(JFreeChart chart) {
    ChartPanel panel = new ChartPanel(chart);
    panel.setPreferredSize(new Dimension(800, 800));
    panel.setMouseZoomable(false);
    ApplicationFrame f = new ApplicationFrame("Gausscian");
    f.setContentPane(panel);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

public static JFreeChart makeScatterChart(String fileName, int width, int height) {

    String title = "Gausscian";
    String xTitle = "X";
    String yTitle = "Y";
    String description = "An area chart demonstration.  We use this "
            + "subtitle as an example of what happens when you get a "
            + "really long title or subtitle.";
    String dataTitles[] = { "Gausscian_1", "Gausscian_2" };
    int count = 1000;
    Random random = new Random();
    
    XYSeries[] series = { new XYSeries(dataTitles[0]), new XYSeries(dataTitles[1]) };
    for (int i = 0; i < count; i++) {
        series[0].add(random.nextGaussian() * 2.0,random.nextGaussian() * 2.0);
        series[1].add(random.nextGaussian() * 2.0,random.nextGaussian() * 2.0);
    } 
    XYSeriesCollection xySeriesDatasset = new XYSeriesCollection();
    xySeriesDatasset.addSeries(series[0]); 
    xySeriesDatasset.addSeries(series[1]); 

    JFreeChart chart = ChartFactory.createScatterPlot(
        title , xTitle, yTitle
         , xySeriesDatasset         //XYDataset dataset
         , PlotOrientation.VERTICAL //PlotOrientation orientation
         , true                     //boolean legend
         , true                     //boolean tooltips
         , false);                  //boolean urls
     
    chart.setBackgroundPaint(Color.white);
    
    final TextTitle subtitle = new TextTitle(description);
    subtitle.setFont(new Font("SansSerif", Font.PLAIN, 12));
    subtitle.setPosition(RectangleEdge.TOP);
    //subtitle.setSpacer(new Spacer(Spacer.RELATIVE, 0.05, 0.05, 0.05, 0.05));
    subtitle.setVerticalAlignment(VerticalAlignment.BOTTOM);
    chart.addSubtitle(subtitle);    
    
    try {
        ChartUtils.saveChartAsPNG(new File(fileName), chart, width, height);
    } catch (IOException e) {
        e.printStackTrace();
    } 
    
    return chart;
}

public static void main(String[] args) {
    JFreeChart chart = makeScatterChart("D:/scatter.png", 800, 800);
    showChart(chart);
}

Output: scatter.png





2.2. Create Line Chart
1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public static JFreeChart makeLineChart(String fileName, int width, int height) {

    String title = "Gausscian";
    String xTitle = "X";
    String yTitle = "Y";
    
    double[][] categoryData = new double[][] {
        {1.0, 4.0, 3.0, 5.0, 5.0, 7.0, 7.0, 8.0},
        {5.0, 7.0, 6.0, 8.0, 4.0, 4.0, 2.0, 1.0},
        {4.0, 3.0, 2.0, 3.0, 6.0, 3.0, 4.0, 3.0}
    };
    String[] categoryTitles = new String[] { "Data1", "Data2", "Data3" };
    String[] categoryAxises = new String[] { "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008" };

    DefaultCategoryDataset categoryDataset = new DefaultCategoryDataset();
    for( int i = 0; i < categoryTitles.length; i++ ) {
        for( int j = 0; j < categoryAxises.length; j++ ) {
            categoryDataset.addValue( categoryData[i][j], categoryTitles[i], categoryAxises[j]);
        }
    }

    JFreeChart chart = ChartFactory.createLineChart(
            title , xTitle, yTitle
            , categoryDataset
            , PlotOrientation.VERTICAL
            , true
            , true
            , false);

    chart.setBackgroundPaint(Color.white);
    
    try {
        ChartUtils.saveChartAsPNG(new File(fileName), chart, width, height);
    } catch (IOException e) {
        e.printStackTrace();
    } 
    
    return chart;
}


Output: line.png





2.3. Create Area Chart (transparent)
1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public static JFreeChart makeAreaChart(String fileName, int width, int height) {

    String title = "Gausscian";
    String xTitle = "X";
    String yTitle = "Y";
    
    double[][] categoryData = new double[][] {
        {1.0, 4.0, 3.0, 5.0, 5.0, 7.0, 7.0, 8.0},
        {5.0, 7.0, 6.0, 8.0, 4.0, 4.0, 2.0, 1.0},
        {4.0, 3.0, 2.0, 3.0, 6.0, 3.0, 4.0, 3.0}
    };
    String[] categoryTitles = new String[] { "Data1", "Data2", "Data3" };
    String[] categoryAxises = new String[] { "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008" };

    DefaultCategoryDataset categoryDataset = new DefaultCategoryDataset();
    for( int i = 0; i < categoryTitles.length; i++ ) {
        for( int j = 0; j < categoryAxises.length; j++ ) {
            categoryDataset.addValue( categoryData[i][j], categoryTitles[i], categoryAxises[j]);
        }
    }

    JFreeChart chart = ChartFactory.createAreaChart(
            title , xTitle, yTitle
            , categoryDataset
            , PlotOrientation.VERTICAL
            , true
            , true
            , false);

    chart.setBackgroundPaint(Color.white);
    
    final CategoryPlot plot = chart.getCategoryPlot();
    plot.setForegroundAlpha(0.5f);
    //plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
    plot.setBackgroundPaint(Color.lightGray);
    plot.setDomainGridlinesVisible(true);
    plot.setDomainGridlinePaint(Color.white);
    plot.setRangeGridlinesVisible(true);
    plot.setRangeGridlinePaint(Color.white);
    
    try {
        ChartUtils.saveChartAsPNG(new File(fileName), chart, width, height);
    } catch (IOException e) {
        e.printStackTrace();
    } 
    
    return chart;
}


Output: area.png




2.4. Create Stacked Area Chart
1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public static JFreeChart makeStackedAreaChart(String fileName, int width, int height) {

    String title = "Gausscian";
    String xTitle = "X";
    String yTitle = "Y";
    
    double[][] categoryData = new double[][] {
        {1.0, 4.0, 3.0, 5.0, 5.0, 7.0, 7.0, 8.0},
        {5.0, 7.0, 6.0, 8.0, 4.0, 4.0, 2.0, 1.0},
        {4.0, 3.0, 2.0, 3.0, 6.0, 3.0, 4.0, 3.0}
    };
    String[] categoryTitles = new String[] { "Data1", "Data2", "Data3" };
    String[] categoryAxises = new String[] { "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008" };

    DefaultCategoryDataset categoryDataset = new DefaultCategoryDataset();
    for( int i = 0; i < categoryTitles.length; i++ ) {
        for( int j = 0; j < categoryAxises.length; j++ ) {
            categoryDataset.addValue( categoryData[i][j], categoryTitles[i], categoryAxises[j]);
        }
    }

    JFreeChart chart = ChartFactory.createStackedAreaChart(
            title , xTitle, yTitle
            , categoryDataset
            , PlotOrientation.VERTICAL
            , true
            , true
            , false);

    chart.setBackgroundPaint(Color.white);

    try {
        ChartUtils.saveChartAsPNG(new File(fileName), chart, width, height);
    } catch (IOException e) {
        e.printStackTrace();
    } 
    
    return chart;
}

Output: stacked_area.png



2.5. Create Bar Chart

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public static JFreeChart makeBarChart(String fileName, int width, int height) {

    String title = "Gausscian";
    String xTitle = "X";
    String yTitle = "Y";
    
    double[][] categoryData = new double[][] {
        {1.0, 4.0, 3.0, 5.0, 5.0, 7.0, 7.0, 8.0},
        {5.0, 7.0, 6.0, 8.0, 4.0, 4.0, 2.0, 1.0},
        {4.0, 3.0, 2.0, 3.0, 6.0, 3.0, 4.0, 3.0}
    };
    String[] categoryTitles = new String[] { "Data1", "Data2", "Data3" };
    String[] categoryAxises = new String[] { "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008" };

    DefaultCategoryDataset categoryDataset = new DefaultCategoryDataset();
    for( int i = 0; i < categoryTitles.length; i++ ) {
        for( int j = 0; j < categoryAxises.length; j++ ) {
            categoryDataset.addValue( categoryData[i][j], categoryTitles[i], categoryAxises[j]);
        }
    }

    JFreeChart chart = ChartFactory.createBarChart(
            title , xTitle, yTitle
            , categoryDataset
            , PlotOrientation.VERTICAL
            , true
            , true
            , false);

    chart.setBackgroundPaint(Color.white);
    
    try {
        ChartUtils.saveChartAsPNG(new File(fileName), chart, width, height);
    } catch (IOException e) {
        e.printStackTrace();
    } 
    
    return chart;
}

Output: bar.png



2.6. Create Stacked Bar Chart

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public static JFreeChart makeStackedBarChart(String fileName, int width, int height) {

     String title = "Gausscian";
     String xTitle = "X";
     String yTitle = "Y";
        
     double[][] categoryData = new double[][] {
            {1.0, 4.0, 3.0, 5.0, 5.0, 7.0, 7.0, 8.0},
            {5.0, 7.0, 6.0, 8.0, 4.0, 4.0, 2.0, 1.0},
            {4.0, 3.0, 2.0, 3.0, 6.0, 3.0, 4.0, 3.0}
        };
        String[] categoryTitles = new String[] { "Data1", "Data2", "Data3" };
        String[] categoryAxises = new String[] { "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008" };

        DefaultCategoryDataset categoryDataset = new DefaultCategoryDataset();
        for( int i = 0; i < categoryTitles.length; i++ ) {
         for( int j = 0; j < categoryAxises.length; j++ ) {
          categoryDataset.addValue( categoryData[i][j], categoryTitles[i], categoryAxises[j]);
         }
        }

     JFreeChart chart = ChartFactory.createStackedBarChart(
                title , xTitle, yTitle
                , categoryDataset
                , PlotOrientation.VERTICAL
                , true
                , true
                , false);

        chart.setBackgroundPaint(Color.white);
        
        try {
   ChartUtils.saveChartAsPNG(new File(fileName), chart, width, height);
  } catch (IOException e) {
   e.printStackTrace();
  } 
        
        return chart;
    }

Output: stacked_bar.png



2.7. Create horizontal Bar Chart

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public static JFreeChart makeHorizontalBarChart(String fileName, int width, int height) {

    String title = "Gausscian";
    String xTitle = "X";
    String yTitle = "Y";
    
    double[][] categoryData = new double[][] {
        {1.0, 4.0, 3.0, 5.0, 5.0, 7.0, 7.0, 8.0},
        {5.0, 7.0, 6.0, 8.0, 4.0, 4.0, 2.0, 1.0},
        {4.0, 3.0, 2.0, 3.0, 6.0, 3.0, 4.0, 3.0}
    };
    String[] categoryTitles = new String[] { "Data1", "Data2", "Data3" };
    String[] categoryAxises = new String[] { "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008" };

    DefaultCategoryDataset categoryDataset = new DefaultCategoryDataset();
    for( int i = 0; i < categoryTitles.length; i++ ) {
        for( int j = 0; j < categoryAxises.length; j++ ) {
            categoryDataset.addValue( categoryData[i][j], categoryTitles[i], categoryAxises[j]);
        }
    }

    JFreeChart chart = ChartFactory.createBarChart(
            title , xTitle, yTitle
            , categoryDataset
            , PlotOrientation.HORIZONTAL
            , true
            , true
            , false);

    chart.setBackgroundPaint(Color.white);
    
    try {
        ChartUtils.saveChartAsPNG(new File(fileName), chart, width, height);
    } catch (IOException e) {
        e.printStackTrace();
    } 
    
    return chart;
}

Output: horizontal_bar.png




2.8. Create Pie Chart

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public static JFreeChart makePieChart(String fileName, int width, int height) {

    String title = "PieChart";
    
    String[] categoryTitles = new String[] { "Data1", "Data3", "Data5", "Data2", "Data4", "Data6" };
    double[] pieData = new double[] { 43.2, 10.0, 27.5, 17.5, 11.0, 19.4 };
    DefaultPieDataset pieDataset = new DefaultPieDataset();
    for( int i = 0; i < categoryTitles.length; i++ ) {
        pieDataset.setValue( categoryTitles[i], pieData[i]);
    }

    JFreeChart chart = ChartFactory.createPieChart(
            title 
            , pieDataset
            , true
            , true
            , false);

    chart.setBackgroundPaint(Color.white);
    
    try {
        ChartUtils.saveChartAsPNG(new File(fileName), chart, width, height);
    } catch (IOException e) {
        e.printStackTrace();
    } 
    
    return chart;
}

Output: pie.png




2.9. Create Multiple Pie Chart

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public static JFreeChart makeMultiplePieChart(String fileName, int width, int height) {

    String title = "MultiplePieChart";
    
    double[][] categoryData = new double[][] {
        { 43.2, 10.0, 27.5, 17.5, 11.0, 19.4 },
        { 11.0, 19.4, 27.5, 17.5, 43.2, 10.0 },
    };
    String[] categoryTitles = new String[] { "Chart1", "Chart2" };
    String[] categoryAxises = new String[] { "Data1", "Data2", "Data3", "Data4", "Data5", "Data6" };

    DefaultCategoryDataset categoryDataset = new DefaultCategoryDataset();
    for( int i = 0; i < categoryTitles.length; i++ ) {
        for( int j = 0; j < categoryAxises.length; j++ ) {
            categoryDataset.addValue( categoryData[i][j], categoryAxises[j], categoryTitles[i]);
        }
    }
    
    JFreeChart chart = ChartFactory.createMultiplePieChart(
            title,  // chart title
            categoryDataset,               // dataset
            TableOrder.BY_COLUMN,
            true,                  // include legend
            true,
            false
        );

    chart.setBackgroundPaint(Color.white);

    try {
        ChartUtils.saveChartAsPNG(new File(fileName), chart, width, height);
    } catch (IOException e) {
        e.printStackTrace();
    } 
    
    return chart;
}


Output: multiple_pie.png