jfreechart graphics drawing (1)
Code:
package number1; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.labels.StandardXYToolTipGenerator; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; import org.jfree.data.xy.XYDataset; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Font; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; //Generate various graphics with java, such as histogram, curve, bar, pie public class JavaCreateMap { // generate graph private void createQXT(String title, String dataName1, String dataName2, String xtitle, String ytitle,String filePath) { try { XYSeries xyseries = new XYSeries (dataName1); XYSeries xyseries1 = new XYSeries (dataName2); XYSeriesCollection xyseriescollection = new XYSeriesCollection();//数据集 double count1 = 1; double count2 = 1; for (int i = 0; i < 31; i++) {//Indicates that the picture has 31 lines of data String data1 = ""+(i+1); String data2 = ""+(i+2); for (int j = 0; j < 2; j++) {//2 jump data per line if (j == 0) { xyseries.add(count1, Double.parseDouble(data1)); count1++; } if (j == 1) { xyseries1.add(count2, Double.parseDouble(data2)); count2++; } } } xyseriescollection.addSeries(xyseries); xyseriescollection.addSeries(xyseries1); JFreeChart chart = createChart(xyseriescollection, title, xtitle, ytitle); // title, x-axis title, y-axis title chart.setBackgroundPaint(Color.white); // set background color chart.setBorderVisible(false); // set no border XYPlot plot = (XYPlot) chart.getPlot(); //saveChartAsJPEG: Indicates a picture saved in jpeg format ChartUtilities.saveChartAsJPEG(new File(filePath), chart, 800, 500);//宽800,高500 } catch (IOException e) { e.printStackTrace (); } } public static JFreeChart createChart(XYDataset xydataset, String title, String xtitle, String ytitle) { JFreeChart jfreechart = ChartFactory.createXYLineChart(title, xtitle, ytitle, xydataset, PlotOrientation.VERTICAL, true, true, false); XYPlot xyplot = (XYPlot) jfreechart.getPlot(); XYLineAndShapeRenderer xylineandshaperenderer = new XYLineAndShapeRenderer (); xylineandshaperenderer.setSeriesStroke(0, new BasicStroke(2.0F, 1, 1, 1.0F, new float[] { 6F, 6F }, 0.0F)); xylineandshaperenderer.setSeriesStroke(1, new BasicStroke(2.0F, 1, 1, 1.0F, null, 0.0F)); xylineandshaperenderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator()); xylineandshaperenderer.setItemLabelFont(new Font("Black Body", Font.TRUETYPE_FONT, 24));// Set the font xyplot.setRenderer (xylineandshaperenderer); NumberAxis numberaxis = (NumberAxis) xyplot.getRangeAxis(); numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); return jfreechart; } public static void main(String[] args) { JavaCreateMap jcm = new JavaCreateMap(); String title="**Company Mining Completion"; String xtitle = "October 2012";// X-axis title String ytitle = "Completion";// Y-axis title String dataName1 = "Plan";//The meaning of the data String dataName2 = "Complete";//The meaning of the data String filePath = "D:\\aa.jpeg"; jcm.createQXT (title, dataName1, dataName2, xtitle, ytitle, filePath); System.out.println("------Generate picture completed"+filePath+"------"); } }
Effect: