Java繪圖庫JFreeChart的使用教程
前言
圖表是一種以簡單方式顯示信息的圖形,通常使用直線和曲線來顯示金額。 JJFreeChart 由 David Gilbert 于 2000 年創(chuàng)立。如今,JFreeChart 是 Java 開發(fā)人員中使用最廣泛的圖表庫。
JFreeChart 允許創(chuàng)建各種交互式和非交互式圖表;可以廣泛地定制; 它允許修改圖表項目的顏色和繪制,圖例,線條或標(biāo)記的樣式。 它會自動繪制軸刻度和圖例??梢詣?chuàng)建折線圖,條形圖,面積圖,散點圖,餅圖,甘特圖和各種專用圖,例如風(fēng)向圖或氣泡圖。它支持多種輸出格式,包括 PNG,JPEG,PDF 和 SVG。
相關(guān)jar包:JFreeChart繪圖包
類級結(jié)構(gòu)構(gòu)解釋了庫中的各種類如何相互交互以創(chuàng)建各種類型的圖表。下圖為JFreeChart的類級結(jié)構(gòu):

| Units | Description |
|---|---|
| File | 具有用戶輸入的源,用于在文件中創(chuàng)建數(shù)據(jù)集。 |
| Database | 具有用戶輸入的源,用于在數(shù)據(jù)庫中創(chuàng)建數(shù)據(jù)集。 |
| Create Dataset | 接受數(shù)據(jù)集并將數(shù)據(jù)集存儲到數(shù)據(jù)集對象中。 |
| General Dataset | 這種類型的數(shù)據(jù)集主要用于餅圖。 |
| Category Dataset | 此類數(shù)據(jù)集用于條形圖、折線圖等。 |
| Series Dataset | 這種類型的數(shù)據(jù)集用于存儲一系列數(shù)據(jù)和構(gòu)建折線圖。 |
| Series Collection Dataset | 將不同類別的系列數(shù)據(jù)集添加到系列集合數(shù)據(jù)集中。這種類型的數(shù)據(jù)集用于 XY 線圖。 |
| Create Chart | 這是為創(chuàng)建最終圖表而執(zhí)行的方法。 |
| Frame/Image | 該圖顯示在一個Swing框架或創(chuàng)建映像。 |
一、快速繪圖教程
1. 折線圖

package plot;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
public class Example {
public static void main(String[] args) {
// 創(chuàng)建數(shù)據(jù)
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(15, "schools", "1970");
dataset.addValue(30, "schools", "1980");
dataset.addValue(60, "schools", "1990");
dataset.addValue(120, "schools", "2000");
dataset.addValue(240, "schools", "2010");
dataset.addValue(300, "schools", "2020");
dataset.addValue(320, "schools", "2022");
// 創(chuàng)建JFreeChart對象
JFreeChart chart = ChartFactory.createLineChart(
"Example", // 圖標(biāo)題
"Year", // x軸標(biāo)題
"Schools Count", // y軸標(biāo)題
dataset, //數(shù)據(jù)集
PlotOrientation.VERTICAL, //圖表方向
false, true, false);
// 利用awt進(jìn)行顯示
ChartFrame chartFrame = new ChartFrame("Test", chart);
chartFrame.pack();
chartFrame.setVisible(true);
}
}2. 條形圖

package plot;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
public class Example {
public static void main(String[] args) {
// 創(chuàng)建數(shù)據(jù)
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(1.0, "A", "Ⅰ");
dataset.addValue(3.0, "A", "Ⅱ");
dataset.addValue(5.0, "A", "Ⅲ");
dataset.addValue(5.0, "A", "Ⅳ");
dataset.addValue(5.0, "B", "Ⅰ");
dataset.addValue(6.0, "B", "Ⅱ");
dataset.addValue(10.0, "B", "Ⅲ");
dataset.addValue(4.0, "B", "Ⅳ");
// 創(chuàng)建JFreeChart對象
JFreeChart chart = ChartFactory.createBarChart(
"Example", // 圖標(biāo)題
"Category",
"Score",
dataset,
PlotOrientation.VERTICAL,
true, true, false);
// 利用awt進(jìn)行顯示
ChartFrame chartFrame = new ChartFrame("Test", chart);
chartFrame.pack();
chartFrame.setVisible(true);
}
}
package plot;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
public class Example {
public static void main(String[] args) {
// 創(chuàng)建數(shù)據(jù)
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(1.0, "A", "Ⅰ");
dataset.addValue(3.0, "A", "Ⅱ");
dataset.addValue(5.0, "A", "Ⅲ");
dataset.addValue(5.0, "A", "Ⅳ");
dataset.addValue(5.0, "B", "Ⅰ");
dataset.addValue(6.0, "B", "Ⅱ");
dataset.addValue(10.0, "B", "Ⅲ");
dataset.addValue(4.0, "B", "Ⅳ");
// 創(chuàng)建JFreeChart對象
JFreeChart chart = ChartFactory.createBarChart3D(
"Example", // 圖標(biāo)題
"Category",
"Score",
dataset,
PlotOrientation.VERTICAL,
true, true, false);
// 利用awt進(jìn)行顯示
ChartFrame chartFrame = new ChartFrame("Test", chart);
chartFrame.pack();
chartFrame.setVisible(true);
}
}3. 餅狀圖

package plot;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;
public class Example {
public static void main(String[] args) {
// 創(chuàng)建數(shù)據(jù)
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("A", 20);
dataset.setValue("B", 10);
dataset.setValue("C", 40);
dataset.setValue("D", 30);
// 創(chuàng)建JFreeChart對象
JFreeChart chart = ChartFactory.createPieChart(
"Example", // 圖標(biāo)題
dataset, // 數(shù)據(jù)集
false, true, true);
// 利用awt進(jìn)行顯示
ChartFrame chartFrame = new ChartFrame("Test", chart);
chartFrame.pack();
chartFrame.setVisible(true);
}
}
package plot;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;
public class Example {
public static void main(String[] args) {
// 創(chuàng)建數(shù)據(jù)
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("A", 20);
dataset.setValue("B", 10);
dataset.setValue("C", 40);
dataset.setValue("D", 30);
// 創(chuàng)建JFreeChart對象
JFreeChart chart = ChartFactory.createPieChart3D(
"Example", // 圖標(biāo)題
dataset, // 數(shù)據(jù)集
false, true, true);
// 利用awt進(jìn)行顯示
ChartFrame chartFrame = new ChartFrame("Test", chart);
chartFrame.pack();
chartFrame.setVisible(true);
}
}4. XY圖

