亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

jfreechart畫折線圖的方法

 更新時(shí)間:2018年06月19日 11:26:37   作者:yuhentian  
這篇文章主要為大家詳細(xì)介紹了jfreechart畫折線圖的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

JFreeChart 是開放源代碼站點(diǎn)SourceForge.net 上的一個(gè) JAVA 項(xiàng)目,它主要用來各種各樣的圖表,這些圖表包括:餅圖、柱狀圖 ( 普通柱狀圖以及堆棧柱狀圖 )、線圖、區(qū)域圖、分布圖、混合圖、甘特圖以及一些儀表盤等等。

應(yīng)用jfreechart來畫圖需要兩個(gè)jar包:jfreechart.jar和jcommon.jar,下載地址。

下面是一個(gè)畫折線圖的例子:

package yuth.jfree.demo; 
/** 
 * 使用 categoryDataset 數(shù)據(jù)集創(chuàng)建折線圖 
 * 當(dāng)數(shù)據(jù)多時(shí),在JPanel中無法完全看到橫坐標(biāo)的值,顯示為省略號。 
 * 解決方法: 
 * 方法1、將報(bào)表保存為圖片時(shí),設(shè)置圖片的寬度足夠大(2000或3000),圖片可以顯示橫坐標(biāo)值。 
 *  這種方法治標(biāo)不治本,所以有了第2種方法 
 * 方法2、設(shè)置X軸上的Lable讓其45度傾斜。 
 */ 
import java.awt.BasicStroke; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Font; 
import java.awt.geom.Ellipse2D; 
import java.io.File; 
import java.io.IOException; 
import java.net.URL; 
import java.text.DecimalFormat; 
import java.util.Random; 
 
import javax.swing.ImageIcon; 
import javax.swing.JPanel; 
import org.jfree.chart.ChartFactory; 
import org.jfree.chart.ChartPanel; 
import org.jfree.chart.ChartUtilities; 
import org.jfree.chart.JFreeChart; 
import org.jfree.chart.axis.CategoryAxis; 
import org.jfree.chart.axis.CategoryLabelPositions; 
import org.jfree.chart.axis.NumberAxis; 
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; 
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.CategoryDataset; 
import org.jfree.data.category.DefaultCategoryDataset; 
import org.jfree.ui.ApplicationFrame; 
import org.jfree.ui.HorizontalAlignment; 
import org.jfree.ui.RectangleEdge; 
import org.jfree.ui.RectangleInsets; 
import org.jfree.ui.RefineryUtilities; 
 
public class LineChartDemo1 extends ApplicationFrame { 
 
 private static final long serialVersionUID = -6354350604313079793L; 
 /* synthetic */static Class class$demo$LineChartDemo1; 
 
 public LineChartDemo1(String string) { 
  super(string); 
 JPanel jpanel = createDemoPanel(); 
 jpanel.setPreferredSize(new Dimension(500, 270)); 
  setContentPane(jpanel); 
 } 
 
 
 
 /** 
 * 如何區(qū)分不同的圖例:根據(jù)DefaultCategoryDataset.addValue()的第二個(gè)參數(shù)是否相同來區(qū)分。 
 * 同一個(gè)圖例的數(shù)據(jù)的添加順序影響最終的顯示;不同圖例的數(shù)據(jù)的添加順序不影響最終的顯示。 
 * @return 
 */ 
 private static CategoryDataset createDataset() { 
 DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset(); 
 //defaultcategorydataset.addValue()的參數(shù)解析:(數(shù)值,圖例名,橫坐標(biāo)值) 
/* 
 //添加數(shù)據(jù)方法1 
 //圖例1 
 defaultcategorydataset.addValue(212.0, "First", "1001.0"); 
  defaultcategorydataset.addValue(504.0, "First", "1001.1"); 
 defaultcategorydataset.addValue(1520.0, "First", "1001.2"); 
 
 //圖例2 
 defaultcategorydataset.addValue(712.0, "Second", "1001.0"); 
  defaultcategorydataset.addValue(1204.0, "Second", "1001.1"); 
 defaultcategorydataset.addValue(1820.0, "Second", "1001.2");  
/*/ 
//* 
 //添加數(shù)據(jù)方法2 
 //實(shí)驗(yàn)隨機(jī)數(shù)來生成測試結(jié)果 
 Random random = new Random(12345); 
 
 //圖例1,40個(gè)數(shù)據(jù) 
 for(int i=0;i<40;i++){ 
  defaultcategorydataset.addValue(random.nextInt(100000), 
   "First",String.valueOf(1000+i) ); 
 } 
 //圖例2,40個(gè)數(shù)據(jù) 
 for (int i = 0; i < 40; i++) { 
  defaultcategorydataset.addValue(random.nextInt(100000), 
   "Second", String.valueOf(1000 + i)); 
 } 
//*/  
  
 return defaultcategorydataset; 
 } 
 
