javaweb给数据进行设置饼图,折线图,柱状图
1.只需要引入三个jar包如下图
然后分别有已经写好的三个绘制工具类
1.饼状图代码
package com.lisheng.util;
import java.awt.Font;
import java.text.DecimalFormat;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.util.Rotation;public class Bzt
{/**
* 创建饼状图
*
* @param title
* 标题
* @param map
* 键值对(水果名称 水果数量)
* @return
*/
public static JFreeChart createPieChart(String title, Map map)
{
DefaultPieDataset dpd = createPieDataset(map);
// 创建PieChart对象(标题,数据集,是否显示图例,是否生成工具提示,是否生成URL链接)
JFreeChart chart = ChartFactory.createPieChart3D(title, dpd, true,true,false);
setPie3DStyle(chart);
return chart;
}/**
* 创建饼状图数据
*
* @param map
* @return
*/
private static DefaultPieDataset createPieDataset(Map map)
{
DefaultPieDataset dpd = new DefaultPieDataset();
Set<String> set = map.keySet();
for (Iterator<String> itor = set.iterator(); itor.hasNext();)
{
String key = itor.next();
double x = Double.parseDouble(map.get(key).toString());
dpd.setValue(key, x);
}
return dpd;
}/**
* 设置饼状图3D样式
*
* @param chart
*/
private static void setPie3DStyle(JFreeChart chart)
{
// 获得3D的水晶饼图对象
PiePlot3D pieplot3d = (PiePlot3D) chart.getPlot();
// 设置开始角度
pieplot3d.setStartAngle(150D);
// 设置方向为”顺时针方向“
pieplot3d.setDirection(Rotation.CLOCKWISE);
// 设置透明度,0.5F为半透明,1为不透明,0为全透明
pieplot3d.setForegroundAlpha(0.5F);
pieplot3d.setNoDataMessage("没有相应的数据显示");
Font font = new Font("宋体", Font.ITALIC, 12);
chart.getTitle().setFont(new Font("宋体", Font.BOLD, 22));
pieplot3d.setLabelFont(font);
chart.getLegend().setItemFont(font);
StandardPieSectionLabelGenerator standarPieIG = new StandardPieSectionLabelGenerator("{0}:({1},{2})",
new DecimalFormat("0.0"), new DecimalFormat("0.0%"));
pieplot3d.setLabelGenerator(standarPieIG);
}}
2.折线图代码
package com.lisheng.util;
import java.awt.Font;
import org.jfree.chart.ChartFactory;
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.data.category.DefaultCategoryDataset;public class Zxt {
/**
* 创建折线图
* @param title
* @param XLabel
* @param YLabel
* @return
*/
public static JFreeChart createLineChart(String title,String XLabel,String YLabel,DefaultCategoryDataset dataset) {
//该数据的Demo由系统提供,故不传入相应的Map或List数据了
// 定义图表对象(折线图名称,横坐标名称,纵坐标名称,数据, 水平显示图像)
JFreeChart chart = ChartFactory.createLineChart(title,XLabel,YLabel,dataset,PlotOrientation.VERTICAL,true,true,true);
setLineStyle(chart);
return chart;
}
/**
* 设置折线图样式
*
* @param chart
*/
private static void setLineStyle(JFreeChart chart){
CategoryPlot plot = chart.getCategoryPlot();
//设置标题字体样式
chart.getTitle().setFont(new Font("黑体", Font.ITALIC,14));
//取得横轴和设置横轴样式
CategoryAxis categoryAxis = plot.getDomainAxis();
categoryAxis.setLabelFont(new Font("宋体", Font.BOLD, 14));
//横轴分类标签
categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD);
categoryAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 14));
plot.setRangeGridlinesVisible(true); //是否显示格子线
plot.setBackgroundAlpha(0.3f); //设置背景透明度
NumberAxis rangeAxis = (NumberAxis)plot.getRangeAxis();
rangeAxis.setLabelFont(new Font("宋体", Font.BOLD, 14));
//设置工具工具提示字体样式
chart.getLegend().setItemFont(new Font("宋体", Font.BOLD, 14));
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
rangeAxis.setAutoRangeIncludesZero(true);
rangeAxis.setUpperMargin(0.20);
rangeAxis.setLabelAngle(Math.PI / 2.0);
}
}
3.柱状图
package com.lisheng.util;
import java.awt.Font;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;import org.jfree.chart.ChartFactory;
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.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.TextAnchor;public class Tzt {
/**
* 创建柱状图
* @param title 标题
* @param XLabel X轴标签
* @param YLabel Y轴标签
* @param map 键值对
* @return
*/
public static JFreeChart createBarChart(String title, String XLabel, String YLabel,DefaultCategoryDataset dataset) {
// 创建一个柱状图(图表标题,X轴显示标签,Y轴显示标签,数据集,图表方向(水平or垂直),是否显示图例[对于简单图应为false],是否生成工具,是否生成url链接)
JFreeChart chart = ChartFactory.createBarChart3D(title, XLabel, YLabel, dataset, PlotOrientation.VERTICAL,
true, true, true);
setBarStyle(chart);
return chart;
}/**
* 设置柱状图样式
*
* @param chart
*/
private static void setBarStyle(JFreeChart chart) {
CategoryPlot plot = chart.getCategoryPlot();
// 设置标题字体样式
chart.getTitle().setFont(new Font("黑体", Font.ITALIC, 14));
// 取得横轴和设置横轴样式
CategoryAxis categoryAxis = plot.getDomainAxis();
categoryAxis.setLabelFont(new Font("宋体", Font.BOLD, 14));
// 横轴分类标签
categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD);
categoryAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 14));
// 取得纵轴和设置纵轴样式
NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();
numberAxis.setLabelFont(new Font("宋体", Font.BOLD, 14));// 显示每个柱的数值,并修改该数值的字体属性
BarRenderer3D renderer = new BarRenderer3D();
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
// 默认的数字显示在柱子中,通过如下两句可调整数字的显示
// 注意:此句很关键,若无此句,那数字的显示会被覆盖,给人数字没有显示出来的问题 ,将数字显示在柱状图上面
renderer.setBasePositiveItemLabelPosition(
new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
renderer.setItemLabelAnchorOffset(10D);
// 设置每个地区所包含的平行柱的之间距离
// renderer.setItemMargin(0.3);
plot.setRenderer(renderer);
}
}
饼状图只需要将数据储存为map类型即可如下
private PicService picService=new Picimpl();
private JFreeChart chart;
//得到饼状图
public String all1()
{
List<PicEntity> pic = picService.getAllpic();----获取数据
Map map=new HashMap(); ---创建map集合
for (PicEntity p : pic)
{
map.put(p.getEename(),p.getPpcount()); 循环将数据放入map中。姓名为key 数量为value
}
Bzt bzt=new Bzt(); --得到饼图工具类
chart=bzt.createPieChart("各个区域客户分布图", map); ------- (“标题”,数据(map))
return "all1";
}public JFreeChart getChart()
{
return chart;
}public void setChart(JFreeChart chart)
{
this.chart = chart;
}
2.创建折线图
private StuService stuService = new StuImpl();
private JFreeChart chart;public JFreeChart getChart()
{
return chart;
}public void setChart(JFreeChart chart)
{
this.chart = chart;
}
public String all()
{
Zxt zxt=new Zxt();
DefaultCategoryDataset data=new DefaultCategoryDataset(); ----创建一个对象
List<Stu> ar=this.stuService.getAllCost("龚小俊"); 条件得到对应数据
for(Stu s:ar)
{
data.addValue(s.getScost(), s.getSname(), s.getSmonth()+"/月");
}
List<Stu> ar1=this.stuService.getAllCost("龚大俊");
for(Stu s:ar1)
{
data.addValue(s.getScost(), s.getSname(), s.getSmonth()+"/月");
}
this.chart=zxt.createLineChart("个人消费比例图","月分","金额", data);
return "myall";
}
3.条状图
package com.lisheng.action;
import java.util.List;
import org.jfree.chart.JFreeChart;
import org.jfree.data.category.DefaultCategoryDataset;import com.lisheng.entity.School;
import com.lisheng.impl.SchoolImpl;
import com.lisheng.service.SchoolService;
import com.lisheng.util.Tzt;public class SchoolAction
{private SchoolService schoolService = new SchoolImpl();
private JFreeChart chart;public JFreeChart getChart()
{
return chart;
}public void setChart(JFreeChart chart)
{
this.chart = chart;
}
public String all()
{
Tzt tzt=new Tzt();
DefaultCategoryDataset data=new DefaultCategoryDataset();
List<School> ar=this.schoolService.getAllSchool(2002);
for(School s:ar)
{
data.addValue(s.getScount(), s.getSname(), s.getSyear()+"/年");
}
List<School> ar1=this.schoolService.getAllSchool(2003);
for(School s:ar1)
{
data.addValue(s.getScount(), s.getSname(), s.getSyear()+"/年");
}
this.chart=tzt.createBarChart("红安县2002年各校人数比例图", "学校", "人数", data);
return "myall";
}}
javaweb给数据进行设置饼图,折线图,柱状图相关推荐
- Matplotlib 2 |折线图| 柱状图| 堆叠图| 面积图| 填图| 饼图| 直方图| 散点图| 极坐标| 图箱型图
目录 Matplotlib 2 |折线图| 柱状图| 堆叠图| 面积图| 填图| 饼图| 直方图| 散点图| 极坐标| 图箱型图 1.基本图表绘制 plt.plot() ts.plot() 由Seri ...
- 【Plotly】python数据可视化神器——绘制折线图
Plotly简介 Plotly是用于机器学习.数据挖掘等领域的数据可视化包. 其图标视觉效果简洁大气,可互动性强,成为我工作中进行数据可视化的一大利器,接下来我们就从最简单的折线图,带领大家逐渐入门p ...
- 使用python读取excel中的数据,并绘制折线图
使用python读取excel中的数据,并绘制折线图 做实验的时候采集到一些数据,从文本拷贝到excel,然后从十六进制转换成十进制.图表是分析数据的有利工具,使用python绘制出的图表简明美观.所 ...
- jfreechart折线图+柱状图、柱状图(堆叠)+折线图、饼状图、环形图
记录jfreechart生成图片测试程,主要是为自己研究过之后的记录:) 折线图+柱状图 柱状图(堆叠)+折线图 获取饼状图 获取环形图 jar包下载地址:https://download.csdn. ...
- poi导出excel文件,并生成原生图表(包括折线图,柱状图,饼状图,面积图)
前段时间,因为客户需要,要做一个导出excel文件功能,并能生成原生的图表的(不是把图片插入到excel文档),找了很多文档看,也看了很多别人的代码,个人也总结了一下,不足之处,请各位大牛谅解. 需要 ...
- chartxy 柱状图_ChartControl 折线图 柱状图
添加折线图(柱状图) 拖动ChartControl到Form上 在Series Collection中添加Line(或Bar) DevExpress.XtraCharts.Series series1 ...
- echarts 折线图 + 柱状图
Echarts 常用各类图表模板配置 注意: 这里主要就是基于各类图表,更多的使用 Echarts 的各类配置项: 以下代码都可以复制到 Echarts 官网,直接预览: 图标模板目录 Echarts ...
- Echarts实现折线图+柱状图+折线图填充
用Echarts练习实现折线图+柱状图+折线图填充,以便后续使用时可直接复制 option = {title: {text: '动态数据',subtext: '纯属虚构'},tooltip: {tri ...
- echarts图表折线图柱状图多个X轴Y轴以及一个Y轴反向
echarts图表折线图柱状图多个X轴Y轴以及一个Y轴反向 option1: {color: ['#21E9F6', '#F2CE2E', '#EE2929', '#006DD9', '#1789FF ...
最新文章
- 好程序员分享24个canvas基础知识小结
- MySQL 性能优化,索引和查询优化
- go panic recover 异常处理
- ThreadLocal小记
- C#配置及使用log4net
- 海量用户标签系统之存储架构设计(Bigmap算法)
- 菜鸟教程java二维数组_asp数组的使用介绍
- NTKO word在线文本编辑控件写页眉页脚
- C语言队列(排队)先进先出.实现全部函数
- 12堂超级搜索术课程笔记链接汇总
- 微信 表情 android,android - 微信自定义表情在哪个文件夹?
- JS-JavaScript打开新页面的两种方式:当前页面打开和新页面打开
- RS/CQI/SINR/RSRP/RSSI都是什么意思?
- 国内外最顶级的12大看板工具
- C语言版,传教士与野人渡河问题,使用深度优先搜索法求解(DFS),变态版,随便输入人数和船的最大载人数,人工智能经典题目,简单易懂,注释到位,没有bug
- 小米5S刷机认真看一眼就能会的简单详细教图文
- 粒子群算法(PSO)的python实现
- 联想 Thinkpad SL410使用体验
- MATLAB 声音文件处理
- c++ stack用法 入门必看 超详细
热门文章
- 错误:UnmappedTerms cannot be cast to org.elasticsearch.search.aggregations.bucket.terms.StringTerms
- python中常见单词意思_819. 最常见的单词(Python)
- 2Pai荣湃单通道数字隔离器π110M30兼容代替Si8410AB-D-IS 3.0kVrms 10Mbps 结构简单 方案更加灵活
- 金山可牛合并成金山网络 金山毒霸全面免费
- java 日历记事本_用Java实现日历记事本.doc
- 4_Spring常用的Annotation组件
- 最新行政区划代码省市区数据库(2018年5月1日 )
- PDFlib Adds support for PHP
- 【图的存储(二维数组方法)+图的遍历(dfs)】案例6-1.3 哥尼斯堡的“七桥问题”
- 手机维修行业商业策划书