1.라인 그래프
CombinedDomainXYPlot plot = new CombinedDomainXYPlot(new DateAxis("Time")); plot.setBackgroundPaint(Color.lightGray); plot.setDomainGridlinePaint(Color.white); plot.setRangeGridlinePaint(Color.white); plot.setForegroundAlpha(0.5f); plot.setBackgroundAlpha(0.5f); TimeSeriesCollection dataset = new TimeSeriesCollection(); dataset.addSeries(new TimeSeries("Speed1", Millisecond.class)); dataset.addSeries(new TimeSeries("Speed2", Millisecond.class)); NumberAxis axisY = new NumberAxis("ms"); axisY.setAutoRangeIncludesZero(false); axisY.setRange(0, 100); XYPlot subplot = new XYPlot(dataset, null, axisY, new StandardXYItemRenderer()); subplot.setBackgroundPaint(Color.lightGray); subplot.setDomainGridlinePaint(Color.white); subplot.setRangeGridlinePaint(Color.white); plot.add(subplot); // 시작지점을 0 으로 초기화 Calendar cal = Calendar.getInstance(); cal.setTime(now.getTime()); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); dataset.getSeries(0).add(new Millisecond(cal.getTime()), 0); dataset.getSeries(1).add(new Millisecond(cal.getTime()), 0); // 아래와 같이 datas에 데이터를 그래프로써 추가 for (int i=0; i<datas.length; i++) { dataset.getSeries(0).add(new Millisecond(now.getTime()), datas[i].value1); dataset.getSeries(1).add(new Millisecond(now.getTime()), datas[i].value2); } // JFreeChart로 그래프 본체 생성 JFreeChart chart = new JFreeChart("GRAPH TITLE", plot); chart.setBorderPaint(Color.black); chart.setBorderVisible(true); chart.setBackgroundPaint(Color.white); // 범례 LegendTitle legend = chart.getLegend(); legend.setBackgroundPaint(Color.white); legend.setMargin(10d, 10d, 10d, 10d); // 이미지 파일로 저장하기 File pngFile = new File("output.png"); ChartUtilities.saveChartAsPNG(pngFile, chart, 800, 600);
2.원형 그래프
DefaultPieDataset pieDataset = new DefaultPieDataset(); Set<String> keys = new TreeSet<String>(countByOSType.keySet()); for (String k : keys) { pieDataset.setValue(k, new Double(countByOSType.get(k))); } JFreeChart pieChart = ChartFactory.createPieChart3D("GRAPH TITLE", pieDataset, true, true, false); /* * // 3D PIE PiePlot3D localPiePlot3D = (PiePlot3D)pieChart.getPlot(); * localPiePlot3D.setDarkerSides(true); * localPiePlot3D.setStartAngle(290.0D); * localPiePlot3D.setDirection(Rotation.CLOCKWISE); * localPiePlot3D.setForegroundAlpha(0.5F); */ // 2D PIE pieChart.setBackgroundPaint(new GradientPaint(new Point(0, 0), new Color(20, 20, 20), new Point(400, 200), Color.DARK_GRAY)); // GRAPH TITLE TextTitle graphTitle = pieChart.getTitle(); graphTitle.setHorizontalAlignment(HorizontalAlignment.CENTER); graphTitle.setPaint(new Color(240, 240, 240)); graphTitle.setFont(new Font("Meiryo UI", 1, 26)); // GRAPH VISUAL PiePlot localPiePlot = (PiePlot) pieChart.getPlot(); localPiePlot.setBackgroundPaint(null); localPiePlot.setInteriorGap(0.04D); localPiePlot.setOutlineVisible(false); // localPiePlot.setSectionPaint("Android", new RadialGradientPaint(new // Point2D.Float(0.0F, 0.0F), 200.0F, new float[]{ 0.0F, 1.0F }, new // Color[] { new Color(200, 200, 255), Color.BLUE })); // localPiePlot.setSectionPaint("iPhone", new RadialGradientPaint(new // Point2D.Float(0.0F, 0.0F), 200.0F, new float[]{ 0.0F, 1.0F }, new // Color[] { new Color(200, 200, 255), Color.RED })); localPiePlot.setBaseSectionOutlinePaint(Color.WHITE); localPiePlot.setSectionOutlinesVisible(true); localPiePlot.setBaseSectionOutlineStroke(new BasicStroke(2.0F)); // LABEL PieSectionLabelGenerator generator = new StandardPieSectionLabelGenerator("{0}: {1} ({2})", new DecimalFormat("0"), new DecimalFormat("0.0%")); localPiePlot.setLabelGenerator(generator); localPiePlot.setLabelFont(new Font("Meiryo UI", 1, 15)); localPiePlot.setLabelLinkPaint(Color.WHITE); // localPiePlot.setLabelLinkStroke(new BasicStroke(2.0F)); localPiePlot.setLabelOutlineStroke(null); localPiePlot.setLabelPaint(Color.WHITE); localPiePlot.setLabelBackgroundPaint(null); // GRAPH COMMENT TextTitle graphCommentText = new TextTitle("Source: http://jo.centis1504.net", new Font("Meiryo UI", 0, 12)); graphCommentText.setPaint(Color.WHITE); graphCommentText.setPosition(RectangleEdge.BOTTOM); graphCommentText.setHorizontalAlignment(HorizontalAlignment.RIGHT); pieChart.addSubtitle(graphCommentText); // save File pngFile = new File("output.png"); ChartUtilities.saveChartAsPNG(pngFile, pieChart, 800, 600);
文字化けしないようにする
ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());]]>