hssfchart.java
来自「EXCEL read and write」· Java 代码 · 共 966 行 · 第 1/2 页
JAVA
966 行
/* ==================================================================== Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.==================================================================== */package org.apache.poi.hssf.usermodel;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import org.apache.poi.hssf.record.AreaFormatRecord;import org.apache.poi.hssf.record.AxisLineFormatRecord;import org.apache.poi.hssf.record.AxisOptionsRecord;import org.apache.poi.hssf.record.AxisParentRecord;import org.apache.poi.hssf.record.AxisRecord;import org.apache.poi.hssf.record.AxisUsedRecord;import org.apache.poi.hssf.record.BOFRecord;import org.apache.poi.hssf.record.BarRecord;import org.apache.poi.hssf.record.BeginRecord;import org.apache.poi.hssf.record.CategorySeriesAxisRecord;import org.apache.poi.hssf.record.ChartFormatRecord;import org.apache.poi.hssf.record.ChartRecord;import org.apache.poi.hssf.record.ChartTitleFormatRecord;import org.apache.poi.hssf.record.DataFormatRecord;import org.apache.poi.hssf.record.DefaultDataLabelTextPropertiesRecord;import org.apache.poi.hssf.record.DimensionsRecord;import org.apache.poi.hssf.record.EOFRecord;import org.apache.poi.hssf.record.EndRecord;import org.apache.poi.hssf.record.FontBasisRecord;import org.apache.poi.hssf.record.FontIndexRecord;import org.apache.poi.hssf.record.FooterRecord;import org.apache.poi.hssf.record.FrameRecord;import org.apache.poi.hssf.record.HCenterRecord;import org.apache.poi.hssf.record.HeaderRecord;import org.apache.poi.hssf.record.LegendRecord;import org.apache.poi.hssf.record.LineFormatRecord;import org.apache.poi.hssf.record.LinkedDataFormulaField;import org.apache.poi.hssf.record.LinkedDataRecord;import org.apache.poi.hssf.record.PlotAreaRecord;import org.apache.poi.hssf.record.PlotGrowthRecord;import org.apache.poi.hssf.record.PrintSetupRecord;import org.apache.poi.hssf.record.ProtectRecord;import org.apache.poi.hssf.record.RecordBase;import org.apache.poi.hssf.record.SCLRecord;import org.apache.poi.hssf.record.SeriesIndexRecord;import org.apache.poi.hssf.record.SeriesRecord;import org.apache.poi.hssf.record.SeriesTextRecord;import org.apache.poi.hssf.record.SeriesToChartGroupRecord;import org.apache.poi.hssf.record.SheetPropertiesRecord;import org.apache.poi.hssf.record.TextRecord;import org.apache.poi.hssf.record.TickRecord;import org.apache.poi.hssf.record.UnitsRecord;import org.apache.poi.hssf.record.UnknownRecord;import org.apache.poi.hssf.record.VCenterRecord;import org.apache.poi.hssf.record.ValueRangeRecord;import org.apache.poi.hssf.record.formula.Area3DPtg;import org.apache.poi.hssf.record.formula.Ptg;/** * Has methods for construction of a chart object. * * @author Glen Stampoultzis (glens at apache.org) */public final class HSSFChart { private ChartRecord chartRecord; private LegendRecord legendRecord; private ChartTitleFormatRecord chartTitleFormat; private SeriesTextRecord chartTitleText; private List series = new ArrayList(); private HSSFChart(ChartRecord chartRecord) { this.chartRecord = chartRecord; } /** * Creates a bar chart. API needs some work. :) * <p> * NOTE: Does not yet work... checking it in just so others * can take a look. */ public void createBarChart( HSSFWorkbook workbook, HSSFSheet sheet ) { List records = new ArrayList(); records.add( createMSDrawingObjectRecord() ); records.add( createOBJRecord() ); records.add( createBOFRecord() ); records.add( createHeaderRecord() ); records.add( createFooterRecord() ); records.add( createHCenterRecord() ); records.add( createVCenterRecord() ); records.add( createPrintSetupRecord() ); // unknown 33 records.add( createFontBasisRecord1() ); records.add( createFontBasisRecord2() ); records.add( createProtectRecord() ); records.add( createUnitsRecord() ); records.add( createChartRecord( 0, 0, 30434904, 19031616 ) ); records.add( createBeginRecord() ); records.add( createSCLRecord( (short) 1, (short) 1 ) ); records.add( createPlotGrowthRecord( 65536, 65536 ) ); records.add( createFrameRecord1() ); records.add( createBeginRecord() ); records.add( createLineFormatRecord(true) ); records.add( createAreaFormatRecord1() ); records.add( createEndRecord() ); records.add( createSeriesRecord() ); records.add( createBeginRecord() ); records.add( createTitleLinkedDataRecord() ); records.add( createValuesLinkedDataRecord() ); records.add( createCategoriesLinkedDataRecord() ); records.add( createDataFormatRecord() ); // records.add(createBeginRecord()); // unknown // records.add(createEndRecord()); records.add( createSeriesToChartGroupRecord() ); records.add( createEndRecord() ); records.add( createSheetPropsRecord() ); records.add( createDefaultTextRecord( DefaultDataLabelTextPropertiesRecord.CATEGORY_DATA_TYPE_ALL_TEXT_CHARACTERISTIC ) ); records.add( createAllTextRecord() ); records.add( createBeginRecord() ); // unknown records.add( createFontIndexRecord( 5 ) ); records.add( createDirectLinkRecord() ); records.add( createEndRecord() ); records.add( createDefaultTextRecord( (short) 3 ) ); // eek, undocumented text type records.add( createUnknownTextRecord() ); records.add( createBeginRecord() ); records.add( createFontIndexRecord( (short) 6 ) ); records.add( createDirectLinkRecord() ); records.add( createEndRecord() ); records.add( createAxisUsedRecord( (short) 1 ) ); createAxisRecords( records ); records.add( createEndRecord() ); records.add( createDimensionsRecord() ); records.add( createSeriesIndexRecord(2) ); records.add( createSeriesIndexRecord(1) ); records.add( createSeriesIndexRecord(3) ); records.add(EOFRecord.instance); sheet.insertChartRecords( records ); workbook.insertChartRecord(); } /** * Returns all the charts for the given sheet. * * NOTE: You won't be able to do very much with * these charts yet, as this is very limited support */ public static HSSFChart[] getSheetCharts(HSSFSheet sheet) { List charts = new ArrayList(); HSSFChart lastChart = null; // Find records of interest List records = sheet.getSheet().getRecords(); for(Iterator it = records.iterator(); it.hasNext();) { RecordBase r = (RecordBase)it.next(); if(r instanceof ChartRecord) { lastChart = new HSSFChart((ChartRecord)r); charts.add(lastChart); } if(r instanceof LegendRecord) { lastChart.legendRecord = (LegendRecord)r; } if(r instanceof SeriesRecord) { HSSFSeries series = lastChart.new HSSFSeries( (SeriesRecord)r ); lastChart.series.add(series); } if(r instanceof ChartTitleFormatRecord) { lastChart.chartTitleFormat = (ChartTitleFormatRecord)r; } if(r instanceof SeriesTextRecord) { // Applies to a series, unless we've seen // a legend already SeriesTextRecord str = (SeriesTextRecord)r; if(lastChart.legendRecord == null && lastChart.series.size() > 0) { HSSFSeries series = (HSSFSeries) lastChart.series.get(lastChart.series.size()-1); series.seriesTitleText = str; } else { lastChart.chartTitleText = str; } } } return (HSSFChart[]) charts.toArray( new HSSFChart[charts.size()] ); } /** Get the X offset of the chart */ public int getChartX() { return chartRecord.getX(); } /** Get the Y offset of the chart */ public int getChartY() { return chartRecord.getY(); } /** Get the width of the chart. {@link ChartRecord} */ public int getChartWidth() { return chartRecord.getWidth(); } /** Get the height of the chart. {@link ChartRecord} */ public int getChartHeight() { return chartRecord.getHeight(); } /** Sets the X offset of the chart */ public void setChartX(int x) { chartRecord.setX(x); } /** Sets the Y offset of the chart */ public void setChartY(int y) { chartRecord.setY(y); } /** Sets the width of the chart. {@link ChartRecord} */ public void setChartWidth(int width) { chartRecord.setWidth(width); } /** Sets the height of the chart. {@link ChartRecord} */ public void setChartHeight(int height) { chartRecord.setHeight(height); } /** * Returns the series of the chart */ public HSSFSeries[] getSeries() { return (HSSFSeries[]) series.toArray(new HSSFSeries[series.size()]); } /** * Returns the chart's title, if there is one, * or null if not */ public String getChartTitle() { if(chartTitleText != null) { return chartTitleText.getText(); } return null; } /** * Changes the chart's title, but only if there * was one already. * TODO - add in the records if not */ public void setChartTitle(String title) { if(chartTitleText != null) { chartTitleText.setText(title); } else { throw new IllegalStateException("No chart title found to change"); } } private SeriesIndexRecord createSeriesIndexRecord( int index ) { SeriesIndexRecord r = new SeriesIndexRecord(); r.setIndex((short)index); return r; } private DimensionsRecord createDimensionsRecord() { DimensionsRecord r = new DimensionsRecord(); r.setFirstRow(0); r.setLastRow(31); r.setFirstCol((short)0); r.setLastCol((short)1); return r; } private HCenterRecord createHCenterRecord() { HCenterRecord r = new HCenterRecord(); r.setHCenter(false); return r; } private VCenterRecord createVCenterRecord() { VCenterRecord r = new VCenterRecord(); r.setVCenter(false); return r; } private PrintSetupRecord createPrintSetupRecord() { PrintSetupRecord r = new PrintSetupRecord(); r.setPaperSize((short)0); r.setScale((short)18); r.setPageStart((short)1); r.setFitWidth((short)1); r.setFitHeight((short)1); r.setLeftToRight(false); r.setLandscape(false); r.setValidSettings(true); r.setNoColor(false); r.setDraft(false); r.setNotes(false); r.setNoOrientation(false); r.setUsePage(false); r.setHResolution((short)0); r.setVResolution((short)0); r.setHeaderMargin(0.5); r.setFooterMargin(0.5); r.setCopies((short)15); // what the ?? return r; } private FontBasisRecord createFontBasisRecord1() { FontBasisRecord r = new FontBasisRecord(); r.setXBasis((short)9120); r.setYBasis((short)5640); r.setHeightBasis((short)200); r.setScale((short)0); r.setIndexToFontTable((short)5); return r; } private FontBasisRecord createFontBasisRecord2() { FontBasisRecord r = createFontBasisRecord1(); r.setIndexToFontTable((short)6); return r; } private ProtectRecord createProtectRecord() { ProtectRecord r = new ProtectRecord(); r.setProtect(false); return r; } private FooterRecord createFooterRecord() { FooterRecord r = new FooterRecord(); r.setFooter(null); return r; } private HeaderRecord createHeaderRecord() { HeaderRecord r = new HeaderRecord(); r.setHeader(null); return r; } private BOFRecord createBOFRecord() { BOFRecord r = new BOFRecord(); r.setVersion((short)600); r.setType((short)20); r.setBuild((short)0x1CFE); r.setBuildYear((short)1997); r.setHistoryBitMask(0x40C9); r.setRequiredVersion(106); return r; } private UnknownRecord createOBJRecord() { byte[] data = { (byte) 0x15, (byte) 0x00, (byte) 0x12, (byte) 0x00, (byte) 0x05, (byte) 0x00, (byte) 0x02, (byte) 0x00, (byte) 0x11, (byte) 0x60, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xB8, (byte) 0x03, (byte) 0x87, (byte) 0x03, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, }; return new UnknownRecord( (short) 0x005D, data ); } private UnknownRecord createMSDrawingObjectRecord() { // Since we haven't created this object yet we'll just put in the raw // form for the moment. byte[] data = { (byte)0x0F, (byte)0x00, (byte)0x02, (byte)0xF0, (byte)0xC0, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x10, (byte)0x00, (byte)0x08, (byte)0xF0, (byte)0x08, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x02, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x02, (byte)0x04, (byte)0x00, (byte)0x00, (byte)0x0F, (byte)0x00, (byte)0x03, (byte)0xF0, (byte)0xA8, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x0F, (byte)0x00, (byte)0x04, (byte)0xF0, (byte)0x28, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x09, (byte)0xF0, (byte)0x10, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x02, (byte)0x00, (byte)0x0A, (byte)0xF0, (byte)0x08, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x04, (byte)0x00, (byte)0x00, (byte)0x05, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x0F, (byte)0x00, (byte)0x04, (byte)0xF0, (byte)0x70, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x92, (byte)0x0C, (byte)0x0A, (byte)0xF0, (byte)0x08, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x02, (byte)0x04, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x0A, (byte)0x00, (byte)0x00, (byte)0x93, (byte)0x00, (byte)0x0B, (byte)0xF0, (byte)0x36, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x7F, (byte)0x00, (byte)0x04, (byte)0x01, (byte)0x04, (byte)0x01, (byte)0xBF, (byte)0x00, (byte)0x08, (byte)0x00, (byte)0x08, (byte)0x00, (byte)0x81, (byte)0x01, (byte)0x4E, (byte)0x00, (byte)0x00, (byte)0x08, (byte)0x83, (byte)0x01, (byte)0x4D, (byte)0x00, (byte)0x00, (byte)0x08, (byte)0xBF, (byte)0x01, (byte)0x10, (byte)0x00, (byte)0x11, (byte)0x00, (byte)0xC0, (byte)0x01, (byte)0x4D, (byte)0x00, (byte)0x00, (byte)0x08, (byte)0xFF, (byte)0x01, (byte)0x08, (byte)0x00, (byte)0x08, (byte)0x00, (byte)0x3F, (byte)0x02, (byte)0x00, (byte)0x00, (byte)0x02, (byte)0x00, (byte)0xBF, (byte)0x03, (byte)0x00, (byte)0x00, (byte)0x08, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x10, (byte)0xF0, (byte)0x12, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x04, (byte)0x00, (byte)0xC0, (byte)0x02, (byte)0x0A, (byte)0x00, (byte)0xF4, (byte)0x00, (byte)0x0E, (byte)0x00, (byte)0x66, (byte)0x01, (byte)0x20, (byte)0x00, (byte)0xE9, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x11, (byte)0xF0, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 }; return new UnknownRecord((short)0x00EC, data); } private void createAxisRecords( List records ) { records.add( createAxisParentRecord() ); records.add( createBeginRecord() ); records.add( createAxisRecord( AxisRecord.AXIS_TYPE_CATEGORY_OR_X_AXIS ) ); records.add( createBeginRecord() ); records.add( createCategorySeriesAxisRecord() ); records.add( createAxisOptionsRecord() ); records.add( createTickRecord1() ); records.add( createEndRecord() ); records.add( createAxisRecord( AxisRecord.AXIS_TYPE_VALUE_AXIS ) ); records.add( createBeginRecord() ); records.add( createValueRangeRecord() ); records.add( createTickRecord2() ); records.add( createAxisLineFormatRecord( AxisLineFormatRecord.AXIS_TYPE_MAJOR_GRID_LINE ) ); records.add( createLineFormatRecord(false) ); records.add( createEndRecord() ); records.add( createPlotAreaRecord() ); records.add( createFrameRecord2() ); records.add( createBeginRecord() ); records.add( createLineFormatRecord2() ); records.add( createAreaFormatRecord2() ); records.add( createEndRecord() ); records.add( createChartFormatRecord() ); records.add( createBeginRecord() ); records.add( createBarRecord() ); // unknown 1022 records.add( createLegendRecord() ); records.add( createBeginRecord() ); // unknown 104f records.add( createTextRecord() ); records.add( createBeginRecord() ); // unknown 104f records.add( createLinkedDataRecord() ); records.add( createEndRecord() ); records.add( createEndRecord() ); records.add( createEndRecord() ); records.add( createEndRecord() ); } private LinkedDataRecord createLinkedDataRecord() { LinkedDataRecord r = new LinkedDataRecord(); r.setLinkType(LinkedDataRecord.LINK_TYPE_TITLE_OR_TEXT); r.setReferenceType(LinkedDataRecord.REFERENCE_TYPE_DIRECT); r.setCustomNumberFormat(false); r.setIndexNumberFmtRecord((short)0); r.setFormulaOfLink( new LinkedDataFormulaField() ); return r; } private TextRecord createTextRecord() { TextRecord r = new TextRecord(); r.setHorizontalAlignment(TextRecord.HORIZONTAL_ALIGNMENT_CENTER); r.setVerticalAlignment(TextRecord.VERTICAL_ALIGNMENT_CENTER); r.setDisplayMode((short)1); r.setRgbColor(0x00000000); r.setX(-37); r.setY(-60); r.setWidth(0); r.setHeight(0); r.setAutoColor(true); r.setShowKey(false); r.setShowValue(false); r.setVertical(false); r.setAutoGeneratedText(true); r.setGenerated(true); r.setAutoLabelDeleted(false); r.setAutoBackground(true); r.setRotation((short)0); r.setShowCategoryLabelAsPercentage(false); r.setShowValueAsPercentage(false); r.setShowBubbleSizes(false); r.setShowLabel(false); r.setIndexOfColorValue((short)77); r.setDataLabelPlacement((short)0); r.setTextRotation((short)0); return r; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?