package plot;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
public class Example {
public static void main(String[] args) {
// 創(chuàng)建數(shù)據(jù)
XYSeries data = new XYSeries("InternetExplorer");
data.add(1.0, 1.0);
data.add(2.0, 2.0);
data.add(3.0, 3.0);
data.add(4.0, 2.0);
data.add(5.0, 1.0);
data.add(6.0, 2.0);
data.add(6.0, 3.0);
data.add(7.0, 2.0);
data.add(8.0, 1.0);
data.add(9.0, 2.0);
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(data);
// 創(chuàng)建JFreeChart對象
JFreeChart chart = ChartFactory.createXYLineChart(
"Example",
"X",
"Y",
dataset,
PlotOrientation.VERTICAL,
false,true, false);
// 利用awt進(jìn)行顯示
ChartFrame chartFrame = new ChartFrame("Test", chart);
chartFrame.pack();
chartFrame.setVisible(true);
}
}5. 散點圖

package plot;
import java.io.IOException;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
public class Example {
public static void main(String[] args) throws IOException {
// 添加數(shù)據(jù)
XYSeriesCollection dataset = new XYSeriesCollection();
XYSeries series1 = new XYSeries("Series1");
for (int i = 0; i < 100; i++) {
series1.add(Math.random()*100, Math.random()*100);
}
dataset.addSeries(series1);
// 創(chuàng)建JFreeChart對象
JFreeChart chart = ChartFactory.createScatterPlot(
"Example",
"X-Axis", "Y-Axis", dataset);
// 利用awt進(jìn)行顯示
ChartFrame chartFrame = new ChartFrame("Test", chart);
chartFrame.pack();
chartFrame.setVisible(true);
}
}6. 氣泡圖

package plot;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.DefaultXYZDataset;
public class ShowExample {
public static void main(String[] args) {
// 創(chuàng)建數(shù)據(jù)
DefaultXYZDataset dataset = new DefaultXYZDataset();
double x[] = { 30, 40, 50, 60, 70, 80 };
double y[] = { 10, 20, 30, 40, 50, 60 };
double z[] = { 1, 2, 3, 4, 2, 1 };
double xyz[][] = { x, y, z };
dataset.addSeries("Series", xyz);
// 創(chuàng)建JFreeChart對象
JFreeChart chart = ChartFactory.createBubbleChart(
"AGE vs WEIGHT vs WORK",
"Weight",
"AGE",
dataset,
PlotOrientation.HORIZONTAL,
true, true, false);
// 利用awt進(jìn)行顯示
ChartFrame chartFrame = new ChartFrame("Test", chart);
chartFrame.pack();
chartFrame.setVisible(true);
}
}7. 時序圖

