JFreeChart在制作折線圖


JFreeChart在制作折線圖的時候可以使用兩種不同的方式

 

package Line;

import java.awt.Color;
import java.awt.Font;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset; import org.jfree.data.category.DefaultCategoryDataset; public class Line {  public static void main(String[] args) {   StandardChartTheme mChartTheme = new StandardChartTheme("CN");   mChartTheme.setLargeFont(new Font("黑體", Font.BOLD, 20));   mChartTheme.setExtraLargeFont(new Font("宋體", Font.PLAIN, 15));   mChartTheme.setRegularFont(new Font("宋體", Font.PLAIN, 15));   ChartFactory.setChartTheme(mChartTheme);   CategoryDataset mDataset = GetDataset();   JFreeChart mChart = ChartFactory.createLineChart(     "折線圖",//圖名字     "年份",//橫坐標     "數量",//縱坐標     mDataset,//數據集     PlotOrientation.VERTICAL,     true, // 顯示圖例     true, // 采用標准生成器      false);// 是否生成超鏈接     CategoryPlot mPlot = (CategoryPlot)mChart.getPlot();   mPlot.setBackgroundPaint(Color.LIGHT_GRAY);   mPlot.setRangeGridlinePaint(Color.BLUE);//背景底部橫虛線   mPlot.setOutlinePaint(Color.RED);//邊界線     ChartFrame mChartFrame = new ChartFrame("折線圖", mChart);   mChartFrame.pack();   mChartFrame.setVisible(true);  }  public static CategoryDataset GetDataset()  {   DefaultCategoryDataset mDataset = new DefaultCategoryDataset();   mDataset.addValue(1, "First", "2013");   mDataset.addValue(3, "First", "2014");   mDataset.addValue(2, "First", "2015");   mDataset.addValue(6, "First", "2016");   mDataset.addValue(5, "First", "2017");   mDataset.addValue(12, "First", "2018");   mDataset.addValue(14, "Second", "2013");   mDataset.addValue(13, "Second", "2014");   mDataset.addValue(12, "Second", "2015");   mDataset.addValue(9, "Second", "2016");   mDataset.addValue(5, "Second", "2017");   mDataset.addValue(7, "Second", "2018");   return mDataset;  } }


第二種方式:

 

package Line;

import java.awt.Font; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartFrame; import org.jfree.chart.JFreeChart; import org.jfree.chart.StandardChartTheme; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; public class XYLine {  public static void main(String[] args) {   StandardChartTheme mChartTheme = new StandardChartTheme("CN");   mChartTheme.setLargeFont(new Font("黑體", Font.BOLD, 20));   mChartTheme.setExtraLargeFont(new Font("宋體", Font.PLAIN, 15));   mChartTheme.setRegularFont(new Font("宋體", Font.PLAIN, 15));   ChartFactory.setChartTheme(mChartTheme);   XYSeriesCollection mCollection = GetCollection();   JFreeChart mChart = ChartFactory.createXYLineChart(     "折線圖",     "X",     "Y",     mCollection,     PlotOrientation.VERTICAL,     true,     true,     false);   ChartFrame mChartFrame = new ChartFrame("折線圖", mChart);   mChartFrame.pack();   mChartFrame.setVisible(true);  }  public static XYSeriesCollection GetCollection()  {   XYSeriesCollection mCollection = new XYSeriesCollection();   XYSeries mSeriesFirst = new XYSeries("First");   mSeriesFirst.add(1.0D, 1.0D);  mSeriesFirst.add(2D, 4D);  mSeriesFirst.add(3D, 3D);  mSeriesFirst.add(4D, 5D);  mSeriesFirst.add(5D, 5D);  mSeriesFirst.add(6D, 7D);  mSeriesFirst.add(7D, 7D);  mSeriesFirst.add(8D, 8D);  XYSeries mSeriesSecond = new XYSeries("Second");  mSeriesSecond.add(1.0D, 5D);  mSeriesSecond.add(2D, 7D);  mSeriesSecond.add(3D, 6D);  mSeriesSecond.add(4D, 8D);  mSeriesSecond.add(5D, 4D);  mSeriesSecond.add(6D, 4D);  mSeriesSecond.add(7D, 2D);  mSeriesSecond.add(8D, 1.0D);   mCollection.addSeries(mSeriesFirst);   mCollection.addSeries(mSeriesSecond);   return mCollection;  } }


 

 

 

  1.   String sql = "select count(id) num, DATE_FORMAT(calltime, '%Y年%m月') ym,modulename mn from  tongji t group by DATE_FORMAT(calltime, '%Y年%m月'),mn";  
  2.         List list = getList(sql);  
  3.         // 繪圖數據集  
  4.         DefaultCategoryDataset dataSet = new DefaultCategoryDataset();  
  5.         for (Object obj : list) {  
  6.             Map<String, Object> map = (Map) obj;  
  7.             dataSet.setValue((Long) map.get("num"), (String) map.get("mn"), map.get("ym").toString());  
  8.         }  
  9.         //如果把createLineChart改為createLineChart3D就變為了3D效果的折線圖       
  10.         JFreeChart  chart = ChartFactory.createLineChart("圖表標題", "X軸標題", "Y軸標題", dataSet,  
  11.                 PlotOrientation.VERTICAL, // 繪制方向  
  12.                 true, // 顯示圖例  
  13.                 true, // 采用標准生成器  
  14.                 false // 是否生成超鏈接  
  15.                 );  
  16.         chart.getTitle().setFont(titleFont); // 設置標題字體  
  17.         chart.getLegend().setItemFont(font);// 設置圖例類別字體  
  18.         chart.setBackgroundPaint(bgColor);// 設置背景色   
  19.         //獲取繪圖區對象  
  20.         CategoryPlot plot = chart.getCategoryPlot();  
  21.         plot.setBackgroundPaint(Color.LIGHT_GRAY); // 設置繪圖區背景色  
  22.         plot.setRangeGridlinePaint(Color.WHITE); // 設置水平方向背景線顏色  
  23.         plot.setRangeGridlinesVisible(true);// 設置是否顯示水平方向背景線,默認值為true  
  24.         plot.setDomainGridlinePaint(Color.WHITE); // 設置垂直方向背景線顏色  
  25.         plot.setDomainGridlinesVisible(true); // 設置是否顯示垂直方向背景線,默認值為false  
  26.           
  27.           
  28.         CategoryAxis domainAxis = plot.getDomainAxis();     
  29.         domainAxis.setLabelFont(font); // 設置橫軸字體  
  30.         domainAxis.setTickLabelFont(font);// 設置坐標軸標尺值字體  
  31.         domainAxis.setLowerMargin(0.01);// 左邊距 邊框距離  
  32.         domainAxis.setUpperMargin(0.06);// 右邊距 邊框距離,防止最后邊的一個數據靠近了坐標軸。  
  33.         domainAxis.setMaximumCategoryLabelLines(2);  
  34.           
  35.         ValueAxis rangeAxis = plot.getRangeAxis();  
  36.         rangeAxis.setLabelFont(font);   
  37.         rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());//Y軸顯示整數  
  38.         rangeAxis.setAutoRangeMinimumSize(1);   //最小跨度  
  39.         rangeAxis.setUpperMargin(0.18);//上邊距,防止最大的一個數據靠近了坐標軸。     
  40.         rangeAxis.setLowerBound(0);   //最小值顯示0  
  41.         rangeAxis.setAutoRange(false);   //不自動分配Y軸數據  
  42.         rangeAxis.setTickMarkStroke(new BasicStroke(1.6f));     // 設置坐標標記大小  
  43.         rangeAxis.setTickMarkPaint(Color.BLACK);     // 設置坐標標記顏色  
  44.   
  45.           
  46.           
  47.      // 獲取折線對象  
  48.         LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();  
  49.         BasicStroke realLine = new BasicStroke(1.8f); // 設置實線  
  50.         // 設置虛線  
  51.         float dashes[] = { 5.0f };   
  52.         BasicStroke brokenLine = new BasicStroke(2.2f, // 線條粗細  
  53.                 BasicStroke.CAP_ROUND, // 端點風格  
  54.                 BasicStroke.JOIN_ROUND, // 折點風格  
  55.                 8f, dashes, 0.6f);   
  56.         for (int i = 0; i < dataSet.getRowCount(); i++) {  
  57.             if (i % 2 == 0)  
  58.                 renderer.setSeriesStroke(i, realLine); // 利用實線繪制  
  59.             else  
  60.                 renderer.setSeriesStroke(i, brokenLine); // 利用虛線繪制  
  61.         }  
  62.           
  63.         plot.setNoDataMessage("無對應的數據,請重新查詢。");  
  64.         plot.setNoDataMessageFont(titleFont);//字體的大小  
  65.         plot.setNoDataMessagePaint(Color.RED);//字體顏色  

 平面折線圖效果

 

 

3D折線圖效果:

 

 

設置線條、數據點顏色和顯示屬性

CategoryPlot mPlot = (CategoryPlot)mChart.getPlot();
mPlot.setBackgroundPaint(Color.WHITE);// 設置繪圖區背景色
mPlot.setRangeGridlinePaint(Color.BLUE);//背景底部橫虛線
mPlot.setOutlinePaint(Color.RED);//邊界線
mPlot.setDomainGridlinePaint(Color.BLUE); // 設置垂直方向背景線顏色
mPlot.setDomainGridlinesVisible(true); // 設置是否顯示垂直方向背景線,默認值為false

LineAndShapeRenderer lasp = (LineAndShapeRenderer) mPlot.getRenderer();// 獲取顯示線條的對象
lasp.setBaseShapesVisible(true);// 設置拐點是否可見/是否顯示拐點
lasp.setDrawOutlines(true);// 設置拐點不同用不同的形狀
lasp.setUseFillPaint(true);// 設置線條是否被顯示填充顏色
lasp.setBaseFillPaint(Color.BLACK);//// 設置拐點顏色
//lasp.setSeriesPaint(series, paint);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM