You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XSSFChartLegend.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xssf.usermodel.charts;
  16. import org.apache.poi.ss.usermodel.charts.ChartLegend;
  17. import org.apache.poi.ss.usermodel.charts.LegendPosition;
  18. import org.apache.poi.util.Internal;
  19. import org.apache.poi.util.Removal;
  20. import org.apache.poi.xddf.usermodel.chart.XDDFChartLegend;
  21. import org.apache.poi.xssf.usermodel.XSSFChart;
  22. import org.openxmlformats.schemas.drawingml.x2006.chart.CTChart;
  23. import org.openxmlformats.schemas.drawingml.x2006.chart.CTLegend;
  24. import org.openxmlformats.schemas.drawingml.x2006.chart.CTLegendPos;
  25. import org.openxmlformats.schemas.drawingml.x2006.chart.STLegendPos;
  26. /**
  27. * Represents a SpreadsheetML chart legend
  28. * @deprecated use {@link XDDFChartLegend} instead
  29. */
  30. @Deprecated
  31. @Removal(version="4.2")
  32. public final class XSSFChartLegend implements ChartLegend {
  33. /**
  34. * Underlaying CTLagend bean
  35. */
  36. private CTLegend legend;
  37. /**
  38. * Create a new SpreadsheetML chart legend
  39. */
  40. public XSSFChartLegend(XSSFChart chart) {
  41. CTChart ctChart = chart.getCTChart();
  42. this.legend = (ctChart.isSetLegend()) ?
  43. ctChart.getLegend() :
  44. ctChart.addNewLegend();
  45. setDefaults();
  46. }
  47. /**
  48. * Set sensible default styling.
  49. */
  50. private void setDefaults() {
  51. if (!legend.isSetOverlay()) {
  52. legend.addNewOverlay();
  53. }
  54. legend.getOverlay().setVal(false);
  55. }
  56. /**
  57. * Return the underlying CTLegend bean.
  58. *
  59. * @return the underlying CTLegend bean
  60. */
  61. @Internal
  62. public CTLegend getCTLegend(){
  63. return legend;
  64. }
  65. @Override
  66. public void setPosition(LegendPosition position) {
  67. if (!legend.isSetLegendPos()) {
  68. legend.addNewLegendPos();
  69. }
  70. legend.getLegendPos().setVal(fromLegendPosition(position));
  71. }
  72. /*
  73. * According to ECMA-376 default position is RIGHT.
  74. */
  75. @Override
  76. public LegendPosition getPosition() {
  77. if (legend.isSetLegendPos()) {
  78. return toLegendPosition(legend.getLegendPos());
  79. } else {
  80. return LegendPosition.RIGHT;
  81. }
  82. }
  83. @Override
  84. public XSSFManualLayout getManualLayout() {
  85. if (!legend.isSetLayout()) {
  86. legend.addNewLayout();
  87. }
  88. return new XSSFManualLayout(legend.getLayout());
  89. }
  90. @Override
  91. public boolean isOverlay() {
  92. return legend.getOverlay().getVal();
  93. }
  94. @Override
  95. public void setOverlay(boolean value) {
  96. legend.getOverlay().setVal(value);
  97. }
  98. private STLegendPos.Enum fromLegendPosition(LegendPosition position) {
  99. switch (position) {
  100. case BOTTOM: return STLegendPos.B;
  101. case LEFT: return STLegendPos.L;
  102. case RIGHT: return STLegendPos.R;
  103. case TOP: return STLegendPos.T;
  104. case TOP_RIGHT: return STLegendPos.TR;
  105. default:
  106. throw new IllegalArgumentException();
  107. }
  108. }
  109. private LegendPosition toLegendPosition(CTLegendPos ctLegendPos) {
  110. switch (ctLegendPos.getVal().intValue()) {
  111. case STLegendPos.INT_B: return LegendPosition.BOTTOM;
  112. case STLegendPos.INT_L: return LegendPosition.LEFT;
  113. case STLegendPos.INT_R: return LegendPosition.RIGHT;
  114. case STLegendPos.INT_T: return LegendPosition.TOP;
  115. case STLegendPos.INT_TR: return LegendPosition.TOP_RIGHT;
  116. default:
  117. throw new IllegalArgumentException();
  118. }
  119. }
  120. }