简体   繁体   English

使用Aspose Words for Java插入条形图

[英]Inserting bar charts using Aspose Words for Java

Is there a way of creating and inserting a bar chart in a Ms Word document using Aspose Words for Java? 有没有一种方法可以使用Aspose Words for Java在Ms Word文档中创建和插入条形图? I can't find a way to do this. 我找不到解决办法。 Thanks. 谢谢。

Aspose.Words for Java currently doesn't allow you to create the bar chart in Word documents. Aspose.Words for Java当前不允许您在Word文档中创建条形图。 However, if you just want to add a static bar chart, you may try Aspose.Cells for Java to create bar chart and render it to image . 但是,如果您只想添加静态条形图,则可以尝试Aspose.Cells for Java 创建条形图并将其呈现为image After that, you can add this bar chart image in Word document using Aspose.Words for Java. 之后,您可以使用Aspose.Words for Java在Word文档中添加此条形图图像。 Do you think this might help in your scenario? 您认为这可能对您有帮助吗? If it does then you can use the following code snippet to create and render the bar chart to image: 如果是这样,则可以使用以下代码段创建条形图并将其呈现为图像:

//Create a new Workbook. //创建一个新的工作簿。

Workbook workbook = new Workbook(); 工作簿工作簿= new Workbook();

//Get the first worksheet. //获取第一个工作表。

Worksheet sheet = workbook.getWorksheets().get(0); 工作表= workbook.getWorksheets()。get(0);

//Set the name of worksheet //设置工作表名称

sheet.setName("Data"); sheet.setName(“ Data”);

//Get the cells collection in the sheet. //获取工作表中的单元格集合。

Cells cells = workbook.getWorksheets().get(0).getCells(); 单元格cell = workbook.getWorksheets()。get(0).getCells();

//Put some values into a cells of the Data sheet. //将一些值放入数据表的单元格中。

cells.get("A1").setValue("Region"); cells.get(“ A1”)。setValue(“ Region”);

cells.get("A2").setValue("France"); cells.get(“ A2”)。setValue(“ France”);

cells.get("A3").setValue("Germany"); cells.get(“ A3”)。setValue(“德国”);

cells.get("A4").setValue("England"); cells.get(“ A4”)。setValue(“ England”);

cells.get("A5").setValue("Sweden"); cells.get(“ A5”)。setValue(“瑞典”);

cells.get("A6").setValue("Italy"); cells.get(“ A6”)。setValue(“ Italy”);

cells.get("A7").setValue("Spain"); cells.get(“ A7”)。setValue(“ Spain”);

cells.get("A8").setValue("Portugal"); cells.get(“ A8”)。setValue(“ Portugal”);

cells.get("B1").setValue("Sale"); cells.get(“ B1”)。setValue(“ Sale”);

cells.get("B2").setValue(70000); cells.get(“ B2”)。setValue(70000);

cells.get("B3").setValue(55000); cells.get(“ B3”)。setValue(55000);

cells.get("B4").setValue(30000); cells.get(“ B4”)。setValue(30000);

cells.get("B5").setValue(40000); cells.get(“ B5”)。setValue(40000);

cells.get("B6").setValue(35000); cells.get(“ B6”)。setValue(35000);

cells.get("B7").setValue(32000); cells.get(“ B7”)。setValue(32000);

cells.get("B8").setValue(10000); cells.get(“ B8”)。setValue(10000);

//Create chart //创建图表

int chartIndex = sheet.getCharts().add(ChartType.COLUMN, 12, 1, 33, 12); int chartIndex = sheet.getCharts()。add(ChartType.COLUMN,12,1,33,12);

Chart chart = sheet.getCharts().get(chartIndex); 图表chart = sheet.getCharts()。get(chartIndex);

//Set properties of chart title //设置图表标题的属性

chart.getTitle().setText("Sales By Region"); chart.getTitle()。setText(“按地区销售”);

chart.getTitle().getTextFont().setBold(true); chart.getTitle()。getTextFont()。setBold(true);

chart.getTitle().getTextFont().setSize(12); chart.getTitle()。getTextFont()。setSize(12);

//Set properties of nseries //设置nseries的属性

chart.getNSeries().add("Data!B2:B8", true); chart.getNSeries()。add(“ Data!B2:B8”,true);

chart.getNSeries().setCategoryData("Data!A2:A8"); chart.getNSeries()。setCategoryData(“ Data!A2:A8”);

//Set the fill colors for the series's data points (France - Portugal(7 points)) //设置系列数据点的填充颜色(法国-葡萄牙(7个点))

ChartPointCollection chartPoints = chart.getNSeries().get(0).getPoints(); ChartPointCollection chartPoints = chart.getNSeries()。get(0).getPoints();

ChartPoint point = chartPoints.get(0); ChartPoint点= chartPoints.get(0);

point.getArea().setForegroundColor(Color.getCyan()); point.getArea()。setForegroundColor(Color.getCyan());

point = chartPoints.get(1); 点= chartPoints.get(1);

point.getArea().setForegroundColor(Color.getBlue()); point.getArea()。setForegroundColor(Color.getBlue());

point = chartPoints.get(2); 点= chartPoints.get(2);

point.getArea().setForegroundColor(Color.getYellow()); point.getArea()。setForegroundColor(Color.getYellow());

point = chartPoints.get(3); 点= chartPoints.get(3);

point.getArea().setForegroundColor(Color.getRed()); point.getArea()。setForegroundColor(Color.getRed());

point = chartPoints.get(4); 点= chartPoints.get(4);

point.getArea().setForegroundColor(Color.getBlack()); point.getArea()。setForegroundColor(Color.getBlack());

point = chartPoints.get(5); 点= chartPoints.get(5);

point.getArea().setForegroundColor(Color.getGreen()); point.getArea()。setForegroundColor(Color.getGreen());

point = chartPoints.get(6); 点= chartPoints.get(6);

point.getArea().setForegroundColor(Color.getMaroon()); point.getArea()。setForegroundColor(Color.getMaroon());

//Set the legend invisible //设置图例不可见

chart.setShowLegend(false); chart.setShowLegend(false);

//Get the Chart mage //获取图表法师

ImageOrPrintOptions imgOpts = new ImageOrPrintOptions(); ImageOrPrintOptions imgOpts = new ImageOrPrintOptions();

imgOpts.setImageFormat(ImageFormat.getPng()); imgOpts.setImageFormat(ImageFormat.getPng());

//Save the chart image file. //保存图表图像文件。

chart.toImage(new FileOutputStream("D:\\Files\\MyChartImage.png"), imgOpts); chart.toImage(new FileOutputStream(“ D:\\ Files \\ MyChartImage.png”),imgOpts);

Disclosure: I work as developer evangelist at Aspose. 披露:我是Aspose的开发人员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM