ValueAxis bottomAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.BOTTOM);
ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
- ScatterChartSerie firstSerie = data.addSerie();
- firstSerie.setXValues(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
- firstSerie.setYValues(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));
+ DataMarker xMarker = new DataMarker(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
+ DataMarker y1Marker = new DataMarker(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));
+ DataMarker y2Marker = new DataMarker(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1));
- ScatterChartSerie secondSerie = data.addSerie();
- secondSerie.setXValues(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
- secondSerie.setYValues(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1));
+
+ data.addSerie(xMarker, y1Marker);
+ data.addSerie(xMarker, y2Marker);
chart.plot(data, bottomAxis, leftAxis);
import org.apache.poi.ss.usermodel.charts.ChartData;
import org.apache.poi.ss.usermodel.charts.ChartAxis;
import org.apache.poi.ss.usermodel.charts.ChartLegend;
+import org.apache.poi.ss.usermodel.charts.ManualLayout;
+import org.apache.poi.ss.usermodel.charts.ManuallyPositionable;
import org.apache.poi.ss.usermodel.charts.ChartDataFactory;
import org.apache.poi.ss.usermodel.charts.ChartAxisFactory;
*
* @author Roman Kashitsyn
*/
-public interface Chart {
+public interface Chart extends ManuallyPositionable {
/**
* @return an appropriate ChartDataFactory implementation
AxisOrientation getOrientation();
/**
- * @param axis orientation
+ * @param orientation axis orientation
*/
void setOrientation(AxisOrientation orientation);
*
* @author Roman Kashitsyn
*/
-public interface ChartLegend {
+public interface ChartLegend extends ManuallyPositionable {
/**
* @return legend position
* @param position new legend position
*/
void setPosition(LegendPosition position);
-
}
--- /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
+ * Specifies the possible ways to store a chart element's position.\r
+ * @author Roman Kashitsyn\r
+ */\r
+public enum LayoutMode {\r
+ /**\r
+ * Specifies that the Width or Height shall be interpreted as the\r
+ * Right or Bottom of the chart element.\r
+ */\r
+ EDGE,\r
+ /**\r
+ * Specifies that the Width or Height shall be interpreted as the\r
+ * Width or Height of the chart element.\r
+ */\r
+ FACTOR\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
+ * Specifies whether to layout the plot area by its inside (not including axis\r
+ * and axis labels) or outside (including axis and axis labels).\r
+ *\r
+ * @author Roman Kashitsyn\r
+ */\r
+public enum LayoutTarget {\r
+ /**\r
+ * Specifies that the plot area size shall determine the\r
+ * size of the plot area, not including the tick marks and\r
+ * axis labels.\r
+ */\r
+ INNER,\r
+ /**\r
+ * Specifies that the plot area size shall determine the\r
+ * size of the plot area, the tick marks, and the axis\r
+ * labels.\r
+ */\r
+ OUTER\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
+ * High level representation of chart element manual layout.\r
+ *\r
+ * @author Roman Kashitsyn\r
+ */\r
+public interface ManualLayout {\r
+\r
+ /**\r
+ * Sets the layout target.\r
+ * @param target new layout target.\r
+ */\r
+ public void setTarget(LayoutTarget target);\r
+\r
+ /**\r
+ * Returns current layout target.\r
+ * @return current layout target\r
+ */\r
+ public LayoutTarget getTarget();\r
+\r
+ /**\r
+ * Sets the x-coordinate layout mode.\r
+ * @param mode new x-coordinate layout mode.\r
+ */\r
+ public void setXMode(LayoutMode mode);\r
+\r
+ /**\r
+ * Returns current x-coordinnate layout mode.\r
+ * @return current x-coordinate layout mode.\r
+ */\r
+ public LayoutMode getXMode();\r
+\r
+ /**\r
+ * Sets the y-coordinate layout mode.\r
+ * @param mode new y-coordinate layout mode.\r
+ */\r
+ public void setYMode(LayoutMode mode);\r
+\r
+ /**\r
+ * Returns current y-coordinate layout mode.\r
+ * @return current y-coordinate layout mode.\r
+ */\r
+ public LayoutMode getYMode();\r
+\r
+ /**\r
+ * Returns the x location of the chart element.\r
+ * @return the x location (left) of the chart element or 0.0 if\r
+ * not set.\r
+ */\r
+ public double getX();\r
+\r
+ /**\r
+ * Specifies the x location (left) of the chart element as a\r
+ * fraction of the width of the chart. If Left Mode is Factor,\r
+ * then the position is relative to the default position for the\r
+ * chart element.\r
+ */\r
+ public void setX(double x);\r
+\r
+\r
+ /**\r
+ * Returns current y location of the chart element.\r
+ * @return the y location (top) of the chart element or 0.0 if not\r
+ * set.\r
+ */\r
+ public double getY();\r
+\r
+ /**\r
+ * Specifies the y location (top) of the chart element as a\r
+ * fraction of the height of the chart. If Top Mode is Factor,\r
+ * then the position is relative to the default position for the\r
+ * chart element.\r
+ */\r
+ public void setY(double y);\r
+\r
+\r
+ /**\r
+ * Specifies how to interpret the Width element for this manual\r
+ * layout.\r
+ * @param mode new width layout mode of this manual layout.\r
+ */\r
+ public void setWidthMode(LayoutMode mode);\r
+\r
+\r
+ /**\r
+ * Returns current width mode of this manual layout.\r
+ * @return width mode of this manual layout.\r
+ */\r
+ public LayoutMode getWidthMode();\r
+\r
+ /**\r
+ * Specifies how to interpret the Height element for this manual\r
+ * layout.\r
+ * @param mode new height mode of this manual layout.\r
+ */\r
+ public void setHeightMode(LayoutMode mode);\r
+\r
+ /**\r
+ * Returns current height mode of this \r
+ * @return height mode of this manual layout.\r
+ */\r
+ public LayoutMode getHeightMode();\r
+\r
+ /**\r
+ * Specifies the width (if Width Mode is Factor) or right (if\r
+ * Width Mode is Edge) of the chart element as a fraction of the\r
+ * width of the chart.\r
+ * @param ratio a fraction of the width of the chart.\r
+ */\r
+ public void setWidthRatio(double ratio);\r
+\r
+ /**\r
+ * Returns current fraction of the width of the chart.\r
+ * @return fraction of the width of the chart or 0.0 if not set.\r
+ */\r
+ public double getWidthRatio();\r
+\r
+ /**\r
+ * Specifies the height (if Height Mode is Factor) or bottom (if\r
+ * Height Mode is edge) of the chart element as a fraction of the\r
+ * height of the chart.\r
+ * @param ratio a fraction of the height of the chart.\r
+ */\r
+ public void setHeightRatio(double ratio);\r
+\r
+ /**\r
+ * Returns current fraction of the height of the chart.\r
+ * @return fraction of the height of the chart or 0.0 if not set.\r
+ */\r
+ public double getHeightRatio();\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
+ * Abstraction of chart element that can be positioned with manual\r
+ * layout.\r
+ *\r
+ * @author Roman Kashitsyn\r
+ */\r
+public interface ManuallyPositionable {\r
+\r
+ /**\r
+ * Returns manual layout for the chart element.\r
+ * @return manual layout for the chart element.\r
+ */\r
+ public ManualLayout getManualLayout();\r
+}\r
package org.apache.poi.ss.usermodel.charts;
import java.util.List;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.util.DataMarker;
+
/**
* @author Roman Kashitsyn
*/
public interface ScatterChartData extends ChartData {
/**
+ * @param xMarker data marker to be used for X value range
+ * @param yMarker data marker to be used for Y value range
* @return a new scatter chart serie
*/
- ScatterChartSerie addSerie();
+ ScatterChartSerie addSerie(DataMarker xMarker, DataMarker yMarker);
/**
* @return list of all series
package org.apache.poi.ss.usermodel.charts;
import org.apache.poi.ss.usermodel.Sheet;
-import org.apache.poi.ss.util.CellRangeAddress;
+import org.apache.poi.ss.util.DataMarker;
import org.apache.poi.ss.usermodel.charts.ChartDataFactory;
/**
public interface ScatterChartSerie {
/**
- * @param sheet a sheet to take range from
- * @param address a column or a row with values
+ * @param xMarker data marker to use for X values.
*/
- void setXValues(Sheet sheet, CellRangeAddress address);
-
+ void setXValues(DataMarker xMarker);
+
/**'
- * @param sheet a sheet to take range from
- * @param address a column or a row with values
+ * @param yMarker data marker to use for Y values.
*/
- void setYValues(Sheet sheet, CellRangeAddress address);
+ void setYValues(DataMarker yMarker);
}
--- /dev/null
+/*\r
+ * ====================================================================\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
+\r
+package org.apache.poi.ss.util;\r
+\r
+import org.apache.poi.ss.usermodel.Sheet;\r
+\r
+/**\r
+ * Represents data marker used in charts.\r
+ * @author Roman Kashitsyn\r
+ */\r
+public class DataMarker {\r
+ \r
+ private Sheet sheet;\r
+ private CellRangeAddress range;\r
+\r
+ /**\r
+ * @param sheet the sheet where data located.\r
+ * @param range the range within that sheet.\r
+ */\r
+ public DataMarker(Sheet sheet, CellRangeAddress range) {\r
+ this.sheet = sheet;\r
+ this.range = range;\r
+ }\r
+\r
+ /**\r
+ * Returns the sheet marker points to.\r
+ * @return sheet marker points to.\r
+ */\r
+ public Sheet getSheet() {\r
+ return sheet;\r
+ }\r
+\r
+ /**\r
+ * Sets sheet marker points to.\r
+ * @param sheet new sheet for the marker.\r
+ */\r
+ public void setSheet(Sheet sheet) {\r
+ this.sheet = sheet;\r
+ }\r
+\r
+ /**\r
+ * Returns range of the marker.\r
+ * @return range of cells marker points to.\r
+ */\r
+ public CellRangeAddress getRange() {\r
+ return range;\r
+ }\r
+\r
+ /**\r
+ * Sets range of the marker.\r
+ * @param range new range for the marker.\r
+ */\r
+ public void setRange(CellRangeAddress range) {\r
+ this.range = range;\r
+ }\r
+\r
+ /**\r
+ * Formats data marker using canonical format, for example\r
+ * `SheetName!$A$1:$A$5'.\r
+ * @return formatted data marker.\r
+ */\r
+ public String formatAsString() {\r
+ String sheetName = (sheet == null) ? (null) : (sheet.getSheetName());\r
+ if (range == null) {\r
+ return null;\r
+ } else {\r
+ return range.formatAsString(sheetName, true);\r
+ }\r
+ }\r
+}\r
import org.apache.poi.xssf.usermodel.charts.XSSFChartDataFactory;
import org.apache.poi.xssf.usermodel.charts.XSSFChartAxis;
import org.apache.poi.xssf.usermodel.charts.XSSFValueAxis;
+import org.apache.poi.xssf.usermodel.charts.XSSFManualLayout;
import org.apache.poi.xssf.usermodel.charts.XSSFChartLegend;
import org.apache.poi.ss.usermodel.charts.ChartData;
import org.apache.poi.ss.usermodel.charts.AxisPosition;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTLayout;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTManualLayout;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTValAx;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPrintSettings;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPageMargins;
import org.openxmlformats.schemas.drawingml.x2006.chart.STLayoutTarget;
}
/**
- * Construct a SpreadsheetML chart from a package part
+ * Construct a SpreadsheetML chart from a package part.
*
* @param part the package part holding the chart data,
* the content type must be <code>application/vnd.openxmlformats-officedocument.drawingml.chart+xml</code>
}
/**
- * Construct a new CTChartSpace bean. By default, it's just an empty placeholder for chart objects
+ * Construct a new CTChartSpace bean.
+ * By default, it's just an empty placeholder for chart objects.
*
* @return a new CTChartSpace bean
*/
chartSpace = CTChartSpace.Factory.newInstance();
chart = chartSpace.addNewChart();
CTPlotArea plotArea = chart.addNewPlotArea();
- CTLayout layout = plotArea.addNewLayout();
- CTManualLayout manualLayout = layout.addNewManualLayout();
- manualLayout.addNewLayoutTarget().setVal(STLayoutTarget.INNER);
- manualLayout.addNewXMode().setVal(STLayoutMode.EDGE);
- manualLayout.addNewYMode().setVal(STLayoutMode.EDGE);
- manualLayout.addNewX().setVal(0);
- manualLayout.addNewY().setVal(0);
- manualLayout.addNewW().setVal(0.65);
- manualLayout.addNewH().setVal(0.8);
+
+ plotArea.addNewLayout();
chart.addNewPlotVisOnly().setVal(true);
+
CTPrintSettings printSettings = chartSpace.addNewPrintSettings();
printSettings.addNewHeaderFooter();
}
public List<? extends XSSFChartAxis> getAxis() {
+ if (axis.isEmpty() && hasAxis()) {
+ parseAxis();
+ }
return axis;
}
- /**
- * Sets the width ratio of the chart.
- * Chart width is ratio multiplied by parent frame width.
- * @param ratio a number between 0 and 1.
- */
- public void setWidthRatio(double ratio) {
- chart.getPlotArea().getLayout().getManualLayout().getW().setVal(ratio);
- }
-
- /**
- * @return relative chart width
- */
- public double getWidthRatio() {
- return chart.getPlotArea().getLayout().getManualLayout().getW().getVal();
- }
-
- /**
- * Sets the height ratio of the chart.
- * Chart height is ratio multiplied by parent frame height.
- * @param ratio a number between 0 and 1.
- */
- public void setHeightRatio(double ratio) {
- chart.getPlotArea().getLayout().getManualLayout().getH().setVal(ratio);
- }
-
- /**
- * @return relative chart height
- */
- public double getHeightRatio() {
- return chart.getPlotArea().getLayout().getManualLayout().getH().getVal();
+ public XSSFManualLayout getManualLayout() {
+ return new XSSFManualLayout(this);
}
/**
}
}
+ private boolean hasAxis() {
+ CTPlotArea ctPlotArea = chart.getPlotArea();
+ int totalAxisCount =
+ ctPlotArea.sizeOfValAxArray() +
+ ctPlotArea.sizeOfCatAxArray() +
+ ctPlotArea.sizeOfDateAxArray() +
+ ctPlotArea.sizeOfSerAxArray();
+ return totalAxisCount > 0;
+ }
+
+ private void parseAxis() {
+ // TODO: add other axis types
+ parseValueAxis();
+ }
+
+ private void parseValueAxis() {
+ for (CTValAx valAx : chart.getPlotArea().getValAxArray()) {
+ axis.add(new XSSFValueAxis(this, valAx));
+ }
+ }
+
}
}
}
+ public XSSFManualLayout getManualLayout() {
+ if (!legend.isSetLayout()) {
+ legend.addNewLayout();
+ }
+ return new XSSFManualLayout(legend.getLayout());
+ }
+
private STLegendPos.Enum fromLegendPosition(LegendPosition position) {
switch (position) {
case BOTTOM: return STLegendPos.B;
--- /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.util.Internal;\r
+import org.apache.poi.ss.usermodel.charts.ManualLayout;\r
+import org.apache.poi.ss.usermodel.charts.LayoutMode;\r
+import org.apache.poi.ss.usermodel.charts.LayoutTarget;\r
+import org.apache.poi.xssf.usermodel.XSSFChart;\r
+import org.openxmlformats.schemas.drawingml.x2006.chart.STLayoutTarget;\r
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTLayout;\r
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTManualLayout;\r
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;\r
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTLayoutMode;\r
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTLayoutTarget;\r
+import org.openxmlformats.schemas.drawingml.x2006.chart.STLayoutMode;\r
+import org.openxmlformats.schemas.drawingml.x2006.chart.STLayoutTarget;\r
+\r
+/**\r
+ * Represents a SpreadsheetML manual layout.\r
+ * @author Roman Kashitsyn\r
+ */\r
+public final class XSSFManualLayout implements ManualLayout {\r
+\r
+ /**\r
+ * Underlaying CTManualLayout bean.\r
+ */\r
+ private CTManualLayout layout;\r
+\r
+ private static final LayoutMode defaultLayoutMode = LayoutMode.EDGE;\r
+ private static final LayoutTarget defaultLayoutTarget = LayoutTarget.INNER;\r
+\r
+ /**\r
+ * Create a new SpreadsheetML manual layout.\r
+ * @param layout a Spreadsheet ML layout that should be used as base.\r
+ */\r
+ public XSSFManualLayout(CTLayout ctLayout) {\r
+ initLayout(ctLayout);\r
+ }\r
+\r
+ /**\r
+ * Create a new SpreadsheetML manual layout for chart.\r
+ * @param chart a chart to create layout for.\r
+ */\r
+ public XSSFManualLayout(XSSFChart chart) {\r
+ CTPlotArea ctPlotArea = chart.getCTChart().getPlotArea();\r
+ CTLayout ctLayout = ctPlotArea.isSetLayout() ?\r
+ ctPlotArea.getLayout() : ctPlotArea.addNewLayout();\r
+\r
+ initLayout(ctLayout);\r
+ }\r
+\r
+ /**\r
+ * Return the underlying CTManualLayout bean.\r
+ *\r
+ * @return the underlying CTManualLayout bean.\r
+ */\r
+ @Internal public CTManualLayout getCTManualLayout(){\r
+ return layout;\r
+ }\r
+\r
+ public void setWidthRatio(double ratio) {\r
+ if (!layout.isSetW()) {\r
+ layout.addNewW();\r
+ }\r
+ layout.getW().setVal(ratio);\r
+ }\r
+\r
+ public double getWidthRatio() {\r
+ if (!layout.isSetW()) {\r
+ return 0.0;\r
+ }\r
+ return layout.getW().getVal();\r
+ }\r
+\r
+ public void setHeightRatio(double ratio) {\r
+ if (!layout.isSetH()) {\r
+ layout.addNewH();\r
+ }\r
+ layout.getH().setVal(ratio);\r
+ }\r
+\r
+ public double getHeightRatio() {\r
+ if (!layout.isSetH()) {\r
+ return 0.0;\r
+ }\r
+ return layout.getH().getVal();\r
+ }\r
+\r
+ public LayoutTarget getTarget() {\r
+ if (!layout.isSetLayoutTarget()) {\r
+ return defaultLayoutTarget;\r
+ }\r
+ return toLayoutTarget(layout.getLayoutTarget());\r
+ }\r
+\r
+ public void setTarget(LayoutTarget target) {\r
+ if (!layout.isSetLayoutTarget()) {\r
+ layout.addNewLayoutTarget();\r
+ }\r
+ layout.getLayoutTarget().setVal(fromLayoutTarget(target));\r
+ }\r
+\r
+ public LayoutMode getXMode() {\r
+ if (!layout.isSetXMode()) {\r
+ return defaultLayoutMode;\r
+ }\r
+ return toLayoutMode(layout.getXMode());\r
+ }\r
+\r
+ public void setXMode(LayoutMode mode) {\r
+ if (!layout.isSetXMode()) {\r
+ layout.addNewXMode();\r
+ }\r
+ layout.getXMode().setVal(fromLayoutMode(mode));\r
+ }\r
+\r
+ public LayoutMode getYMode() {\r
+ if (!layout.isSetYMode()) {\r
+ return defaultLayoutMode;\r
+ }\r
+ return toLayoutMode(layout.getYMode());\r
+ }\r
+\r
+ public void setYMode(LayoutMode mode) {\r
+ if (!layout.isSetYMode()) {\r
+ layout.addNewYMode();\r
+ }\r
+ layout.getYMode().setVal(fromLayoutMode(mode));\r
+ }\r
+\r
+ public double getX() {\r
+ if (!layout.isSetX()) {\r
+ return 0.0;\r
+ }\r
+ return layout.getX().getVal();\r
+ }\r
+\r
+ public void setX(double x) {\r
+ if (!layout.isSetX()) {\r
+ layout.addNewX();\r
+ }\r
+ layout.getX().setVal(x);\r
+ }\r
+\r
+ public double getY() {\r
+ if (!layout.isSetY()) {\r
+ return 0.0;\r
+ }\r
+ return layout.getY().getVal();\r
+ }\r
+\r
+ public void setY(double y) {\r
+ if (!layout.isSetY()) {\r
+ layout.addNewY();\r
+ }\r
+ layout.getY().setVal(y);\r
+ }\r
+\r
+ public LayoutMode getWidthMode() {\r
+ if (!layout.isSetWMode()) {\r
+ return defaultLayoutMode;\r
+ }\r
+ return toLayoutMode(layout.getWMode());\r
+ }\r
+\r
+ public void setWidthMode(LayoutMode mode) {\r
+ if (!layout.isSetWMode()) {\r
+ layout.addNewWMode();\r
+ }\r
+ layout.getWMode().setVal(fromLayoutMode(mode));\r
+ }\r
+\r
+ public LayoutMode getHeightMode() {\r
+ if (!layout.isSetHMode()) {\r
+ return defaultLayoutMode;\r
+ }\r
+ return toLayoutMode(layout.getHMode());\r
+ }\r
+\r
+ public void setHeightMode(LayoutMode mode) {\r
+ if (!layout.isSetHMode()) {\r
+ layout.addNewHMode();\r
+ }\r
+ layout.getHMode().setVal(fromLayoutMode(mode));\r
+ }\r
+\r
+ private void initLayout(CTLayout ctLayout) {\r
+ if (ctLayout.isSetManualLayout()) {\r
+ this.layout = ctLayout.getManualLayout();\r
+ } else {\r
+ this.layout = ctLayout.addNewManualLayout();\r
+ }\r
+ }\r
+\r
+ private STLayoutMode.Enum fromLayoutMode(LayoutMode mode) {\r
+ switch (mode) {\r
+ case EDGE: return STLayoutMode.EDGE;\r
+ case FACTOR: return STLayoutMode.FACTOR;\r
+ default:\r
+ throw new IllegalArgumentException();\r
+ }\r
+ }\r
+\r
+ private LayoutMode toLayoutMode(CTLayoutMode ctLayoutMode) {\r
+ switch (ctLayoutMode.getVal().intValue()) {\r
+ case STLayoutMode.INT_EDGE: return LayoutMode.EDGE;\r
+ case STLayoutMode.INT_FACTOR: return LayoutMode.FACTOR;\r
+ default:\r
+ throw new IllegalArgumentException();\r
+ }\r
+ }\r
+\r
+ private STLayoutTarget.Enum fromLayoutTarget(LayoutTarget target) {\r
+ switch (target) {\r
+ case INNER: return STLayoutTarget.INNER;\r
+ case OUTER: return STLayoutTarget.OUTER;\r
+ default:\r
+ throw new IllegalArgumentException();\r
+ }\r
+ }\r
+\r
+ private LayoutTarget toLayoutTarget(CTLayoutTarget ctLayoutTarget) {\r
+ switch (ctLayoutTarget.getVal().intValue()) {\r
+ case STLayoutTarget.INT_INNER: return LayoutTarget.INNER;\r
+ case STLayoutTarget.INT_OUTER: return LayoutTarget.OUTER;\r
+ default:\r
+ throw new IllegalArgumentException();\r
+ }\r
+ }\r
+}\r
import org.apache.poi.ss.usermodel.Chart;
import org.apache.poi.ss.usermodel.Sheet;
-import org.apache.poi.ss.util.CellRangeAddress;
+import org.apache.poi.ss.util.DataMarker;
import org.apache.poi.ss.usermodel.charts.ScatterChartData;
import org.apache.poi.ss.usermodel.charts.ScatterChartSerie;
import org.apache.poi.ss.usermodel.charts.ChartDataFactory;
private int id;
private int order;
private boolean useCache;
- private Sheet xSheet;
- private Sheet ySheet;
- private CellRangeAddress xAddress;
- private CellRangeAddress yAddress;
+ private DataMarker xMarker;
+ private DataMarker yMarker;
public Serie(int id, int order) {
super();
this.useCache = false;
}
- public void setXValues(Sheet sheet, CellRangeAddress address) {
- this.xSheet = sheet;
- this.xAddress = address;
+ public void setXValues(DataMarker marker) {
+ xMarker = marker;
}
- public void setYValues(Sheet sheet, CellRangeAddress address) {
- this.ySheet = sheet;
- this.yAddress = address;
+ public void setYValues(DataMarker marker) {
+ yMarker = marker;
}
/**
CTAxDataSource xVal = scatterSer.addNewXVal();
CTNumRef numRef = xVal.addNewNumRef();
- numRef.setF(xAddress.formatAsString(xSheet.getSheetName(), true));
+ numRef.setF(xMarker.formatAsString());
CTNumDataSource yVal = scatterSer.addNewYVal();
numRef = yVal.addNewNumRef();
- numRef.setF(yAddress.formatAsString(ySheet.getSheetName(), true));
+ numRef.setF(yMarker.formatAsString());
}
}
- public XSSFScatterChartData.Serie addSerie() {
+ public XSSFScatterChartData.Serie addSerie(DataMarker xMarker, DataMarker yMarker) {
int numOfSeries = series.size();
Serie newSerie = new Serie(numOfSeries, numOfSeries);
+ newSerie.setXValues(xMarker);
+ newSerie.setYValues(yMarker);
series.add(newSerie);
return newSerie;
}
createAxis(id, pos);
}
+ public XSSFValueAxis(XSSFChart chart, CTValAx ctValAx) {
+ super(chart);
+ this.ctValAx = ctValAx;
+ }
+
public long getId() {
return ctValAx.getAxId().getVal();
}
--- /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 junit.framework.TestCase;\r
+\r
+import org.apache.poi.ss.usermodel.*;\r
+import org.apache.poi.ss.usermodel.charts.ChartLegend;\r
+import org.apache.poi.ss.usermodel.charts.LegendPosition;\r
+import org.apache.poi.ss.usermodel.charts.ManualLayout;\r
+import org.apache.poi.ss.usermodel.charts.LayoutMode;\r
+import org.apache.poi.ss.usermodel.charts.LayoutTarget;\r
+import org.apache.poi.xssf.usermodel.*;\r
+\r
+public final class TestXSSFManualLayout extends TestCase {\r
+ \r
+ /*\r
+ * Accessor methods are not trivial. They use lazy underlying bean\r
+ * initialization so there can be some errors (NPE, for example).\r
+ */\r
+ public void testAccessorMethods() throws Exception {\r
+ final double newRatio = 1.1;\r
+ final double newCoordinate = 0.3;\r
+ final LayoutMode nonDefaultMode = LayoutMode.FACTOR;\r
+ final LayoutTarget nonDefaultTarget = LayoutTarget.OUTER;\r
+\r
+ ManualLayout layout = getEmptyLayout();\r
+\r
+ layout.setWidthRatio(newRatio);\r
+ assertTrue(layout.getWidthRatio() == newRatio);\r
+\r
+ layout.setHeightRatio(newRatio);\r
+ assertTrue(layout.getHeightRatio() == newRatio);\r
+\r
+ layout.setX(newCoordinate);\r
+ assertTrue(layout.getX() == newCoordinate);\r
+\r
+ layout.setY(newCoordinate);\r
+ assertTrue(layout.getY() == newCoordinate);\r
+\r
+ layout.setXMode(nonDefaultMode);\r
+ assertTrue(layout.getXMode() == nonDefaultMode);\r
+\r
+ layout.setYMode(nonDefaultMode);\r
+ assertTrue(layout.getYMode() == nonDefaultMode);\r
+\r
+ layout.setWidthMode(nonDefaultMode);\r
+ assertTrue(layout.getWidthMode() == nonDefaultMode);\r
+\r
+ layout.setHeightMode(nonDefaultMode);\r
+ assertTrue(layout.getHeightMode() == nonDefaultMode);\r
+\r
+ layout.setTarget(nonDefaultTarget);\r
+ assertTrue(layout.getTarget() == nonDefaultTarget);\r
+\r
+ }\r
+\r
+ /*\r
+ * Layout must have reasonable default values and must not throw\r
+ * any exceptions.\r
+ */\r
+ public void testDefaultValues() throws Exception {\r
+ ManualLayout layout = getEmptyLayout();\r
+ \r
+ assertNotNull(layout.getTarget());\r
+ assertNotNull(layout.getXMode());\r
+ assertNotNull(layout.getYMode());\r
+ assertNotNull(layout.getHeightMode());\r
+ assertNotNull(layout.getWidthMode());\r
+ /*\r
+ * According to interface, 0.0 should be returned for\r
+ * uninitialized double properties.\r
+ */\r
+ assertTrue(layout.getX() == 0.0);\r
+ assertTrue(layout.getY() == 0.0);\r
+ assertTrue(layout.getWidthRatio() == 0.0);\r
+ assertTrue(layout.getHeightRatio() == 0.0);\r
+ }\r
+\r
+ private ManualLayout getEmptyLayout() {\r
+ Workbook wb = new XSSFWorkbook();\r
+ Sheet sheet = wb.createSheet();\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
+ ChartLegend legend = chart.getOrCreateLegend();\r
+ return legend.getManualLayout();\r
+ }\r
+}\r
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
+import org.apache.poi.ss.util.DataMarker;
import org.apache.poi.ss.usermodel.charts.*;
import org.apache.poi.xssf.usermodel.charts.XSSFChartDataFactory;
ScatterChartData scatterChartData =
XSSFChartDataFactory.getInstance().createScatterChartData();
- ScatterChartSerie serie = scatterChartData.addSerie();
- serie.setXValues(sheet, new CellRangeAddress(0,0,1,10));
- serie.setYValues(sheet, new CellRangeAddress(1,1,1,10));
+ DataMarker xMarker = new DataMarker(sheet, new CellRangeAddress(0,0,1,10));
+ DataMarker yMarker = new DataMarker(sheet, new CellRangeAddress(1,1,1,10));
+ ScatterChartSerie serie = scatterChartData.addSerie(xMarker, yMarker);
assertEquals(scatterChartData.getSeries().size(), 1);