View | Details | Raw Unified | Return to bug 49581
Collapse All | Expand All

(-)src/java/org/apache/poi/hssf/record/chart/ChartEndBlockRecord.java (+15 lines)
Lines 35-40 Link Here
35
	private short iObjectKind;
35
	private short iObjectKind;
36
	private byte[] unused;
36
	private byte[] unused;
37
37
38
	public ChartEndBlockRecord() {
39
	}
40
	
38
	public ChartEndBlockRecord(RecordInputStream in) {
41
	public ChartEndBlockRecord(RecordInputStream in) {
39
		rt = in.readShort();
42
		rt = in.readShort();
40
		grbitFrt = in.readShort();
43
		grbitFrt = in.readShort();
Lines 80-83 Link Here
80
		buffer.append("[/ENDBLOCK]\n");
83
		buffer.append("[/ENDBLOCK]\n");
81
		return buffer.toString();
84
		return buffer.toString();
82
	}
85
	}
86
	
87
	@Override
88
	public ChartEndBlockRecord clone() {
89
		ChartEndBlockRecord record = new ChartEndBlockRecord();
90
		
91
		record.rt = rt ;
92
		record.grbitFrt = grbitFrt ;
93
		record.iObjectKind = iObjectKind ;
94
		record.unused = unused.clone() ;
95
		
96
		return record;
97
	}
83
}
98
}
(-)src/java/org/apache/poi/hssf/record/chart/ChartStartBlockRecord.java (+17 lines)
Lines 37-42 Link Here
37
	private short iObjectInstance1;
37
	private short iObjectInstance1;
38
	private short iObjectInstance2;
38
	private short iObjectInstance2;
39
39
40
	public ChartStartBlockRecord() {
41
	}
42
	
40
	public ChartStartBlockRecord(RecordInputStream in) {
43
	public ChartStartBlockRecord(RecordInputStream in) {
41
		rt = in.readShort();
44
		rt = in.readShort();
42
		grbitFrt = in.readShort();
45
		grbitFrt = in.readShort();
Lines 80-83 Link Here
80
		buffer.append("[/STARTBLOCK]\n");
83
		buffer.append("[/STARTBLOCK]\n");
81
		return buffer.toString();
84
		return buffer.toString();
82
	}
85
	}
86
	
87
	@Override
88
	public ChartStartBlockRecord clone() {
89
		ChartStartBlockRecord record = new ChartStartBlockRecord();
90
		
91
		record.rt = rt;
92
		record.grbitFrt = grbitFrt;
93
		record.iObjectKind = iObjectKind;
94
		record.iObjectContext = iObjectContext;
95
		record.iObjectInstance1 = iObjectInstance1;
96
		record.iObjectInstance2 = iObjectInstance2;
97
		
98
		return record;
99
	}
83
}
100
}
(-)src/java/org/apache/poi/hssf/record/RecordFactory.java (+1 lines)
Lines 221-226 Link Here
221
		WriteAccessRecord.class,
221
		WriteAccessRecord.class,
222
		WriteProtectRecord.class,
222
		WriteProtectRecord.class,
223
		WSBoolRecord.class,
223
		WSBoolRecord.class,
224
		DataFormatRecord.class,
224
225
225
		// chart records
226
		// chart records
226
		BeginRecord.class,
227
		BeginRecord.class,
(-)src/java/org/apache/poi/ss/util/CellRangeAddressBase.java (-2 / +2 lines)
Lines 27-40 Link Here
27
 *
27
 *
28
 * @author Josh Micich
28
 * @author Josh Micich
29
 */
29
 */
