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); } }


更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
