00001
00002
00003
00004
00005
00006
00007 package org.swtchart.internal.compress;
00008
00009 import java.util.ArrayList;
00010
00014 public abstract class Compress implements ICompress {
00015
00017 protected int previousXGridIndex;
00018
00020 protected int previousYGridIndex;
00021
00023 protected CompressConfig config;
00024
00026 protected CompressConfig prevConfig;
00027
00029 protected boolean compressed;
00030
00032 protected double[] xSeries = null;
00033
00035 protected double[] ySeries = null;
00036
00038 protected transient double[] compressedXSeries = null;
00039
00041 protected transient double[] compressedYSeries = null;
00042
00044 protected transient int[] compressedIndexes = null;
00045
00047 protected double xLower;
00048
00050 protected double xUpper;
00051
00053 protected double yLower;
00054
00056 protected double yUpper;
00057
00059 private boolean isXLogScale;
00060
00062 private boolean isYLogScale;
00063
00065 private long widthInPixel;
00066
00068 private long heightInPixel;
00069
00070
00071
00072
00073 public void setXSeries(double[] xSeries) {
00074 if (xSeries == null) {
00075 return;
00076 }
00077
00078 double[] copiedSeries = new double[xSeries.length];
00079 System.arraycopy(xSeries, 0, copiedSeries, 0, xSeries.length);
00080
00081 this.xSeries = copiedSeries;
00082 compressedXSeries = copiedSeries;
00083 compressedIndexes = new int[xSeries.length];
00084 for (int i = 0; i < xSeries.length; i++) {
00085 compressedIndexes[i] = i;
00086 }
00087
00088 compressed = false;
00089 }
00090
00091
00092
00093
00094 public void setYSeries(double[] ySeries) {
00095 if (ySeries == null) {
00096 return;
00097 }
00098
00099 double[] copiedSeries = new double[ySeries.length];
00100 System.arraycopy(ySeries, 0, copiedSeries, 0, ySeries.length);
00101
00102 this.ySeries = copiedSeries;
00103 compressedYSeries = copiedSeries;
00104
00105 compressed = false;
00106 }
00107
00108
00109
00110
00111 public double[] getCompressedXSeries() {
00112 double[] copiedSeries = new double[compressedXSeries.length];
00113 System.arraycopy(compressedXSeries, 0, copiedSeries, 0,
00114 compressedXSeries.length);
00115
00116 return copiedSeries;
00117 }
00118
00119
00120
00121
00122 public double[] getCompressedYSeries() {
00123 double[] copiedSeries = new double[compressedYSeries.length];
00124 System.arraycopy(compressedYSeries, 0, copiedSeries, 0,
00125 compressedYSeries.length);
00126
00127 return copiedSeries;
00128 }
00129
00130
00131
00132
00133 public int[] getCompressedIndexes() {
00134 int[] copiedSeries = new int[compressedIndexes.length];
00135 System.arraycopy(compressedIndexes, 0, copiedSeries, 0,
00136 compressedIndexes.length);
00137
00138 return copiedSeries;
00139 }
00140
00141
00142
00143
00144 final public boolean compress(CompressConfig compressConfig) {
00145
00146 if ((compressConfig.equals(prevConfig) && compressed)
00147 || xSeries == null || ySeries == null) {
00148 return false;
00149 }
00150
00151
00152 prevConfig = new CompressConfig(compressConfig);
00153
00154 this.config = compressConfig;
00155
00156
00157 xLower = config.getXLowerValue();
00158 xUpper = config.getXUpperValue();
00159 yLower = config.getYLowerValue();
00160 yUpper = config.getYUpperValue();
00161 isXLogScale = config.isXLogScale();
00162 isYLogScale = config.isYLogScale();
00163 widthInPixel = config.getWidthInPixel();
00164 heightInPixel = config.getHeightInPixel();
00165
00166 previousXGridIndex = -1;
00167 previousYGridIndex = -1;
00168
00169 ArrayList<Double> xList = new ArrayList<Double>();
00170 ArrayList<Double> yList = new ArrayList<Double>();
00171 ArrayList<Integer> indexList = new ArrayList<Integer>();
00172
00173
00174 addNecessaryPlots(xList, yList, indexList);
00175
00176 compressedXSeries = new double[xList.size()];
00177 compressedYSeries = new double[yList.size()];
00178 compressedIndexes = new int[indexList.size()];
00179 for (int i = 0; i < xList.size(); i++) {
00180 compressedXSeries[i] = xList.get(i);
00181 compressedYSeries[i] = yList.get(i);
00182 compressedIndexes[i] = indexList.get(i);
00183 }
00184
00185 compressed = true;
00186
00187 return true;
00188 }
00189
00200 abstract protected void addNecessaryPlots(ArrayList<Double> xList,
00201 ArrayList<Double> yList, ArrayList<Integer> indexList);
00202
00219 protected void addToList(ArrayList<Double> xList, ArrayList<Double> yList,
00220 ArrayList<Integer> indexList, double x, double y, int index) {
00221 xList.add(x);
00222 yList.add(y);
00223 indexList.add(index);
00224 }
00225
00235 protected boolean isInSameGridAsPrevious(double x, double y) {
00236 int xGridIndex;
00237 int yGridIndex;
00238
00239
00240 if (isXLogScale) {
00241 double lower = Math.log10(xLower);
00242 double upper = Math.log10(xUpper);
00243 xGridIndex = (int) ((Math.log10(x) - lower) / (upper - lower) * widthInPixel);
00244 } else {
00245 xGridIndex = (int) ((x - xLower) / (xUpper - xLower) * widthInPixel);
00246 }
00247
00248
00249 if (isYLogScale) {
00250 double lower = Math.log10(yLower);
00251 double upper = Math.log10(yUpper);
00252 yGridIndex = (int) ((Math.log10(y) - lower) / (upper - lower) * heightInPixel);
00253 } else {
00254 yGridIndex = (int) ((y - yLower) / (yUpper - yLower) * heightInPixel);
00255 }
00256
00257
00258 boolean isInSameGridAsPrevious = (xGridIndex == previousXGridIndex && yGridIndex == previousYGridIndex);
00259
00260
00261 previousXGridIndex = xGridIndex;
00262 previousYGridIndex = yGridIndex;
00263
00264 return isInSameGridAsPrevious;
00265 }
00266 }