一 JFreeChart生成餅狀圖
實現(xiàn)的思路
1、初始化圖表數(shù)據(jù)
2、獲得圖表數(shù)據(jù)集DefaultPieDataset
3、利用chart工廠創(chuàng)建一個jfreechart實例
JFreeChart chart = ChartFactory.createPieChart3D(title, // 圖表標(biāo)題
dataset, // 圖表數(shù)據(jù)集
true, // 是否顯示圖例
false, // 是否生成工具(提示)
false // 是否生成URL鏈接
);
4、通過TextTitle類設(shè)置餅圖的標(biāo)題與字體:
void setFont(Font font)標(biāo)題字體
void setPaint(Paint paint)標(biāo)題字體顏色
void setText(String title)標(biāo)題內(nèi)容
5、通過LegendTitle類設(shè)置圖例的字體:
void setItemFont(Font font)標(biāo)題字體
6、獲得餅圖實例PiePlot,設(shè)置餅圖參數(shù):
void setLabelFont(Font font)標(biāo)簽字體
void setForegroundAlpha(float alpha)指定圖片的透明度(0.0-1.0)
void setLabelGenerator(PieSectionLabelGenerator generator)分類標(biāo)簽的格式,設(shè)置成null則整個標(biāo)簽包括連接結(jié)都不顯示
void setStartAngle(double angle)餅圖的初始角度
源代碼
import java.awt.Color;
import java.awt.Font;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
public class CreatePieChart {
/**
* 獲得數(shù)據(jù)集。
* @return org.jfree.data.general.DefaultPieDataset
*/
private static DefaultPieDataset getDataSet() {
DefaultPieDataset dfp = new DefaultPieDataset();
dfp.setValue("研發(fā)人員", 35);
dfp.setValue("市場策劃人員", 10);
dfp.setValue("市場推廣人員", 25);
dfp.setValue("網(wǎng)絡(luò)維護人員", 5);
dfp.setValue("財務(wù)人員", 15);
return dfp;
}
/**
* 生成餅狀圖。
*/
public static void makePieChart3D() {
String title = "餅狀圖";
// 獲得數(shù)據(jù)集
DefaultPieDataset dataset = getDataSet();
// 利用chart工廠創(chuàng)建一個jfreechart實例
JFreeChart chart = ChartFactory.createPieChart3D(title, // 圖表標(biāo)題
dataset, // 圖表數(shù)據(jù)集
true, // 是否顯示圖例
false, // 是否生成工具(提示)
false // 是否生成URL鏈接
);
// 設(shè)置pieChart的標(biāo)題與字體
Font font = new Font("宋體", Font.BOLD, 25);
TextTitle textTitle = new TextTitle(title);
textTitle.setFont(font);
chart.setTitle(textTitle);
chart.setTextAntiAlias(false);
// 設(shè)置背景色
chart.setBackgroundPaint(new Color(255, 255, 255));
// 設(shè)置圖例字體
LegendTitle legend = chart.getLegend(0);
legend.setItemFont(new Font("宋體", 1, 15));
// 設(shè)置標(biāo)簽字體
PiePlot plot = (PiePlot) chart.getPlot();
plot.setLabelFont(new Font("宋體", Font.TRUETYPE_FONT, 12));
// 指定圖片的透明度(0.0-1.0)
plot.setForegroundAlpha(0.95f);
// 圖片中顯示百分比:自定義方式,{0} 表示選項, {1} 表示數(shù)值, {2} 表示所占比例 ,小數(shù)點后兩位
plot.setLabelGenerator(new StandardPieSectionLabelGenerator(
"{0}={1}({2})", NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%")));
// 圖例顯示百分比:自定義方式, {0} 表示選項, {1} 表示數(shù)值, {2} 表示所占比例
plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0} ({2})"));
// 設(shè)置第一個餅塊截面開始的位置,默認(rèn)是12點鐘方向
plot.setStartAngle(90);
/***********************************************************/
ChartFrame frame = new ChartFrame(title, chart, true);
frame.pack();
frame.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
// 3D餅狀圖
makePieChart3D();
}
}
二 JFreeChart生成柱狀圖
實現(xiàn)的思路
1、初始化圖表數(shù)據(jù)
2、獲得圖表數(shù)據(jù)集CategoryDataset
3、利用chart工廠創(chuàng)建一個jfreechart實例
JFreeChart chart = ChartFactory.createBarChart3D(title, // 圖表標(biāo)題
"X軸", // X軸的顯示標(biāo)簽
"Y軸", // Y軸的顯示標(biāo)簽
dataset, // 數(shù)據(jù)集
PlotOrientation.VERTICAL, // 圖表方向:水平、垂直
true, // 是否顯示圖例
true, // 是否生成工具(提示)
true // 是否生成URL鏈接
);
4、通過TextTitle類設(shè)置餅圖的標(biāo)題與字體:
void setFont(Font font)標(biāo)題字體
void setPaint(Paint paint)標(biāo)題字體顏色
void setText(String title)標(biāo)題內(nèi)容
5、獲得柱狀圖實例CategoryPlot:
CategoryPlot plot = chart.getCategoryPlot();
6、獲得橫軸(CategoryAxis)并設(shè)置格式
CategoryAxis categoryAxis = plot.getDomainAxis();
void setLabelFont(Font font)橫軸標(biāo)簽字體
void setTickLabelFont(Font font)橫軸標(biāo)記字體
7、獲得縱軸(NumberAxis)并設(shè)置格式
NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();
void setLabelFont(Font font)縱軸標(biāo)簽字體
源代碼
import org.jfree.chart.ChartFactory;
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.plot.PlotOrientation;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
public class CreateBarChart {
/**
* 獲得數(shù)據(jù)集。
* @return org.jfree.data.category.CategoryDataset
*/
private static CategoryDataset getDataset() {
double[][] data = new double[][] { { 751, 800, 260, 600, 200 },
{ 400, 560, 240, 300, 150 }, { 600, 450, 620, 220, 610 } };
String[] rowKeys = { "CPU", "硬盤", "內(nèi)存" };
String[] columnKeys = { "北京", "上海", "廣州", "南京", "深圳" };
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
rowKeys, columnKeys, data);
return dataset;
}
/**
* 生成柱狀圖。
*/
public static void makeBarChart3D() {
String title = "電腦配件三月銷量";
// 獲得數(shù)據(jù)集
CategoryDataset dataset = getDataset();
JFreeChart chart = ChartFactory.createBarChart3D(title, // 圖表標(biāo)題
"配件", // 目錄軸的顯示標(biāo)簽
"銷量", // 數(shù)值軸的顯示標(biāo)簽
dataset, // 數(shù)據(jù)集
PlotOrientation.VERTICAL, // 圖表方向:水平、垂直
true, // 是否顯示圖例
true, // 是否生成工具(提示)
true // 是否生成URL鏈接
);
// 設(shè)置標(biāo)題字體
Font font = new Font("宋體", Font.BOLD, 18);
TextTitle textTitle = new TextTitle(title);
textTitle.setFont(font);
chart.setTitle(textTitle);
chart.setTextAntiAlias(false);
// 設(shè)置背景色
chart.setBackgroundPaint(new Color(255, 255, 255));
// 設(shè)置圖例字體
LegendTitle legend = chart.getLegend(0);
legend.setItemFont(new Font("宋體", Font.TRUETYPE_FONT, 14));
// 獲得柱狀圖的Plot對象
CategoryPlot plot = chart.getCategoryPlot();
// 取得橫軸
CategoryAxis categoryAxis = plot.getDomainAxis();
// 設(shè)置橫軸顯示標(biāo)簽的字體
categoryAxis.setLabelFont(new Font("宋體", Font.BOLD, 16));
// 設(shè)置橫軸標(biāo)記的字體
categoryAxis.setTickLabelFont(new Font("宋休", Font.TRUETYPE_FONT, 16));
// 取得縱軸
NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();
// 設(shè)置縱軸顯示標(biāo)簽的字體
numberAxis.setLabelFont(new Font("宋體", Font.BOLD, 16));
/**************************************************************/
ChartFrame frame = new ChartFrame(title, chart, true);
frame.pack();
frame.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
// 3D柱狀圖
makeBarChart3D();
}
}
三 JFreeChart生成折線圖
實現(xiàn)的思路
1、初始化圖表數(shù)據(jù)
2、獲得圖表數(shù)據(jù)集CategoryDataset
3、利用chart工廠創(chuàng)建一個jfreechart實例
JFreeChart chart = ChartFactory.createLineChart(title, // 圖表標(biāo)題
"X軸", // X軸的顯示標(biāo)簽
"Y軸", // Y軸的顯示標(biāo)簽
dataset, // 數(shù)據(jù)集
PlotOrientation.VERTICAL, // 圖表方向:水平、垂直
true, // 是否顯示圖例
true, // 是否生成工具(提示)
false // 是否生成URL鏈接
);
4、通過TextTitle類設(shè)置餅圖的標(biāo)題與字體:
void setFont(Font font)標(biāo)題字體
void setPaint(Paint paint)標(biāo)題字體顏色
void setText(String title)標(biāo)題內(nèi)容
5、獲得折線圖實例CategoryPlot
CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
void setDomainGridlinesVisible(boolean flag)設(shè)置X軸網(wǎng)格是否中見
void setRangeGridlinesVisible(boolean flag)設(shè)置Y軸網(wǎng)格是否可見
void setBackgroundPaint(Color color)設(shè)置背景色
6、獲得橫軸(CategoryAxis)并設(shè)置格式
CategoryAxis categoryAxis = plot.getDomainAxis();
void setLabelFont(Font font)橫軸標(biāo)簽字體
void setTickLabelFont(Font font)橫軸標(biāo)記字體
7、獲得縱軸(NumberAxis)并設(shè)置格式
NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();
void setLabelFont(Font font)縱軸標(biāo)簽字體
源代碼
importjava.awt.Color;
importjava.awt.Font;
importorg.jfree.chart.ChartFactory;
importorg.jfree.chart.ChartFrame;
importorg.jfree.chart.JFreeChart;
importorg.jfree.chart.axis.CategoryAxis;
importorg.jfree.chart.axis.CategoryLabelPositions;
importorg.jfree.chart.axis.NumberAxis;
importorg.jfree.chart.plot.CategoryPlot;
importorg.jfree.chart.plot.PlotOrientation;
importorg.jfree.chart.renderer.category.LineAndShapeRenderer;
importorg.jfree.chart.title.LegendTitle;
importorg.jfree.chart.title.TextTitle;
importorg.jfree.data.category.CategoryDataset;
importorg.jfree.data.general.DatasetUtilities;
public class CreateLineChart {
/**
* 獲得數(shù)據(jù)集。
* @return org.jfree.data.category.CategoryDataset
*/
private static CategoryDataset getDataset() {
double[][] data = new double[][] { { 751, 800, 260, 600, 200 },
{ 400, 560, 240, 300, 150 }, { 600, 450, 620, 220, 610 } };
String[] rowKeys = { "CPU", "硬盤", "內(nèi)存" };
String[] columnKeys = { "北京", "上海", "廣州", "南京", "深圳" };
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
rowKeys, columnKeys, data);
return dataset;
}
/**
* 生成折線圖。
*/
public static void makeLineChart() {
String title = "電腦配件三月銷量";
// 獲得數(shù)據(jù)集
CategoryDataset dataset = getDataset();
JFreeChart chart = ChartFactory.createLineChart(title, // 圖表標(biāo)題
"配件", // 目錄軸的顯示標(biāo)簽
"銷量", // 數(shù)值軸的顯示標(biāo)簽
dataset, // 數(shù)據(jù)集
PlotOrientation.VERTICAL, // 圖表方向:水平、垂直
true, // 是否顯示圖例
true, // 是否生成工具(提示)
false // 是否生成URL鏈接
);
chart.setTextAntiAlias(false);
// 設(shè)置背景色
chart.setBackgroundPaint(Color.WHITE);
// 設(shè)置圖標(biāo)題的字體
Font font = new Font("宋體", Font.BOLD, 20);
TextTitle textTitle = new TextTitle(title);
textTitle.setFont(font);
chart.setTitle(textTitle);
// 設(shè)置X軸Y軸的字體
Font labelFont = new Font("宋體", Font.BOLD, 16);
chart.setBackgroundPaint(Color.WHITE);
// 設(shè)置圖例字體
LegendTitle legend = chart.getLegend(0);
legend.setItemFont(new Font("宋體", Font.TRUETYPE_FONT, 14));
// 獲得plot
CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
// x軸 分類軸網(wǎng)格是否可見
categoryplot.setDomainGridlinesVisible(true);
// y軸 數(shù)據(jù)軸網(wǎng)格是否可見
categoryplot.setRangeGridlinesVisible(true);
// 虛線色彩
categoryplot.setRangeGridlinePaint(Color.WHITE);
// 虛線色彩
categoryplot.setDomainGridlinePaint(Color.WHITE);
// 設(shè)置背景色
categoryplot.setBackgroundPaint(Color.lightGray);
// 設(shè)置軸和面板之間的距離
CategoryAxis domainAxis = categoryplot.getDomainAxis();
// 設(shè)置橫軸標(biāo)簽標(biāo)題字體
domainAxis.setLabelFont(labelFont);
// 設(shè)置橫軸數(shù)值標(biāo)簽字體
domainAxis.setTickLabelFont(new Font("宋體", Font.TRUETYPE_FONT, 14));
// 橫軸上的
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD);
// 設(shè)置距離圖片左端距離
domainAxis.setLowerMargin(0.0);
// 設(shè)置距離圖片右端距離
domainAxis.setUpperMargin(0.0);
NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
// 設(shè)置縱軸顯示標(biāo)簽的字體
numberaxis.setLabelFont(labelFont);
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
numberaxis.setAutoRangeIncludesZero(true);
// 獲得renderer
LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot
.getRenderer();
// series 點(即數(shù)據(jù)點)可見
lineandshaperenderer.setBaseShapesVisible(true);
// series 點(即數(shù)據(jù)點)間有連線可見
lineandshaperenderer.setBaseLinesVisible(true);
/*******************************************************/
ChartFrame frame = new ChartFrame(title, chart, true);
frame.pack();
frame.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
// 曲線圖
makeLineChart();
}
}
四 JFreeChart生成時序圖
實現(xiàn)的思路
1、實例化TimeSeries對象
TimeSeries timeseries = new TimeSeries("Data");
2、創(chuàng)建TimeSeriesCollection集合對象
TimeSeriesCollection dataset = new TimeSeriesCollection(timeseries);
3、利用chart工廠創(chuàng)建一個jfreechart實例
JFreeChart chart = ChartFactory.createBarChart3D(title, // 圖表標(biāo)題
"X軸", // X軸的顯示標(biāo)簽
"Y軸", // Y軸的顯示標(biāo)簽
dataset, // 數(shù)據(jù)集
true, // 是否顯示圖例
true, // 是否生成工具(提示)
true // 是否生成URL鏈接
);
4、通過TextTitle類設(shè)置餅圖的標(biāo)題與字體:
void setFont(Font font)標(biāo)題字體
void setPaint(Paint paint)標(biāo)題字體顏色
void setText(String title)標(biāo)題內(nèi)容
5、通過LegendTitle類設(shè)置圖例的字體:
void setItemFont(Font font)標(biāo)題字體
6、獲得時序圖實例XYPlot:
XYPlot plot = chart.getXYPlot();
7、獲取X軸對象
DateAxis axis = (DateAxis) plot.getDomainAxis();
void setDateFormatOverride(DateFormat formatter)設(shè)置日期顯示格式
void setLabelFont(Font font)設(shè)置X軸標(biāo)簽字體
8、獲取Y軸對象
NumberAxis numAxis = (NumberAxis) plot.getRangeAxis();
void setLabelFont(Font font)設(shè)置Y軸標(biāo)簽字體
源代碼
import java.awt.Color;
import java.awt.Font;
import java.text.SimpleDateFormat;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
public class CreateTimeSeriesChart {
public static void makeTimeSeriesChart() {
// 實例化TimeSeries對象
TimeSeries timeseries = new TimeSeries("Data");
// 實例化Day
Day day = new Day(1,1,2009);
double d = 3000D;
// 添加一年365天的數(shù)據(jù)
for(int i = 0 ; i < 365 ; i++){
// 創(chuàng)建隨機數(shù)據(jù)
d = d+(Math.random() - 0.5) * 10;
// 向數(shù)據(jù)集合中添加數(shù)據(jù)
timeseries.add(day,d);
day = (Day)day.next();
}
// 創(chuàng)建TimeSeriesCollection集合對象
TimeSeriesCollection dataset = new TimeSeriesCollection(timeseries);
// 生成時序圖
JFreeChart chart = ChartFactory.createTimeSeriesChart("上證指數(shù)時序圖",//標(biāo)題
"日期", //時間軸標(biāo)簽
"指數(shù)", //數(shù)據(jù)軸標(biāo)簽
dataset, //數(shù)據(jù)集合
true, //是否顯示圖例標(biāo)識
true, //是否顯示tooltips
false); //是否支持超鏈接
String title = "上證指數(shù)時序圖";
// 設(shè)置圖例字體
LegendTitle legend = chart.getLegend(0);
legend.setItemFont(new Font("宋體", Font.TRUETYPE_FONT, 15));
// 設(shè)置標(biāo)題字體
Font font = new Font("宋體", Font.BOLD, 20);
TextTitle textTitle = new TextTitle(title);
textTitle.setFont(font);
chart.setTitle(textTitle);
// Plot 對象的獲取操作
XYPlot plot = chart.getXYPlot();
// X 軸對象的獲取操作
DateAxis axis = (DateAxis) plot.getDomainAxis();
// 設(shè)置日期顯示格式
axis.setDateFormatOverride(new SimpleDateFormat("MM-dd-yyyy"));
// 設(shè)置X軸標(biāo)簽字體
axis.setLabelFont(new Font("宋體", Font.BOLD, 14));
// Y 軸對象的獲取操作
NumberAxis numAxis = (NumberAxis) plot.getRangeAxis();
// 設(shè)置Y軸標(biāo)簽字體
numAxis.setLabelFont(new Font("宋體", Font.BOLD, 14));
/***************************************************************/
ChartFrame cf = new ChartFrame("時序圖", chart);
cf.pack();
cf.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
// 時序圖
makeTimeSeriesChart();
}
}