I’m working on creating a chart with the datasouce as Pivot Table. The following is the process I use to create the chart.
Hi,
Thank you for the quick reply, I tried the latest version and the problem persists. I will upload the sample code shortly.
Here is the small program I have written describing the issue.
Hi,
Thank you Shakeel.
Hi,
import com.aspose.cells.Cells;
import com.aspose.cells.Chart;
import com.aspose.cells.ChartType;
import com.aspose.cells.ListObject;
import com.aspose.cells.ListObjectCollection;
import com.aspose.cells.PivotFieldType;
import com.aspose.cells.PivotTable;
import com.aspose.cells.PivotTableCollection;
import com.aspose.cells.PivotTableStyleType;
import com.aspose.cells.Style;
import com.aspose.cells.TableStyleType;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
import java.util.Arrays;
import java.util.List;
/**
-
@author vsriram
*/
public class PivotChartCreation {public static void main(String args[]) {
try { buildWorkbook(); } catch (Exception e) { System.out.println("There's a problem!!! " + e.getMessage()); }
}
private static void buildWorkbook() throws Exception {
// The path to the documents directory. String dataDir = ""; //Utils.getDataDir(PivotChartCreation.class); // Instantiating an Workbook object Workbook workbook = new Workbook(); Worksheet sheet = workbook.getWorksheets().get(0); sheet.setName("Data"); Cells cells = sheet.getCells(); int endRow = printRawData(cells);
//Added========================================
workbook.calculateFormula();
ListObjectCollection listObjectCollection = sheet.getListObjects();
int tableIndex = listObjectCollection.add(0, 0, endRow, 2, true);
ListObject listObject = listObjectCollection.get(tableIndex);
listObject.setTableStyleType(TableStyleType.NONE);
// Adding a new sheet
int pivotIndex = workbook.getWorksheets().add();
Worksheet sheet2 = workbook.getWorksheets().get(pivotIndex);
sheet2.setName("PivotTable");
// Getting the pivottables collection in the sheet
PivotTableCollection pivotTables = sheet2.getPivotTables();
int index = pivotTables.add("=Data!A1:C24", "B35", "PivotTable1");
PivotTable pivotTable = pivotTables.get(index);
pivotTable.setRowGrand(false);
pivotTable.setColumnGrand(false);
pivotTable.setPivotTableStyleType(PivotTableStyleType.PIVOT_TABLE_STYLE_LIGHT_22);
pivotTable.setShowPivotStyleRowHeader(true);
pivotTable.setShowPivotStyleColumnHeader(true);
pivotTable.setShowPivotStyleRowStripes(true);
pivotTable.addFieldToArea(PivotFieldType.ROW, 0);
pivotTable.getRowFields().get(0).setNumber(17);
pivotTable.addFieldToArea(PivotFieldType.COLUMN, 1);
pivotTable.addFieldToArea(PivotFieldType.DATA, 2);
pivotTable.getDataFields().get(0).setNumber(1);
sheet2.autoFitColumns();
pivotTable.refreshData();
pivotTable.calculateData();
// Adding a column chart
int chartId = sheet2.getCharts().add(ChartType.COLUMN, 2, 1, 30, 11);
Chart chart = sheet2.getCharts().get(chartId);
// Setting the pivot chart data source
chart.setPivotSource("PivotTable1");
chart.setHidePivotFieldButtons(false);
//Added========================================
chart.setType(ChartType.COLUMN_STACKED);
chart.calculate();
workbook.save(dataDir + “pivotChart_actual.xlsx”);
}
private static int printRawData(Cells cells) {
int rawDataStart = 1;
// Setting the values to the cells
Cell cell = cells.get("A" + rawDataStart);
cell.setValue("Booking Date");
cell = cells.get("B" + rawDataStart);
cell.setValue("Room Type");
cell = cells.get("C" + rawDataStart);
cell.setValue("Booking");
List<String> dates = Arrays.asList(
"=DATE(2018,12,1)",
"=DATE(2017,12,1)",
"=DATE(2016,12,1)",
"=DATE(2016,9,1)",
"=DATE(2016,6,1)",
"=DATE(2016,3,1)",
"=DATE(2016,1,1)",
"=DATE(2015,12,1)",
"=DATE(2015,11,1)",
"=DATE(2015,11,1)",
“=DATE(2015,10,1)”,
“=DATE(2015,10,1)”,
“=DATE(2015,10,1)”,
“=DATE(2015,9,1)”,
“=DATE(2015,7,1)”,
“=DATE(2015,4,1)”,
“=DATE(2015,1,1)”,
“=DATE(2014,12,1)”,
“=DATE(2014,12,1)”,
“=DATE(2014,6,1)”,
“=DATE(2014,1,1)”,
“=DATE(2013,1,1)”,
“=DATE(2012,1,1)”);
for (int i = 0; i < dates.size(); i++) {
cell = cells.get("A" + ((rawDataStart + 1) + i));
cell.setFormula(dates.get(i));
Style style = cell.getStyle();
style.setNumber(17);
cell.setStyle(style);
}
List<String> roomType = Arrays.asList(
"single",
"double",
"double",
"double",
"double",
"double",
"single",
"double",
"suite",
"single",
"double",
"suite",
"single",
"double",
"double",
"single",
"single",
"double",
"single",
"double",
"double",
"single",
"single");
for (int i = 0; i < roomType.size(); i++) {
cell = cells.get("B" + ((rawDataStart + 1) + i));
cell.setValue(roomType.get(i));
cell = cells.get("C" + ((rawDataStart + 1) + i));
cell.setValue(1);
}
return dates.size();
}
}
Thanks for getting back, Shakeel.
Hi,
Hi Shakeel,
Hi,
Thanks for the update Shekeel, I will look forward to your findings.
Hi,
import com.aspose.cells.Cells;
import com.aspose.cells.Chart;
import com.aspose.cells.ChartType;
import com.aspose.cells.ListObject;
import com.aspose.cells.ListObjectCollection;
import com.aspose.cells.PivotFieldType;
import com.aspose.cells.PivotTable;
import com.aspose.cells.PivotTableCollection;
import com.aspose.cells.PivotTableStyleType;
import com.aspose.cells.Style;
import com.aspose.cells.TableStyleType;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
import java.util.Arrays;
import java.util.List;
/**
-
@author vsriram
*/
public class PivotChartCreation {public static void main(String args[]) {
try { buildWorkbook(); } catch (Exception e) { System.out.println("There's a problem!!! " + e.getMessage()); }
}
private static void buildWorkbook() throws Exception {
// The path to the documents directory. String dataDir = ""; //Utils.getDataDir(PivotChartCreation.class); // Instantiating an Workbook object Workbook workbook = new Workbook(); Worksheet sheet = workbook.getWorksheets().get(0); sheet.setName("Data"); Cells cells = sheet.getCells(); int endRow = printRawData(cells); //Added======================================== workbook.calculateFormula(); ListObjectCollection listObjectCollection = sheet.getListObjects(); int tableIndex = listObjectCollection.add(0, 0, endRow, 2, true); ListObject listObject = listObjectCollection.get(tableIndex); listObject.setTableStyleType(TableStyleType.NONE); // Adding a new sheet int pivotIndex = workbook.getWorksheets().add(); Worksheet sheet2 = workbook.getWorksheets().get(pivotIndex); sheet2.setName("PivotTable"); // Getting the pivottables collection in the sheet PivotTableCollection pivotTables = sheet2.getPivotTables(); int index = pivotTables.add("=Data!A1:C24", "B35", "PivotTable1"); PivotTable pivotTable = pivotTables.get(index); pivotTable.setRowGrand(false); pivotTable.setColumnGrand(false); pivotTable.setPivotTableStyleType(PivotTableStyleType.PIVOT_TABLE_STYLE_LIGHT_22); pivotTable.setShowPivotStyleRowHeader(true); pivotTable.setShowPivotStyleColumnHeader(true); pivotTable.setShowPivotStyleRowStripes(true); pivotTable.addFieldToArea(PivotFieldType.ROW, 0); pivotTable.getRowFields().get(0).setNumber(17); pivotTable.addFieldToArea(PivotFieldType.COLUMN, 1); pivotTable.addFieldToArea(PivotFieldType.DATA, 2); pivotTable.getDataFields().get(0).setNumber(1); sheet2.autoFitColumns(); pivotTable.refreshData(); pivotTable.calculateData(); // Adding a column chart int chartId = sheet2.getCharts().add(ChartType.COLUMN, 2, 1, 30, 11); Chart chart = sheet2.getCharts().get(chartId); // Setting the pivot chart data source </font><font color="#ff0000"><b>chart.setPivotSource("PivotTable!PivotTable1");</b></font><font color="#800080"> chart.setHidePivotFieldButtons(false); //Added======================================== chart.setType(ChartType.COLUMN_STACKED); chart.calculate(); workbook.save(dataDir + "pivotChart_actual.xlsx");
}
private static int printRawData(Cells cells) {
int rawDataStart = 1; // Setting the values to the cells Cell cell = cells.get("A" + rawDataStart); cell.setValue("Booking Date"); cell = cells.get("B" + rawDataStart); cell.setValue("Room Type"); cell = cells.get("C" + rawDataStart); cell.setValue("Booking"); List<String> dates = Arrays.asList( "=DATE(2018,12,1)", "=DATE(2017,12,1)", "=DATE(2016,12,1)", "=DATE(2016,9,1)", "=DATE(2016,6,1)", "=DATE(2016,3,1)", "=DATE(2016,1,1)", "=DATE(2015,12,1)", "=DATE(2015,11,1)", "=DATE(2015,11,1)", "=DATE(2015,10,1)", "=DATE(2015,10,1)", "=DATE(2015,10,1)", "=DATE(2015,9,1)", "=DATE(2015,7,1)", "=DATE(2015,4,1)", "=DATE(2015,1,1)", "=DATE(2014,12,1)", "=DATE(2014,12,1)", "=DATE(2014,6,1)", "=DATE(2014,1,1)", "=DATE(2013,1,1)", "=DATE(2012,1,1)"); for (int i = 0; i < dates.size(); i++) { cell = cells.get("A" + ((rawDataStart + 1) + i)); cell.setFormula(dates.get(i)); Style style = cell.getStyle(); style.setNumber(17); cell.setStyle(style); } List<String> roomType = Arrays.asList( "single", "double", "double", "double", "double", "double", "single", "double", "suite", "single", "double", "suite", "single", "double", "double", "single", "single", "double", "single", "double", "double", "single", "single"); for (int i = 0; i < roomType.size(); i++) { cell = cells.get("B" + ((rawDataStart + 1) + i)); cell.setValue(roomType.get(i)); cell = cells.get("C" + ((rawDataStart + 1) + i)); cell.setValue(1); } return dates.size();
}
}