package plot;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.SeriesException;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
public class ShowExample {
public static void main(String[] args) {
// 創(chuàng)建數(shù)據(jù)
TimeSeries series = new TimeSeries("Random Data");
Second current = new Second();
double value = 100.0;
for (int i = 0; i < 4000; i++) {
try {
value = value + Math.random() - 0.5;
series.add(current, value);
current = (Second) current.next();
} catch (SeriesException e) {
System.err.println("Error adding to series");
}
}
XYDataset dataset = (XYDataset) new TimeSeriesCollection(series);
// 創(chuàng)建JFreeChart對象
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Computing Test",
"Seconds",
"Value",
dataset,
false,false, false);
// 利用awt進(jìn)行顯示
ChartFrame chartFrame = new ChartFrame("Test", chart);
chartFrame.pack();
chartFrame.setVisible(true);
}
}二、常見類與方法
在本章中,我們將討論一些在JFreeChart庫重要的軟件包,類和方法。這些軟件包,類和方法是最常見的,同時建立了各種使用JFreeChart庫圖表。
1. ChartFactory 類
ChartFactory是org.jfree.chart包中抽象類。它提供了實用方法的集合,用于生成標(biāo)準(zhǔn)的圖表。以下是幾個重要方法的列表:
類構(gòu)造方法
| 方法 | 描述 |
|---|---|
| ChartFactory() | ChartFactory類的默認(rèn)構(gòu)造函數(shù)。 |
類方法
| S.N. | 方法 & 描述 |
|---|---|
| 1 | createPieChart(java.lang.String title, PieDataset dataset, boolean legend, boolean tooltips, boolean urls) 此方法使用默認(rèn)設(shè)置創(chuàng)建一個餅圖。它返回JFreeChart類型的對象。 |
| 2 | createPieChart3D(java.lang.String title, PieDataset dataset, boolean legend, boolean tooltips, boolean urls) 此方法使用指定的數(shù)據(jù)集三維/3D餅圖。 |
| 3 | createBarChart(java.lang.String title, java.lang.String categoryAxisLabel, java.lang.String valueAxisLabel, CategoryDataset dataset, PlotOrientation orientation, boolean legend, boolean tooltips, boolean urls) 參數(shù)java.lang.String categoryAxisLabel標(biāo)簽放置在X軸的值。該參數(shù)的java.lang.String valueAxisLabel標(biāo)簽放置在Y軸的數(shù)值。此方法創(chuàng)建一個條形圖。 |
| 4 | createBarChart3D(java.lang.String title, java.lang.String categoryAxisLabel, java.lang.String valueAxisLabel, CategoryDataset dataset, PlotOrientation orientation, boolean legend, boolean tooltips, boolean urls) 此方法創(chuàng)建一個柱形圖具有3D效果。它返回JFreeChart類型的對象。 |
| 5 | createLineChart(java.lang.String title, java.lang.String categoryAxisLabel, java.lang.String valueAxisLabel, CategoryDataset dataset, PlotOrientation orientation, boolean legend, boolean tooltips, boolean urls) 此方法使用默認(rèn)設(shè)置創(chuàng)建一個折線圖。 |
| 6 | createLineChart3D(java.lang.String title, java.lang.String categoryAxisLabel, java.lang.String valueAxisLabel, CategoryDataset dataset, PlotOrientation orientation, boolean legend, boolean tooltips, boolean urls) 此方法創(chuàng)建一個折線圖與3D效果。 |
| 7 | createXYLineChart(java.lang.String title, java.lang.String xAxisLabel, java.lang.String yAxisLabel, XYDataset dataset, PlotOrientation orientation, boolean legend, boolean tooltips, boolean urls) 此方法使用默認(rèn)設(shè)置創(chuàng)建基于XYDataset的折線圖。 |
2. ChartFrame 類
ChartFrame類在org.jfree.chart包中,提供所有的幀相關(guān)的功能和工具。 ChartFrame類繼承自父類,如Frame, Window, Container, Component 類功能。
類構(gòu)造方法
| S.N. | 構(gòu)造方法及描述 |
|---|---|
| 1 | ChartFrame (java.lang.Frame String, JfreeChart chart) 它構(gòu)建一個框架/frame。 |
| 2 | Chart Frame (java.lang.Frame String, JfreeChart chart, boolean scrollpane) 它構(gòu)建一個框架/frame。 |
類方法
| S.N. | 構(gòu)造方法及描述 |
|---|---|
| 1 | getChartPanel() 此方法返回圖表面板的框架/frame。 |
3. ChartPanel 類
org.jfree.chart包中的ChartPanel類用于swing GUI部件,用于顯示JfreeChart對象。
類構(gòu)造方法
| S.N. | 構(gòu)造方法及描述 |
|---|---|
| 1 | ChartPanel(JFreeChart chart) 此構(gòu)造一個面板/panel ,顯示指定的圖表。 |
| 2 | ChartPanel(JFreeChart chart, boolean useBuffer) 這個構(gòu)造函數(shù)構(gòu)造包含圖表的面板/panel。 |
| 3 | ChartPanel(JFreeChart chart, boolean properties, boolean save, boolean print, boolean zoom, boolean tooltips) 此構(gòu)造一個JFreeChart面板。 |
類方法
| S.N. | 方法及描述 |
|---|---|
| 1 | setPreferredSize(java.awt.Dimension) 該方法用于java.awt中設(shè)置的幀大小。 Dimension類對象作為參數(shù)。這個方法是從javax.swing.JComponent中實現(xiàn)。 |
4. ChartUtilities 類
org.jfree.chart包中的CharUtilites類提供JFreeCharts包括將圖表轉(zhuǎn)換成圖像文件格式,如PNG,JPEG和創(chuàng)建HTML圖像映射方法的實用方法的集合。
類構(gòu)造函數(shù)
| S.N. | 構(gòu)造方法及描述 |
|---|---|
| 1 | ChartUtilities() 這是類的一個默認(rèn)構(gòu)造函數(shù) |
類方法
| S.N. | 方法及描述 |
|---|---|
| 1 | saveChartAsPNG(java.io.File file, JfreeChart chart, int width, int height) 此方法轉(zhuǎn)換和保存圖表為PNG格式指定的文件。 |
| 2 | saveChartAsJPEG(java.io.File file, JfreeChart chart, int width, int height) 此方法轉(zhuǎn)換并保存一個圖表,以JPEG格式指定的文件。 |
5. JFreeChart 類
JFreeChart 類是在org.jfree.chart包的核心類。這個類提供了JFreeChart的方法來創(chuàng)建柱狀圖,折線圖,餅圖和XY坐標(biāo)圖,包括時間序列數(shù)據(jù)。
類構(gòu)造函數(shù)
| S.N. | 構(gòu)造方法及描述 |
|---|---|
| 1 | JfreeChart(Plot plot) 此構(gòu)造函數(shù)創(chuàng)建基于所提供的節(jié)點一個新的圖表。 |
| 2 | JfreeChart(java.lang.String title, java.awt.Font titleFont, Plot plot, boolean createLegend) 該構(gòu)造函數(shù)創(chuàng)建一個新的圖表給定標(biāo)題和繪圖。 |
| 3 | JfreeChart(java.lang.String title, Plot plot) 該構(gòu)造函數(shù)創(chuàng)建一個新的圖表給定標(biāo)題和繪圖。 |
類方法
| S.N. | 方法及描述 |
|---|---|
| 1 | getXYPlot() 此方法返回節(jié)點圖表作為XYPlot。使用XYPolt我們可以XY圖表做了一些實用操作。 |
6. PiePlot 類
這個類是org.jfree.chart.plot包的一部分,來自同一個包擴(kuò)展Plot 類。這個類提供了一些方法來創(chuàng)建餅圖塊。
類的構(gòu)造函數(shù)
| S.N. | 構(gòu)造方法及描述 |
|---|---|
| 1 | PiePlot() 它創(chuàng)建了一個新的繪圖。 |
| 2 | PiePlot(PieDataset dataset) 它創(chuàng)建了一個繪圖,餅圖由指定的數(shù)據(jù)集。 |
類方法
| S.N. | 方法及描述 |
|---|---|
| 1 | setStartAngle(double angle) 此方法設(shè)置起始角度和發(fā)送PlotChangeEvent向所有注冊的偵聽器 |
7. PiePlot3D 類
PiePlot3D類和PiePlot類在同一個包中的子類。因此,這兩個類有相同的功能,PiePlot類只不過是用于創(chuàng)建3D圖形。
類的構(gòu)造函數(shù)
| S.N. | 構(gòu)造方法及描述 |
|---|---|
| 1 | PiePlot3D() 此構(gòu)造函數(shù)創(chuàng)建沒有數(shù)據(jù)集的新實例。 |
| 2 | PiePlot3D(PieDataset dataset) 該構(gòu)造函數(shù)創(chuàng)建一個餅圖和使用指定的數(shù)據(jù)集三維效果。 |
類方法
| S.N. | 方法及描述 |
|---|---|
| 1 | setForegroundAlpha(float alpha) 它設(shè)置alpha透明度并向所有發(fā)送PlotChangeEvent注冊的偵聽器。 |
| 2 | setInteriorGap(double percent) 它設(shè)置了內(nèi)部差距并發(fā)送PlotChangeEvent向所有注冊的偵聽器。這種控制的餅圖繪圖邊緣與繪圖區(qū)本身(即,其中部分標(biāo)簽顯示的區(qū)域)之間的空間。這個方法是從父類PiePlot繼承。 |
8. PlotOrientation 類
這是一個串行化類從org.jfree.chart.plot封裝,它是用來顯示一個二維曲線圖的方位。方向可以是垂直的或水平的。它設(shè)置Y軸的方向。傳統(tǒng)的繪圖有一個垂直的Y軸。
字段摘要
| S.N. | 類型 | 字段 & 描述 |
|---|---|---|
| 1 | PlotOrientation | HORIZONTAL 為一個曲線圖,其中所述范圍軸(Y軸)是水平。 |
| 2 | PlotOrientation | VERTICAL 為一個曲線圖,其中所述范圍軸(Y軸)是垂直的。這個默認(rèn)的方向。 |
類方法
| S.N. | 方法及描述 |
|---|---|
| 1 | isHorizontal() 如果這個方向是水平的,此方法返回true,否則返回false。 |
| 2 | isVertical() 如果這個方向是VERTICAL,此方法返回true,否則返回false。 |
9. XYPlot 類
這在org.jfree.chart.plot包可用一個通用類,并將其用于在(X,Y)對的形式標(biāo)繪數(shù)據(jù)。這個曲線圖可以從實現(xiàn)XYDataSet接口的任何其它類中使用的數(shù)據(jù)。 XYPlot利用一個XYItemRenderer的畫在圖上的每個點。
類的構(gòu)造函數(shù)
| S.N. | 構(gòu)造方法及描述 |
|---|---|
| 1 | XYPlot() 該構(gòu)造器沒有數(shù)據(jù)集,無軸,無渲染器創(chuàng)建一個新的XYPlot實例。 |
| 2 | XYPlot(XYDataset dataset,ValueAxis domainAxis,ValueAxis rangeAxis,XYItemRenderer renderer),此構(gòu)造函數(shù)創(chuàng)建一個新的繪圖并指定數(shù)據(jù)集,軸和渲染。 |
類方法
| S.N. | 方法及描述 |
|---|---|
| 1 | setRenderer(XYItemRenderer renderer) 此方法設(shè)置渲染器的主要數(shù)據(jù)集,并發(fā)送更改事件向所有注冊的偵聽器。 |
10. NumberAxis 類
這個類是org.jfree.chart.axis封裝,它可以訪問任意軸的數(shù)值數(shù)據(jù)。當(dāng)我們設(shè)置任何軸的范圍為默認(rèn)值,它根據(jù)所述數(shù)據(jù)的范圍配合。但使用NumberAxis,類我們可以設(shè)置較低的利潤率和定義域和值域軸的上側(cè)邊距。
類的構(gòu)造函數(shù)
| S.N. | 構(gòu)造方法及描述 |
|---|---|
| 1 | NumberAxis( ) 這是NumberAxis一個默認(rèn)的構(gòu)造函數(shù)。 |
| 2 | NumberAxis( java.lang.String label) 構(gòu)造函數(shù)NumberAxis使用必要的默認(rèn)值在哪里。 |
類方法
| S.N. | 方法及描述 |
|---|---|
| 1 | setLowerMargin(double margin) 它為軸心的利潤率較低(為軸心范圍的百分比),并發(fā)送一個AxisChangeEvent所有已注冊的偵聽器。這個方法是從父類ValueAxis繼承。 |
| 2 | setUpperMargin(double margin) 它設(shè)置于所述軸的上緣(視軸范圍的百分比),并發(fā)送一個AxisChangeEvent給所有注冊的監(jiān)聽器。這種方法也存在于ValueAxis類。 |
11. XYLineAndShapeRenderer 類
這是在org.jfree.chart.renderer.xy包下的類,它需要連接數(shù)據(jù)點與線,并繪制形狀,在每個數(shù)據(jù)點下是可用的。這個渲染器類是專為XYPlot類配合使用。
類的構(gòu)造函數(shù)
| S.N. | 構(gòu)造和描述 |
|---|---|
| 1 | XYLineAndShapeRenderer() 它創(chuàng)建了一個新的渲染器有兩種線條和形狀可見。 |
| 2 | XYLineAndShapeRenderer (boolean lines, boolean shapes) 它創(chuàng)建了一個新的渲染與特定的屬性。 |
類方法
| S.N. | 方法及描述 |
|---|---|
| 1 | setSeriesPaint(int series, java.awt.Paint paint) 此方法設(shè)置用于一系列的涂料,并發(fā)送RendererChangeEvent給所有注冊的監(jiān)聽器。這個方法是從AbstratRenderer抽象類從渲染器包中JFreeChart的API。 |
| 2 | setSeriesStroke(int series, java.awt.Stroke stroke) 此方法設(shè)置用于一系列的流程,并發(fā)送RendererChangeEvent向所有注冊的偵聽器。這個方法是從AbstratRenderer抽象類,它是這個包的超類。 |
12. XYItemRenderer通用數(shù)據(jù)集
這是用于使一個單一的(X,Y)格式項在XYPlot接口。org.Jfree.data.general包其具有類和接口,以定義不同類型的數(shù)據(jù)集來構(gòu)造圖。
PieDataset
這是作為一個通用的數(shù)據(jù)集,其中值與鍵相關(guān)聯(lián)的接口。正如其名稱所暗示的,可以使用這個數(shù)據(jù)集提供數(shù)據(jù)的餅圖。此接口擴(kuò)展KeyedValues數(shù)據(jù)集的接口。所有使用此接口的方法取自KeyedValues,Values和數(shù)據(jù)集的接口。
13. DefaultPieDataset 類
這是一個默認(rèn)的實現(xiàn)類PieDataset接口。
類的構(gòu)造函數(shù)
| S.N. | 構(gòu)造函數(shù)和描述 |
|---|---|
| 1 | DefaultPieDataset() 該構(gòu)造函數(shù)創(chuàng)建一個新的數(shù)據(jù)集,初始為空。 |
| 2 | DefaultPieDataset(KeyedValues data) 它從一個KeyedValues實例復(fù)制數(shù)據(jù)創(chuàng)建了一個新的數(shù)據(jù)集。 |
類方法
| S.N. | 方法及描述 |
|---|---|
| 1 | setValue(java.lang.Comparable key, double value) 它設(shè)置數(shù)據(jù)值的鍵,發(fā)送DatasetChangeEvent向所有注冊的偵聽器。 |
| 2 | setValue(java.lang.Comparable key, java.lang.Number value) 它設(shè)置數(shù)據(jù)值的鍵,發(fā)送DatasetChangeEvent向所有注冊的偵聽器。 |
14. SeriesException 類
這是一個異常類。它會引發(fā)發(fā)生在時間序列中數(shù)據(jù)集的數(shù)據(jù)的異常。異常是引發(fā)上的重復(fù)或無效數(shù)據(jù)的次數(shù)。時間序列不能與重復(fù)應(yīng)用,格式必須是有效的。
15. DefaultCategoryDataset
這是一個默認(rèn)的實現(xiàn)類CategoryDataset接口。
類的構(gòu)造函數(shù)
| S.N. | 構(gòu)造函數(shù)及描述 |
|---|---|
| 1 | DefaultCategoryDataset() 此構(gòu)造函數(shù)創(chuàng)建新的空數(shù)據(jù)集。 |
類方法
| S.N. | 方法及描述 |
|---|---|
| 1 | addValue(double value, java.lang.Comparable rowKey, java.lang.Comparable columnKey) 這種方法增加了一個值,以使用可比的鍵表。 |
| 2 | addValue(java.lang.Number value, java.lang.Comparable rowKey, java.lang.Comparable columnKey) 這種方法增加了一個值的表。 |
| 3 | setValue(double value, java.lang.Comparable rowKey, java.lang.Comparable columnKey) 此方法添加或在表中更新的值,并發(fā)送aDatasetChangeEvent給所有注冊的監(jiān)聽器。 |
| 4 | setValue(java.lang.Number value, java.lang.Comparable rowKey, java.lang.Comparable columnKey) 此方法添加或在表中更新的值,并發(fā)送DatasetChangeEvent給所有注冊的監(jiān)聽器。 |
參見JFreeChart的API,用于各種其他方法和字段的詳細(xì)信息。
16. 序列數(shù)據(jù)集
系列數(shù)據(jù)集用于XY圖表。該軟件包是org.Jfree.data.xy,其中包含類和屬于XY圖表接口。核心接口是XYDataset。
XYDataset
這是通過該數(shù)據(jù)中的(X,Y)的項目的形式可被訪問的接口。正如其名稱所提示的,可以使用這個數(shù)據(jù)集服務(wù)XY圖表。一些在這個接口中的方法都取自SeriesDateset接口。
XYZDataset
這是通過該數(shù)據(jù)的形式(x,y,z)的項目可被訪問的接口。正如其名稱所暗示的,可以使用這個數(shù)據(jù)集服務(wù)XYZ圖。一些在這個接口中的方法都取自SeriesDateset。
XYSeries
這是一類,它代表了在所述形式的零個或多個數(shù)據(jù)項(x,y)的序列。默認(rèn)情況下,該系列中的數(shù)據(jù)項都按升序排列由x值,并重復(fù)允許的x值。無論是排序和復(fù)制缺省值可以在構(gòu)造函數(shù)中被改變。 Y值可以表示為空值代表缺失值。
類構(gòu)造函數(shù)
| S.N. | 構(gòu)造函數(shù)描述 |
|---|---|
| 1 | XYSeries(java.lang.Comparable key) 該構(gòu)造函數(shù)創(chuàng)建一個新的空系列。 |
| 2 | XYSeries(java.lang.Comparable key, boolean autoSort) 它構(gòu)造一個新的空系列,具有自動排序標(biāo)志集的請求,并且重復(fù)的值是允許的。 |
| 3 | XYSeries(java.lang.Comparable key, boolean autoSort, boolean allowDuplicateXValues) 它構(gòu)造一個新的xy系列不包含任何數(shù)據(jù)。 |
類方法
| S.N. | 方法描述 |
|---|---|
| 1 | add(double x, double y) 這種方法增加了數(shù)據(jù)項成系列。 |
在上述方法中使用的教程例子。如果想了解其余的方法和字段,請參考JFreeChart的API。
XYSeriesCollection
XYSeriesCollection類有類似父類AbstractIntervelDataset,AbstractXYDatset,AbstractSeriesDataset和AbstractDataset。一些在這個類中的方法屬于這個類的父類。
類的構(gòu)造函數(shù)
| S.N. | 構(gòu)造函數(shù)描述 |
|---|---|
| 1 | XYSeriesCollection() 它構(gòu)造一個空的數(shù)據(jù)集。 |
| 2 | XYSeriesCollection(XYSeries xyseries)它構(gòu)建了一個數(shù)據(jù)集,并用一個系列的填充。 |
類方法
| S.N. | 方法及描述 |
|---|---|
| 1 | addSeries(XYSeries series) 這種方法增加了一系列的收集和發(fā)送DatasetChangeEvent向所有注冊的偵聽器。 |
參見JFreeChart的API其余的方法和字段。
Default XYZDataset :
DefaultXYZDataset類都有父類,如AbstractIntervelDataset,AbstractXYDatset,AbstractSeriesDataset,AbstractDataset和AbstractXYZDataset。一些在這個類中的方法屬于這一類的父類。
類的構(gòu)造函數(shù)
| S.N. | 構(gòu)造方法及描述 |
|---|---|
| 1 | DefaultXYZDataset() 它構(gòu)造一個空的數(shù)據(jù)集。 |
類方法
| S.N. | 方法及描述 |
|---|---|
| 1 | addSeries(java.lang.Comparable seriesKey, double[ ][ ] data ) 該方法增加了一系列的收集和發(fā)送DatasetChangeEvent向所有注冊的偵聽器。 |
請參考JFreeChart的API,其余的方法和字段。
JFreeCharts的時間序列
該軟件包是org.jfree.data.time。該軟件包包含用于時間相關(guān)的數(shù)據(jù)的類和接口。
TimeSeries :
此類表示數(shù)據(jù)項的期值的形式,其中一段是RegularTimePeriod抽象類,如時間,日,小時,分鐘和秒類的一些實例序列。
類的構(gòu)造函數(shù)
| S.N. | 構(gòu)造方法及描述 |
|---|---|
| 1 | TimeSeries(java.lang.Comparable name) 它創(chuàng)建新的空系列。 |
| 2 | TimeSeries(java.lang.Comarable name, java.lang.String domain, java.lang.Strin range) 它會創(chuàng)建一個不包含任何數(shù)據(jù)的新的時間序列。 |
類方法
| S.N. | 方法及描述 |
|---|---|
| 1 | add(RegularTimePeriod period,double value) 該方法增加了一個新的數(shù)據(jù)項用以串聯(lián)。 |
其余的方法和字段參見JFreeChart的API。
TimeSeriesCollection :
這是作為時間序列的對象的集合的類。這個類實現(xiàn)了XYDataset接口,以及它擴(kuò)展了IntervelXYDataset接口。這使得它可以方便地收集序列數(shù)據(jù)對象。
類的構(gòu)造函數(shù)
| S.N. | 構(gòu)造方法及描述 |
|---|---|
| 1 | TimeSeriesCollection() 它構(gòu)造一個空的數(shù)據(jù)集,綁在默認(rèn)時區(qū)。 |
| 2 | TimeSeriesCollection(TimeSeries series) 它構(gòu)造一個包含單個系列(更多可添加),綁在默認(rèn)時區(qū)的數(shù)據(jù)集。 |
| 3 | TimeSeriesCollection(TimeSeries series, java.util.TimeZone zone) 它構(gòu)造包含單個系列(更可添加),綁定到特定的時間段的數(shù)據(jù)集。 |
| 4 | TimeSeriesCollection(java.util.TimeZone zone) 它構(gòu)造一個空的數(shù)據(jù)集時,綁定到特定的時間區(qū)。 |
類方法
| S.N. | 方法及描述 |
|---|---|
| 1 | addSeries(TimeSeries series) 方法增加了一系列的收集和發(fā)送DatasetChangeEvent向所有注冊的偵聽器。 |
其余的方法和字段請參考JFreeChart的API。
Second 類:
這個類表示一個特定的日子一秒鐘。這個類是不可變的,這是對所有RegularTimePeriod子類的要求。
類的構(gòu)造函數(shù)
| S.N. | 構(gòu)造函數(shù)及描述 |
|---|---|
| 1 | Second() 它構(gòu)造一個新的Second,基于系統(tǒng)的日期/時間。 |
| 2 | Second(java.util.Date time) 它構(gòu)造從指定日期/時間和默認(rèn)時區(qū)的新實例。 |
| 3 | Second(java.util.Date time, java.util.TimeZone zone, java.util.Locale locale) 它創(chuàng)建基于所提供的時間和時區(qū)的新的Second對象。 |
| 4 | Second(int second, int minute, int hour, int day, int month, int year)它創(chuàng)建了一個新的Second對象。 |
| 5 | Second(int second, Minute minute) 它構(gòu)建了一個新的Second。 |
類方法
| S.N. | 方法及描述 |
|---|---|
| 1 | getSecond() 它返回分鐘和秒。 |
| 2 | next() 它返回當(dāng)前的下一秒。 |
其余的方法和字段請參考JFreeChart的API。
JFreeCharts 中的幀:
該軟件包是org.jfree.ui。這是包所屬JFreeChart的JCommons的API。它包含用于創(chuàng)建預(yù)配置的圖表框架的實用程序類。
ApplicationFrame :
這是用于創(chuàng)建簡單的應(yīng)用程序的主框架的基類。幀監(jiān)聽窗口關(guān)閉事件,并作出反應(yīng),關(guān)閉JVM。這是很好的小型演示應(yīng)用。對于企業(yè)應(yīng)用程序,需要使用一些更穩(wěn)健的東西。在這個類中的主要核心方法取自Component, Container, Window, Frame 和Jframe類。
類構(gòu)造函數(shù)
| S.N. | 構(gòu)造方法及描述 |
|---|---|
| 1 | ApplicationFrame(java.lang.String title) 它會創(chuàng)建一個字符串標(biāo)題的應(yīng)用程序框架。 |
這個類有助于創(chuàng)建AWT框架。這就是為什么我們使用這個類作為父類在本教程中的例子的原因。
其采取父類的方法用于打開一個框架,關(guān)閉一個框架,改變大小,改變背景或前景顏色和監(jiān)聽器。
RefineryUtilities :
這是關(guān)于用戶界面的工具方法的類的集合。
類方法
| S.N. | 方法及描述 |
|---|---|
| 1 | centerFrameOnScreen(java.awt.Window frame) 它定位在屏幕的中間的指定幀。 |
在上述方法中使用的教程例子以外的類,方法和字段參見JFreeChart的API。
三、高級用法
1. 圖形的展示與保存
package plot;
import java.io.File;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
public class Example {
public static void main(String[] args) throws IOException {
// 創(chuàng)建數(shù)據(jù)
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue( 15 , "schools" , "1970" );
dataset.addValue( 30 , "schools" , "1980" );
dataset.addValue( 60 , "schools" , "1990" );
dataset.addValue( 120 , "schools" , "2000" );
dataset.addValue( 240 , "schools" , "2010" );
dataset.addValue( 300 , "schools" , "2014" );
// 創(chuàng)建JFreeChart對象
JFreeChart chart = ChartFactory.createLineChart(
"Example", // 圖標(biāo)題
"Year", // x軸
"Schools Count",// y軸
dataset,
PlotOrientation.VERTICAL,
false,true,false);
// 保存到本地(PNG或者JPEG格式)
File f = new File( "chart.png" );
ChartUtilities.saveChartAsPNG(f ,chart, 800 ,400);
// 保存為矢量圖形(需要借助JFreeSVG包)
SVGGraphics2D g2 = new SVGGraphics2D(640, 480);
chart.draw(g2, new Rectangle(640, 480));
File f = new File("SVGBarChartDemo.svg");
SVGUtils.writeToSVG(f, g2.getSVGElement());
// 利用swing進(jìn)行展示
JPanel jPanel = new ChartPanel(chart);
JFrame frame = new JFrame("JFreechart Test");
frame.add(jPanel);
frame.setBounds(0, 0, 800, 500);
frame.setVisible(true);
// 利用awt進(jìn)行顯示
ChartFrame chartFrame = new ChartFrame("Test", chart);
chartFrame.pack();
chartFrame.setVisible(true);
}
}2. 通用設(shè)置
package plot;
import java.awt.BasicStroke;
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.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.ValueAxis;
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.LegendTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.RectangleEdge;
public class Test {
public static void main(String[] args) {
// 添加數(shù)據(jù)集
DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
dataSet.addValue(3542, "2020年", "第一季度");// Y軸-圖例-X軸
dataSet.addValue(3692, "2020年", "第二季度");// Y軸-圖例-X軸
dataSet.addValue(8542, "2020年", "第三季度");// Y軸-圖例-X軸
dataSet.addValue(5742, "2020年", "第四季度");// Y軸-圖例-X軸
dataSet.addValue(1242, "2021年", "第一季度");// Y軸-圖例-X軸
dataSet.addValue(2612, "2021年", "第二季度");// Y軸-圖例-X軸
dataSet.addValue(1942, "2021年", "第三季度");// Y軸-圖例-X軸
dataSet.addValue(4612, "2021年", "第四季度");// Y軸-圖例-X軸
// 主題樣式設(shè)置
StandardChartTheme standardChartTheme = new StandardChartTheme("CN"); // 創(chuàng)建主題樣式
standardChartTheme.setExtraLargeFont(new Font("宋體", Font.BOLD, 64)); // 設(shè)置標(biāo)題字體
standardChartTheme.setRegularFont(new Font("宋體", Font.BOLD, 32)); // 設(shè)置圖例的字體
standardChartTheme.setLargeFont(new Font("宋體", Font.BOLD, 20)); // 設(shè)置軸向的字體
standardChartTheme.setChartBackgroundPaint(Color.WHITE);// 設(shè)置主題背景色
ChartFactory.setChartTheme(standardChartTheme);// 應(yīng)用主題樣式
// 定義圖表對象
JFreeChart chart = ChartFactory.createLineChart(
// 圖表的標(biāo)題-橫軸標(biāo)題-縱軸標(biāo)題-數(shù)據(jù)集
"JFreeChart折線圖", "銷售額", "季度", dataSet,
// 圖表方向-圖例-工具提示-超鏈接
PlotOrientation.VERTICAL,true, false, false);
// chart.setTitle(new TextTitle(title[0], new Font("宋書", Font.BOLD, 64)));// 重新設(shè)置標(biāo)題
// chart.removeLegend();// 是否移除圖例
CategoryPlot plot = (CategoryPlot)chart.getPlot(); // 獲得圖表顯示對象
plot.setOutlineVisible(false);// 是否顯示外邊框
plot.setOutlinePaint(Color.WHITE);// 外邊框顏色
// plot.setOutlineStroke(new BasicStroke(2.f));// 外邊框框線粗細(xì)
plot.setBackgroundPaint(Color.WHITE);// 白色背景
plot.setNoDataMessage("無圖表數(shù)據(jù)");// 無數(shù)據(jù)提示
plot.setNoDataMessageFont(new Font("宋體", Font.BOLD, 32));// 提示字體
plot.setNoDataMessagePaint(Color.RED);// 提示字體顏色
// 圖例
LegendTitle legend = chart.getLegend();// 圖例對象
legend.setPosition(RectangleEdge.BOTTOM);// 圖例所在位置(上、下、左、右)
legend.setVisible(true);// 是否顯示圖例
legend.setBorder(2, 0, 0, 2); // 圖例上下左右邊框粗細(xì)
legend.setItemFont(new Font("宋體", Font.BOLD, 32));// 圖例大小
// 網(wǎng)格線
plot.setDomainGridlinePaint(Color.BLUE);
plot.setDomainGridlinesVisible(true);// 豎線
plot.setRangeGridlinePaint(Color.BLACK);
plot.setRangeGridlinesVisible(true);// 橫線
// 橫坐標(biāo)
CategoryAxis xAxis = plot.getDomainAxis();
xAxis.setTickLabelFont(new Font("宋體", Font.BOLD, 25));// 設(shè)置X軸值字體
xAxis.setLabelFont(new Font("宋書", Font.BOLD, 32));// 設(shè)置X軸標(biāo)簽字體
xAxis.setAxisLineStroke(new BasicStroke(1f)); // 設(shè)置X軸線粗細(xì)
xAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);// 傾斜
xAxis.setAxisLinePaint(Color.BLACK);// 軸線顏色
xAxis.setUpperMargin(0.18D);// 右邊距
xAxis.setLowerMargin(0.1D);// 左邊距
// 縱坐標(biāo)
ValueAxis yAxis = plot.getRangeAxis();
yAxis.setTickLabelFont(new Font("宋體", Font.BOLD, 25));// 設(shè)置y軸字體
yAxis.setLabelFont(new Font("宋體", Font.BOLD, 32));// 設(shè)置X軸標(biāo)簽字體
yAxis.setAxisLineStroke(new BasicStroke(1f)); // 設(shè)置y軸線粗細(xì)
yAxis.setAxisLinePaint(Color.BLACK);// 軸線顏色
yAxis.setUpperMargin(0.18D);// 上邊距
yAxis.setLowerMargin(0.1D);// 下邊距
LineAndShapeRenderer r1 = new LineAndShapeRenderer();
// 數(shù)據(jù)標(biāo)簽
r1.setBaseItemLabelsVisible(true);// 是否顯示數(shù)據(jù)標(biāo)簽
r1.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());// 數(shù)據(jù)標(biāo)簽格式
r1.setSeriesPaint(0, Color.BLUE);
r1.setBaseShapesFilled(Boolean.TRUE); // 在數(shù)據(jù)點顯示實心的小圖標(biāo)
r1.setBaseShapesVisible(true); // 設(shè)置顯示小圖標(biāo)
r1.setBaseItemLabelFont(new Font("Dialog", Font.BOLD, 12));// 設(shè)置數(shù)字的字體大小
r1.setBaseStroke(new BasicStroke(4f));
// 線條顏色
r1.setSeriesPaint(0, new Color(204, 232, 207));
r1.setSeriesPaint(1, new Color(0x00, 0xff, 0xff));
r1.setSeriesShapesVisible(0, false);
r1.setSeriesShapesVisible(1, false);
plot.setDataset(dataSet);
// 應(yīng)用以上設(shè)置
plot.setRenderer(r1);
// 條形圖間距設(shè)置
// BarRenderer r = (BarRenderer) plot.getRenderer();
// renderer.setItemMargin(0.01);
// 顯示圖表
ChartFrame chartFrame = new ChartFrame("Test", chart);
chartFrame.pack();
chartFrame.setVisible(true);
}
}3. 其他設(shè)置
3.1 坐標(biāo)軸刻度設(shè)置
package com.test;
import java.io.File;
import java.io.IOException;
import javax.swing.JFrame;
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.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
public class Test {
public static void main(String[] args) throws IOException {
// 創(chuàng)建數(shù)據(jù)
final XYSeries data = new XYSeries( "InternetExplorer" );
data.add( 1.0 , 4.0 );
data.add( 2.0 , 5.0 );
data.add( 3.0 , 4.0 );
data.add( 4.0 , 5.0 );
data.add( 5.0 , 4.0 );
data.add( 6.0 , 5.0 );
data.add( 6.0 , 4.0 );
data.add( 7.0 , 5.0 );
data.add( 8.0 , 4.0 );
data.add( 9.0 , 5.0 );
final XYSeriesCollection dataset = new XYSeriesCollection( );
dataset.addSeries(data);
// 創(chuàng)建JFreeChart對象
JFreeChart chart = ChartFactory.createXYLineChart(
"Example", "X", "Y", dataset,
PlotOrientation.VERTICAL,
false, true, false);
// 設(shè)置坐標(biāo)軸刻度間距
XYPlot plot = (XYPlot) chart.getPlot();
NumberAxis xAxis = (NumberAxis) plot.getDomainAxis();
xAxis.setTickUnit(new NumberTickUnit(2));
// 保存到本地
File lineChart = new File( "1.png" );
ChartUtilities.saveChartAsPNG(lineChart ,chart, 1000 ,500);
}
}四、其他圖表
1. 時間序列圖

