import java.awt.Font;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
public class CategoryServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 6228999300370044372L;
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("image/jpeg");
//创建3D柱形图
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(610, "广州", "猪肉");
dataset.addValue(220, "广州", "牛肉");
dataset.addValue(530, "广州", "鸡肉");
dataset.addValue(340, "广州", "鱼肉");
JFreeChart chart = ChartFactory.createBarChart3D("肉类销量统计图", "肉类",
"销量", dataset, PlotOrientation.VERTICAL, true, false,
false);
Font font = new Font("宋体", 0, 12);//创建字体对象
chart.getLegend().setItemFont(font);//设置图例字体
chart.getTitle().setFont(font);//设置标题字体
CategoryPlot plot = chart.getCategoryPlot();
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setTickLabelFont(font);//设置X轴坐标上的文字
domainAxis.setLabelFont(font);//设置X轴标题上的文字
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setTickLabelFont(font);//设置Y轴坐标上的文字
rangeAxis.setLabelFont(font);//设置Y轴标题上的文字
// 输出图片
ChartUtilities.writeChartAsJPEG(response.getOutputStream(),0.5f,chart,800,600,null);
}
}
2.
import java.awt.Font;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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;
public class PieServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 8827627228843788090L;
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("image/jpeg");
// 设置数据集
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("初中高级程序员", 0.55);
dataset.setValue("项目经理", 0.1);
dataset.setValue("系统分析师", 0.1);
dataset.setValue("软件架构师", 0.1);
dataset.setValue("其他", 0.2);
//创建3D饼图
JFreeChart chart = ChartFactory.createPieChart3D("饼图", // chart title
dataset,// data
true, // include legend
true,
false);
// 设置图表属性
PiePlot piePlot = (PiePlot) chart.getPlot();
piePlot.setCircular(true);//饼图是否一定是正圆
Font font = new Font("宋体", 0, 12);//创建字体对象
piePlot.setLabelFont(font);//设置标签字体
chart.getTitle().setFont(font);//设置标题字体
//没有数据的时候显示的内容
piePlot.setNoDataMessage("无数据显示");
piePlot.setCircular(false);
piePlot.setLabelGap(0.02D);
// 输出图片
ChartUtilities.writeChartAsJPEG(response.getOutputStream(),0.5f,chart,800,600,null);
}
}
3.
import java.awt.Font;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
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 PolyLine {
public static void createPolyLine() {
/*
*
* 报表字体
*/
final Font CHART_FONT = new Font("宋体", 12, 12);
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(100, "ten", "jfree");
dataset.addValue(150, "ten", "chart");
dataset.addValue(300, "ten", "struts");
dataset.addValue(100, "ten", "hibernate");
/*
*
* 三维折线图把createLineChart换成createLineChart3D就可以实现三维折线图。
*/
JFreeChart chart = ChartFactory.createLineChart3D(
"chart", // 标题
"num", // 横坐标
"type", // 纵坐标
dataset, // 数据
PlotOrientation.VERTICAL, // 竖直图表
true, // 是否显示legend
false, // 是否显示tooltip
false // 是否使用url链接
);
// XYPlot plot = null;
// 设置字体,解决中文乱码问题
chart.getTitle().setFont(CHART_FONT);
chart.getLegend().setItemFont(CHART_FONT);
// plot.getRangeAxis().setLabelFont(CHART_FONT);
// plot.getDomainAxis().setLabelFont(new Font("宋体", 12, 24));
FileOutputStream fos = null;
try {
fos = new FileOutputStream("src/poly3d.png");
ChartUtilities.writeChartAsPNG(fos, chart, 400, 300);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
PolyLine.createPolyLine();
}
}
饼图
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
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.StandardChartTheme;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class Test1 extends ApplicationFrame {
/**
*
*/
private static final long serialVersionUID = -8028194135638567202L;
public static final Font DEFAULT_FONT = new Font("宋体", Font.PLAIN, 12);
public Test1(String title) {
super(title);
JPanel localJPanel = createDemoPanel();
localJPanel.setPreferredSize(new Dimension(500, 270));
setContentPane(localJPanel);
}
public static JPanel createDemoPanel() {
DefaultPieDataset defaultpiedataset = createDataset();
ChartFactory.setChartTheme(createStandardChartTheme());
JFreeChart jfreechart = ChartFactory.createPieChart("饼图",
defaultpiedataset, true, true, false);
jfreechart.setBackgroundPaint(new Color(222, 222, 255));
//获取PiePlot对象,用于设置饼图的相关属性,如:颜色、形状……
PiePlot pieplot = (PiePlot) jfreechart.getPlot();
pieplot.setSectionPaint("壹", new Color(222, 222, 255));
pieplot.setBackgroundPaint(Color.white);
pieplot.setCircular(true);
//自定义显示百分比样式
//图片中显示百分比:自定义方式,{0} 表示选项,{1} 表示数值, {2} 表示所占百分比
pieplot.setLabelGenerator(new StandardPieSectionLabelGenerator(
"{0} = {2}", NumberFormat.getNumberInstance(), NumberFormat.getPercentInstance()));
//设置图片突起部分
// pieplot.setExplodePercent("Section 1", 0.5D);
pieplot.setNoDataMessage("No data available");
ChartPanel chartpanel = new ChartPanel(jfreechart);
chartpanel.setPreferredSize(new Dimension(500, 270));
//旋转
// Rotator rotator = new Rotator(pieplot);
// rotator.start();
return chartpanel;
}
private static DefaultPieDataset createDataset() {
DefaultPieDataset defaultpiedataset = new DefaultPieDataset();
defaultpiedataset.setValue("壹", 23.3);
defaultpiedataset.setValue("贰", 56.5);
defaultpiedataset.setValue("参", 43.3);
defaultpiedataset.setValue("肆", 11.1);
return defaultpiedataset;
}
/**
* @param args
*/
public static void main(String[] args) {
Test1 test1 = new Test1("");
test1.pack();
RefineryUtilities.centerFrameOnScreen(test1);
test1.setVisible(true);
}
//设计饼图的一个标准模板
public static StandardChartTheme createStandardChartTheme() {
//创建主题样式
StandardChartTheme standardChartTheme = new StandardChartTheme("CN");
//设置标题字体
standardChartTheme.setExtraLargeFont(DEFAULT_FONT);
//设置图例子图
standardChartTheme.setRegularFont(DEFAULT_FONT);
//设置轴向字体
standardChartTheme.setLargeFont(DEFAULT_FONT);
return standardChartTheme;
}
}
条形图
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
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.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class Test2 extends ApplicationFrame {
/**
*
*/
private static final long serialVersionUID = 1107365396303352467L;
public static final Font DEFAULT_FONT = new Font("宋体", Font.PLAIN, 12);
public Test2(String title) {
super(title);
JFreeChart chart = createChart(createDataset());
ChartPanel chartPanel = new ChartPanel(chart, false);
chartPanel.setPreferredSize(new Dimension(500, 270));
setContentPane(chartPanel);
}
private CategoryDataset createDataset() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(1.0, "Row 1", "Column 1");
dataset.addValue(5.0, "Row 1", "Column 2");
dataset.addValue(3.0, "Row 1", "Column 3");
dataset.addValue(2.0, "Row 2", "Column 1");
dataset.addValue(3.0, "Row 2", "Column 2");
dataset.addValue(2.0, "Row 2", "Column 3");
return dataset;
}
private JFreeChart createChart(CategoryDataset dataset) {
JFreeChart chart = ChartFactory.createBarChart("BarChartDemo", // chart title
"Category", // domain axis label
"Value", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips?
false // URLs?
);
chart.setBackgroundPaint(Color.white);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setRangeGridlinePaint(Color.white);
//刻度轴刻度设置
NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
//设置颜色
BarRenderer renderer = (BarRenderer) plot.getRenderer();
renderer.setSeriesPaint(0, Color.gray);
renderer.setSeriesPaint(1, Color.orange);
renderer.setDrawBarOutline(false);
renderer.setDrawBarOutline(false);//设置外廓线不可见
GradientPaint gradientpaint = new GradientPaint(0.0F, 0.0F, Color.blue,
0.0F, 0.0F, new Color(0, 0, 64));
GradientPaint gradientpaint_7_ = new GradientPaint(0.0F, 0.0F,
Color.green, 0.0F, 0.0F, new Color(0, 64, 0));
GradientPaint gradientpaint_8_ = new GradientPaint(0.0F, 0.0F,
Color.red, 0.0F, 0.0F, new Color(64, 0, 0));
renderer.setSeriesPaint(0, gradientpaint);
renderer.setSeriesPaint(1, gradientpaint_7_);
renderer.setSeriesPaint(2, gradientpaint_8_);
//设置种类里直方条形图之间的空间
renderer.setItemMargin(0.0);
//设置种类标签旋转的角度,逆时针旋转
CategoryAxis categoryaxis = plot.getDomainAxis();
categoryaxis.setCategoryLabelPositions(CategoryLabelPositions
.createUpRotationLabelPositions(Math.PI / 6));
return chart;
}
/**
* @param args
*/
public static void main(String[] args) {
Test2 demo = new Test2("Bar Demo");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
相关推荐
JFreeChart是一个强大的Java库,它可以用来创建多种类型的图表,包括柱形图、饼图和折线图。本篇文章将详细介绍如何使用JFreeChart来实现这些统计图形。 首先,为了使用JFreeChart库,我们需要导入以下三个依赖库:...
`JFreeChart`库是一个强大的工具,可以用来创建各种类型的图表,包括柱形图、饼图和折线图。以下是对这些知识点的详细说明: **JFreeChart库** `JFreeChart`是一个开源Java库,用于生成高质量的2D图表。它支持多种...
JFreeChart是一个开源的Java图表库,提供了丰富的API来绘制各种统计图表,如条形图、饼图、折线图等。以下是对这些图表生成过程中的关键知识点的详细解释: ### 饼图生成(PieChart) #### 创建数据集(Dataset) ...
这个库提供了丰富的图表类型,包括饼状图、柱形图、折线图、区域图、时序图、甘特图和仪表盘,以及组合图,能够满足各种数据可视化的需求。下面将对这些图表类型及其在实际应用中的使用进行详细阐述。 1. **饼状图...
Java中,JFreeChart同样支持创建折线图,并且支持多系列数据、自定义轴刻度以及添加数据标记等功能。 在实际开发中,Java程序员通常会结合数据库查询结果或者CSV、Excel等数据文件来生成这些图表。他们需要处理数据...
JFreeChart是Java的一个图形库,它支持多种类型的图表,包括条形图(柱形图)、饼图、线图、散点图、面积图等。它的设计目标是提供一个易于使用的API,以便开发人员能够快速地创建高质量的图表。JFreeChart不仅支持...
2. 常见的可视化图表类型:包括柱形图、条形图(如堆积柱形图、分组柱形图)、折线图(如堆叠折线图、曲线图)、散点图(如气泡图、散点矩阵)、饼图和圆环图(如扇形图),以及它们的各种变形,以适应不同的数据...
开发者可以使用AnyChart创建各种类型的图表,如柱形图、折线图、饼图等。 3.Axiis:Axiis是一个针对Flex的数据可视化框架。它被设计成一个精确和模块化的表述框架,开发人员和设计人员可以使用Axiis创建强大的数据...