--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+package org.apache.poi.xssf.usermodel.examples;\r
+\r
+import org.apache.poi.ss.usermodel.*;\r
+import org.apache.poi.ss.usermodel.charts.*;\r
+import org.apache.poi.ss.util.CellRangeAddress;\r
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;\r
+\r
+import java.io.FileOutputStream;\r
+\r
+/**\r
+ * Line chart example.\r
+ *\r
+ * @author Martin Andersson\r
+ */\r
+public class LineChart {\r
+\r
+ public static void main(String[] args) throws Exception {\r
+ Workbook wb = new XSSFWorkbook();\r
+ Sheet sheet = wb.createSheet("linechart");\r
+ final int NUM_OF_ROWS = 3;\r
+ final int NUM_OF_COLUMNS = 10;\r
+\r
+ // Create a row and put some cells in it. Rows are 0 based.\r
+ Row row;\r
+ Cell cell;\r
+ for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++) {\r
+ row = sheet.createRow((short) rowIndex);\r
+ for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {\r
+ cell = row.createCell((short) colIndex);\r
+ cell.setCellValue(colIndex * (rowIndex + 1));\r
+ }\r
+ }\r
+\r
+ Drawing drawing = sheet.createDrawingPatriarch();\r
+ ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);\r
+\r
+ Chart chart = drawing.createChart(anchor);\r
+ ChartLegend legend = chart.getOrCreateLegend();\r
+ legend.setPosition(LegendPosition.TOP_RIGHT);\r
+\r
+ LineChartData data = chart.getChartDataFactory().createLineChartData();\r
+\r
+ // Use a category axis for the bottom axis.\r
+ ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);\r
+ ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);\r
+ leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);\r
+\r
+ ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));\r
+ ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));\r
+ ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1));\r
+\r
+\r
+ data.addSerie(xs, ys1);\r
+ data.addSerie(xs, ys2);\r
+\r
+ chart.plot(data, bottomAxis, leftAxis);\r
+\r
+ // Write the output to a file\r
+ FileOutputStream fileOut = new FileOutputStream("ooxml-line-chart.xlsx");\r
+ wb.write(fileOut);\r
+ fileOut.close();\r
+ }\r
+}\r
--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+\r
+package org.apache.poi.ss.usermodel.charts;\r
+\r
+/**\r
+ * Enumeration of possible axis tick marks.\r
+ *\r
+ * @author Martin Andersson\r
+ */\r
+public enum AxisTickMark {\r
+ NONE,\r
+ CROSS,\r
+ IN,\r
+ OUT\r
+}\r
*/
@Beta
public interface ChartAxis {
-
+
/**
* @return axis id
*/
* @param axis that this axis should cross
*/
void crossAxis(ChartAxis axis);
+
+ /**
+ * @return visibility of the axis.
+ */
+ boolean isVisible();
+
+ /**
+ * @param value visibility of the axis.
+ */
+ void setVisible(boolean value);
+
+ /**
+ * @return major tick mark.
+ */
+ AxisTickMark getMajorTickMark();
+
+ /**
+ * @param tickMark major tick mark type.
+ */
+ void setMajorTickMark(AxisTickMark tickMark);
+
+ /**
+ * @return minor tick mark.
+ */
+ AxisTickMark getMinorTickMark();
+
+ /**
+ * @param tickMark minor tick mark type.
+ */
+ void setMinorTickMark(AxisTickMark tickMark);
}
/**
* A factory for different charts data types.
*
- * @author Roman Kashitsyn
+ * @author Roman Kashitsyn, Martin Andersson
*/
@Beta
public interface ChartDataFactory {
-
+
/**
* @return an appropriate ScatterChartData instance
*/
ScatterChartData createScatterChartData();
+ /**
+ * @return a LineChartData instance
+ */
+ LineChartData createLineChartData();
+
}
--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+\r
+package org.apache.poi.ss.usermodel.charts;\r
+\r
+import org.apache.poi.ss.util.CellReference;\r
+\r
+/**\r
+ * Basic settings for all chart series.\r
+ *\r
+ * @author Martin Andersson\r
+ */\r
+public interface ChartSerie {\r
+\r
+ /**\r
+ * Sets the title of the series as a string literal.\r
+ *\r
+ * @param title\r
+ */\r
+ void setTitle(String title);\r
+\r
+ /**\r
+ * Sets the title of the series as a cell reference.\r
+ *\r
+ * @param titleReference\r
+ */\r
+ void setTitle(CellReference titleReference);\r
+\r
+ /**\r
+ * @return title as string literal.\r
+ */\r
+ String getTitleString();\r
+\r
+ /**\r
+ * @return title as cell reference.\r
+ */\r
+ CellReference getTitleCellReference();\r
+\r
+ /**\r
+ * @return title type.\r
+ */\r
+ TitleType getTitleType();\r
+}\r
--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+\r
+package org.apache.poi.ss.usermodel.charts;\r
+\r
+import org.apache.poi.util.Beta;\r
+\r
+import java.util.List;\r
+\r
+/**\r
+ * @author Martin Andersson\r
+ */\r
+@Beta\r
+public interface LineChartData extends ChartData {\r
+\r
+ /**\r
+ * @param categories data source for categories.\r
+ * @param values data source for values.\r
+ * @return a new line chart serie.\r
+ */\r
+ LineChartSerie addSerie(ChartDataSource<?> categories, ChartDataSource<? extends Number> values);\r
+\r
+ /**\r
+ * @return list of all series.\r
+ */\r
+ List<? extends LineChartSerie> getSeries();\r
+}\r
--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+\r
+package org.apache.poi.ss.usermodel.charts;\r
+\r
+import org.apache.poi.util.Beta;\r
+\r
+/**\r
+ * Represents a line chart serie.\r
+ *\r
+ * @author Martin Andersson\r
+ */\r
+@Beta\r
+public interface LineChartSerie extends ChartSerie {\r
+\r
+ /**\r
+ * @return data source used for category axis data.\r
+ */\r
+ ChartDataSource<?> getCategoryAxisData();\r
+\r
+ /**\r
+ * @return data source used for value axis.\r
+ */\r
+ ChartDataSource<? extends Number> getValues();\r
+\r
+}\r
* Represents scatter charts serie.
*
* @author Roman Kashitsyn
+ * @author Martin Andersson
*/
@Beta
-public interface ScatterChartSerie {
+public interface ScatterChartSerie extends ChartSerie {
/**
* @return data source used for X axis values
--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+\r
+package org.apache.poi.ss.usermodel.charts;\r
+\r
+/**\r
+ * Title types for charts.\r
+ *\r
+ * @author Martin Andersson\r
+ */\r
+public enum TitleType {\r
+ STRING,\r
+ CELL_REFERENCE\r
+}\r
--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+\r
+package org.apache.poi.xssf.usermodel.charts;\r
+\r
+import org.apache.poi.ss.usermodel.charts.ChartSerie;\r
+import org.apache.poi.ss.usermodel.charts.TitleType;\r
+import org.apache.poi.ss.util.CellReference;\r
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTSerTx;\r
+\r
+/**\r
+ * @author Martin Andersson\r
+ */\r
+public abstract class AbstractXSSFChartSerie implements ChartSerie {\r
+\r
+ private String titleValue;\r
+ private CellReference titleRef;\r
+ private TitleType titleType;\r
+\r
+ public void setTitle(CellReference titleReference) {\r
+ titleType = TitleType.CELL_REFERENCE;\r
+ titleRef = titleReference;\r
+ }\r
+\r
+ public void setTitle(String title) {\r
+ titleType = TitleType.STRING;\r
+ titleValue = title;\r
+ }\r
+\r
+ public CellReference getTitleCellReference() {\r
+ if (TitleType.CELL_REFERENCE.equals(titleType)) {\r
+ return titleRef;\r
+ }\r
+ throw new IllegalStateException("Title type is not CellReference.");\r
+ }\r
+\r
+ public String getTitleString() {\r
+ if (TitleType.STRING.equals(titleType)) {\r
+ return titleValue;\r
+ }\r
+ throw new IllegalStateException("Title type is not String.");\r
+ }\r
+\r
+ public TitleType getTitleType() {\r
+ return titleType;\r
+ }\r
+\r
+ protected boolean isTitleSet() {\r
+ return titleType != null;\r
+ }\r
+\r
+ protected CTSerTx getCTSerTx() {\r
+ CTSerTx tx = CTSerTx.Factory.newInstance();\r
+ switch (titleType) {\r
+ case CELL_REFERENCE:\r
+ tx.addNewStrRef().setF(titleRef.formatAsString());\r
+ return tx;\r
+ case STRING:\r
+ tx.setV(titleValue);\r
+ return tx;\r
+ default:\r
+ throw new IllegalStateException("Unkown title type: " + titleType);\r
+ }\r
+ }\r
+}\r
package org.apache.poi.xssf.usermodel.charts;
-import org.apache.poi.ss.usermodel.charts.AxisCrosses;
-import org.apache.poi.ss.usermodel.charts.AxisOrientation;
-import org.apache.poi.ss.usermodel.charts.AxisPosition;
-import org.apache.poi.ss.usermodel.charts.ChartAxis;
+import org.apache.poi.ss.usermodel.charts.*;
import org.apache.poi.util.Beta;
import org.apache.poi.xssf.usermodel.XSSFChart;
-import org.openxmlformats.schemas.drawingml.x2006.chart.CTAxPos;
-import org.openxmlformats.schemas.drawingml.x2006.chart.CTCatAx;
-import org.openxmlformats.schemas.drawingml.x2006.chart.CTCrosses;
-import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumFmt;
-import org.openxmlformats.schemas.drawingml.x2006.chart.CTScaling;
-import org.openxmlformats.schemas.drawingml.x2006.chart.STTickLblPos;
+import org.openxmlformats.schemas.drawingml.x2006.chart.*;
/**
* Category axis type.
return ctCatAx.getCrosses();
}
+ @Override
+ protected CTBoolean getDelete() {
+ return ctCatAx.getDelete();
+ }
+
+ @Override
+ protected CTTickMark getMajorCTTickMark() {
+ return ctCatAx.getMajorTickMark();
+ }
+
+ @Override
+ protected CTTickMark getMinorCTTickMark() {
+ return ctCatAx.getMinorTickMark();
+ }
+
public void crossAxis(ChartAxis axis) {
ctCatAx.getCrossAx().setVal(axis.getId());
}
ctCatAx.addNewCrosses();
ctCatAx.addNewCrossAx();
ctCatAx.addNewTickLblPos().setVal(STTickLblPos.NEXT_TO);
+ ctCatAx.addNewDelete();
+ ctCatAx.addNewMajorTickMark();
+ ctCatAx.addNewMinorTickMark();
setPosition(pos);
setOrientation(AxisOrientation.MIN_MAX);
setCrosses(AxisCrosses.AUTO_ZERO);
+ setVisible(true);
+ setMajorTickMark(AxisTickMark.CROSS);
+ setMinorTickMark(AxisTickMark.NONE);
}
}
import org.apache.poi.ss.usermodel.charts.AxisPosition;
import org.apache.poi.ss.usermodel.charts.AxisOrientation;
import org.apache.poi.ss.usermodel.charts.AxisCrosses;
+import org.apache.poi.ss.usermodel.charts.AxisTickMark;
import org.apache.poi.util.Beta;
import org.apache.poi.xssf.usermodel.XSSFChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTAxPos;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTBoolean;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumFmt;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTCrosses;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTOrientation;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTLogBase;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTScaling;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTTickMark;
import org.openxmlformats.schemas.drawingml.x2006.chart.STOrientation;
import org.openxmlformats.schemas.drawingml.x2006.chart.STAxPos;
import org.openxmlformats.schemas.drawingml.x2006.chart.STCrosses;
+import org.openxmlformats.schemas.drawingml.x2006.chart.STTickMark;
/**
* Base class for all axis types.
getCTCrosses().setVal(fromAxisCrosses(crosses));
}
+ public boolean isVisible() {
+ return !getDelete().getVal();
+ }
+
+ public void setVisible(boolean value) {
+ getDelete().setVal(!value);
+ }
+
+ public AxisTickMark getMajorTickMark() {
+ return toAxisTickMark(getMajorCTTickMark());
+ }
+
+ public void setMajorTickMark(AxisTickMark tickMark) {
+ getMajorCTTickMark().setVal(fromAxisTickMark(tickMark));
+ }
+
+ public AxisTickMark getMinorTickMark() {
+ return toAxisTickMark(getMinorCTTickMark());
+ }
+
+ public void setMinorTickMark(AxisTickMark tickMark) {
+ getMinorCTTickMark().setVal(fromAxisTickMark(tickMark));
+ }
+
protected abstract CTAxPos getCTAxPos();
protected abstract CTNumFmt getCTNumFmt();
protected abstract CTScaling getCTScaling();
protected abstract CTCrosses getCTCrosses();
+ protected abstract CTBoolean getDelete();
+ protected abstract CTTickMark getMajorCTTickMark();
+ protected abstract CTTickMark getMinorCTTickMark();
private static STOrientation.Enum fromAxisOrientation(AxisOrientation orientation) {
switch (orientation) {
default: return AxisPosition.BOTTOM;
}
}
+
+ private static STTickMark.Enum fromAxisTickMark(AxisTickMark tickMark) {
+ switch (tickMark) {
+ case NONE: return STTickMark.NONE;
+ case IN: return STTickMark.IN;
+ case OUT: return STTickMark.OUT;
+ case CROSS: return STTickMark.CROSS;
+ default:
+ throw new IllegalArgumentException("Unknown AxisTickMark: " + tickMark);
+ }
+ }
+
+ private static AxisTickMark toAxisTickMark(CTTickMark ctTickMark) {
+ switch (ctTickMark.getVal().intValue()) {
+ case STTickMark.INT_NONE: return AxisTickMark.NONE;
+ case STTickMark.INT_IN: return AxisTickMark.IN;
+ case STTickMark.INT_OUT: return AxisTickMark.OUT;
+ case STTickMark.INT_CROSS: return AxisTickMark.CROSS;
+ default: return AxisTickMark.CROSS;
+ }
+ }
}
return new XSSFScatterChartData();
}
+ /**
+ * @return new line charts data instance
+ */
+ public XSSFLineChartData createLineChartData() {
+ return new XSSFLineChartData();
+ }
+
/**
* @return factory instance
*/
--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+ ==================================================================== */\r
+\r
+package org.apache.poi.xssf.usermodel.charts;\r
+\r
+import org.apache.poi.ss.usermodel.Chart;\r
+import org.apache.poi.ss.usermodel.charts.ChartAxis;\r
+import org.apache.poi.ss.usermodel.charts.ChartDataSource;\r
+import org.apache.poi.ss.usermodel.charts.LineChartData;\r
+import org.apache.poi.ss.usermodel.charts.LineChartSerie;\r
+import org.apache.poi.util.Beta;\r
+import org.apache.poi.xssf.usermodel.XSSFChart;\r
+import org.openxmlformats.schemas.drawingml.x2006.chart.*;\r
+\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+/**\r
+ * @author Martin Andersson\r
+ */\r
+@Beta\r
+public class XSSFLineChartData implements LineChartData {\r
+\r
+ /**\r
+ * List of all data series.\r
+ */\r
+ private List<Serie> series;\r
+\r
+ public XSSFLineChartData() {\r
+ series = new ArrayList<Serie>();\r
+ }\r
+\r
+ static class Serie extends AbstractXSSFChartSerie implements LineChartSerie {\r
+ private int id;\r
+ private int order;\r
+ private ChartDataSource<?> categories;\r
+ private ChartDataSource<? extends Number> values;\r
+\r
+ protected Serie(int id, int order,\r
+ ChartDataSource<?> categories,\r
+ ChartDataSource<? extends Number> values) {\r
+ this.id = id;\r
+ this.order = order;\r
+ this.categories = categories;\r
+ this.values = values;\r
+ }\r
+\r
+ public ChartDataSource<?> getCategoryAxisData() {\r
+ return categories;\r
+ }\r
+\r
+ public ChartDataSource<? extends Number> getValues() {\r
+ return values;\r
+ }\r
+\r
+ protected void addToChart(CTLineChart ctLineChart) {\r
+ CTLineSer ctLineSer = ctLineChart.addNewSer();\r
+ ctLineSer.addNewIdx().setVal(id);\r
+ ctLineSer.addNewOrder().setVal(order);\r
+\r
+ // No marker symbol on the chart line.\r
+ ctLineSer.addNewMarker().addNewSymbol().setVal(STMarkerStyle.NONE);\r
+\r
+ CTAxDataSource catDS = ctLineSer.addNewCat();\r
+ XSSFChartUtil.buildAxDataSource(catDS, categories);\r
+ CTNumDataSource valueDS = ctLineSer.addNewVal();\r
+ XSSFChartUtil.buildNumDataSource(valueDS, values);\r
+\r
+ if (isTitleSet()) {\r
+ ctLineSer.setTx(getCTSerTx());\r
+ }\r
+ }\r
+ }\r
+\r
+ public LineChartSerie addSerie(ChartDataSource<?> categoryAxisData, ChartDataSource<? extends Number> values) {\r
+ if (!values.isNumeric()) {\r
+ throw new IllegalArgumentException("Value data source must be numeric.");\r
+ }\r
+ int numOfSeries = series.size();\r
+ Serie newSerie = new Serie(numOfSeries, numOfSeries, categoryAxisData, values);\r
+ series.add(newSerie);\r
+ return newSerie;\r
+ }\r
+\r
+ public List<? extends LineChartSerie> getSeries() {\r
+ return series;\r
+ }\r
+\r
+ public void fillChart(Chart chart, ChartAxis... axis) {\r
+ if (!(chart instanceof XSSFChart)) {\r
+ throw new IllegalArgumentException("Chart must be instance of XSSFChart");\r
+ }\r
+\r
+ XSSFChart xssfChart = (XSSFChart) chart;\r
+ CTPlotArea plotArea = xssfChart.getCTChart().getPlotArea();\r
+ CTLineChart lineChart = plotArea.addNewLineChart();\r
+ lineChart.addNewVaryColors().setVal(false);\r
+\r
+ for (Serie s : series) {\r
+ s.addToChart(lineChart);\r
+ }\r
+\r
+ for (ChartAxis ax : axis) {\r
+ lineChart.addNewAxId().setVal(ax.getId());\r
+ }\r
+ }\r
+}\r
/**
* Package private ScatterChartSerie implementation.
*/
- static class Serie implements ScatterChartSerie {
+ static class Serie extends AbstractXSSFChartSerie implements ScatterChartSerie {
private int id;
private int order;
private ChartDataSource<?> xs;
CTNumDataSource yVal = scatterSer.addNewYVal();
XSSFChartUtil.buildNumDataSource(yVal, ys);
+
+ if (isTitleSet()) {
+ scatterSer.setTx(getCTSerTx());
+ }
}
}
import org.apache.poi.ss.usermodel.charts.AxisOrientation;
import org.apache.poi.ss.usermodel.charts.AxisCrossBetween;
import org.apache.poi.ss.usermodel.charts.AxisCrosses;
+import org.apache.poi.ss.usermodel.charts.AxisTickMark;
import org.apache.poi.util.Beta;
import org.apache.poi.xssf.usermodel.XSSFChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTValAx;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTAxPos;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTBoolean;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumFmt;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTCrosses;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTScaling;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTTickMark;
import org.openxmlformats.schemas.drawingml.x2006.chart.STCrossBetween;
import org.openxmlformats.schemas.drawingml.x2006.chart.STTickLblPos;
return ctValAx.getCrosses();
}
+ @Override
+ protected CTBoolean getDelete() {
+ return ctValAx.getDelete();
+ }
+
+ @Override
+ protected CTTickMark getMajorCTTickMark() {
+ return ctValAx.getMajorTickMark();
+ }
+
+ @Override
+ protected CTTickMark getMinorCTTickMark() {
+ return ctValAx.getMinorTickMark();
+ }
+
public void crossAxis(ChartAxis axis) {
ctValAx.getCrossAx().setVal(axis.getId());
}
ctValAx.addNewCrosses();
ctValAx.addNewCrossAx();
ctValAx.addNewTickLblPos().setVal(STTickLblPos.NEXT_TO);
+ ctValAx.addNewDelete();
+ ctValAx.addNewMajorTickMark();
+ ctValAx.addNewMinorTickMark();
setPosition(pos);
setOrientation(AxisOrientation.MIN_MAX);
setCrossBetween(AxisCrossBetween.MIDPOINT_CATEGORY);
setCrosses(AxisCrosses.AUTO_ZERO);
+ setVisible(true);
+ setMajorTickMark(AxisTickMark.CROSS);
+ setMinorTickMark(AxisTickMark.NONE);
}
private static STCrossBetween.Enum fromCrossBetween(AxisCrossBetween crossBetween) {
assertTrue(Math.abs(axis.getMaximum() - newValue) < EPSILON);
}
+ public void testVisibleAccessMethods() {
+ axis.setVisible(true);
+ assertTrue(axis.isVisible());
+
+ axis.setVisible(false);
+ assertFalse(axis.isVisible());
+ }
+
+ public void testMajorTickMarkAccessMethods() {
+ axis.setMajorTickMark(AxisTickMark.NONE);
+ assertEquals(AxisTickMark.NONE, axis.getMajorTickMark());
+
+ axis.setMajorTickMark(AxisTickMark.IN);
+ assertEquals(AxisTickMark.IN, axis.getMajorTickMark());
+
+ axis.setMajorTickMark(AxisTickMark.OUT);
+ assertEquals(AxisTickMark.OUT, axis.getMajorTickMark());
+
+ axis.setMajorTickMark(AxisTickMark.CROSS);
+ assertEquals(AxisTickMark.CROSS, axis.getMajorTickMark());
+ }
+
+ public void testMinorTickMarkAccessMethods() {
+ axis.setMinorTickMark(AxisTickMark.NONE);
+ assertEquals(AxisTickMark.NONE, axis.getMinorTickMark());
+
+ axis.setMinorTickMark(AxisTickMark.IN);
+ assertEquals(AxisTickMark.IN, axis.getMinorTickMark());
+
+ axis.setMinorTickMark(AxisTickMark.OUT);
+ assertEquals(AxisTickMark.OUT, axis.getMinorTickMark());
+
+ axis.setMinorTickMark(AxisTickMark.CROSS);
+ assertEquals(AxisTickMark.CROSS, axis.getMinorTickMark());
+ }
}
--- /dev/null
+package org.apache.poi.xssf.usermodel.charts;\r
+\r
+import junit.framework.TestCase;\r
+import org.apache.poi.ss.usermodel.*;\r
+import org.apache.poi.ss.usermodel.charts.*;\r
+import org.apache.poi.ss.util.CellRangeAddress;\r
+import org.apache.poi.ss.util.SheetBuilder;\r
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;\r
+\r
+/**\r
+ * @author Martin Andersson\r
+ */\r
+public class TestXSSFLineChartData extends TestCase {\r
+\r
+ private static final Object[][] plotData = {\r
+ {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"},\r
+ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}\r
+ };\r
+\r
+ public void testOneSeriePlot() throws Exception {\r
+ Workbook wb = new XSSFWorkbook();\r
+ Sheet sheet = new SheetBuilder(wb, plotData).build();\r
+ Drawing drawing = sheet.createDrawingPatriarch();\r
+ ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 1, 1, 10, 30);\r
+ Chart chart = drawing.createChart(anchor);\r
+\r
+ ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);\r
+ ChartAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);\r
+\r
+ LineChartData lineChartData =\r
+ chart.getChartDataFactory().createLineChartData();\r
+\r
+ ChartDataSource<String> xs = DataSources.fromStringCellRange(sheet, CellRangeAddress.valueOf("A1:J1"));\r
+ ChartDataSource<Number> ys = DataSources.fromNumericCellRange(sheet, CellRangeAddress.valueOf("A2:J2"));\r
+ LineChartSerie serie = lineChartData.addSerie(xs, ys);\r
+\r
+ assertNotNull(serie);\r
+ assertEquals(1, lineChartData.getSeries().size());\r
+ assertTrue(lineChartData.getSeries().contains(serie));\r
+\r
+ chart.plot(lineChartData, bottomAxis, leftAxis);\r
+ }\r
+}\r