* High level representation of chart legend.
*
* @author Roman Kashitsyn
+ * @author Martin Andersson
*/
@Beta
public interface ChartLegend extends ManuallyPositionable {
-
+
/**
* @return legend position
*/
* @param position new legend position
*/
void setPosition(LegendPosition position);
+
+ /**
+ * @return overlay value.
+ */
+ boolean isOverlay();
+
+ /**
+ * If true the legend is positioned over the chart area otherwise
+ * the legend is displayed next to it.
+ *
+ * Default is no overlay.
+ *
+ * @param value
+ */
+ void setOverlay(boolean value);
}
/**
* Represents a SpreadsheetML chart legend
* @author Roman Kashitsyn
+ * @author Martin Andersson
*/
@Beta
public final class XSSFChartLegend implements ChartLegend {
this.legend = (ctChart.isSetLegend()) ?
ctChart.getLegend() :
ctChart.addNewLegend();
+
+ setDefaults();
+ }
+
+ /**
+ * Set sensible default styling.
+ */
+ private void setDefaults() {
+ if (!legend.isSetOverlay()) {
+ legend.addNewOverlay();
+ }
+ legend.getOverlay().setVal(false);
}
/**
return new XSSFManualLayout(legend.getLayout());
}
+ public boolean isOverlay() {
+ return legend.getOverlay().getVal();
+ }
+
+ public void setOverlay(boolean value) {
+ legend.getOverlay().setVal(value);
+ }
+
private STLegendPos.Enum fromLegendPosition(LegendPosition position) {
switch (position) {
case BOTTOM: return STLegendPos.B;
import org.apache.poi.ss.usermodel.charts.LegendPosition;
import org.apache.poi.xssf.usermodel.*;
+/**
+ * Tests ChartLegend
+ *
+ * @author Martin Andersson
+ * @author Cedric dot Walter at gmail dot com
+ */
public final class TestXSSFChartLegend extends TestCase {
-
+
public void testLegendPositionAccessMethods() throws Exception {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet();
legend.setPosition(LegendPosition.TOP_RIGHT);
assertEquals(LegendPosition.TOP_RIGHT, legend.getPosition());
}
+
+ public void test_setOverlay_defaultChartLegend_expectOverlayInitialValueSetToFalse() {
+ // Arrange
+ Workbook wb = new XSSFWorkbook();
+ Sheet sheet = wb.createSheet();
+ Drawing drawing = sheet.createDrawingPatriarch();
+ ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 1, 1, 10, 30);
+ Chart chart = drawing.createChart(anchor);
+ ChartLegend legend = chart.getOrCreateLegend();
+
+ // Act
+
+ // Assert
+ assertFalse(legend.isOverlay());
+ }
+
+ public void test_setOverlay_chartLegendSetToTrue_expectOverlayInitialValueSetToTrue() {
+ // Arrange
+ Workbook wb = new XSSFWorkbook();
+ Sheet sheet = wb.createSheet();
+ Drawing drawing = sheet.createDrawingPatriarch();
+ ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 1, 1, 10, 30);
+ Chart chart = drawing.createChart(anchor);
+ ChartLegend legend = chart.getOrCreateLegend();
+
+ // Act
+ legend.setOverlay(true);
+
+ // Assert
+ assertTrue(legend.isOverlay());
+ }
}