package com.langhua.line; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Font; import java.io.FileOutputStream; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.CategoryAxis; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.axis.ValueAxis; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.category.LineAndShapeRenderer; import org.jfree.chart.title.TextTitle; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.ui.HorizontalAlignment; import org.jfree.ui.RectangleEdge; /** * 线状图形创建 * @author Administrator * */ public class Line { public static void main(String[] args) { //数据 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.addValue(212, "Classes类", "JDK 1.0版本"); dataset.addValue(504, "Classes类", "JDK 1.1版本"); dataset.addValue(1520, "Classes类", "SDK 1.2版本"); dataset.addValue(1842, "Classes类", "SDK 1.3版本"); dataset.addValue(2991, "Classes类", "SDK 1.4版本"); JFreeChart chart = ChartFactory.createLineChart( "Java Standard Class Library","Release版本","Class数量",dataset,PlotOrientation.VERTICAL,false, true,false ); //配置chart Line.configFont(chart); //设置标题 TextTitle t = new TextTitle("Number of Classes By Release"); t.setFont(new Font("Comic Sans MS", Font.PLAIN, 15)); chart.addSubtitle(t); //设置标题,并把标题放到最下面的右边 TextTitle source = new TextTitle("Source: Java In A Nutshell (4th Edition) by David Flanagan (O’Reilly)"); source.setFont(new Font("Comic Sans MS", Font.PLAIN, 15)); //放到底部 source.setPosition(RectangleEdge.BOTTOM); //放到右边 source.setHorizontalAlignment(HorizontalAlignment.RIGHT); //添加 chart.addSubtitle(source); //设置背影色 chart.setBackgroundPaint(Color.white); CategoryPlot plot = (CategoryPlot)chart.getPlot(); //设置网格线 plot.setRangeGridlinePaint(Color.white); // customise the range axis...(这个我不知道是啥子) NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); //设置线与线之间的连结点的样式,比如说是否空心,颜色等 LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot .getRenderer(); renderer.setShapesVisible(true); renderer.setDrawOutlines(true); renderer.setUseFillPaint(true); renderer.setFillPaint(Color.yellow); FileOutputStream fos_jpg = null; try { fos_jpg = new FileOutputStream("c:\\Line.jpg"); ChartUtilities.writeChartAsJPEG(fos_jpg,0.99f,chart,800,600, null); fos_jpg.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 解决乱码和样式等问题 * @param chart */ private static void configFont(JFreeChart chart) { // 配置字体 Font xfont = new Font("宋体", Font.CENTER_BASELINE, 12);// X轴 Font yfont = new Font("宋体", Font.CENTER_BASELINE, 20);// Y轴 Font kfont = new Font("宋体", Font.CENTER_BASELINE, 18);// 底部 Font titleFont = new Font("微软雅黑", Font.CENTER_BASELINE, 25); // 图片标题 CategoryPlot plot = chart.getCategoryPlot();// 图形的绘制结构对象 //数据轴网格线条颜色 plot.setRangeGridlinePaint(Color.BLUE); //数据轴网格线条笔触 plot.setRangeGridlineStroke(new BasicStroke(1.0f)); // 图片标题 chart.setTitle(new TextTitle(chart.getTitle().getText(),titleFont)); //X轴 CategoryAxis domainAxis = plot.getDomainAxis(); //设置X轴标题字体 domainAxis.setLabelFont(xfont); //设置X轴字体 domainAxis.setTickLabelFont(xfont); //设置字体颜色 domainAxis.setTickLabelPaint(Color.BLUE); //横轴上的label斜显示 //domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); //domainAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45); //分类轴边距,同种类型之间的距离,比如说Row 1之间的 //domainAxis.setCategoryMargin(0.2f); //分类轴下(左)边距,就是离左边的距离 domainAxis.setLowerMargin(0.1); //分类轴下(右)边距,就是离最右边的距离 domainAxis.setUpperMargin(0.1); //Y 轴 ValueAxis rangeAxis = plot.getRangeAxis(); //设置Y轴标题字体 rangeAxis.setLabelFont(yfont); //设置Y轴字体 rangeAxis.setTickLabelFont(yfont); // 字体颜色 rangeAxis.setLabelPaint(Color.RED); } }
基于XY的Line
package com.langhua.line; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Font; import java.io.FileOutputStream; 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.axis.ValueAxis; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; import org.jfree.chart.title.TextTitle; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; import org.jfree.ui.RectangleInsets; /** * 基于XY的Line * @author Administrator * */ public class XYLine { public static void main(String[] args){ XYSeries series1 = new XYSeries("第一"); series1.add(1.0, 1.0); series1.add(2.0, 4.0); series1.add(3.0, 3.0); series1.add(4.0, 5.0); series1.add(5.0, 5.0); series1.add(6.0, 7.0); series1.add(7.0, 7.0); series1.add(8.0, 8.0); XYSeries series2 = new XYSeries("第二"); series2.add(1.0, 5.0); series2.add(2.0, 7.0); series2.add(3.0, 6.0); series2.add(4.0, 8.0); series2.add(5.0, 4.0); series2.add(6.0, 4.0); series2.add(7.0, 2.0); series2.add(8.0, 1.0); series2.add(9.0, 1.0); series2.add(10.0, 1.0); series2.add(8.1, 1.2); series2.add(8.2, 1.3); series2.add(8.5, 1.4); series2.add(11.0, 1.0); XYSeries series3 = new XYSeries("第三"); series3.add(3.0, 4.0); series3.add(4.0, 3.0); series3.add(5.0, 2.0); series3.add(6.0, 3.0); series3.add(7.0, 6.0); series3.add(8.0, 3.0); series3.add(9.0, 4.0); series3.add(10.0, 3.0); XYSeries series4 = new XYSeries("第四"); series4.add(2.0, 4.0); series4.add(3.0, 3.0); series4.add(6.0, 2.0); series4.add(1.0, 3.0); series4.add(2.0, 6.0); XYSeriesCollection dataset = new XYSeriesCollection(); dataset.addSeries(series1); dataset.addSeries(series2); dataset.addSeries(series3); dataset.addSeries(series4); JFreeChart chart = ChartFactory.createXYLineChart("Line Chart例子","X坐标","Y坐标",dataset,PlotOrientation.VERTICAL,true,true,false ); XYLine.configFont(chart); // 背景色 chart.setBackgroundPaint(Color.white); // 获得XYPlot对象 XYPlot plot = (XYPlot) chart.getPlot(); //设置坐标后面的背景色 plot.setBackgroundPaint(Color.lightGray); plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0)); plot.setDomainGridlinePaint(Color.white); plot.setRangeGridlinePaint(Color.white); XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer(); renderer.setShapesVisible(true); renderer.setShapesFilled(true); // change the auto tick unit selection to integer units only... NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); FileOutputStream fos_jpg = null; try { fos_jpg = new FileOutputStream("c:\\LineXY.jpg"); ChartUtilities.writeChartAsJPEG(fos_jpg,0.99f,chart,800,600, null); fos_jpg.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 配置字体等信息 * @param chart */ private static void configFont(JFreeChart chart) { // 配置字体 Font xfont = new Font("宋体", Font.CENTER_BASELINE, 12);// X轴 Font yfont = new Font("宋体", Font.CENTER_BASELINE, 20);// Y轴 Font kfont = new Font("宋体", Font.CENTER_BASELINE, 18);// 底部 Font titleFont = new Font("微软雅黑", Font.CENTER_BASELINE, 25); // 图片标题 XYPlot plot = (XYPlot) chart.getPlot();// 图形的绘制结构对象 //数据轴网格线条颜色 plot.setRangeGridlinePaint(Color.BLUE); //数据轴网格线条笔触 plot.setRangeGridlineStroke(new BasicStroke(1.0f)); // 图片标题 chart.setTitle(new TextTitle(chart.getTitle().getText(),titleFont)); chart.getLegend().setItemFont(kfont); //X轴 ValueAxis domainAxis = plot.getDomainAxis(); //设置X轴标题字体 domainAxis.setLabelFont(xfont); //设置X轴字体 domainAxis.setTickLabelFont(xfont); //设置字体颜色 domainAxis.setTickLabelPaint(Color.BLUE); //横轴上的label斜显示 //domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); //domainAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45); //分类轴边距,同种类型之间的距离,比如说Row 1之间的 //domainAxis.setCategoryMargin(0.2f); //分类轴下(左)边距,就是离左边的距离 domainAxis.setLowerMargin(0.1); //分类轴下(右)边距,就是离最右边的距离 domainAxis.setUpperMargin(0.1); //Y 轴 ValueAxis rangeAxis = plot.getRangeAxis(); //设置Y轴标题字体 rangeAxis.setLabelFont(yfont); //设置Y轴字体 rangeAxis.setTickLabelFont(yfont); // 字体颜色 rangeAxis.setLabelPaint(Color.RED); } }

