]> source.dussan.org Git - poi.git/commitdiff
#58130 CF DataBar example and tests
authorNick Burch <nick@apache.org>
Mon, 20 Jul 2015 04:43:36 +0000 (04:43 +0000)
committerNick Burch <nick@apache.org>
Mon, 20 Jul 2015 04:43:36 +0000 (04:43 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1691869 13f79535-47bb-0310-9956-ffa450edef68

src/examples/src/org/apache/poi/ss/examples/ConditionalFormats.java
src/java/org/apache/poi/hssf/usermodel/HSSFCreationHelper.java
src/java/org/apache/poi/ss/usermodel/CreationHelper.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCreationHelper.java
src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java

index fa01148cea4a1c743070496fc2d60b41c24209d5..7358edf6823e75044184f394025e050d4e084779 100644 (file)
@@ -57,8 +57,7 @@ public class ConditionalFormats {
         shadeBands(wb.createSheet("Shade Bands"));\r
         iconSets(wb.createSheet("Icon Sets"));\r
         colourScales(wb.createSheet("Colour Scales"));\r
-        \r
-        // TODO Add data bars, see bug #58130\r
+        dataBars(wb.createSheet("Data Bars"));\r
 \r
         // Write the output to a file\r
         String file = "cf-poi.xls";\r
@@ -541,4 +540,69 @@ public class ConditionalFormats {
         ((ExtendedColor)cs3.getColors()[1]).setARGBHex("FF63BE7B");\r
         sheetCF.addConditionalFormatting(regions, rule3);\r
     }\r
+    \r
+    /**\r
+     * DataBars / Data-Bars allow you to have bars shown vary\r
+     *  based on the values, from full to empty\r
+     */\r
+    static void dataBars(Sheet sheet) {\r
+        sheet.createRow(0).createCell(0).setCellValue("Data Bars");\r
+        Row r = sheet.createRow(1);\r
+        r.createCell(1).setCellValue("Green Positive");\r
+        r.createCell(2).setCellValue("Blue Mix");\r
+        r.createCell(3).setCellValue("Red Negative");\r
+        r = sheet.createRow(2);\r
+        r.createCell(1).setCellValue(0);\r
+        r.createCell(2).setCellValue(0);\r
+        r.createCell(3).setCellValue(0);\r
+        r = sheet.createRow(3);\r
+        r.createCell(1).setCellValue(5);\r
+        r.createCell(2).setCellValue(-5);\r
+        r.createCell(3).setCellValue(-5);\r
+        r = sheet.createRow(4);\r
+        r.createCell(1).setCellValue(10);\r
+        r.createCell(2).setCellValue(10);\r
+        r.createCell(3).setCellValue(-10);\r
+        r = sheet.createRow(5);\r
+        r.createCell(1).setCellValue(5);\r
+        r.createCell(2).setCellValue(5);\r
+        r.createCell(3).setCellValue(-5);\r
+        r = sheet.createRow(6);\r
+        r.createCell(1).setCellValue(20);\r
+        r.createCell(2).setCellValue(-10);\r
+        r.createCell(3).setCellValue(-20);\r
+        sheet.setColumnWidth(0, 3000);\r
+        sheet.setColumnWidth(1, 5000);\r
+        sheet.setColumnWidth(2, 5000);\r
+        sheet.setColumnWidth(3, 5000);\r
+\r
+        SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();\r
+        \r
+        ExtendedColor color = sheet.getWorkbook().getCreationHelper().createExtendedColor();\r
+        color.setARGBHex("FF63BE7B");\r
+        CellRangeAddress[] regions = { CellRangeAddress.valueOf("B2:B7") };\r
+        ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(color);\r
+        DataBarFormatting db1 = rule1.getDataBarFormatting();\r
+        db1.getMinThreshold().setRangeType(RangeType.MIN);\r
+        db1.getMaxThreshold().setRangeType(RangeType.MAX);\r
+        sheetCF.addConditionalFormatting(regions, rule1);\r
+        \r
+        color = sheet.getWorkbook().getCreationHelper().createExtendedColor();\r
+        color.setARGBHex("FF5A8AC6");\r
+        regions = new CellRangeAddress[] { CellRangeAddress.valueOf("C2:C7") };\r
+        ConditionalFormattingRule rule2 = sheetCF.createConditionalFormattingRule(color);\r
+        DataBarFormatting db2 = rule2.getDataBarFormatting();\r
+        db2.getMinThreshold().setRangeType(RangeType.MIN);\r
+        db2.getMaxThreshold().setRangeType(RangeType.MAX);\r
+        sheetCF.addConditionalFormatting(regions, rule2);\r
+        \r
+        color = sheet.getWorkbook().getCreationHelper().createExtendedColor();\r
+        color.setARGBHex("FFF8696B");\r
+        regions = new CellRangeAddress[] { CellRangeAddress.valueOf("D2:D7") };\r
+        ConditionalFormattingRule rule3 = sheetCF.createConditionalFormattingRule(color);\r
+        DataBarFormatting db3 = rule3.getDataBarFormatting();\r
+        db3.getMinThreshold().setRangeType(RangeType.MIN);\r
+        db3.getMaxThreshold().setRangeType(RangeType.MAX);\r
+        sheetCF.addConditionalFormatting(regions, rule3);\r
+    }\r
 }\r
index faee42b83f57b0c01bf2dcde3ccfb81a458c5123..6dc1bcfa8808ff64974e7d945f0bd32acc3bb6af 100644 (file)
@@ -17,6 +17,7 @@
 
 package org.apache.poi.hssf.usermodel;
 
+import org.apache.poi.hssf.record.common.ExtendedColor;
 import org.apache.poi.ss.usermodel.CreationHelper;
 
 public class HSSFCreationHelper implements CreationHelper {
@@ -42,7 +43,11 @@ public class HSSFCreationHelper implements CreationHelper {
                return new HSSFHyperlink(type);
        }
 
-       /**
+       public HSSFExtendedColor createExtendedColor() {
+        return new HSSFExtendedColor(new ExtendedColor());
+    }
+
+    /**
         * Creates a HSSFFormulaEvaluator, the object that evaluates formula cells.
         *
         * @return a HSSFFormulaEvaluator instance
index c07323723248ec3dc3ab5ada57d0902cba972f83..893ce41ec286fe5bd54af5d57edb2d4e8a01365c 100644 (file)
@@ -51,6 +51,12 @@ public interface CreationHelper {
      * @return a FormulaEvaluator instance
      */
     FormulaEvaluator createFormulaEvaluator();
+    
+    /**
+     * Creates a XSSF-style Color object, used for extended sheet
+     *  formattings and conditional formattings
+     */
+    ExtendedColor createExtendedColor();
 
     ClientAnchor createClientAnchor();
 }
index 390c20fa914b962ac35ae75f390594894ef44384..154fec6ff43a8457adba96a38adf37fd64e6799b 100644 (file)
@@ -39,6 +39,10 @@ public class XSSFCreationHelper implements CreationHelper {
                return workbook.createDataFormat();
        }
 
+    public XSSFColor createExtendedColor() {
+        return new XSSFColor();
+    }
+
     /**
      * Create a new XSSFHyperlink.
      *
index d14b556f440dd6a8269c5c6b057a96e56eda9f24..a12dbc13d98b10bb4158b943e293ecb87627143d 100644 (file)
@@ -1138,7 +1138,50 @@ public abstract class BaseTestConditionalFormatting extends TestCase {
     }\r
     \r
     public void testCreateDataBarFormatting() {\r
-        // TODO Implement then test\r
+        Workbook workbook = _testDataProvider.createWorkbook();\r
+        Sheet sheet = workbook.createSheet();\r
+\r
+        String colorHex = "FFFFEB84";\r
+        ExtendedColor color = workbook.getCreationHelper().createExtendedColor();\r
+        color.setARGBHex(colorHex);\r
+        SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();\r
+        ConditionalFormattingRule rule1 = \r
+                sheetCF.createConditionalFormattingRule(color);\r
+        DataBarFormatting dbFmt = rule1.getDataBarFormatting();\r
+        \r
+        assertEquals(false, dbFmt.isIconOnly());\r
+        assertEquals(true, dbFmt.isLeftToRight());\r
+        assertEquals(0,   dbFmt.getWidthMin());\r
+        assertEquals(100, dbFmt.getWidthMax());\r
+        assertColour(colorHex, dbFmt.getColor());\r
+        \r
+        dbFmt.getMinThreshold().setRangeType(RangeType.MIN);\r
+        dbFmt.getMaxThreshold().setRangeType(RangeType.MAX);\r
+        \r
+        CellRangeAddress [] regions = { CellRangeAddress.valueOf("A1:A5") };\r
+        sheetCF.addConditionalFormatting(regions, rule1);\r
+        \r
+        // Save, re-load and re-check\r
+        workbook = _testDataProvider.writeOutAndReadBack(workbook);\r
+        sheetCF = sheet.getSheetConditionalFormatting();\r
+        assertEquals(1, sheetCF.getNumConditionalFormattings());\r
+        \r
+        ConditionalFormatting cf = sheetCF.getConditionalFormattingAt(0);\r
+        assertEquals(1, cf.getNumberOfRules());\r
+        rule1 = cf.getRule(0);\r
+        dbFmt = rule1.getDataBarFormatting();\r
+        assertEquals(ConditionType.DATA_BAR, rule1.getConditionTypeType());\r
+        \r
+        assertEquals(false, dbFmt.isIconOnly());\r
+        assertEquals(true, dbFmt.isLeftToRight());\r
+        assertEquals(0,   dbFmt.getWidthMin());\r
+        assertEquals(100, dbFmt.getWidthMax());\r
+        assertColour(colorHex, dbFmt.getColor());\r
+\r
+        assertEquals(RangeType.MIN, dbFmt.getMinThreshold().getRangeType());\r
+        assertEquals(RangeType.MAX, dbFmt.getMaxThreshold().getRangeType());\r
+        assertEquals(null, dbFmt.getMinThreshold().getValue());\r
+        assertEquals(null, dbFmt.getMaxThreshold().getValue());\r
     }\r
     \r
     public void testBug55380() {\r