Search in sources :

Example 1 with DataFormatRecord

use of org.apache.poi.hssf.record.chart.DataFormatRecord in project poi by apache.

the class HSSFChart method removeSeries.

public boolean removeSeries(HSSFSeries remSeries) {
    int deep = 0;
    int chartDeep = -1;
    int lastSeriesDeep = -1;
    int seriesIdx = -1;
    boolean removeSeries = false;
    boolean chartEntered = false;
    boolean result = false;
    final List<RecordBase> records = sheet.getSheet().getRecords();
    /* store first series as template and find last series index */
    Iterator<RecordBase> iter = records.iterator();
    while (iter.hasNext()) {
        RecordBase record = iter.next();
        if (record instanceof BeginRecord) {
            deep++;
        } else if (record instanceof EndRecord) {
            deep--;
            if (lastSeriesDeep == deep) {
                lastSeriesDeep = -1;
                if (removeSeries) {
                    removeSeries = false;
                    result = true;
                    iter.remove();
                }
            }
            if (chartDeep == deep) {
                break;
            }
        }
        if (record instanceof ChartRecord) {
            if (record == chartRecord) {
                chartDeep = deep;
                chartEntered = true;
            }
        } else if (record instanceof SeriesRecord) {
            if (chartEntered) {
                if (remSeries.series == record) {
                    lastSeriesDeep = deep;
                    removeSeries = true;
                } else {
                    seriesIdx++;
                }
            }
        } else if (record instanceof DataFormatRecord) {
            if (chartEntered && !removeSeries) {
                DataFormatRecord dataFormatRecord = (DataFormatRecord) record;
                dataFormatRecord.setSeriesIndex((short) seriesIdx);
                dataFormatRecord.setSeriesNumber((short) seriesIdx);
            }
        }
        if (removeSeries) {
            iter.remove();
        }
    }
    return result;
}
Also used : EndRecord(org.apache.poi.hssf.record.chart.EndRecord) RecordBase(org.apache.poi.hssf.record.RecordBase) BeginRecord(org.apache.poi.hssf.record.chart.BeginRecord) DataFormatRecord(org.apache.poi.hssf.record.chart.DataFormatRecord) ChartRecord(org.apache.poi.hssf.record.chart.ChartRecord) SeriesRecord(org.apache.poi.hssf.record.chart.SeriesRecord)

Example 2 with DataFormatRecord

use of org.apache.poi.hssf.record.chart.DataFormatRecord in project poi by apache.

the class HSSFChart method createSeries.

