#58130 CF DataBar example and tests

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1691869 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2015-07-20 04:43:36 +00:00
parent 104d9ad8e3
commit b7d27d44ce
5 changed files with 126 additions and 4 deletions

View File

@ -57,8 +57,7 @@ public class ConditionalFormats {
shadeBands(wb.createSheet("Shade Bands"));
iconSets(wb.createSheet("Icon Sets"));
colourScales(wb.createSheet("Colour Scales"));
// TODO Add data bars, see bug #58130
dataBars(wb.createSheet("Data Bars"));
// Write the output to a file
String file = "cf-poi.xls";
@ -541,4 +540,69 @@ public class ConditionalFormats {
((ExtendedColor)cs3.getColors()[1]).setARGBHex("FF63BE7B");
sheetCF.addConditionalFormatting(regions, rule3);
}
/**
* DataBars / Data-Bars allow you to have bars shown vary
* based on the values, from full to empty
*/
static void dataBars(Sheet sheet) {
sheet.createRow(0).createCell(0).setCellValue("Data Bars");
Row r = sheet.createRow(1);
r.createCell(1).setCellValue("Green Positive");
r.createCell(2).setCellValue("Blue Mix");
r.createCell(3).setCellValue("Red Negative");
r = sheet.createRow(2);
r.createCell(1).setCellValue(0);
r.createCell(2).setCellValue(0);
r.createCell(3).setCellValue(0);
r = sheet.createRow(3);
r.createCell(1).setCellValue(5);
r.createCell(2).setCellValue(-5);
r.createCell(3).setCellValue(-5);
r = sheet.createRow(4);
r.createCell(1).setCellValue(10);
r.createCell(2).setCellValue(10);
r.createCell(3).setCellValue(-10);
r = sheet.createRow(5);
r.createCell(1).setCellValue(5);
r.createCell(2).setCellValue(5);
r.createCell(3).setCellValue(-5);
r = sheet.createRow(6);
r.createCell(1).setCellValue(20);
r.createCell(2).setCellValue(-10);
r.createCell(3).setCellValue(-20);
sheet.setColumnWidth(0, 3000);
sheet.setColumnWidth(1, 5000);
sheet.setColumnWidth(2, 5000);
sheet.setColumnWidth(3, 5000);
SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
ExtendedColor color = sheet.getWorkbook().getCreationHelper().createExtendedColor();
color.setARGBHex("FF63BE7B");
CellRangeAddress[] regions = { CellRangeAddress.valueOf("B2:B7") };
ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(color);
DataBarFormatting db1 = rule1.getDataBarFormatting();
db1.getMinThreshold().setRangeType(RangeType.MIN);
db1.getMaxThreshold().setRangeType(RangeType.MAX);
sheetCF.addConditionalFormatting(regions, rule1);
color = sheet.getWorkbook().getCreationHelper().createExtendedColor();
color.setARGBHex("FF5A8AC6");
regions = new CellRangeAddress[] { CellRangeAddress.valueOf("C2:C7") };
ConditionalFormattingRule rule2 = sheetCF.createConditionalFormattingRule(color);
DataBarFormatting db2 = rule2.getDataBarFormatting();
db2.getMinThreshold().setRangeType(RangeType.MIN);
db2.getMaxThreshold().setRangeType(RangeType.MAX);
sheetCF.addConditionalFormatting(regions, rule2);
color = sheet.getWorkbook().getCreationHelper().createExtendedColor();
color.setARGBHex("FFF8696B");
regions = new CellRangeAddress[] { CellRangeAddress.valueOf("D2:D7") };
ConditionalFormattingRule rule3 = sheetCF.createConditionalFormattingRule(color);
DataBarFormatting db3 = rule3.getDataBarFormatting();
db3.getMinThreshold().setRangeType(RangeType.MIN);
db3.getMaxThreshold().setRangeType(RangeType.MAX);
sheetCF.addConditionalFormatting(regions, rule3);
}
}

View 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

View File

@ -52,5 +52,11 @@ public interface CreationHelper {
*/
FormulaEvaluator createFormulaEvaluator();
/**
* Creates a XSSF-style Color object, used for extended sheet
* formattings and conditional formattings
*/
ExtendedColor createExtendedColor();
ClientAnchor createClientAnchor();
}

View File

@ -39,6 +39,10 @@ public class XSSFCreationHelper implements CreationHelper {
return workbook.createDataFormat();
}
public XSSFColor createExtendedColor() {
return new XSSFColor();
}
/**
* Create a new XSSFHyperlink.
*

View File

@ -1138,7 +1138,50 @@ public abstract class BaseTestConditionalFormatting extends TestCase {
}
public void testCreateDataBarFormatting() {
// TODO Implement then test
Workbook workbook = _testDataProvider.createWorkbook();
Sheet sheet = workbook.createSheet();
String colorHex = "FFFFEB84";
ExtendedColor color = workbook.getCreationHelper().createExtendedColor();
color.setARGBHex(colorHex);
SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
ConditionalFormattingRule rule1 =
sheetCF.createConditionalFormattingRule(color);
DataBarFormatting dbFmt = rule1.getDataBarFormatting();
assertEquals(false, dbFmt.isIconOnly());
assertEquals(true, dbFmt.isLeftToRight());
assertEquals(0, dbFmt.getWidthMin());
assertEquals(100, dbFmt.getWidthMax());
assertColour(colorHex, dbFmt.getColor());
dbFmt.getMinThreshold().setRangeType(RangeType.MIN);
dbFmt.getMaxThreshold().setRangeType(RangeType.MAX);
CellRangeAddress [] regions = { CellRangeAddress.valueOf("A1:A5") };
sheetCF.addConditionalFormatting(regions, rule1);
// Save, re-load and re-check
workbook = _testDataProvider.writeOutAndReadBack(workbook);
sheetCF = sheet.getSheetConditionalFormatting();
assertEquals(1, sheetCF.getNumConditionalFormattings());
ConditionalFormatting cf = sheetCF.getConditionalFormattingAt(0);
assertEquals(1, cf.getNumberOfRules());
rule1 = cf.getRule(0);
dbFmt = rule1.getDataBarFormatting();
assertEquals(ConditionType.DATA_BAR, rule1.getConditionTypeType());
assertEquals(false, dbFmt.isIconOnly());
assertEquals(true, dbFmt.isLeftToRight());
assertEquals(0, dbFmt.getWidthMin());
assertEquals(100, dbFmt.getWidthMax());
assertColour(colorHex, dbFmt.getColor());
assertEquals(RangeType.MIN, dbFmt.getMinThreshold().getRangeType());
assertEquals(RangeType.MAX, dbFmt.getMaxThreshold().getRangeType());
assertEquals(null, dbFmt.getMinThreshold().getValue());
assertEquals(null, dbFmt.getMaxThreshold().getValue());
}
public void testBug55380() {