jfreechart畫折線圖的方法
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)的值,顯示為省略號(hà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ù)來生成測(cè)試結(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, //圖表對(duì)象
1024, //圖像寬度 ,這個(gè)將決定圖表的橫坐標(biāo)值是否能完全顯示還是顯示省略號(hà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é)果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springboot+mybatis快速插入大量數(shù)據(jù)的具體實(shí)現(xiàn)
最近導(dǎo)入表格數(shù)據(jù)時(shí)需要同時(shí)插入修改大量數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于springboot+mybatis快速插入大量數(shù)據(jù)的具體實(shí)現(xiàn),文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
spring boot整合RabbitMQ實(shí)例詳解(Fanout模式)
這篇文章主要介紹了spring boot整合RabbitMQ的實(shí)例講解(Fanout模式),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-04-04
springboot 項(xiàng)目容器啟動(dòng)后如何自動(dòng)執(zhí)行指定方法
這篇文章主要介紹了springboot 項(xiàng)目容器啟動(dòng)后如何自動(dòng)執(zhí)行指定方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
java并發(fā)編程JUC CountDownLatch線程同步
這篇文章主要介紹CountDownLatch是什么、CountDownLatch 如何工作、CountDownLatch 的代碼例子來展開對(duì)java并發(fā)編程JUC CountDownLatch線程同步,需要的朋友可以參考下面文章內(nèi)容2021-09-09
SpringSecurity+OAuth2.0?搭建認(rèn)證中心和資源服務(wù)中心流程分析
OAuth?2.0?主要用于在互聯(lián)網(wǎng)上安全地委托授權(quán),廣泛應(yīng)用于身份驗(yàn)證和授權(quán)場(chǎng)景,這篇文章介紹SpringSecurity+OAuth2.0?搭建認(rèn)證中心和資源服務(wù)中心,感興趣的朋友一起看看吧2024-01-01
java實(shí)現(xiàn)MD5加密算法的實(shí)例代碼
這篇文章主要介紹了java實(shí)現(xiàn)MD5加密算法的實(shí)例代碼,有需要的朋友可以參考一下2013-12-12
SpringBoot Application注解原理及代碼詳解
這篇文章主要介紹了SpringBoot Application注解原理及代碼詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
idea中項(xiàng)目前端網(wǎng)頁(yè)圖標(biāo)不顯示的原因及解決
這篇文章主要介紹了idea中項(xiàng)目前端網(wǎng)頁(yè)圖標(biāo)不顯示的原因及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
Java微信公眾平臺(tái)開發(fā)(4) 回復(fù)消息的分類及實(shí)體的創(chuàng)建
這篇文章主要為大家詳細(xì)介紹了Java微信公眾平臺(tái)開發(fā)第四步,回復(fù)消息的分類及實(shí)體的創(chuàng)建,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04