public HSSFSeries createSeries() throws Exception {
    ArrayList<RecordBase> seriesTemplate = new ArrayList<RecordBase>();
    boolean seriesTemplateFilled = false;
    int idx = 0;
    int deep = 0;
    int chartRecordIdx = -1;
    int chartDeep = -1;
    int lastSeriesDeep = -1;
    int endSeriesRecordIdx = -1;
    int seriesIdx = 0;
    final List<RecordBase> records = sheet.getSheet().getRecords();
    /* store first series as template and find last series index */
    for (final RecordBase record : records) {
        idx++;
        if (record instanceof BeginRecord) {
            deep++;
        } else if (record instanceof EndRecord) {
            deep--;
            if (lastSeriesDeep == deep) {
                lastSeriesDeep = -1;
                endSeriesRecordIdx = idx;
                if (!seriesTemplateFilled) {
                    seriesTemplate.add(record);
                    seriesTemplateFilled = true;
                }
            }
            if (chartDeep == deep) {
                break;
            }
        }
        if (record instanceof ChartRecord) {
            if (record == chartRecord) {
                chartRecordIdx = idx;
                chartDeep = deep;
            }
        } else if (record instanceof SeriesRecord) {
            if (chartRecordIdx != -1) {
                seriesIdx++;
                lastSeriesDeep = deep;
            }
        }
        if (lastSeriesDeep != -1 && !seriesTemplateFilled) {
            seriesTemplate.add(record);
        }
    }
    /* check if a series was found */
    if (endSeriesRecordIdx == -1) {
        return null;
    }
    /* next index in the records list where the new series can be inserted */
    idx = endSeriesRecordIdx + 1;
    HSSFSeries newSeries = null;
    /* duplicate record of the template series */
    ArrayList<RecordBase> clonedRecords = new ArrayList<RecordBase>();
    for (final RecordBase record : seriesTemplate) {
        Record newRecord = null;
        if (record instanceof BeginRecord) {
            newRecord = new BeginRecord();
        } else if (record instanceof EndRecord) {
            newRecord = new EndRecord();
        } else if (record instanceof SeriesRecord) {
            SeriesRecord seriesRecord = (SeriesRecord) ((SeriesRecord) record).clone();
            newSeries = new HSSFSeries(seriesRecord);
            newRecord = seriesRecord;
        } else if (record instanceof LinkedDataRecord) {
            LinkedDataRecord linkedDataRecord = ((LinkedDataRecord) record).clone();
            if (newSeries != null) {
                newSeries.insertData(linkedDataRecord);
            }
            newRecord = linkedDataRecord;
        } else if (record instanceof DataFormatRecord) {
            DataFormatRecord dataFormatRecord = ((DataFormatRecord) record).clone();
            dataFormatRecord.setSeriesIndex((short) seriesIdx);
            dataFormatRecord.setSeriesNumber((short) seriesIdx);
            newRecord = dataFormatRecord;
        } else if (record instanceof SeriesTextRecord) {
            SeriesTextRecord seriesTextRecord = (SeriesTextRecord) ((SeriesTextRecord) record).clone();
            if (newSeries != null) {
                newSeries.setSeriesTitleText(seriesTextRecord);
            }
            newRecord = seriesTextRecord;
        } else if (record instanceof Record) {
            newRecord = (Record) ((Record) record).clone();
        }
        if (newRecord != null) {
            clonedRecords.add(newRecord);
        }
    }
    /* check if a user model series object was created */
    if (newSeries == null) {
        return null;
    }
    /* transfer series to record list */
    for (final RecordBase record : clonedRecords) {
        records.add(idx++, record);
    }
    return newSeries;
}
Also used : EndRecord(org.apache.poi.hssf.record.chart.EndRecord) SeriesTextRecord(org.apache.poi.hssf.record.chart.SeriesTextRecord) ArrayList(java.util.ArrayList) ChartRecord(org.apache.poi.hssf.record.chart.ChartRecord) SeriesRecord(org.apache.poi.hssf.record.chart.SeriesRecord) RecordBase(org.apache.poi.hssf.record.RecordBase) BeginRecord(org.apache.poi.hssf.record.chart.BeginRecord) DataFormatRecord(org.apache.poi.hssf.record.chart.DataFormatRecord) UnknownRecord(org.apache.poi.hssf.record.UnknownRecord) VCenterRecord(org.apache.poi.hssf.record.VCenterRecord) AxisUsedRecord(org.apache.poi.hssf.record.chart.AxisUsedRecord) LinkedDataRecord(org.apache.poi.hssf.record.chart.LinkedDataRecord) FooterRecord(org.apache.poi.hssf.record.FooterRecord) SCLRecord(org.apache.poi.hssf.record.SCLRecord) ChartRecord(org.apache.poi.hssf.record.chart.ChartRecord) ValueRangeRecord(org.apache.poi.hssf.record.chart.ValueRangeRecord) LegendRecord(org.apache.poi.hssf.record.chart.LegendRecord) SeriesTextRecord(org.apache.poi.hssf.record.chart.SeriesTextRecord) AxisParentRecord(org.apache.poi.hssf.record.chart.AxisParentRecord) DefaultDataLabelTextPropertiesRecord(org.apache.poi.hssf.record.chart.DefaultDataLabelTextPropertiesRecord) PrintSetupRecord(org.apache.poi.hssf.record.PrintSetupRecord) Record(org.apache.poi.hssf.record.Record) SeriesToChartGroupRecord(org.apache.poi.hssf.record.chart.SeriesToChartGroupRecord) EOFRecord(org.apache.poi.hssf.record.EOFRecord) AreaFormatRecord(org.apache.poi.hssf.record.chart.AreaFormatRecord) BeginRecord(org.apache.poi.hssf.record.chart.BeginRecord) HCenterRecord(org.apache.poi.hssf.record.HCenterRecord) EndRecord(org.apache.poi.hssf.record.chart.EndRecord) PlotGrowthRecord(org.apache.poi.hssf.record.chart.PlotGrowthRecord) UnitsRecord(org.apache.poi.hssf.record.chart.UnitsRecord) HeaderRecord(org.apache.poi.hssf.record.HeaderRecord) SeriesRecord(org.apache.poi.hssf.record.chart.SeriesRecord) BarRecord(org.apache.poi.hssf.record.chart.BarRecord) ChartFormatRecord(org.apache.poi.hssf.record.chart.ChartFormatRecord) AxisLineFormatRecord(org.apache.poi.hssf.record.chart.AxisLineFormatRecord) CategorySeriesAxisRecord(org.apache.poi.hssf.record.chart.CategorySeriesAxisRecord) ChartTitleFormatRecord(org.apache.poi.hssf.record.chart.ChartTitleFormatRecord) FrameRecord(org.apache.poi.hssf.record.chart.FrameRecord) PlotAreaRecord(org.apache.poi.hssf.record.chart.PlotAreaRecord) BOFRecord(org.apache.poi.hssf.record.BOFRecord) DataFormatRecord(org.apache.poi.hssf.record.chart.DataFormatRecord) ProtectRecord(org.apache.poi.hssf.record.ProtectRecord) TextRecord(org.apache.poi.hssf.record.chart.TextRecord) FontBasisRecord(org.apache.poi.hssf.record.chart.FontBasisRecord) AxisOptionsRecord(org.apache.poi.hssf.record.chart.AxisOptionsRecord) SeriesIndexRecord(org.apache.poi.hssf.record.chart.SeriesIndexRecord) SheetPropertiesRecord(org.apache.poi.hssf.record.chart.SheetPropertiesRecord) TickRecord(org.apache.poi.hssf.record.chart.TickRecord) AxisRecord(org.apache.poi.hssf.record.chart.AxisRecord) DimensionsRecord(org.apache.poi.hssf.record.DimensionsRecord) FontIndexRecord(org.apache.poi.hssf.record.chart.FontIndexRecord) LineFormatRecord(org.apache.poi.hssf.record.chart.LineFormatRecord) LinkedDataRecord(org.apache.poi.hssf.record.chart.LinkedDataRecord)

