热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

java生成二值图,如何创建javapoi条形图,结合两个条形值,如给定的图像?

Iwanttocreatebarchartsomethinglikethiswithjavapoi,(Hastwosetofbars),Tillnowcouldfindonlyfo

ZSLta.png

I want to create bar chart something like this with java poi , (Has two set of bars),Till now could find only for single type of bars, can anyone help me out with any reference??

Thanks.

解决方案

Not clear from your question what code for creating bar chart using apache poi you have found already. Until now apache poi does only supporting line chart and scatter chart but not bar chart directly. So creating a bar chart is only possible using the underlaying low level objects. For a reference do starting with org.openxmlformats.schemas.drawingml.x2006.chart.CTChart.

Example code which creates your example chart:

import java.io.FileOutputStream;

import org.apache.poi.ss.usermodel.*;

import org.apache.poi.ss.util.*;

import org.apache.poi.ss.SpreadsheetVersion;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import org.apache.poi.xssf.usermodel.XSSFChart;

import org.openxmlformats.schemas.drawingml.x2006.chart.CTChart;

import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;

import org.openxmlformats.schemas.drawingml.x2006.chart.CTBarChart;

import org.openxmlformats.schemas.drawingml.x2006.chart.CTBoolean;

import org.openxmlformats.schemas.drawingml.x2006.chart.CTBarSer;

import org.openxmlformats.schemas.drawingml.x2006.chart.CTAxDataSource;

import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource;

import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumRef;

import org.openxmlformats.schemas.drawingml.x2006.chart.CTStrRef;

import org.openxmlformats.schemas.drawingml.x2006.chart.CTSerTx;

import org.openxmlformats.schemas.drawingml.x2006.chart.CTCatAx;

import org.openxmlformats.schemas.drawingml.x2006.chart.CTValAx;

import org.openxmlformats.schemas.drawingml.x2006.chart.CTScaling;

import org.openxmlformats.schemas.drawingml.x2006.chart.CTLegend;

import org.openxmlformats.schemas.drawingml.x2006.chart.STAxPos;

import org.openxmlformats.schemas.drawingml.x2006.chart.STBarDir;

import org.openxmlformats.schemas.drawingml.x2006.chart.STOrientation;

import org.openxmlformats.schemas.drawingml.x2006.chart.STLegendPos;

import org.openxmlformats.schemas.drawingml.x2006.chart.STTickLblPos;

