チャートに背景として画像を設定する
Contents
[
Hide
]
Aspose.Cellsを使用すると、プロットエリア、チャートエリア、または凡例ボックスなどのさまざまなオブジェクトの塗りつぶし効果としてグラデーション、テクスチャ、パターン、または画像を設定することができます。このドキュメントでは、チャートの背景に画像を追加する方法を示しています。
以下のチャートはサンプルコードで作成されました。
サンプルコードの実行後の出力チャートを示す画像
チャートの背景塗りつぶしとして画像を設定するためのJavaコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(SetPictureAsBackgroundFillInChart.class); | |
// Create a new Workbook. | |
Workbook workbook = new Workbook(); | |
// Get the first worksheet. | |
Worksheet sheet = workbook.getWorksheets().get(0); | |
// Set the name of worksheet | |
sheet.setName("Data"); | |
// Get the cells collection in the sheet. | |
Cells cells = workbook.getWorksheets().get(0).getCells(); | |
// Put some values into a cells of the Data sheet. | |
cells.get("A1").putValue("Region"); | |
cells.get("A2").putValue("France"); | |
cells.get("A3").putValue("Germany"); | |
cells.get("A4").putValue("England"); | |
cells.get("A5").putValue("Sweden"); | |
cells.get("A6").putValue("Italy"); | |
cells.get("A7").putValue("Spain"); | |
cells.get("A8").putValue("Portugal"); | |
cells.get("B1").putValue("Sale"); | |
cells.get("B2").putValue(70000); | |
cells.get("B3").putValue(55000); | |
cells.get("B4").putValue(30000); | |
cells.get("B5").putValue(40000); | |
cells.get("B6").putValue(35000); | |
cells.get("B7").putValue(32000); | |
cells.get("B8").putValue(10000); | |
// Add a chart sheet. | |
int sheetIndex = workbook.getWorksheets().add(SheetType.CHART); | |
sheet = workbook.getWorksheets().get(sheetIndex); | |
// Set the name of worksheet | |
sheet.setName("Chart"); | |
// Create chart | |
int chartIndex = 0; | |
chartIndex = sheet.getCharts().add(ChartType.COLUMN, 1, 1, 25, 10); | |
Chart chart = sheet.getCharts().get(chartIndex); | |
// Set some properties of chart plot area. To set a picture as fill format and make the border invisible. | |
File file = new File(dataDir + "aspose-logo.png"); | |
byte[] data = new byte[(int) file.length()]; | |
FileInputStream fis = new FileInputStream(file); | |
fis.read(data); | |
chart.getPlotArea().getArea().getFillFormat().setImageData(data); | |
chart.getPlotArea().getBorder().setVisible(false); | |
// Set properties of chart title | |
chart.getTitle().setText("Sales By Region"); | |
chart.getTitle().getFont().setColor(Color.getBlue()); | |
chart.getTitle().getFont().setBold(true); | |
chart.getTitle().getFont().setSize(12); | |
// Set properties of nseries | |
chart.getNSeries().add("Data!B2:B8", true); | |
chart.getNSeries().setCategoryData("Data!A2:A8"); | |
chart.getNSeries().setColorVaried(true); | |
// Set the Legend. | |
Legend legend = chart.getLegend(); | |
legend.setPosition(LegendPositionType.TOP); | |
// Save the excel file | |
workbook.save(dataDir + "column_chart.xls"); |