-
1 # 列克美食
-
2 # 程式設計頌
用jfreechart
jfreechart繪製柱狀圖
import java.io.File;
import java.io.IOException;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
/*
* 繪製柱狀圖
*你亮哥
* */
public class BarChart3DDemo
{
public static void main(String[] args)
{
try
{
//設定主題
ChartFactory.setChartTheme(Theme.getTheme());
//構造資料
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(100, "JAVA","1");
dataset.addValue(200, "js","1");
dataset.addValue(200, "C++", "2");
dataset.addValue(300, "C", "3");
dataset.addValue(400, "HTML", "4");
dataset.addValue(400, "CSS", "5");
/*
* public static JFreeChart createBarChart3D(
* java.lang.String title, 設定圖表的標題
* java.lang.String categoryAxisLabel, 設定分類軸的標示
* java.lang.String valueAxisLabel, 設定值軸的標示
* CategoryDataset dataset, 設定資料
* PlotOrientation orientation, 設定圖表的方向
* boolean legend, 設定是否顯示圖例
* boolean tooltips,設定是否生成熱點工具
* boolean urls) 設定是否顯示url
*/
JFreeChart chart = ChartFactory.createBarChart3D("程式語言統計", "語言",
"學習人數", dataset, PlotOrientation.VERTICAL, true, false,
false);
//儲存圖表
ChartUtilities.saveChartAsPNG(new File("E:/chart/BarChart3D.png"), chart, 800, 500);
System.out.println("繪圖完成");
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
===================================================================================
//一條線 有點 有數
package Test;
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.axis.AxisSpace;
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.RectangleInsets;
import org.jfree.ui.TextAnchor;
public class try123 {
public static void main(String[] args){
//首先構造資料
TimeSeries timeSeries = new TimeSeries("BMI", Month.class);
// 時間曲線資料集合
TimeSeriesCollection lineDataset = new TimeSeriesCollection();
// 構造資料集合
timeSeries.add(new Month(1, 2009), 45);
timeSeries.add(new Month(2, 2009), 46);
timeSeries.add(new Month(3, 2009), 1);
timeSeries.add(new Month(4, 2009), 500);
timeSeries.add(new Month(5, 2009), 43);
timeSeries.add(new Month(6, 2009), 324);
timeSeries.add(new Month(7, 2009), 632);
timeSeries.add(new Month(8, 2009), 34);
timeSeries.add(new Month(9, 2009), 12);
timeSeries.add(new Month(10, 2009), 543);
timeSeries.add(new Month(11, 2009), 32);
timeSeries.add(new Month(12, 2009), 225);
lineDataset.addSeries(timeSeries);
JFreeChart chart = ChartFactory.createTimeSeriesChart("", "date", "bmi", lineDataset, true, true, true);
//增加標題
chart.setTitle(new TextTitle("XXXBMI指數", new Font("隸書", Font.ITALIC, 15)));
chart.setAntiAlias(true);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setAxisOffset(new RectangleInsets(10,10,10,10));//圖片區與座標軸的距離
plot.setOutlinePaint(Color.PINK);
plot.setInsets(new RectangleInsets(15,15,15,15));//座標軸與最外延的距離
// plot.setOrientation(PlotOrientation.HORIZONTAL);//圖形的方向,包括座標軸。
AxisSpace as = new AxisSpace();
as.setLeft(25);
as.setRight(25);
plot.setFixedRangeAxisSpace(as);
chart.setPadding(new RectangleInsets(5,5,5,5));
chart.setNotify(true);
// 設定曲線是否顯示資料點
XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)plot.getRenderer();
xylineandshaperenderer.setBaseShapesVisible(true);
// 設定曲線顯示各資料點的值
XYItemRenderer xyitem = plot.getRenderer();
xyitem.setBaseItemLabelsVisible(true);
xyitem.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.INSIDE10, TextAnchor.BASELINE_LEFT));
xyitem.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
xyitem.setBaseItemLabelFont(new Font("Dialog", 1, 14));
plot.setRenderer(xyitem);
//顯示
ChartFrame frame = new ChartFrame("try1", chart);
frame.pack();
frame.setVisible(true);
}
}
回覆列表
實現柱形圖的java程式碼:
import java.awt.Font;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
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 BarChart {
ChartPanel frame1;
public BarChart(){
CategoryDataset dataset = getDataSet();
JFreeChart chart = ChartFactory.createBarChart3D(
"水果", // 圖表標題
"水果種類", // 目錄軸的顯示標籤
"數量", // 數值軸的顯示標籤
dataset, // 資料集
PlotOrientation.VERTICAL, // 圖表方向:水平、垂直
true, // 是否顯示圖例(對於簡單的柱狀圖必須是false)
false, // 是否生成工具
false // 是否生成URL連結
);
//從這裡開始
CategoryPlot plot=chart.getCategoryPlot();//獲取圖表區域物件
CategoryAxis domainAxis=plot.getDomainAxis(); //水平底部列表
domainAxis.setLabelFont(new Font("黑體",Font.BOLD,14)); //水平底部標題
domainAxis.setTickLabelFont(new Font("宋體",Font.BOLD,12)); //垂直標題
ValueAxis rangeAxis=plot.getRangeAxis();//獲取柱狀
rangeAxis.setLabelFont(new Font("黑體",Font.BOLD,15));
chart.getLegend().setItemFont(new Font("黑體", Font.BOLD, 15));
chart.getTitle().setFont(new Font("宋體",Font.BOLD,20));//設定標題字型
//到這裡結束,雖然程式碼有點多,但只為一個目的,解決漢字亂碼問題
frame1=new ChartPanel(chart,true); //這裡也可以用chartFrame,可以直接生成一個獨立的Frame
}
private static CategoryDataset getDataSet() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(100, "北京", "蘋果");
dataset.addValue(100, "上海", "蘋果");
dataset.addValue(100, "廣州", "蘋果");
dataset.addValue(200, "北京", "梨子");
dataset.addValue(200, "上海", "梨子");
dataset.addValue(200, "廣州", "梨子");
dataset.addValue(300, "北京", "葡萄");
dataset.addValue(300, "上海", "葡萄");
dataset.addValue(300, "廣州", "葡萄");
dataset.addValue(400, "北京", "香蕉");
dataset.addValue(400, "上海", "香蕉");
dataset.addValue(400, "廣州", "香蕉");
dataset.addValue(500, "北京", "荔枝");
dataset.addValue(500, "上海", "荔枝");
dataset.addValue(500, "廣州", "荔枝");
return dataset;
}
public ChartPanel getChartPanel(){
return frame1;
}
}
效果圖如下:
但我們把private static CategoryDataset getDataSet(){}方法中的資料變化一下後,又會形成另一種效果,比如說我們改成:
private static CategoryDataset getDataSet() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(100, "蘋果", "蘋果");
dataset.addValue(200, "梨子", "梨子");
dataset.addValue(300, "葡萄", "葡萄");
dataset.addValue(400, "香蕉", "香蕉");
dataset.addValue(500, "荔枝", "荔枝");
return dataset;
}
效果圖如下:
三, 實現餅狀圖的java程式碼:
package com.njue.testJFreeChart;
import java.awt.Font;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
public class PieChart {
ChartPanel frame1;
public PieChart(){
DefaultPieDataset data = getDataSet();
JFreeChart chart = ChartFactory.createPieChart3D("水果產量",data,true,false,false);
//設定百分比
PiePlot pieplot = (PiePlot) chart.getPlot();
DecimalFormat df = new DecimalFormat("0.00%");//獲得一個DecimalFormat物件,主要是設定小數問題
NumberFormat nf = NumberFormat.getNumberInstance();//獲得一個NumberFormat物件
StandardPieSectionLabelGenerator sp1 = new StandardPieSectionLabelGenerator("{0} {2}", nf, df);//獲得StandardPieSectionLabelGenerator物件
pieplot.setLabelGenerator(sp1);//設定餅圖顯示百分比
//沒有資料的時候顯示的內容
pieplot.setNoDataMessage("無資料顯示");
pieplot.setCircular(false);
pieplot.setLabelGap(0.02D);
pieplot.setIgnoreNullValues(true);//設定不顯示空值
pieplot.setIgnoreZeroValues(true);//設定不顯示負值
frame1=new ChartPanel (chart,true);
chart.getTitle().setFont(new Font("宋體",Font.BOLD,20));//設定標題字型
PiePlot piePlot= (PiePlot) chart.getPlot();//獲取圖表區域物件
piePlot.setLabelFont(new Font("宋體",Font.BOLD,10));//解決亂碼
chart.getLegend().setItemFont(new Font("黑體",Font.BOLD,10));
}
private static DefaultPieDataset getDataSet() {
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("蘋果",100);
dataset.setValue("梨子",200);
dataset.setValue("葡萄",300);
dataset.setValue("香蕉",400);
dataset.setValue("荔枝",500);
return dataset;
}
public ChartPanel getChartPanel(){
return frame1;
}
}
效果圖如下:
四, 實現折線圖的java程式碼:
package com.njue.testJFreeChart;
import java.awt.Font;
import java.text.SimpleDateFormat;
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.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
public class TimeSeriesChart {
ChartPanel frame1;
public TimeSeriesChart(){
XYDataset xydataset = createDataset();
JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("Legal & General單位信托基金價格", "日期", "價格",xydataset, true, true, true);
XYPlot xyplot = (XYPlot) jfreechart.getPlot();
DateAxis dateaxis = (DateAxis) xyplot.getDomainAxis();
dateaxis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
frame1=new ChartPanel(jfreechart,true);
dateaxis.setLabelFont(new Font("黑體",Font.BOLD,14)); //水平底部標題
dateaxis.setTickLabelFont(new Font("宋體",Font.BOLD,12)); //垂直標題
ValueAxis rangeAxis=xyplot.getRangeAxis();//獲取柱狀
rangeAxis.setLabelFont(new Font("黑體",Font.BOLD,15));
jfreechart.getLegend().setItemFont(new Font("黑體", Font.BOLD, 15));
jfreechart.getTitle().setFont(new Font("宋體",Font.BOLD,20));//設定標題字型
}
private static XYDataset createDataset() { //這個資料集有點多,但都不難理解
TimeSeries timeseries = new TimeSeries("legal & general歐洲指數信任",
org.jfree.data.time.Month.class);
timeseries.add(new Month(2, 2001), 181.80000000000001D);
timeseries.add(new Month(3, 2001), 167.30000000000001D);
timeseries.add(new Month(4, 2001), 153.80000000000001D);
timeseries.add(new Month(5, 2001), 167.59999999999999D);
timeseries.add(new Month(6, 2001), 158.80000000000001D);
timeseries.add(new Month(7, 2001), 148.30000000000001D);
timeseries.add(new Month(8, 2001), 153.90000000000001D);
timeseries.add(new Month(9, 2001), 142.69999999999999D);
timeseries.add(new Month(10, 2001), 123.2D);
timeseries.add(new Month(11, 2001), 131.80000000000001D);
timeseries.add(new Month(12, 2001), 139.59999999999999D);
timeseries.add(new Month(1, 2002), 142.90000000000001D);
timeseries.add(new Month(2, 2002), 138.69999999999999D);
timeseries.add(new Month(3, 2002), 137.30000000000001D);
timeseries.add(new Month(4, 2002), 143.90000000000001D);
timeseries.add(new Month(5, 2002), 139.80000000000001D);
timeseries.add(new Month(6, 2002), 137D);
timeseries.add(new Month(7, 2002), 132.80000000000001D);
TimeSeries timeseries1 = new TimeSeries("legal & general英國指數信任",
org.jfree.data.time.Month.class);
timeseries1.add(new Month(2, 2001), 129.59999999999999D);
timeseries1.add(new Month(3, 2001), 123.2D);
timeseries1.add(new Month(4, 2001), 117.2D);
timeseries1.add(new Month(5, 2001), 124.09999999999999D);
timeseries1.add(new Month(6, 2001), 122.59999999999999D);
timeseries1.add(new Month(7, 2001), 119.2D);
timeseries1.add(new Month(8, 2001), 116.5D);
timeseries1.add(new Month(9, 2001), 112.7D);
timeseries1.add(new Month(10, 2001), 101.5D);
timeseries1.add(new Month(11, 2001), 106.09999999999999D);
timeseries1.add(new Month(12, 2001), 110.3D);
timeseries1.add(new Month(1, 2002), 111.7D);
timeseries1.add(new Month(2, 2002), 111D);
timeseries1.add(new Month(3, 2002), 109.59999999999999D);
timeseries1.add(new Month(4, 2002), 113.2D);
timeseries1.add(new Month(5, 2002), 111.59999999999999D);
timeseries1.add(new Month(6, 2002), 108.8D);
timeseries1.add(new Month(7, 2002), 101.59999999999999D);
TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
timeseriescollection.addSeries(timeseries);
timeseriescollection.addSeries(timeseries1);
return timeseriescollection;
}
public ChartPanel getChartPanel(){
return frame1;
}
}
效果圖如下:
再來看一下主方法:
import java.awt.GridLayout;
import javax.swing.JFrame;
public class mainClass {
public static void main(String args[]){
JFrame frame=new JFrame("Java資料統計圖");
frame.setLayout(new GridLayout(2,2,10,10));
frame.add(new BarChart().getChartPanel()); //新增柱形圖
frame.add(new BarChart1().getChartPanel()); //新增柱形圖的另一種效果
frame.add(new PieChart().getChartPanel()); //新增餅狀圖
frame.add(new TimeSeriesChart().getChartPanel()); //新增折線圖
frame.setBounds(50, 50, 800, 600);
frame.setVisible(true);
}
}