java生成柱状图

5 篇文章 0 订阅

生成柱状图

代码如下:

public class BarChart {
	 //1-写一个ChartPanel变量
	 ChartPanel jframe;

	 //2-BarChart的无参数的构造方法
	public BarChart() {
          DefaultCategoryDataset data=(DefaultCategoryDataset) getDataSet();
          JFreeChart chart=ChartFactory.createBarChart3D(
        		  "水果销售数据统计",  //图表标题
        		  "水果种类",//目录轴的显示标签 
        		  "数量",//数值轴的显示标签 
        		  data, 
        		  PlotOrientation.VERTICAL,  //图表方向 水平 垂直
        		  true,  //是否显示图例(对于简单的图表建议显示图例)
        		  false,//是否生成工具 
        		  false);  //是否生成网址链接
          //字体设置
             //获得图表区域对象
            CategoryPlot plot=chart.getCategoryPlot();
            //水平底部列表 
            CategoryAxis domain =plot.getDomainAxis();
            //垂直标题字体设置
            domain.setTickLabelFont(new Font("黑体", Font.BOLD, 16));
            //水平底部标题设置
            domain.setLabelFont(new Font("黑体", Font.BOLD, 20));
            //获取柱状体
            ValueAxis rangeAxis=plot.getRangeAxis();
            rangeAxis.setLabelFont(new Font("黑体", Font.BOLD, 16));
            //设置lengend字体
            chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 16));
            chart.getTitle().setFont(new Font("黑体", Font.BOLD, 16));
		 //初始化Jframe
          jframe=new ChartPanel(chart);
          
		
	}
	
	//3-图表数据设置
	public static CategoryDataset getDataSet() {
		 DefaultCategoryDataset data=new DefaultCategoryDataset();
		 //设置数据
		 data.setValue(100, "北京", "苹果");
		 data.setValue(70, "北京", "香蕉");
		 data.setValue(60, "北京", "西瓜");
		 data.setValue(20, "上海", "苹果");
		 data.setValue(100, "上海", "香蕉");
		 data.setValue(100, "上海", "西瓜");
		 data.setValue(100, "广州", "苹果");
		 data.setValue(100, "广州", "香蕉");
		 data.setValue(100, "广州", "西瓜");
		 return data;
	}
	
	//4-返回一个ChartPanel
	public ChartPanel getPanel() {
		return jframe;
	}
	
}

调用的测试类的代码如下:

public class Test {
	public static void main(String[] args) {
		JFrame j=new JFrame();
		JDialog jd=new JDialog();
		jd.setBounds(50, 50, 600, 600);
		jd.add(new BarChart().getPanel());
		jd.setVisible(true);
	}
}

效果图如下:
在这里插入图片描述

  • 8
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
使用Apache POI 3.7版本生成柱状图的步骤如下: 1.创建一个Workbook对象,表示整个Excel文件。 ``` Workbook wb = new HSSFWorkbook(); ``` 2.创建一个Sheet对象,表示Excel文件中的一个sheet。 ``` Sheet sheet = wb.createSheet("Sheet1"); ``` 3.创建一个Row对象,表示某个单元格所在行。 ``` Row row = sheet.createRow(0); ``` 4.创建一个Cell对象,表示一个单元格。 ``` Cell cell = row.createCell(0); ``` 5.设置单元格的值。 ``` cell.setCellValue("柱状图"); ``` 6.创建一个Drawing对象,用于绘制图形。 ``` Drawing drawing = sheet.createDrawingPatriarch(); ``` 7.创建一个ClientAnchor对象,表示图形在单元格中的位置和大小。 ``` ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15); ``` 其中,(0, 0)表示图形左上角在单元格(0,0)处,(0, 5)表示图形右下角在单元格(5,10)处。 8.创建一个Chart对象,表示图形。 ``` Chart chart = drawing.createChart(anchor); ``` 9.创建一个ChartLegend对象,表示图例。 ``` ChartLegend legend = chart.getOrCreateLegend(); legend.setPosition(LegendPosition.BOTTOM); ``` 10.创建一个BarChartData对象,表示柱状图的数据。 ``` BarChartData data = new BarChartDataBuilder() .setCategories(new String[]{"A", "B", "C"}) .addSeries("Series 1", new double[]{1, 2, 3}) .addSeries("Series 2", new double[]{2, 3, 4}) .build(); ``` 其中,setCategories方法设置X轴上的标签,addSeries方法添加数据系列。 11.创建一个ChartAxis对象,表示X轴。 ``` ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM); bottomAxis.setMajorTickMark(AxisTickMark.NONE); ``` 12.创建一个ChartAxis对象,表示Y轴。 ``` ChartAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT); leftAxis.setCrosses(AxisCrosses.AUTO_ZERO); ``` 13.创建一个BarChartSeries对象,表示柱状图的数据系列。 ``` BarChartSeries series1 = data.getSeries().get(0); BarChartSeries series2 = data.getSeries().get(1); ``` 14.创建一个BarChartSeriesRender对象,表示柱状图的样式。 ``` BarChartSeriesRender render1 = new BarChartSeriesRender(); render1.setFill(new SimpleSolidFill(Color.BLUE)); BarChartSeriesRender render2 = new BarChartSeriesRender(); render2.setFill(new SimpleSolidFill(Color.RED)); ``` 15.将数据系列和样式添加到图形中。 ``` chart.plot(data, bottomAxis, leftAxis, render1, render2); ``` 完整的代码示例: ``` Workbook wb = new HSSFWorkbook(); Sheet sheet = wb.createSheet("Sheet1"); Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("柱状图"); Drawing drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15); Chart chart = drawing.createChart(anchor); ChartLegend legend = chart.getOrCreateLegend(); legend.setPosition(LegendPosition.BOTTOM); BarChartData data = new BarChartDataBuilder() .setCategories(new String[]{"A", "B", "C"}) .addSeries("Series 1", new double[]{1, 2, 3}) .addSeries("Series 2", new double[]{2, 3, 4}) .build(); ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM); bottomAxis.setMajorTickMark(AxisTickMark.NONE); ChartAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT); leftAxis.setCrosses(AxisCrosses.AUTO_ZERO); BarChartSeries series1 = data.getSeries().get(0); BarChartSeries series2 = data.getSeries().get(1); BarChartSeriesRender render1 = new BarChartSeriesRender(); render1.setFill(new SimpleSolidFill(Color.BLUE)); BarChartSeriesRender render2 = new BarChartSeriesRender(); render2.setFill(new SimpleSolidFill(Color.RED)); chart.plot(data, bottomAxis, leftAxis, render1, render2); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值