package plot;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickUnit;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardXYItemLabelGenerator;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.TextAnchor;
import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;
public class TimeSeriesChart {
private TimeSeriesChart() {
// A網(wǎng)站的訪問量統(tǒng)計
@SuppressWarnings("deprecation")
TimeSeries timeSeries1 = new TimeSeries("A", Month.class);
// 添加數(shù)據(jù)
timeSeries1.add(new Month(1, 2016), 154);
timeSeries1.add(new Month(2, 2016), 256);
timeSeries1.add(new Month(3, 2016), 312);
timeSeries1.add(new Month(4, 2016), 489);
timeSeries1.add(new Month(5, 2016), 563);
timeSeries1.add(new Month(6, 2016), 555);
timeSeries1.add(new Month(7, 2016), 359);
timeSeries1.add(new Month(8, 2016), 291);
timeSeries1.add(new Month(9, 2016), 123);
timeSeries1.add(new Month(10, 2016), 438);
timeSeries1.add(new Month(11, 2016), 286);
// A網(wǎng)站的訪問量統(tǒng)計
TimeSeries timeSeries2 = new TimeSeries("A", Month.class);
// 添加數(shù)據(jù)
timeSeries2.add(new Month(1, 2016), 124);
timeSeries2.add(new Month(2, 2016), 326);
timeSeries2.add(new Month(3, 2016), 12);
timeSeries2.add(new Month(4, 2016), 567);
timeSeries2.add(new Month(5, 2016), 546);
timeSeries2.add(new Month(6, 2016), 123);
timeSeries2.add(new Month(7, 2016), 222);
timeSeries2.add(new Month(8, 2016), 545);
timeSeries2.add(new Month(9, 2016), 56);
timeSeries2.add(new Month(10, 2016), 543);
timeSeries2.add(new Month(11, 2016), 221);
// 定義時間序列的集合
TimeSeriesCollection lineDataset = new TimeSeriesCollection();
lineDataset.addSeries(timeSeries1);
lineDataset.addSeries(timeSeries2);
// JFreeChart chart = ChartFactory.createXYStepChart("Time line graph", "M", "F", xySeriesCollection, PlotOrientation.HORIZONTAL, false, false, false);
JFreeChart chart = ChartFactory.createTimeSeriesChart("Time line graph", "M", "F", lineDataset, false, false, false);
//設(shè)置主標(biāo)題
chart.setTitle(new TextTitle("A,B網(wǎng)站訪問量統(tǒng)計對比圖"));
//設(shè)置子標(biāo)題
TextTitle subtitle = new TextTitle("2016年度", new Font("宋體", Font.BOLD, 12));
chart.addSubtitle(subtitle);
chart.setAntiAlias(true);
//設(shè)置時間軸的范圍。
XYPlot plot = (XYPlot) chart.getPlot();
DateAxis dateaxis = (DateAxis) plot.getDomainAxis();
dateaxis.setDateFormatOverride(new SimpleDateFormat("M"));
dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.MONTH, 2));
//設(shè)置曲線是否顯示數(shù)據(jù)點
XYLineAndShapeRenderer xylinerenderer = (XYLineAndShapeRenderer) plot.getRenderer();
xylinerenderer.setBaseShapesVisible(true);
//設(shè)置曲線顯示各數(shù)據(jù)點的值
XYItemRenderer xyitem = plot.getRenderer();
xyitem.setBaseItemLabelsVisible(true);
xyitem.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
xyitem.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
xyitem.setBaseItemLabelFont(new Font("Dialog", Font.BOLD, 12));
plot.setRenderer(xyitem);
JPanel jPanel = new ChartPanel(chart);
JFrame frame = new JFrame("JFreechart Test");
frame.add(jPanel);
frame.setBounds(0, 0, 800, 600);
frame.setVisible(true);
}
public static void main(String[] args) {
TimeSeriesChart timeSeriesChart = new TimeSeriesChart();
}
}2. XY階梯面積圖