public class BarChart {

private static CTChart createDefaultBarChart(Chart chart, CellReference firstDataCell, CellReference lastDataCell, boolean seriesInCols) {

CTChart ctChart = ((XSSFChart)chart).getCTChart();

CTPlotArea ctPlotArea = ctChart.getPlotArea();

CTBarChart ctBarChart = ctPlotArea.addNewBarChart();

CTBoolean ctBoolean = ctBarChart.addNewVaryColors();

ctBoolean.setVal(true);

ctBarChart.addNewBarDir().setVal(STBarDir.COL);

int firstDataRow = firstDataCell.getRow();

int lastDataRow = lastDataCell.getRow();

int firstDataCol = firstDataCell.getCol();

int lastDataCol = lastDataCell.getCol();

String dataSheet = firstDataCell.getSheetName();

int idx = 0;

if (seriesInCols) { //the series are in the columns of the data cells

for (int c = firstDataCol + 1; c

CTBarSer ctBarSer = ctBarChart.addNewSer();

CTSerTx ctSerTx = ctBarSer.addNewTx();

CTStrRef ctStrRef = ctSerTx.addNewStrRef();

ctStrRef.setF(new CellReference(dataSheet, firstDataRow, c, true, true).formatAsString());

ctBarSer.addNewIdx().setVal(idx++);

CTAxDataSource cttAxDataSource = ctBarSer.addNewCat();

ctStrRef = cttAxDataSource.addNewStrRef();

ctStrRef.setF(new AreaReference(

new CellReference(dataSheet, firstDataRow + 1, firstDataCol, true, true),

new CellReference(dataSheet, lastDataRow, firstDataCol, true, true),

SpreadsheetVersion.EXCEL2007).formatAsString());

CTNumDataSource ctNumDataSource = ctBarSer.addNewVal();

CTNumRef ctNumRef = ctNumDataSource.addNewNumRef();

ctNumRef.setF(new AreaReference(

new CellReference(dataSheet, firstDataRow + 1, c, true, true),

new CellReference(dataSheet, lastDataRow, c, true, true),

SpreadsheetVersion.EXCEL2007).formatAsString());

//at least the border lines in Libreoffice Calc ;-)

ctBarSer.addNewSpPr().addNewLn().addNewSolidFill().addNewSrgbClr().setVal(new byte[] {0,0,0});

}

} else { //the series are in the rows of the data cells

for (int r = firstDataRow + 1; r

CTBarSer ctBarSer = ctBarChart.addNewSer();

CTSerTx ctSerTx = ctBarSer.addNewTx();

CTStrRef ctStrRef = ctSerTx.addNewStrRef();

ctStrRef.setF(new CellReference(dataSheet, r, firstDataCol, true, true).formatAsString());

ctBarSer.addNewIdx().setVal(idx++);

CTAxDataSource cttAxDataSource = ctBarSer.addNewCat();

ctStrRef = cttAxDataSource.addNewStrRef();

ctStrRef.setF(new AreaReference(

new CellReference(dataSheet, firstDataRow, firstDataCol + 1, true, true),

new CellReference(dataSheet, firstDataRow, lastDataCol, true, true),

SpreadsheetVersion.EXCEL2007).formatAsString());

CTNumDataSource ctNumDataSource = ctBarSer.addNewVal();

CTNumRef ctNumRef = ctNumDataSource.addNewNumRef();

ctNumRef.setF(new AreaReference(

new CellReference(dataSheet, r, firstDataCol + 1, true, true),

new CellReference(dataSheet, r, lastDataCol, true, true),

SpreadsheetVersion.EXCEL2007).formatAsString());

//at least the border lines in Libreoffice Calc ;-)

ctBarSer.addNewSpPr().addNewLn().addNewSolidFill().addNewSrgbClr().setVal(new byte[] {0,0,0});

}

}

//telling the BarChart that it has axes and giving them Ids

ctBarChart.addNewAxId().setVal(123456);

ctBarChart.addNewAxId().setVal(123457);

//cat axis

CTCatAx ctCatAx = ctPlotArea.addNewCatAx();

ctCatAx.addNewAxId().setVal(123456); //id of the cat axis

CTScaling ctScaling = ctCatAx.addNewScaling();

ctScaling.addNewOrientation().setVal(STOrientation.MIN_MAX);

ctCatAx.addNewDelete().setVal(false);

ctCatAx.addNewAxPos().setVal(STAxPos.B);

ctCatAx.addNewCrossAx().setVal(123457); //id of the val axis

ctCatAx.addNewTickLblPos().setVal(STTickLblPos.NEXT_TO);

//val axis

CTValAx ctValAx = ctPlotArea.addNewValAx();

ctValAx.addNewAxId().setVal(123457); //id of the val axis

ctScaling = ctValAx.addNewScaling();

ctScaling.addNewOrientation().setVal(STOrientation.MIN_MAX);

ctValAx.addNewDelete().setVal(false);

ctValAx.addNewAxPos().setVal(STAxPos.L);

ctValAx.addNewCrossAx().setVal(123456); //id of the cat axis

ctValAx.addNewTickLblPos().setVal(STTickLblPos.NEXT_TO);

//legend

CTLegend ctLegend = ctChart.addNewLegend();

ctLegend.addNewLegendPos().setVal(STLegendPos.B);

ctLegend.addNewOverlay().setVal(false);

return ctChart;

}

public static void main(String[] args) throws Exception {

Workbook wb = new XSSFWorkbook();

Sheet sheet = wb.createSheet("Sheet1");

DataFormat format = wb.createDataFormat();

CellStyle oneDecimal = wb.createCellStyle();

oneDecimal.setDataFormat(format.getFormat("0.0"));

//put some data in the sheet

Row row;

Cell cell;

row = sheet.createRow(0);

row.createCell(0);

row.createCell(1).setCellValue("Male");

row.createCell(2).setCellValue("Female");

for (int r &#61; 1; r <7; r&#43;&#43;) {

row &#61; sheet.createRow(r);

cell &#61; row.createCell(0);

cell.setCellValue(1975 &#43; r*5);

cell &#61; row.createCell(1);

cell.setCellValue(java.util.concurrent.ThreadLocalRandom.current().nextDouble(40.0, 70.0));

cell.setCellStyle(oneDecimal);

cell &#61; row.createCell(2);

cell.setCellValue(java.util.concurrent.ThreadLocalRandom.current().nextDouble(40.0, 70.0));

cell.setCellStyle(oneDecimal);

}

//create empty chart in the sheet

Drawing drawing &#61; sheet.createDrawingPatriarch();

ClientAnchor anchor &#61; drawing.createAnchor(0, 0, 0, 0, 0, 7, 8, 20);

Chart chart &#61; drawing.createChart(anchor);

//create the references to the chart data

CellReference firstDataCell &#61; new CellReference(sheet.getSheetName(), 0, 0, true, true);

CellReference lastDataCell &#61; new CellReference(sheet.getSheetName(), 6, 2, true, true);

//create a default bar chart from the data

CTChart ctBarChart &#61; createDefaultBarChart(chart, firstDataCell, lastDataCell, true);

//now we can customizing the chart

//legend position:

ctBarChart.getLegend().unsetLegendPos();

ctBarChart.getLegend().addNewLegendPos().setVal(STLegendPos.R);

//data labels:

CTBoolean ctboolean &#61; CTBoolean.Factory.newInstance();

ctboolean.setVal(true);

ctBarChart.getPlotArea().getBarChartArray(0).addNewDLbls().setShowVal(ctboolean);

ctboolean.setVal(false);

ctBarChart.getPlotArea().getBarChartArray(0).getDLbls().setShowSerName(ctboolean);

ctBarChart.getPlotArea().getBarChartArray(0).getDLbls().setShowPercent(ctboolean);

ctBarChart.getPlotArea().getBarChartArray(0).getDLbls().setShowLegendKey(ctboolean);

ctBarChart.getPlotArea().getBarChartArray(0).getDLbls().setShowCatName(ctboolean);

ctBarChart.getPlotArea().getBarChartArray(0).getDLbls().setShowLeaderLines(ctboolean);

ctBarChart.getPlotArea().getBarChartArray(0).getDLbls().setShowBubbleSize(ctboolean);

//val axis maximum:

ctBarChart.getPlotArea().getValAxArray(0).getScaling().addNewMax().setVal(100);

//cat axis title:

ctBarChart.getPlotArea().getCatAxArray(0).addNewTitle().addNewOverlay().setVal(false);

ctBarChart.getPlotArea().getCatAxArray(0).getTitle().addNewTx().addNewRich().addNewBodyPr();

ctBarChart.getPlotArea().getCatAxArray(0).getTitle().getTx().getRich().addNewP().addNewR().setT("School Year");

//series colors:

ctBarChart.getPlotArea().getBarChartArray(0).getSerArray(0).getSpPr().addNewSolidFill().addNewSrgbClr().setVal(new byte[]{0,0,(byte)255});

ctBarChart.getPlotArea().getBarChartArray(0).getSerArray(1).getSpPr().addNewSolidFill().addNewSrgbClr().setVal(new byte[]{0,(byte)255,0});

FileOutputStream fileOut &#61; new FileOutputStream("BarChart.xlsx");

wb.write(fileOut);

wb.close();

}

}

This example needs the full jar of all of the schemas ooxml-schemas-1.3.jar as mentioned in the FAQ-N10025.



推荐阅读
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 本文介绍了一个Java猜拳小游戏的代码,通过使用Scanner类获取用户输入的拳的数字,并随机生成计算机的拳,然后判断胜负。该游戏可以选择剪刀、石头、布三种拳,通过比较两者的拳来决定胜负。 ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 本文介绍了在Mac上搭建php环境后无法使用localhost连接mysql的问题,并通过将localhost替换为127.0.0.1或本机IP解决了该问题。文章解释了localhost和127.0.0.1的区别,指出了使用socket方式连接导致连接失败的原因。此外,还提供了相关链接供读者深入了解。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • 本文讨论了在openwrt-17.01版本中,mt7628设备上初始化启动时eth0的mac地址总是随机生成的问题。每次随机生成的eth0的mac地址都会写到/sys/class/net/eth0/address目录下,而openwrt-17.01原版的SDK会根据随机生成的eth0的mac地址再生成eth0.1、eth0.2等,生成后的mac地址会保存在/etc/config/network下。 ... [详细]
  • WhenIusepythontoapplythepymysqlmoduletoaddafieldtoatableinthemysqldatabase,itdo ... [详细]
  • 如何提高PHP编程技能及推荐高级教程
    本文介绍了如何提高PHP编程技能的方法,推荐了一些高级教程。学习任何一种编程语言都需要长期的坚持和不懈的努力,本文提醒读者要有足够的耐心和时间投入。通过实践操作学习,可以更好地理解和掌握PHP语言的特异性,特别是单引号和双引号的用法。同时,本文也指出了只走马观花看整体而不深入学习的学习方式无法真正掌握这门语言,建议读者要从整体来考虑局部,培养大局观。最后,本文提醒读者完成一个像模像样的网站需要付出更多的努力和实践。 ... [详细]
  • 大数据Hadoop生态(20)MapReduce框架原理OutputFormat的开发笔记
    本文介绍了大数据Hadoop生态(20)MapReduce框架原理OutputFormat的开发笔记,包括outputFormat接口实现类、自定义outputFormat步骤和案例。案例中将包含nty的日志输出到nty.log文件,其他日志输出到other.log文件。同时提供了一些相关网址供参考。 ... [详细]
  • 本文讨论了在shiro java配置中加入Shiro listener后启动失败的问题。作者引入了一系列jar包,并在web.xml中配置了相关内容,但启动后却无法正常运行。文章提供了具体引入的jar包和web.xml的配置内容,并指出可能的错误原因。该问题可能与jar包版本不兼容、web.xml配置错误等有关。 ... [详细]
author-avatar
Superficial1987542_y3
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有