 private static JFreeChart createChart(CategoryDataset categorydataset) { 
 JFreeChart jfreechart = ChartFactory.createLineChart( 
  "jfreechart test",// 圖表標(biāo)題 
  "X", // 主軸標(biāo)簽(x軸) 
  "Y",// 范圍軸標(biāo)簽(y軸) 
  categorydataset, // 數(shù)據(jù)集 
  PlotOrientation.VERTICAL,// 方向 
  false, // 是否包含圖例 
  true, // 提示信息是否顯示 
  false);// 是否使用urls 
  
 // 改變圖表的背景顏色 
 jfreechart.setBackgroundPaint(Color.white); 
  
 CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot(); 
 categoryplot.setBackgroundPaint(Color.lightGray); 
 categoryplot.setRangeGridlinePaint(Color.white); 
 categoryplot.setRangeGridlinesVisible(false); 
 
 //修改范圍軸。 我們將默認(rèn)刻度值 (允許顯示小數(shù)) 改成只顯示整數(shù)的刻度值。 
 NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis(); 
 numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 
  
 // 設(shè)置X軸上的Lable讓其45度傾斜 
 CategoryAxis domainAxis = categoryplot.getDomainAxis(); 
 domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // 設(shè)置X軸上的Lable讓其45度傾斜 
  domainAxis.setLowerMargin(0.0); // 設(shè)置距離圖片左端距離 
  domainAxis.setUpperMargin(0.0); // 設(shè)置距離圖片右端距離 
  
  
  
 LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot 
  .getRenderer(); 
 lineandshaperenderer.setShapesVisible(true); 
 lineandshaperenderer.setDrawOutlines(true); 
 lineandshaperenderer.setUseFillPaint(true); 
 lineandshaperenderer.setBaseFillPaint(Color.white); 
 lineandshaperenderer.setSeriesStroke(0, new BasicStroke(3.0F)); 
 lineandshaperenderer.setSeriesOutlineStroke(0, new BasicStroke(2.0F)); 
 lineandshaperenderer.setSeriesShape(0, new Ellipse2D.Double(-5.0, -5.0, 
  10.0, 10.0)); 
 lineandshaperenderer.setItemMargin(0.4); //設(shè)置x軸每個(gè)值的間距(不起作用??) 
  
 // 顯示數(shù)據(jù)值 
 DecimalFormat decimalformat1 = new DecimalFormat("##.##");// 數(shù)據(jù)點(diǎn)顯示數(shù)據(jù)值的格式 
 lineandshaperenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator( 
  "{2}", decimalformat1));// 設(shè)置數(shù)據(jù)項(xiàng)標(biāo)簽的生成器 
 lineandshaperenderer.setBaseItemLabelsVisible(true);// 基本項(xiàng)標(biāo)簽顯示 
 lineandshaperenderer.setBaseShapesFilled(true);// 在數(shù)據(jù)點(diǎn)顯示實(shí)心的小圖標(biāo) 
   
  
 return jfreechart; 
 } 
 
 public static JPanel createDemoPanel() { 
 JFreeChart jfreechart = createChart(createDataset()); 
 
 try { 
  ChartUtilities.saveChartAsJPEG( 
   new File("D:/LineChartDemo1.png"), //文件保存物理路徑包括路徑和文件名 
   // 1.0f, //圖片質(zhì)量 ,0.0f~1.0f 
   jfreechart, //圖表對象 
   1024, //圖像寬度 ,這個(gè)將決定圖表的橫坐標(biāo)值是否能完全顯示還是顯示省略號 
   768); 
 } catch (IOException e) { 
  // TODO Auto-generated catch block 
  e.printStackTrace(); 
 } //圖像高度 
   
 return new ChartPanel(jfreechart); 
 } 
 
 public static void main(String[] strings) { 
 LineChartDemo1 linechartdemo1 = new LineChartDemo1( 
  "JFreeChart - Line Chart Demo 1"); 
 linechartdemo1.pack(); 
 RefineryUtilities.centerFrameOnScreen(linechartdemo1); 
 linechartdemo1.setVisible(true); 
 } 
 
 /* synthetic */ 
 static Class class$(String string) { 
 Class var_class; 
 try { 
  var_class = Class.forName(string); 
 } catch (ClassNotFoundException classnotfoundexception) { 
  throw new NoClassDefFoundError(classnotfoundexception.getMessage()); 
 } 
 return var_class; 
 } 
} 

運(yùn)行結(jié)果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論