package plot;
import java.awt.Color;
import java.io.IOException;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
public class Example {
public static void main(String[] args) throws IOException {
// 創(chuàng)建XYSeriesCollection對象
XYSeriesCollection dataset = new XYSeriesCollection();
// 系列一數(shù)據(jù)
XYSeries series1 = new XYSeries("Series1");
series1.add(2, 4);
series1.add(3, 6);
series1.add(5, 2);
series1.add(8, 5);
series1.add(1, 8);
// 系列二數(shù)據(jù)
XYSeries series2 = new XYSeries("Series2");
series2.add(5, 6);
series2.add(9, 5);
series2.add(10, 9);
series2.add(18, 11);
series2.add(15, 18);
// 添加到數(shù)據(jù)集
dataset.addSeries(series1);
dataset.addSeries(series2);
// 創(chuàng)建JFreeChart對象
JFreeChart chart = ChartFactory.createXYStepAreaChart("XY Step Area Chart", // Chart Title
"X-Axis", // X-Axis Label
"Y-Axis", // Y-Axis Label
dataset);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setBackgroundPaint(new Color(229, 150, 97, 60));
// 利用awt進(jìn)行顯示
ChartFrame chartFrame = new ChartFrame("Test", chart);
chartFrame.pack();
chartFrame.setVisible(true);
}
}3. 條形圖與折線圖結(jié)合

