JFreeChart--柱状图

来源:互联网 发布:java图书管理系统报告 编辑:程序博客网 时间:2024/04/18 11:02

JFreeChart是JAVA平台上的一个开放的图表绘制类库。它完全使用JAVA语言编写,是为applications, applets, servlets 以及JSP等使用所设计。JFreeChart可生成饼图(pie charts)、柱状图(bar charts)、散点图(scatter plots)、时序图(time series)、甘特图(Gantt charts)等等多种图表,并且可以产生PNG和JPEG格式的输出,还可以与PDF和EXCEL关联。


源码:

@Testpublic void createHistogram() {//1、创建数据集DefaultCategoryDataset dataset = new DefaultCategoryDataset();dataset.addValue(510, "深圳", "苹果");dataset.addValue(320, "深圳", "香蕉");dataset.addValue(580, "深圳", "橘子");dataset.addValue(390, "深圳", "梨子");//2、创建JFreechartJFreeChart chart = ChartFactory.createBarChart("水果销量统计图", "水果", "销量", dataset);//3、获取图标区域对象CategoryPlot plot = chart.getCategoryPlot();//4、水平底部列表CategoryAxis axis = plot.getDomainAxis();axis.setLabelFont(new Font("黑体", Font.BOLD, 14));//水平底部标题axis.setTickLabelFont(new Font("宋体", Font.BOLD, 12));//垂直标题//5、获取柱状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));try {ChartUtilities.writeChartAsJPEG(new FileOutputStream("E:/Histogram.jpg"), chart, 400, 300, null);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}


源码:

@Testpublic void createHistogram2() {//1、创建数据集DefaultCategoryDataset dataset = new DefaultCategoryDataset();dataset.addValue(1320, "苹果", "");dataset.addValue(720, "香蕉", "");dataset.addValue(830, "橘子", "");dataset.addValue(400, "梨子", "");//2、创建JFreechartJFreeChart chart = ChartFactory.createBarChart("水果销量统计图", "水果", "销量", dataset);//3、获取图标区域对象CategoryPlot plot = chart.getCategoryPlot();//4、水平底部列表CategoryAxis axis = plot.getDomainAxis();axis.setLabelFont(new Font("黑体", Font.BOLD, 14));//水平底部标题axis.setTickLabelFont(new Font("宋体", Font.BOLD, 12));//垂直标题//5、获取柱状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));try {ChartUtilities.writeChartAsJPEG(new FileOutputStream("E:/Histogram2.jpg"), chart, 400, 300, null);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}


源码:

@Testpublic void createHistogram3() {//1、创建数据集DefaultCategoryDataset dataset = new DefaultCategoryDataset();dataset.addValue(1230, "苹果", "鹤壁");dataset.addValue(1110, "香蕉", "鹤壁");dataset.addValue(1120, "橘子", "鹤壁");dataset.addValue(1210, "梨子", "鹤壁");dataset.addValue(720, "苹果", "西安");dataset.addValue(750, "香蕉", "西安");dataset.addValue(860, "橘子", "西安");dataset.addValue(800, "梨子", "西安");dataset.addValue(830, "苹果", "深圳");dataset.addValue(789, "香蕉", "深圳");dataset.addValue(820, "橘子", "深圳");dataset.addValue(900, "梨子", "深圳");dataset.addValue(700, "苹果", "北京");dataset.addValue(780, "香蕉", "北京");dataset.addValue(830, "橘子", "北京");dataset.addValue(920, "梨子", "北京");//2、创建JFreechartJFreeChart chart = ChartFactory.createBarChart("水果销量统计图", "水果", "销量", dataset);//3、获取图标区域对象CategoryPlot plot = chart.getCategoryPlot();//4、水平底部列表CategoryAxis axis = plot.getDomainAxis();axis.setLabelFont(new Font("黑体", Font.BOLD, 14));//水平底部标题axis.setTickLabelFont(new Font("宋体", Font.BOLD, 12));//垂直标题//5、获取柱状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));try {ChartUtilities.writeChartAsJPEG(new FileOutputStream("E:/Histogram3.jpg"), chart, 400, 300, null);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}


源码:

@Testpublic void createHistogram4() {//1、创建数据集DefaultCategoryDataset dataset = new DefaultCategoryDataset();dataset.addValue(1230, "苹果", "鹤壁");dataset.addValue(1110, "香蕉", "鹤壁");dataset.addValue(1120, "橘子", "鹤壁");dataset.addValue(1210, "梨子", "鹤壁");dataset.addValue(720, "苹果", "西安");dataset.addValue(750, "香蕉", "西安");dataset.addValue(860, "橘子", "西安");dataset.addValue(800, "梨子", "西安");dataset.addValue(830, "苹果", "深圳");dataset.addValue(789, "香蕉", "深圳");dataset.addValue(820, "橘子", "深圳");dataset.addValue(900, "梨子", "深圳");dataset.addValue(700, "苹果", "北京");dataset.addValue(780, "香蕉", "北京");dataset.addValue(830, "橘子", "北京");dataset.addValue(920, "梨子", "北京");//2、创建JFreechartJFreeChart chart = ChartFactory.createBarChart("水果销量统计图", "水果", "销量", dataset);//3、获取图标区域对象CategoryPlot plot = chart.getCategoryPlot();plot.setBackgroundPaint(Color.WHITE);//设置网格背景色plot.setDomainGridlinePaint(Color.PINK);//设置竖线颜色plot.setRangeGridlinePaint(Color.PINK);//设置网格横线颜色//显示每个柱的数值,并修改该数值的字体属性BarRenderer3D renderer3d = new BarRenderer3D();renderer3d.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());renderer3d.setBaseItemLabelsVisible(true);renderer3d.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));renderer3d.setItemLabelAnchorOffset(10D);renderer3d.setItemMargin(0.4);plot.setRenderer(renderer3d);//4、水平底部列表CategoryAxis axis = plot.getDomainAxis();axis.setLabelFont(new Font("黑体", Font.BOLD, 14));//水平底部标题axis.setTickLabelFont(new Font("宋体", Font.BOLD, 12));//垂直标题//5、获取柱状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));try {ChartUtilities.writeChartAsJPEG(new FileOutputStream("E:/Histogram4.jpg"), chart, 400, 300, null);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}


源码:

@Testpublic void createHistogram5() {//1、创建数据集DefaultCategoryDataset dataset = new DefaultCategoryDataset();dataset.addValue(1230, "苹果", "鹤壁");dataset.addValue(1110, "香蕉", "鹤壁");dataset.addValue(1120, "橘子", "鹤壁");dataset.addValue(1210, "梨子", "鹤壁");dataset.addValue(720, "苹果", "西安");dataset.addValue(750, "香蕉", "西安");dataset.addValue(860, "橘子", "西安");dataset.addValue(800, "梨子", "西安");dataset.addValue(830, "苹果", "深圳");dataset.addValue(789, "香蕉", "深圳");dataset.addValue(820, "橘子", "深圳");dataset.addValue(900, "梨子", "深圳");dataset.addValue(700, "苹果", "北京");dataset.addValue(780, "香蕉", "北京");dataset.addValue(830, "橘子", "北京");dataset.addValue(920, "梨子", "北京");//2、创建JFreechartJFreeChart chart = ChartFactory.createBarChart("水果销量统计图", "水果", "销量", dataset);//3、获取图标区域对象CategoryPlot plot = chart.getCategoryPlot();plot.setBackgroundPaint(Color.WHITE);//设置网格背景色plot.setDomainGridlinePaint(Color.PINK);//设置竖线颜色plot.setRangeGridlinePaint(Color.PINK);//设置网格横线颜色//显示每个柱的数值,并修改该数值的字体属性BarRenderer3D renderer3d = new BarRenderer3D();renderer3d.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());renderer3d.setBaseItemLabelsVisible(true);renderer3d.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));renderer3d.setItemLabelAnchorOffset(10D);renderer3d.setItemMargin(0.4);plot.setRenderer(renderer3d);plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);//4、水平底部列表CategoryAxis axis = plot.getDomainAxis();axis.setLabelFont(new Font("黑体", Font.BOLD, 14));//水平底部标题axis.setTickLabelFont(new Font("宋体", Font.BOLD, 12));//垂直标题//5、获取柱状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));try {ChartUtilities.writeChartAsJPEG(new FileOutputStream("E:/Histogram5.jpg"), chart, 400, 300, null);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}


源码:

@Testpublic void createHistogram5() {//1、创建数据集DefaultCategoryDataset dataset = new DefaultCategoryDataset();dataset.addValue(1230, "苹果", "鹤壁");dataset.addValue(1110, "香蕉", "鹤壁");dataset.addValue(1120, "橘子", "鹤壁");dataset.addValue(1210, "梨子", "鹤壁");dataset.addValue(720, "苹果", "西安");dataset.addValue(750, "香蕉", "西安");dataset.addValue(860, "橘子", "西安");dataset.addValue(800, "梨子", "西安");dataset.addValue(830, "苹果", "深圳");dataset.addValue(789, "香蕉", "深圳");dataset.addValue(820, "橘子", "深圳");dataset.addValue(900, "梨子", "深圳");dataset.addValue(700, "苹果", "北京");dataset.addValue(780, "香蕉", "北京");dataset.addValue(830, "橘子", "北京");dataset.addValue(920, "梨子", "北京");//2、创建JFreechartJFreeChart chart = ChartFactory.createBarChart("水果销量统计图", "水果", "销量", dataset);//3、获取图标区域对象CategoryPlot plot = chart.getCategoryPlot();plot.setBackgroundPaint(Color.WHITE);//设置网格背景色plot.setDomainGridlinePaint(Color.PINK);//设置竖线颜色plot.setRangeGridlinePaint(Color.PINK);//设置网格横线颜色//显示每个柱的数值,并修改该数值的字体属性BarRenderer3D renderer3d = new BarRenderer3D();renderer3d.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());renderer3d.setBaseItemLabelsVisible(true);renderer3d.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));renderer3d.setItemLabelAnchorOffset(10D);renderer3d.setItemMargin(0.4);plot.setRenderer(renderer3d);plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);//4、水平底部列表CategoryAxis axis = plot.getDomainAxis();axis.setLabelFont(new Font("黑体", Font.BOLD, 14));//水平底部标题axis.setTickLabelFont(new Font("宋体", Font.BOLD, 12));//垂直标题//5、获取柱状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));try {ChartUtilities.writeChartAsJPEG(new FileOutputStream("E:/Histogram5.jpg"), chart, 400, 300, null);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}

0 0
原创粉丝点击