标签:
杂谈 |
目录:
一、图形及代码:
1、饼形图
2.1、普通柱形图
2.2、XYDataset柱形图
3、折线图
4、区域图
5、时序图
6、组合图
二web应用jfreeChar(区域图)
三:DataUtils类代码(所有代码中均应用的类,得到随即数据)
一、图形及代码
1、饼形图
-
-
import java.awt.Font;
-
import java.io.FileOutputStream;
-
import java.io.IOException;
-
import java.io.OutputStream;
-
import org.jfree.chart.ChartFactory;
-
import org.jfree.chart.ChartUtilities;
-
import org.jfree.chart.JFreeChart;
-
import org.jfree.chart.plot.PiePlot;
-
import org.jfree.data.general.DefaultPieDataset;
-
import org.jfree.data.general.PieDataset;
-
-
public class PieChart {
-
public static void main(String[] agrs) throws IOException{ -
// 打开一个输出流 -
OutputStream outputStream=new FileOutputStream("PieChart.png"); -
// 获取数据集对象 -
PieDataset dataset = createPieDataset(); -
// 创建图形对象 -
JFreeChart jfreechart = ChartFactory.createPieChart3D("08年图书销量排行榜", -
dataset, true, true, false); -
// 获得图表区域对象 -
PiePlot pieplot = (PiePlot) jfreechart.getPlot(); -
// 设置图表区域的标签字体 -
pieplot.setLabelFont(new Font("宋体", 0, 12)); -
// 设置图表区域无数据时的默认显示文字 -
pieplot.setNoDataMessage("没有销售数据"); -
// 设置图表区域不是圆形,由于是3D的饼形图,建议设置为false -
pieplot.setCircular(false); -
// 设置图表区域文字与图表区域的间隔距离,0.02表示2% -
pieplot.setLabelGap(0.02D); -
// 将图表已数据流的方式返回给客户端 -
ChartUtilities.writeChartAsPNG(outputStream, jfreechart, -
500, 270); -
} -
-
private static PieDataset createPieDataset() { -
// 创建饼形图数据集对象 -
DefaultPieDataset defaultpiedataset = new DefaultPieDataset(); -
// 分别图形区域的说明和数据 -
defaultpiedataset.setValue("JAVA", DataUtils.getRandomData()); -
defaultpiedataset.setValue("C/C++", DataUtils.getRandomData()); -
defaultpiedataset.setValue("PHP", DataUtils.getRandomData()); -
defaultpiedataset.setValue("JavaScript", DataUtils.getRandomData()); -
defaultpiedataset.setValue("Ajax", DataUtils.getRandomData()); -
return defaultpiedataset; -
} -
-
}
package com.helloweenvsfei.jfreechart;
import java.awt.Font;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
public class PieChart {
public static void main(String[] agrs) throws IOException{
// 打开一个输出流
OutputStream outputStream=new FileOutputStream("PieChart.png");
// 获取数据集对象
PieDataset dataset = createPieDataset();
// 创建图形对象
JFreeChart jfreechart = ChartFactory.createPieChart3D("08年图书销量排行榜",
dataset, true, true, false);
// 获得图表区域对象
PiePlot pieplot = (PiePlot) jfreechart.getPlot();
// 设置图表区域的 签字体
pieplot.setLabelFont(new Font("宋体", 0, 12));
// 设置图表区域 数据时的默认显示文字
pieplot.setNoDataMessage("没有销售数据");
// 设置图表区域不是圆形,由于是3D的饼形图,建议设置为false
pieplot.setCircular(false);
// 设置图表区域文字与图表区域的间隔距离,0.02表示2%
pieplot.setLabelGap(0.02D);
// 将图表已数据流的方式返回给客户端
ChartUtilities.writeChartAsPNG(outputStream, jfreechart,
500, 270);
}
private static PieDataset createPieDataset() {
// 创建饼形图数据集对象
DefaultPieDataset defaultpiedataset = new DefaultPieDataset();
// 分别图形区域的说明和数据
defaultpiedataset.setValue("JAVA", DataUtils.getRandomData());
defaultpiedataset.setValue("C/C++", DataUtils.getRandomData());
defaultpiedataset.setValue("PHP", DataUtils.getRandomData());
defaultpiedataset.setValue("JavaScript", DataUtils.getRandomData());
defaultpiedataset.setValue("Ajax", DataUtils.getRandomData());
return defaultpiedataset;
}
}
效果:
2.1、普通柱形图
-
import java.io.FileOutputStream;
-
import java.io.IOException;
-
import java.io.OutputStream;
-
import org.jfree.chart.ChartFactory;
-
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.plot.CategoryPlot;
-
import org.jfree.chart.plot.PlotOrientation;
-
import org.jfree.chart.renderer.category.BarRenderer3D;
-
import org.jfree.data.category.CategoryDataset;
-
import org.jfree.data.category.DefaultCategoryDataset;
-
-
public class BarChart {
-
public static void main(String[] agrs) throws IOException{ -
// 打开一个输出流 -
OutputStream outputStream=new FileOutputStream("BarChart.png"); -
// 获取数据集对象 -
CategoryDataset dataset = createDataset(); -
// 创建图形对象 -
JFreeChart jfreechart = ChartFactory.createBarChart3D("08年图书销售量排名", -
"按季度", "销量", dataset, PlotOrientation.VERTICAL, true, true, -
false); -
// 获得图表区域对象 -
CategoryPlot categoryPlot = (CategoryPlot) jfreechart.getPlot(); -
// 设置网格线可见 -
categoryPlot.setDomainGridlinesVisibl e(true); -
// 获得x轴对象 -
CategoryAxis categoryAxis = categoryPlot.getDomainAxis(); -
// 设置x轴显示的分类名称的显示位置,如果不设置则水平显示 -
// 设置后,可以斜像显示,但分类角度,图表空间有限时,建议采用 -
categoryAxis.setCategoryLabelPosition s(CategoryLabelPositions -
.createUpRotationLabelPos itions(0.39269908169872414D)); -
categoryAxis.setCategoryMargin(0.0D); -
// 获显示图形对象 -
BarRenderer3D barRenderer3d = (BarRenderer3D) categoryPlot -
.getRenderer(); -
// 设置不显示边框线 -
barRenderer3d.setDrawBarOutline(false); -
// 将图表已数据流的方式返回给客户端 -
ChartUtilities.writeChartAsPNG(outputStream, jfreechart, -
500, 270); -
} -
-
-
private static CategoryDataset createDataset() { -
// 时间维度 -
String[] category1 = { "第一季度", "第二季度", "第三季度", "第四季度" }; -
// 分类维度 -
String[] category2 = { "JAVA", "C/C++", "PHP" }; -
DefaultCategoryDataset defaultdataset = new DefaultCategoryDataset(); -
for (int i = 0; i < category1.length; i++) { -
String category = category1[i]; -
for (int j = 0; j < category2.length; j++) { -
String cat = category2[j]; -
// 模拟添加数据 -
defaultdataset.addValue(DataUtils.getRandomData(), cat, category); -
} -
} -
return defaultdataset; -
} -
}
package com.helloweenvsfei.jfreechart;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.jfree.chart.ChartFactory;
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.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
public class BarChart {
public static void main(String[] agrs) throws IOException{
// 打开一个输出流
OutputStream outputStream=new FileOutputStream("BarChart.png");
// 获取数据集对象
CategoryDataset dataset = createDataset();
// 创建图形对象
JFreeChart jfreechart = ChartFactory.createBarChart3D("08年图书销售量排名",
"按季度", "销量", dataset, PlotOrientation.VERTICAL, true, true,
false);
// 获得图表区域对象
CategoryPlot categoryPlot = (CategoryPlot) jfreechart.getPlot();
// 设置网 线可见
categoryPlot.setDomainGridlinesVisible(true);
// 获得x轴对象
CategoryAxis categoryAxis = categoryPlot.getDomainAxis();
// 设置x轴显示的分类名称的显示位置,如果不设置则水平显示
// 设置后,可以斜像显示,但分类角度,图表空间有限时,建议采用
categoryAxis.setCategoryLabelPositions(CategoryLabelPositions
.createUpRotationLabelPositions(0.39269908169872414D));
categoryAxis.setCategoryMargin(0.0D);
// 获显示图形对象
BarRenderer3D barRenderer3d = (BarRenderer3D) categoryPlot
.getRenderer();
// 设置不显示边框线
barRenderer3d.setDrawBarOutline(false);
// 将图表已数据流的方式返回给客户端
ChartUtilities.writeChartAsPNG(outputStream, jfreechart,
500, 270);
}
private static CategoryDataset createDataset() {
// 时间维度
String[] category1 = { "第一季度", "第二季度", "第三季度", "第四季度" };
// 分类维度
String[] category2 = { "JAVA", "C/C++", "PHP" };
DefaultCategoryDataset defaultdataset = new DefaultCategoryDataset();
for (int i = 0; i < category1.length; i++) {
String category = category1[i];
for (int j = 0; j < category2.length; j++) {
String cat = category2[j];
// 模拟添 数据
defaultdataset.addValue(DataUtils.getRandomData(), cat, category);
}
}
return defaultdataset;
}
}
效果:
2.2、XYDataset柱形图
-
-
import java.awt.Color;
-
import java.io.FileOutputStream;
-
import java.io.IOException;
-
import java.io.OutputStream;
-
-
import org.jfree.chart.ChartFactory;
-
import org.jfree.chart.ChartUtilities;
-
import org.jfree.chart.JFreeChart;
-
import org.jfree.chart.plot.PlotOrientation;
-
import org.jfree.chart.plot.XYPlot;
-
import org.jfree.chart.renderer.xy.XYBarRenderer;
-
import org.jfree.data.xy.IntervalXYDataset;
-
import org.jfree.data.xy.XYBarDataset;
-
import org.jfree.data.xy.XYSeries;
-
import org.jfree.data.xy.XYSeriesCollection;
-
public class BarChart2 {
-
public static void main(String[] agrs) throws IOException{ -
// 打开一个输出流 -
OutputStream outputStream=new FileOutputStream("BarChart2.png"); -
// 获取数据集对象 -
IntervalXYDataset dataset = createDataset(); -
// 创建图形对象 -
JFreeChart jfreechart = ChartFactory.createXYBarChart("08年图书销售量", null, -
false, "销量", dataset, PlotOrientation.VERTICAL, true, false, -
false); -
// 设置背景为白色 -
jfreechart.setBackgroundPaint(Color.white); -
// 获得图表区域对象 -
XYPlot xyplot = (XYPlot) jfreechart.getPlot(); -
// 设置区域对象背景为灰色 -
xyplot.setBackgroundPaint(Color.lightGray); -
// 设置区域对象网格线调为白色 -
xyplot.setDomainGridlinePaint(Color.white); -
xyplot.setRangeGridlinePaint(Color.white); -
// 获显示图形对象 -
XYBarRenderer xybarrenderer = (XYBarRenderer) xyplot.getRenderer(); -
// 设置不显示边框线 -
xybarrenderer.setDrawBarOutline(false); -
-
// 将图表已数据流的方式返回给客户端 -
ChartUtilities.writeChartAsPNG(outputStream, jfreechart, -
500, 270); -
} -
-
-
private static IntervalXYDataset createDataset() { -
// 创建基本数据 -
XYSeries xyseries = new XYSeries("JAVA"); -
XYSeries xyseries1 = new XYSeries("PHP"); -
for (int i = 1; i <= 12; i++) { -
// 添加数据 -
xyseries.add(i,DataUtils.getRandomData()); -
xyseries1.add(i,DataUtils.getRandomData()); -
} -
XYSeriesCollection xyseriescollection = new XYSeriesCollection(); -
xyseriescollection.addSeries(xyseries); -
xyseriescollection.addSeries(xyseries1); -
return new XYBarDataset(xyseriescollection, 0.90000000000000002D); -
} -
}
-
</span>
package com.helloweenvsfei.jfreechart;
import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.XYBarDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
public class BarChart2 {
public static void main(String[] agrs) throws IOException{
// 打开一个输出流
OutputStream outputStream=new FileOutputStream("BarChart2.png");
// 获取数据集对象
IntervalXYDataset dataset = createDataset();
// 创建图形对象
JFreeChart jfreechart = ChartFactory.createXYBarChart("08年图书销售量", null,
false, "销量", dataset, PlotOrientation.VERTICAL, true, false,
false);
// 设置背景为白色
jfreechart.setBackgroundPaint(Color.white);
// 获得图表区域对象
XYPlot xyplot = (XYPlot) jfreechart.getPlot();
// 设置区域对象背景为灰色
xyplot.setBackgroundPaint(Color.lightGray);
// 设置区域对象网 线调为白色
xyplot.setDomainGridlinePaint(Color.white);
xyplot.setRangeGridlinePaint(Color.white);
// 获显示图形对象
XYBarRenderer xybarrenderer = (XYBarRenderer) xyplot.getRenderer();
// 设置不显示边框线
xybarrenderer.setDrawBarOutline(false);
// 将图表已数据流的方式返回给客户端
ChartUtilities.writeChartAsPNG(outputStream, jfreechart,
500, 270);
}
private static IntervalXYDataset createDataset() {
// 创建基本数据
XYSeries xyseries = new XYSeries("JAVA");
XYSeries xyseries1 = new XYSeries("PHP");
for (int i = 1; i <= 12; i++) {
// 添 数据
xyseries.add(i,DataUtils.getRandomData());
xyseries1.add(i,DataUtils.getRandomData());
}
XYSeriesCollection xyseriescollection = new XYSeriesCollection();
xyseriescollection.addSeries(xyseries);
xyseriescollection.addSeries(xyseries1);
return new XYBarDataset(xyseriescollection, 0.90000000000000002D);
}
}
效果:
3、折线图
-
-
import java.awt.BasicStroke;
-
import java.awt.Color;
-
import java.awt.Font;
-
import java.io.FileOutputStream;
-
import java.io.IOException;
-
import java.io.OutputStream;
-
import java.util.Date;
-
import org.jfree.chart.ChartFactory;
-
import org.jfree.chart.ChartUtilities;
-
import org.jfree.chart.JFreeChart;
-
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.HorizontalAlignment;
-
import org.jfree.ui.RectangleEdge;
-
-
public class LineChart {
-
public static void main(String[] agrs) throws IOException{ -
-
// 打开一个输出流 -
OutputStream outputStream=new FileOutputStream("LineChart.png"); -
-
// 获取数据集对象 -
CategoryDataset dataset = createDataset(); -
// 创建图形对象 -
JFreeChart jfreechart = ChartFactory.createLineChart("08年图书销售量", -
null, "销售量", dataset, PlotOrientation.VERTICAL, false, true, -
false); -
// 设置图表的子标题 -
jfreechart.addSubtitle(new TextTitle("按月份")); -
// 创建一个标题 -
TextTitle texttitle = new TextTitle("日期: " + new Date()); -
// 设置标题字体 -
texttitle.setFont(new Font("黑体", 0, 10)); -
// 设置标题向下对齐 -
texttitle.setPosition(RectangleEdge.BOTTOM); -
// 设置标题向右对齐 -
texttitle.setHorizontalAlignment(HorizontalAlignment.RIGHT); -
// 添加图表的子标题 -
jfreechart.addSubtitle(texttitle); -
// 设置图表的背景色为白色 -
jfreechart.setBackgroundPaint(Color.white); -
// 获得图表区域对象 -
CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot(); -
categoryplot.setBackgroundPaint(Color.lightGray); -
categoryplot.setRangeGridlinesVisible (false); -
// 获显示线条对象 -
LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot -
.getRenderer(); -
lineandshaperenderer.setBaseShapesVisible(true); -
lineandshaperenderer.setDrawOutlines(true); -
lineandshaperenderer.setUseFillPaint(true); -
lineandshaperenderer.setBaseFillPaint(Color.white); -
// 设置折线加粗 -
lineandshaperenderer.setSeriesStroke(0, new BasicStroke(3F)); -
lineandshaperenderer.setSeriesOutlineStroke(0, new BasicStroke(2.0F)); -
// 设置折线拐点 -
lineandshaperenderer.setSeriesShape(0, -
new java.awt.geom.Ellipse2D.Double(-5D, -5D, 10D, 10D)); -
-
// 将图表已数据流的方式返回给客户端 -
ChartUtilities.writeChartAsPNG(outputStream, jfreechart, -
500, 270); -
} -
-
-
private static CategoryDataset createDataset() { -
DefaultCategoryDataset defaultdataset = new DefaultCategoryDataset(); -
for (int i = 1; i <= 12; i++) { -
defaultdataset.addValue(DataUtils.getRandomData(), "JAVA", i + "月"); -
defaultdataset.addValue(DataUtils.getRandomData(), "PHP", i + "月"); -
-
} -
return defaultdataset; -
} -
-
}
package com.helloweenvsfei.jfreechart;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
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.HorizontalAlignment;
import org.jfree.ui.RectangleEdge;
public class LineChart {
public static void main(String[] agrs) throws IOException{
// 打开一个输出流
OutputStream outputStream=new FileOutputStream("LineChart.png");
// 获取数据集对象
CategoryDataset dataset = createDataset();
// 创建图形对象
JFreeChart jfreechart = ChartFactory.createLineChart("08年图书销售量",
null, "销售量", dataset, PlotOrientation.VERTICAL, false, true,
false);
// 设置图表的子 题
jfreechart.addSubtitle(new TextTitle("按月份"));
// 创建一个 题
TextTitle texttitle = new TextTitle("日期: " + new Date());
// 设置 题字体
texttitle.setFont(new Font("黑体", 0, 10));
// 设置 题向下对齐
texttitle.setPosition(RectangleEdge.BOTTOM);
// 设置 题向右对齐
texttitle.setHorizontalAlignment(HorizontalAlignment.RIGHT);
// 添 图表的子 题
jfreechart.addSubtitle(texttitle);
// 设置图表的背景色为白色
jfreechart.setBackgroundPaint(Color.white);
// 获得图表区域对象
CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
categoryplot.setBackgroundPaint(Color.lightGray);
categoryplot.setRangeGridlinesVisible(false);
// 获显示线条对象
LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot
.getRenderer();
lineandshaperenderer.setBaseShapesVisible(true);
lineandshaperenderer.setDrawOutlines(true);
lineandshaperenderer.setUseFillPaint(true);
lineandshaperenderer.setBaseFillPaint(Color.white);
// 设置折线 粗
lineandshaperenderer.setSeriesStroke(0, new BasicStroke(3F));
lineandshaperenderer.setSeriesOutlineStroke(0, new BasicStroke(2.0F));
// 设置折线拐点
lineandshaperenderer.setSeriesShape(0,
new java.awt.geom.Ellipse2D.Double(-5D, -5D, 10D, 10D));
// 将图表已数据流的方式返回给客户端
ChartUtilities.writeChartAsPNG(outputStream, jfreechart,
500, 270);
}
private static CategoryDataset createDataset() {
DefaultCategoryDataset defaultdataset = new DefaultCategoryDataset();
for (int i = 1; i <= 12; i++) {
defaultdataset.addValue(DataUtils.getRandomData(), "JAVA", i + "月");
defaultdataset.addValue(DataUtils.getRandomData(), "PHP", i + "月");
}
return defaultdataset;
}
}
效果 :
4、区域图
-
import java.awt.Color;
-
import java.io.FileOutputStream;
-
import java.io.IOException;
-
import java.io.OutputStream;
-
-
-
import org.jfree.chart.ChartFactory;
-
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.plot.CategoryPlot;
-
import org.jfree.chart.plot.PlotOrientation;
-
import org.jfree.data.category.CategoryDataset;
-
import org.jfree.data.category.DefaultCategoryDataset;
-
import org.jfree.ui.RectangleInsets;
-
-
public class AreaChart {
-
public static void main(String[] agrs) throws IOException{ -
// 打开一个输出流 -
OutputStream outputStream=new FileOutputStream("AreaChart.png"); -
-
// 获取数据集对象 -
CategoryDataset dataset = createDataset(); -
// 创建图形对象 -
JFreeChart jfreechart = ChartFactory.createAreaChart("08年图书销售量排名", -
"按季度", "销量", dataset, PlotOrientation.VERTICAL, true, true, -
false); -
// 获得图表区域对象 -
CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot(); -
//设置前景色为50%透明 -
categoryplot.setForegroundAlpha(0.5F); -
//设置X轴偏移量 -
categoryplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D)); -
//设置北景色为浅灰色 -
categoryplot.setBackgroundPaint(Color.lightGray); -
//设置显示网格线 -
categoryplot.setDomainGridlinesVisibl e(true); -
//设置网格线为白色 -
categoryplot.setDomainGridlinePaint(Color.white); -
//设置显示边界线 -
categoryplot.setRangeGridlinesVisible (true); -
//设置显示边界线为白色 -
categoryplot.setRangeGridlinePaint(Color.white); -
//获得X轴对象 -
CategoryAxis categoryaxis = categoryplot.getDomainAxis(); -
//设置X轴标签位置为45度角 -
categoryaxis.setCategoryLabelPosition s(CategoryLabelPositions.UP_45); -
//设置与X轴的补白为0 -
categoryaxis.setLowerMargin(0.0D); -
categoryaxis.setUpperMargin(0.0D); -
-
// 将图表已数据流的方式返回给客户端 -
ChartUtilities.writeChartAsPNG(outputStream, jfreechart, -
500, 270); -
-
} -
-
private static CategoryDataset createDataset() { -
// 时间维度 -
String[] category1 = { "第一季度", "第二季度", "第三季度", "第四季度" }; -
// 分类维度 -
String[] category2 = { "JAVA", "C/C++", "PHP" }; -
DefaultCategoryDataset defaultdataset = new DefaultCategoryDataset(); -
for (int i = 0; i < category1.length; i++) { -
String category = category1[i]; -
for (int j = 0; j < category2.length; j++) { -
String cat = category2[j]; -
// 模拟添加数据 -
defaultdataset.addValue(DataUtils.getRandomData(), cat, category); -
} -
} -
return defaultdataset; -
} -
-
-
}
package com.helloweenvsfei.jfreechart;
import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.jfree.chart.ChartFactory;
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.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.RectangleInsets;
public class AreaChart {
public static void main(String[] agrs) throws IOException{
// 打开一个输出流
OutputStream outputStream=new FileOutputStream("AreaChart.png");
// 获取数据集对象
CategoryDataset dataset = createDataset();
// 创建图形对象
JFreeChart jfreechart = ChartFactory.createAreaChart("08年图书销售量排名",
"按季度", "销量", dataset, PlotOrientation.VERTICAL, true, true,
false);
// 获得图表区域对象
CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
//设置前景色为50%透明
categoryplot.setForegroundAlpha(0.5F);
//设置X轴偏移量
categoryplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
//设置北景色为浅灰色
categoryplot.setBackgroundPaint(Color.lightGray);
//设置显示网 线
categoryplot.setDomainGridlinesVisible(true);
//设置网 线为白色
categoryplot.setDomainGridlinePaint(Color.white);
//设置显示边界线
categoryplot.setRangeGridlinesVisible(true);
//设置显示边界线为白色
categoryplot.setRangeGridlinePaint(Color.white);
//获得X轴对象
CategoryAxis categoryaxis = categoryplot.getDomainAxis();
//设置X轴 签位置为45度角
categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
//设置与X轴的补白为0
categoryaxis.setLowerMargin(0.0D);
categoryaxis.setUpperMargin(0.0D);
// 将图表已数据流的方式返回给客户端
ChartUtilities.writeChartAsPNG(outputStream, jfreechart,
500, 270);
}
private static CategoryDataset createDataset() {
// 时间维度
String[] category1 = { "第一季度", "第二季度", "第三季度", "第四季度" };
// 分类维度
String[] category2 = { "JAVA", "C/C++", "PHP" };
DefaultCategoryDataset defaultdataset = new DefaultCategoryDataset();
for (int i = 0; i < category1.length; i++) {
String category = category1[i];
for (int j = 0; j < category2.length; j++) {
String cat = category2[j];
// 模拟添 数据
defaultdataset.addValue(DataUtils.getRandomData(), cat, category);
}
}
return defaultdataset;
}
}
效果:
5、时序图
-
import java.awt.BasicStroke;
-
import java.io.FileOutputStream;
-
import java.io.IOException;
-
import java.io.OutputStream;
-
-
import org.jfree.chart.ChartFactory;
-
import org.jfree.chart.ChartUtilities;
-
import org.jfree.chart.JFreeChart;
-
import org.jfree.chart.plot.XYPlot;
-
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;

加载中…