package plot;
import java.io.IOException;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
public class ShowExample {
public static void main(String[] args) throws IOException {
// 創(chuàng)建數(shù)據(jù)
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(1.0, "A", "Ⅰ");
dataset.addValue(3.0, "A", "Ⅱ");
dataset.addValue(5.0, "A", "Ⅲ");
dataset.addValue(5.0, "A", "Ⅳ");
dataset.addValue(5.0, "B", "Ⅰ");
dataset.addValue(6.0, "B", "Ⅱ");
dataset.addValue(10.0, "B", "Ⅲ");
dataset.addValue(4.0, "B", "Ⅳ");
// 創(chuàng)建CategoryPlot對象
CategoryPlot plot = new CategoryPlot();
// 添加第一個數(shù)據(jù)集并渲染為line
CategoryItemRenderer lineRenderer = new LineAndShapeRenderer();
plot.setDataset(0, dataset);
plot.setRenderer(0, lineRenderer);
// 添加第二個數(shù)據(jù)集并渲染為線條bar
CategoryItemRenderer baRenderer = new BarRenderer();
plot.setDataset(1, dataset);
plot.setRenderer(1, baRenderer);
// 設(shè)置坐標(biāo)軸
plot.setDomainAxis(new CategoryAxis("Time"));
plot.setRangeAxis(new NumberAxis("Value"));
// 創(chuàng)建JFreeChart對象
JFreeChart chart = new JFreeChart(plot);
// 利用awt進(jìn)行顯示
ChartFrame chartFrame = new ChartFrame("Test", chart);
chartFrame.pack();
chartFrame.setVisible(true);
}
}參考資料
https://www.roseindia.net/chartgraphs/index.shtml
到此這篇關(guān)于Java繪圖庫JFreeChart的使用教程的文章就介紹到這了,更多相關(guān)Java繪圖庫JFreeChart內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring-boot整合ehcache實現(xiàn)緩存機制的方法
spring-boot是一個快速的集成框架,其設(shè)計目的是用來簡化新Spring應(yīng)用的初始搭建以及開發(fā)過程。這篇文章主要介紹了spring-boot整合ehcache實現(xiàn)緩存機制,需要的朋友可以參考下2018-01-01
mybatis-plus實體類中出現(xiàn)非數(shù)據(jù)庫映射字段解決辦法
這篇文章主要介紹了mybatis-plus實體類中出現(xiàn)非數(shù)據(jù)庫映射字段解決辦法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Java利用StampedLock實現(xiàn)讀寫鎖的方法詳解
在jdk8以后,java提供了一個性能更優(yōu)越的讀寫鎖并發(fā)類StampedLock,該類的設(shè)計初衷是作為一個內(nèi)部工具類,用于輔助開發(fā)其它線程安全組件。本文就來和大家一起學(xué)習(xí)下StampedLock的功能和使用2022-10-10
JAVA刪除字符串固定下標(biāo)字串的實現(xiàn)
本文主要介紹了JAVA刪除字符串固定下標(biāo)字串的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
springboot項目啟動自動跳轉(zhuǎn)到瀏覽器的操作代碼
這篇文章主要介紹了springboot項目啟動自動跳轉(zhuǎn)到瀏覽器的操作代碼,本文圖文實例代碼相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-03-03

