Revision 19516 trunk/extensions/extRasterTools-SE/src/org/gvsig/raster/beans/canvas/layers/GraphicHistogram.java

View differences:

GraphicHistogram.java
29 29
 * @author Nacho Brodin (nachobrodin@gmail.com)
30 30
 */
31 31
public class GraphicHistogram extends DrawableElement {
32
	public static final int TYPE_LINE            = 1;
33
	public static final int TYPE_FILL            = 2;
32 34

  
33
	public static final int TYPE_LINE = 1;
34
	public static final int TYPE_FILL = 2;
35
	private int             border    = 2; 
35
	public static final int VIEW_LINEAL          = 0;
36
	public static final int VIEW_ACUMMULATED     = 1;
37
	public static final int VIEW_LOGARITHMIC     = 2;
38
	public static final int VIEW_ACUMMULATEDLOG  = 3;
39

  
40
	private int             border               = 2;
41

  
42
	// Valores de la funci?n original
43
	private double[]        histogramValues      = null;
44

  
45
	// Valores que se pintan en la gr?fica
46
	private double[]        valuesLineal         = null;
47
	private double[]        valuesAcummulated    = null;
48
	private double[]        valuesLogarithmic    = null;
49
	private double[]        valuesAcummulatedLog = null;
50

  
51
	private int             typeViewed           = 0;
52

  
53
	private int             type                 = TYPE_LINE;
36 54
	
37
	//Valores de la funci?n original
38
	private double[]         hValues = null;
39
	//Valores que se pintan en la gr?fica 
40
	private double[]            valuesY = null;
41
	
42
	private int              type = TYPE_LINE;
43
	private double           min, max;
44
	
45 55
	/**
46 56
	 * Constructor. Asigna el color
47 57
	 * @param c
......
68 78
	 * Dibujado de la l?nea de incremento exponencial sobre el canvas.
69 79
	 */
70 80
	protected void paint(Graphics g) {
71
		if (valuesY == null)
81
		double[] valuesToDraw = recalcHistogram(typeViewed);
82
		
83
		if (valuesToDraw == null)
72 84
			return;
73 85

  
74 86
		g.setColor(color);
......
77 89
		switch (type) {
78 90
			case TYPE_FILL:
79 91
				width = canvas.getCanvasMaxX() - canvas.getCanvasMinX();
80
				for (int i = 0; i < valuesY.length; i++) {
81
					int x1 = (int) Math.round(((double) i - 0.5D) * (width / (valuesY.length - 1.0D)));
82
					int x2 = (int) Math.round(((double) i + 0.5D) * (width / (valuesY.length - 1.0D)));
92
				for (int i = 0; i < valuesToDraw.length; i++) {
93
					int x1 = (int) Math.round(((double) i - 0.5D) * (width / (valuesToDraw.length - 1.0D)));
94
					int x2 = (int) Math.round(((double) i + 0.5D) * (width / (valuesToDraw.length - 1.0D)));
83 95
					x1 += canvas.getCanvasMinX();
84 96
					x2 += canvas.getCanvasMinX();
85
					int y1 = valueToPixelY(valuesY[i]);
97
					int y1 = valueToPixelY(valuesToDraw[i]);
86 98
					if (x1 < (canvas.getCanvasMinX() + border))
87 99
						x1 = canvas.getCanvasMinX() + border;
88 100
					if (x2 > (canvas.getCanvasMaxX() - border))
......
93 105
				break;
94 106
			default:
95 107
				width = canvas.getCanvasMaxX() - canvas.getCanvasMinX();
96
				for (int i = 1; i < valuesY.length; i++) {
97
					int x1 = (int) Math.round(canvas.getCanvasMinX() + ((i - 1.0D) * (width / (valuesY.length - 1.0D))));
98
					int x2 = (int) Math.round(canvas.getCanvasMinX() + (i * (width / (valuesY.length - 1.0D))));
99
					g.drawLine(x1, valueToPixelY(valuesY[i - 1]), x2, valueToPixelY(valuesY[i]));
108
				for (int i = 1; i < valuesToDraw.length; i++) {
109
					int x1 = (int) Math.round(canvas.getCanvasMinX() + ((i - 1.0D) * (width / (valuesToDraw.length - 1.0D))));
110
					int x2 = (int) Math.round(canvas.getCanvasMinX() + (i * (width / (valuesToDraw.length - 1.0D))));
111
					g.drawLine(x1, valueToPixelY(valuesToDraw[i - 1]), x2, valueToPixelY(valuesToDraw[i]));
100 112
				}
101 113
				break;
102 114
		}
......
112 124
	}
113 125
	
114 126
	/**
115
	 * Obtiene el m?nimo valor de la gr?fica
116
	 * @return
127
	 * Asigna el tipo de vista para el histograma a mostrar. No hace un repintado
128
	 * @param type
117 129
	 */
118
	public double getMin() {
119
		return min;
130
	public void setTypeViewed(int type) {
131
		this.typeViewed = type;
120 132
	}
121 133
	
122 134
	/**
123
	 * Obtiene el m?ximo valor de la gr?fica
124
	 * @return
135
	 * Convertimos los valores del histograma a visualizar a una escala de
136
	 * porcentajes que es lo que representa visualmente el componente
137
	 * @param values
125 138
	 */
126
	public double getMax() {
127
		return max;
139
	private void convertToPercent(double[] values) {
140
		double max = Double.NEGATIVE_INFINITY;
141
		for (int i = 0; i < values.length; i++)
142
			if (values[i] > max)
143
				max = values[i];
144
		
145
		for (int i = 0; i < values.length; i++)
146
			values[i] = values[i] / max;
128 147
	}
129 148
	
130 149
	/**
131
	 * Asigna el m?nimo valor para el histograma
132
	 * @param min
150
	 * Calculamos el histograma a dibujar en cuestion. Los tipos son:<p>
151
	 * 	VIEW_LINEAL<br>
152
	 * 	VIEW_ACUMMULATED<br>
153
	 * 	VIEW_LOGARITHMIC
154
	 * 
155
	 * @param type
156
	 * @return
133 157
	 */
134
	public void setMin(double min) {
135
		this.min = min;
158
	private double[] recalcHistogram(int type) {
159
		double min;
160
		switch (type) {
161
			// Calcular la grafica acumulada
162
			case VIEW_ACUMMULATED:
163
				// Si ya estan calculados, no los vuelve a calcular
164
				if (valuesAcummulated != null) return valuesAcummulated;
165

  
166
				valuesAcummulated = new double[histogramValues.length];
167
				if (valuesAcummulated.length >= 1) {
168
					valuesAcummulated[0] = histogramValues[0];
169
					for (int i = 1; i < histogramValues.length; i++)
170
						valuesAcummulated[i] = histogramValues[i] + valuesAcummulated[i - 1];
171
				}
172
				
173
				convertToPercent(valuesAcummulated);
174

  
175
				return valuesAcummulated;
176

  
177
			// Calcular la grafica logaritmica
178
			case VIEW_LOGARITHMIC:
179
				// Si ya estan calculados, no los vuelve a calcular
180
				if (valuesLogarithmic != null) return valuesLogarithmic;
181

  
182
				/** Falta Codigo para la logaritmica **/
183
				
184
				min = Double.MAX_VALUE;
185
				for (int i = 0; i < histogramValues.length; i++)
186
					if (histogramValues[i] < min)
187
						min = histogramValues[i];
188
				
189
				valuesLogarithmic = new double[histogramValues.length];
190
				for (int i = 0; i < histogramValues.length; i++)
191
					valuesLogarithmic[i] = java.lang.Math.log(histogramValues[i] - min + 1.0);
192
				
193
				convertToPercent(valuesLogarithmic);
194
				
195
				return valuesLogarithmic;
196
				
197
				
198
				// Calcular la grafica acumulada
199
			case VIEW_ACUMMULATEDLOG:
200
				// Si ya estan calculados, no los vuelve a calcular
201
				if (valuesAcummulatedLog != null) return valuesAcummulatedLog;
202

  
203
				valuesAcummulatedLog = new double[histogramValues.length];
204
				if (valuesAcummulatedLog.length >= 1) {
205
					valuesAcummulatedLog[0] = histogramValues[0];
206
					for (int i = 1; i < histogramValues.length; i++)
207
						valuesAcummulatedLog[i] = histogramValues[i] + valuesAcummulatedLog[i - 1];
208
				}
209
				
210
				min = Double.MAX_VALUE;
211
				for (int i = 0; i < valuesAcummulatedLog.length; i++)
212
					if (valuesAcummulatedLog[i] < min)
213
						min = valuesAcummulatedLog[i];
214
				
215
				for (int i = 0; i < valuesAcummulatedLog.length; i++)
216
					valuesAcummulatedLog[i] = java.lang.Math.log(valuesAcummulatedLog[i] - min + 1.0);
217
				
218
				convertToPercent(valuesAcummulatedLog);
219

  
220
				return valuesAcummulatedLog;
221

  
222
			// Si no es logaritmica ni acumulada, lo tratamos como si fuera lineal
223
			default:
224
				// Si ya estan calculados, no los vuelve a calcular
225
				if (valuesLineal != null) return valuesLineal;
226

  
227
				valuesLineal = new double[histogramValues.length];
228
				for (int i = 0; i < histogramValues.length; i++)
229
					valuesLineal[i] = histogramValues[i];
230

  
231
				convertToPercent(valuesLineal);
232

  
233
				return valuesLineal;
234
		}
136 235
	}
137 236
	
138 237
	/**
139
	 * Asigna el m?ximo valor para el histograma
140
	 * @param max
141
	 */
142
	public void setMax(double max) {
143
		this.max = max;
144
	}
145

  
146
	/**
147 238
	 * Asigna el histograma a visualizar
148 239
	 * @param histogramDrawed
149 240
	 */
150 241
	public void setHistogramDrawed(double[] histogramDrawed) {
151
		hValues = histogramDrawed;
242
		histogramValues = histogramDrawed;
243

  
244
		valuesLineal = null;
245
		valuesAcummulated = null;
246
		valuesLogarithmic = null;
247
		valuesAcummulatedLog = null;
152 248
		
153
		max = Double.NEGATIVE_INFINITY;
154
		for (int i = 0; i < histogramDrawed.length; i++) {
155
			if (histogramDrawed[i] > max)
156
				max = histogramDrawed[i];
157
		}
158
		
159
		valuesY = new double[histogramDrawed.length];
160
		for (int i = 0; i < histogramDrawed.length; i++)
161
			valuesY[i] = histogramDrawed[i] / max;
162
		
163 249
		if (canvas != null)
164 250
			canvas.repaint();
165 251
	}

Also available in: Unified diff