package jas.hist; import jas.plot.Overlay; import jas.util.xml.HasXMLRepresentation; import java.util.Observable; /** * JASHist2DHistogramData is used for data which can be viewed as a 2D plot * but NOT as a ScatterPlot. The subclass JASHistScatterPlotData is used * for data which can also be displayed as a ScatterPlot. */ class JASHist2DHistogramData extends JASHistData { JASHist2DHistogramData(DataManager dm,Rebinnable2DHistogramData ds) { super(dm); dataSource = ds; initTransientData(); JASHistStyle s = null; if (ds instanceof HasStyle) s = ((HasStyle) ds).getStyle(); if (s == null) s = createStyle(); setStyle(s); String property = System.getProperty("hurry", "false"); hurry = property != null && property.equalsIgnoreCase("true"); } JASHistStyle createStyle() { return new JASHist2DHistogramStyle(); } private void initTransientData() { zLimitsValid = false; isBinned = false; } public void setStyle(JASHistStyle style) { if (!(style instanceof JASHist2DHistogramStyle)) throw new IllegalArgumentException("Style is not subclass of JASHist2DHistogramStyle"); if (this.style != null) this.style.deleteObserver(this); this.style = (JASHist2DHistogramStyle) style; this.style.addObserver(this); } public String getTitle() { return dataSource.getTitle(); } Overlay createOverlay() { return new TwoDOverlay(this); } void writeAsXML(XMLPrintWriter pw, boolean snapshot) { String theXAxisType = pw.convertAxisTypeToString(dataSource.getXAxisType()); String theYAxisType = pw.convertAxisTypeToString(dataSource.getYAxisType()); pw.setAttribute("type","histogram2d"); pw.openTag("data2d"); if (snapshot) { double[][][] result = dataSource.rebin(xBins,xLow,xHigh,yBins,yLow,yHigh,true,hurry, style.getShowOverflow()); if (result == null) result = new double[1][xBins][yBins]; double[][] data = result[0]; pw.setAttribute("title",getTitle()); pw.setAttribute("xSize",data.length); pw.setAttribute("ySize",data[0].length); pw.openTag("bins2d"); double[][] positiveError = new double[0][0]; double[][] negativeError = new double[0][0]; boolean havePlusError = (result.length > 1); boolean haveMinusError = (result.length > 2); if (havePlusError) { positiveError = result[1]; if (haveMinusError) { negativeError = result[2]; } } for (int i=0; i < data.length; i++) { for (int j=0; j < data[i].length; j++) { pw.print(data[i][j]); if (havePlusError && positiveError.length > i && positiveError[i].length > j) { pw.print("," + positiveError[i][j]); if (haveMinusError && negativeError.length > i && negativeError[i].length > j) { pw.println("," + negativeError[i][j]); } else { pw.println(); } } else { pw.println(); } } } pw.closeTag(); //output the x axis attributes pw.printBinnedDataAxisAttributes( "x", "" + getXMin(), "" + getXMax(), "" + dataSource.getXBins(), theXAxisType); //output the y axis attributes pw.printBinnedDataAxisAttributes( "y", "" + getYMin(), "" + getYMax(), "" + dataSource.getYBins(), theYAxisType); if (dataSource instanceof HasStatistics) { Statistics stats = ((HasStatistics) dataSource).getStatistics(); if (stats != null) { pw.openTag("statistics"); String[] names = stats.getStatisticNames(); for (int i=0; i"); String[] labels = getAxisLabels(); for (int i=0; i < labels.length; i++) { jas.hist.JASHistXMLUtils.writeTabs(pw, (indentLevel + 2)); pw.println(""); } jas.hist.JASHistXMLUtils.writeTabs(pw, (indentLevel + 1)); pw.println(""); } */ String histStyleName = JASHist2DHistogramStyle.getHistStyleName(style.getHistStyle()); pw.setAttribute("histStyle",histStyleName); if (histStyleName.equals("STYLE_COLORMAP")) { pw.setAttribute("colorMapScheme", JASHist2DHistogramStyle.getColorMapSchemeName(style.getColorMapScheme())); } pw.setAttribute("shapeColor", jas.util.ColorConverter.colorToString(style.getShapeColor())); pw.setAttribute("overflowBinColor", jas.util.ColorConverter.colorToString(style.getOverflowBinColor())); pw.setAttribute("startDataColor", jas.util.ColorConverter.colorToString(style.getStartDataColor())); pw.setAttribute("endDataColor", jas.util.ColorConverter.colorToString(style.getEndDataColor())); pw.setAttribute("showOverflow",style.getShowOverflow()); pw.setAttribute("showPlot",style.getShowPlot()); if (style.getLogZ()) pw.setAttribute("logZ",true); pw.printTag("style2d"); pw.closeTag(); } boolean isRebinnable() { return dataSource.isRebinnable(); } double getXMin() { double result = dataSource.getXMin(); if (style.getShowOverflow()) { result -= (dataSource.getXMax()-result)/dataSource.getXBins(); } return result; } double getXMax() { double result = dataSource.getXMax(); if (style.getShowOverflow()) { result += (result-dataSource.getXMin())/dataSource.getXBins(); } return result; } double getYMin() { double result = dataSource.getYMin(); if (style.getShowOverflow()) { result -= (dataSource.getYMax()-result)/dataSource.getYBins(); } return result; } double getYMax() { double result = dataSource.getYMax(); if (style.getShowOverflow()) { result += (result-dataSource.getYMin())/dataSource.getYBins(); } return result; } int getXBins() { return dataSource.getXBins(); } int getYBins() { return dataSource.getYBins(); } void setXRange(int xBins,double xLow, double xHigh) { if (isRebinnable()) { if (xBins != this.xBins || xLow != this.xLow || xHigh != this.xHigh) { this.xBins = xBins; isBinned = false; zLimitsValid = false; } } else { this.xBins = dataSource.getXBins(); } this.xLow = xLow; this.xHigh = xHigh; } void setYRange(int yBins,double yLow, double yHigh) { if (isRebinnable()) { if (yBins != this.yBins || yLow != this.yLow || yHigh != this.yHigh) { this.yBins = yBins; isBinned = false; zLimitsValid = false; } } else { this.yBins = dataSource.getYBins(); } this.yLow = yLow; this.yHigh = yHigh; } private void doBin() { // no support for String axes yet isBinned = true; // Set before call to rebin to avoid race condition double xl, xh, yl, yh; if (isRebinnable()) { xl = xLow; xh = xHigh; yl = yLow; yh = yHigh; } else { xl = ((Rebinnable2DHistogramData) dataSource).getXMin(); xh = ((Rebinnable2DHistogramData) dataSource).getXMax(); yl = ((Rebinnable2DHistogramData) dataSource).getYMin(); yh = ((Rebinnable2DHistogramData) dataSource).getYMax(); } double[][][] result = dataSource.rebin(xBins,xl,xh,yBins,yl,yh,true,hurry,style.getShowOverflow()); if (result == null) result = new double[1][xBins][yBins]; // apply normalization if (normalization != null) { double factor = 1./normalization.getNormalizationFactor(); for (int k=0; k