Example 3 with DataFormatRecord

use of org.apache.poi.hssf.record.chart.DataFormatRecord in project poi by apache.

the class HSSFChart method createDataFormatRecord.

private DataFormatRecord createDataFormatRecord() {
    DataFormatRecord r = new DataFormatRecord();
    r.setPointNumber((short) -1);
    r.setSeriesIndex((short) 0);
    r.setSeriesNumber((short) 0);
    r.setUseExcel4Colors(false);
    return r;
}
Also used : DataFormatRecord(org.apache.poi.hssf.record.chart.DataFormatRecord)

Aggregations

DataFormatRecord (org.apache.poi.hssf.record.chart.DataFormatRecord)3 RecordBase (org.apache.poi.hssf.record.RecordBase)2 BeginRecord (org.apache.poi.hssf.record.chart.BeginRecord)2 ChartRecord (org.apache.poi.hssf.record.chart.ChartRecord)2 EndRecord (org.apache.poi.hssf.record.chart.EndRecord)2 SeriesRecord (org.apache.poi.hssf.record.chart.SeriesRecord)2 ArrayList (java.util.ArrayList)1 BOFRecord (org.apache.poi.hssf.record.BOFRecord)1 DimensionsRecord (org.apache.poi.hssf.record.DimensionsRecord)1 EOFRecord (org.apache.poi.hssf.record.EOFRecord)1 FooterRecord (org.apache.poi.hssf.record.FooterRecord)1 HCenterRecord (org.apache.poi.hssf.record.HCenterRecord)1 HeaderRecord (org.apache.poi.hssf.record.HeaderRecord)1 PrintSetupRecord (org.apache.poi.hssf.record.PrintSetupRecord)1 ProtectRecord (org.apache.poi.hssf.record.ProtectRecord)1 Record (org.apache.poi.hssf.record.Record)1 SCLRecord (org.apache.poi.hssf.record.SCLRecord)1 UnknownRecord (org.apache.poi.hssf.record.UnknownRecord)1 VCenterRecord (org.apache.poi.hssf.record.VCenterRecord)1 AreaFormatRecord (org.apache.poi.hssf.record.chart.AreaFormatRecord)1