30
public abstract class CellRangeAddressBase {
30
public class CellRangeAddressBase {
31
31
32
	private int _firstRow;
32
	private int _firstRow;
33
	private int _firstCol;
33
	private int _firstCol;
34
	private int _lastRow;
34
	private int _lastRow;
35
	private int _lastCol;
35
	private int _lastCol;
36
36
37
	protected CellRangeAddressBase(int firstRow, int lastRow, int firstCol, int lastCol) {
37
	public CellRangeAddressBase(int firstRow, int lastRow, int firstCol, int lastCol) {
38
		_firstRow = firstRow;
38
		_firstRow = firstRow;
39
		_lastRow = lastRow;
39
		_lastRow = lastRow;
40
		_firstCol = firstCol;
40
		_firstCol = firstCol;
(-)src/scratchpad/src/org/apache/poi/hssf/usermodel/HSSFChart.java (-18 / +227 lines)
Lines 35-42 Link Here
35
import org.apache.poi.hssf.record.UnknownRecord;
35
import org.apache.poi.hssf.record.UnknownRecord;
36
import org.apache.poi.hssf.record.VCenterRecord;
36
import org.apache.poi.hssf.record.VCenterRecord;
37
import org.apache.poi.hssf.record.formula.Area3DPtg;
37
import org.apache.poi.hssf.record.formula.Area3DPtg;
38
import org.apache.poi.hssf.record.formula.AreaPtgBase;
38
import org.apache.poi.hssf.record.formula.Ptg;
39
import org.apache.poi.hssf.record.formula.Ptg;
39
import org.apache.poi.hssf.record.chart.LinkedDataRecord;
40
import org.apache.poi.ss.util.CellRangeAddressBase;
40
41
41
/**
42
/**
42
 * Has methods for construction of a chart object.
43
 * Has methods for construction of a chart object.
Lines 44-49 Link Here
44
 * @author Glen Stampoultzis (glens at apache.org)
45
 * @author Glen Stampoultzis (glens at apache.org)
45
 */
46
 */
46
public final class HSSFChart {
47
public final class HSSFChart {
48
	private HSSFSheet sheet;
47
	private ChartRecord chartRecord;
49
	private ChartRecord chartRecord;
48
50
49
	private LegendRecord legendRecord;
51
	private LegendRecord legendRecord;
Lines 53-60 Link Here
53
	
55
	
54
	private List<HSSFSeries> series = new ArrayList<HSSFSeries>();
56
	private List<HSSFSeries> series = new ArrayList<HSSFSeries>();
55
57
56
	private HSSFChart(ChartRecord chartRecord) {
58
	private HSSFChart(HSSFSheet sheet, ChartRecord chartRecord) {
57
		this.chartRecord = chartRecord;
59
		this.chartRecord = chartRecord;
60
		this.sheet = sheet;
58
	}
61
	}
59
62
60
	/**
63
	/**
Lines 146-167 Link Here
146
		for(RecordBase r : records) {
149
		for(RecordBase r : records) {
147
150
148
			if(r instanceof ChartRecord) {
151
			if(r instanceof ChartRecord) {
149
				lastChart = new HSSFChart((ChartRecord)r);
152
				lastSeries = null;
153
				
154
				lastChart = new HSSFChart(sheet,(ChartRecord)r);
150
				charts.add(lastChart);
155
				charts.add(lastChart);
151
			}
156
			} else if(r instanceof LegendRecord) {
152
			if(r instanceof LegendRecord) {
153
				lastChart.legendRecord = (LegendRecord)r;
157
				lastChart.legendRecord = (LegendRecord)r;
154
			}
158
			} else if(r instanceof SeriesRecord) {
155
			if(r instanceof SeriesRecord) {
156
				HSSFSeries series = lastChart.new HSSFSeries( (SeriesRecord)r );
159
				HSSFSeries series = lastChart.new HSSFSeries( (SeriesRecord)r );
157
				lastChart.series.add(series);
160
				lastChart.series.add(series);
158
				lastSeries = series;
161
				lastSeries = series;
159
			}
162
			} else if(r instanceof ChartTitleFormatRecord) {
160
			if(r instanceof ChartTitleFormatRecord) {
161
				lastChart.chartTitleFormat =
163
				lastChart.chartTitleFormat =
162
					(ChartTitleFormatRecord)r;
164
					(ChartTitleFormatRecord)r;
163
			}
165
			} else if(r instanceof SeriesTextRecord) {
164
			if(r instanceof SeriesTextRecord) {
165
				// Applies to a series, unless we've seen
166
				// Applies to a series, unless we've seen
166
				//  a legend already
167
				//  a legend already
167
				SeriesTextRecord str = (SeriesTextRecord)r;
168
				SeriesTextRecord str = (SeriesTextRecord)r;
Lines 173-184 Link Here
173
				} else {
174
				} else {
174
					lastChart.chartTitleText = str;
175
					lastChart.chartTitleText = str;
175
				}
176
				}
176
			}
177
			} else if (r instanceof LinkedDataRecord) {
177
			if(r instanceof LinkedDataRecord) {
178
				LinkedDataRecord linkedDataRecord = (LinkedDataRecord) r;
178
				LinkedDataRecord data = (LinkedDataRecord)r;
179
				if (lastSeries != null) {
179
				lastSeries.insertData( data );
180
					lastSeries.insertData(linkedDataRecord);
180
			}
181
				}
181
			if(r instanceof ValueRangeRecord){
182
			} else if(r instanceof ValueRangeRecord){
182
				lastChart.valueRanges.add((ValueRangeRecord)r);
183
				lastChart.valueRanges.add((ValueRangeRecord)r);
183
			}
184
			}
184
		}
185
		}
Lines 914-920 Link Here
914
			this.series = series;
915
			this.series = series;
915
		}
916
		}
916
917
917
		public void insertData(LinkedDataRecord data){
918
		/* package */ void insertData(LinkedDataRecord data){
918
			switch(dataReaded){
919
			switch(dataReaded){
919
				case 0: dataName = data;
920
				case 0: dataName = data;
920
				break;
921
				break;
Lines 928-933 Link Here
928
			dataReaded++;
929
			dataReaded++;
929
		}
930
		}
930
		
931
		
932
		/* package */ void setSeriesTitleText(SeriesTextRecord seriesTitleText)
933
		{
934
			this.seriesTitleText = seriesTitleText;
935
		}
936
		
931
		public short getNumValues() {
937
		public short getNumValues() {
932
			return series.getNumValues();
938
			return series.getNumValues();
933
		}
939
		}
Lines 996-1000 Link Here
996
		public SeriesRecord getSeries() {
1002
		public SeriesRecord getSeries() {
997
			return series;
1003
			return series;
998
		}
1004
		}
1005
		
1006
		private CellRangeAddressBase getCellRange(LinkedDataRecord linkedDataRecord) {
1007
			if (linkedDataRecord == null)
1008
			{
1009
				return null ;
1010
			}
1011
			
1012
			int firstRow = 0;
1013
			int lastRow = 0;
1014
			int firstCol = 0;
1015
			int lastCol = 0;
1016
			
1017
			for (Ptg ptg : linkedDataRecord.getFormulaOfLink()) {
1018
				if (ptg instanceof AreaPtgBase) {
1019
					AreaPtgBase areaPtg = (AreaPtgBase) ptg;
1020
					
1021
					firstRow = areaPtg.getFirstRow();
1022
					lastRow = areaPtg.getLastRow();
1023
					
1024
					firstCol = areaPtg.getFirstColumn();
1025
					lastCol = areaPtg.getLastColumn();
1026
				}
1027
			}
1028
			
1029
			return new CellRangeAddressBase(firstRow, lastRow, firstCol, lastCol);
1030
		}
1031
		
1032
		public CellRangeAddressBase getValuesCellRange() {
1033
			return getCellRange(dataValues);
1034
		}
1035
	
1036
		public CellRangeAddressBase getCategoryLabelsCellRange() {
1037
			return getCellRange(dataCategoryLabels);
1038
		}
1039
	
1040
		private Integer setVerticalCellRange(LinkedDataRecord linkedDataRecord,
1041
				                             CellRangeAddressBase range) {
1042
			if (linkedDataRecord == null)
1043
			{
1044
				return null;
1045
			}
1046
			
1047
			List<Ptg> ptgList = new ArrayList<Ptg>();
1048
			
1049
			int rowCount = (range.getLastRow() - range.getFirstRow()) + 1;
1050
			int colCount = (range.getLastColumn() - range.getFirstColumn()) + 1;
1051
			
1052
			for (Ptg ptg : linkedDataRecord.getFormulaOfLink()) {
1053
				if (ptg instanceof AreaPtgBase) {
1054
					AreaPtgBase areaPtg = (AreaPtgBase) ptg;
1055
					
1056
					areaPtg.setFirstRow(range.getFirstRow());
1057
					areaPtg.setLastRow(range.getLastRow());
1058
					
1059
					areaPtg.setFirstColumn(range.getFirstColumn());
1060
					areaPtg.setLastColumn(range.getLastColumn());
1061
					ptgList.add(areaPtg);
1062
				}
1063
			}
1064
			
1065
			linkedDataRecord.setFormulaOfLink(ptgList.toArray(new Ptg[ptgList.size()]));
1066
			
1067
			return rowCount * colCount;
1068
		}
1069
		
1070
		public void setValuesCellRange(CellRangeAddressBase range) {
1071
			Integer count = setVerticalCellRange(dataValues, range);
1072
			if (count == null)
1073
			{
1074
				return;
1075
			}
1076
			
1077
			series.setNumValues((short)(int)count);
1078
		}
1079
		
1080
		public void setCategoryLabelsCellRange(CellRangeAddressBase range) {
1081
			Integer count = setVerticalCellRange(dataCategoryLabels, range);
1082
			if (count == null)
1083
			{
1084
				return;
1085
			}
1086
			
1087
			series.setNumCategories((short)(int)count);
1088
		}
999
	}
1089
	}
1090
	
1091
	public HSSFSeries createSeries() throws Exception {
1092
		ArrayList<RecordBase> seriesTemplate = new ArrayList<RecordBase>();
1093
		boolean seriesTemplateFilled = false;
1094
		
1095
		int idx = 0;
1096
		int deep = 0;
1097
		int chartRecordIdx = -1;
1098
		int chartDeep = -1;
1099
		int lastSeriesDeep = -1;
1100
		int endSeriesRecordIdx = -1;
1101
		int seriesIdx = 0;
1102
		List<RecordBase> records = sheet.getSheet().getRecords();
1103
		
1104
		/* store first series as template and find last series index */
1105
		for(RecordBase record : records) {		
1106
			
1107
			if (record instanceof BeginRecord) {
1108
				deep++;
1109
			} else if (record instanceof EndRecord) {
1110
				deep--;
1111
				
1112
				if (lastSeriesDeep == deep) {
1113
					lastSeriesDeep = -1;
1114
					endSeriesRecordIdx = idx;
1115
					if (!seriesTemplateFilled) {
1116
						seriesTemplate.add(record);
1117
						seriesTemplateFilled = true;
1118
					}
1119
				}
1120
				
1121
				if (chartDeep == deep) {
1122
					break;
1123
				}
1124
			}
1125
			
1126
			if (record instanceof ChartRecord) {
1127
				if (record == chartRecord) {
1128
					chartRecordIdx = idx;
1129
					chartDeep = deep;
1130
				}
1131
			} else if (record instanceof SeriesRecord) {
1132
				if (chartRecordIdx != -1) {
1133
					seriesIdx++;
1134
					lastSeriesDeep = deep;
1135
				}
1136
			}
1137
			
1138
			if (lastSeriesDeep != -1 && !seriesTemplateFilled) {
1139
				seriesTemplate.add(record) ;
1140
			}
1141
		}
1142
		
1143
		/* check if a series was found */
1144
		if (endSeriesRecordIdx == -1) {
1145
			return null;
1146
		}
1147
		
1148
		/* next index in the records list where the new series can be inserted */
1149
		idx = endSeriesRecordIdx + 1;
1150
1151
		HSSFSeries series = null;
1152
		
1153
		/* duplicate record of the template series */
1154
		ArrayList<RecordBase> clonedRecords = new ArrayList<RecordBase>();
1155
		for(RecordBase record : seriesTemplate) {		
1156
			
1157
			Record newRecord = null;
1158
			
1159
			if (record instanceof BeginRecord) {
1160
				newRecord = new BeginRecord();
1161
			} else if (record instanceof EndRecord) {
1162
				newRecord = new EndRecord();
1163
			} else if (record instanceof SeriesRecord) {
1164
				SeriesRecord seriesRecord = (SeriesRecord) ((SeriesRecord)record).clone();
1165
				series = new HSSFSeries(seriesRecord);
1166
				newRecord = seriesRecord;
1167
			} else if (record instanceof LinkedDataRecord) {
1168
				LinkedDataRecord linkedDataRecord = (LinkedDataRecord) ((LinkedDataRecord)record).clone();
1169
				if (series != null) {
1170
					series.insertData(linkedDataRecord);
1171
				}
1172
				newRecord = linkedDataRecord;
1173
			} else if (record instanceof DataFormatRecord) {
1174
				DataFormatRecord dataFormatRecord = (DataFormatRecord) ((DataFormatRecord)record).clone();
1175
				
1176
				dataFormatRecord.setSeriesIndex((short)(seriesIdx)) ;
1177
				dataFormatRecord.setSeriesNumber((short)(seriesIdx)) ;
1178
				
1179
				newRecord = dataFormatRecord;
1180
			} else if (record instanceof SeriesTextRecord) {
1181
				SeriesTextRecord seriesTextRecord = (SeriesTextRecord) ((SeriesTextRecord)record).clone();
1182
				if (series != null) {
1183
					series.setSeriesTitleText(seriesTextRecord);
1184
				}
1185
				newRecord = seriesTextRecord;
1186
			} else if (record instanceof Record) {
1187
				newRecord = (Record) ((Record)record).clone();
1188
			}
1189
			
1190
			if (newRecord != null)
1191
			{
1192
				clonedRecords.add(newRecord);
1193
			}
1194
		}
1195
		
1196
		/* check if a user model series object was created */
1197
		if (series == null)
1198
		{
1199
			return null;
1200
		}
1201
		
1202
		/* transfer series to record list */
1203
		for(RecordBase record : clonedRecords) {		
1204
			records.add(idx++, record);
1205
		}
1206
		
1207
		return series;
1208
	}
1000
}
1209
}

Return to bug 49581