casa
$Rev:20696$
|
00001 //# Plot.h: Plot objects to be drawn on a canvas. 00002 //# Copyright (C) 2008 00003 //# Associated Universities, Inc. Washington DC, USA. 00004 //# 00005 //# This library is free software; you can redistribute it and/or modify it 00006 //# under the terms of the GNU Library General Public License as published by 00007 //# the Free Software Foundation; either version 2 of the License, or (at your 00008 //# option) any later version. 00009 //# 00010 //# This library is distributed in the hope that it will be useful, but WITHOUT 00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00012 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00013 //# License for more details. 00014 //# 00015 //# You should have received a copy of the GNU Library General Public License 00016 //# along with this library; if not, write to the Free Software Foundation, 00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. 00018 //# 00019 //# Correspondence concerning AIPS++ should be addressed as follows: 00020 //# Internet email: aips2-request@nrao.edu. 00021 //# Postal address: AIPS++ Project Office 00022 //# National Radio Astronomy Observatory 00023 //# 520 Edgemont Road 00024 //# Charlottesville, VA 22903-2475 USA 00025 //# 00026 //# $Id: $ 00027 #ifndef PLOT_H_ 00028 #define PLOT_H_ 00029 00030 #include <graphics/GenericPlotter/PlotData.h> 00031 #include <graphics/GenericPlotter/PlotItem.h> 00032 00033 #include <casa/BasicSL/String.h> 00034 00035 #include <casa/namespace.h> 00036 00037 namespace casa { 00038 00039 // A Plot basically consists of data and customization information. The data 00040 // is held by the base class whereas any customization should be handled in 00041 // subclasses. 00042 class Plot : public virtual PlotItem { 00043 public: 00044 Plot() { } 00045 00046 virtual ~Plot() { } 00047 00048 00049 // ABSTRACT METHODS // 00050 00051 // Returns the data associated with this plot. 00052 virtual PlotDataPtr data() const = 0; 00053 00054 // Should be called whenever the underlying PlotData has changed, to let 00055 // the plot and its attached canvas know that the plot needs to be redrawn. 00056 virtual void dataChanged() = 0; 00057 00058 // It's likely that any plot subclass (scatter, bar, function, etc.) will 00059 // have lines of some sort, so it's safe to have in the base class. 00060 00061 // Returns true if this plot has lines shown, false otherwise. 00062 virtual bool linesShown() const = 0; 00063 00064 // Sets whether or not lines are shown. If linesShown is true, the 00065 // implementation can decide whether to choose the line style shown, or 00066 // just use the last set PlotLine. 00067 virtual void setLinesShown(bool linesShown = true) = 0; 00068 00069 // Returns a copy of the line used for this plot. Note: if lines are not 00070 // shown, then this behavior is undefined. The last shown line can be 00071 // returned, or a line with style NOLINE, or a null pointer, or.... 00072 virtual PlotLinePtr line() const = 0; 00073 00074 // Sets the plot lines to the given line. Implies setLinesShown(true) 00075 // unless the given line's style is set to NOLINE. 00076 virtual void setLine(const PlotLine& line) = 0; 00077 00078 00079 // IMPLEMENTED METHODS // 00080 00081 // Convenience methods for setting line. 00082 // <group> 00083 virtual void setLine(const PlotLinePtr line) { 00084 if(!line.null()) setLine(*line); 00085 else setLinesShown(false); 00086 } 00087 virtual void setLine(const String& color, 00088 PlotLine::Style style = PlotLine::SOLID, 00089 double width = 1.0) { 00090 PlotLinePtr l = line(); 00091 l->setColor(color); 00092 l->setStyle(style); 00093 l->setWidth(width); 00094 setLine(*l); 00095 } 00096 virtual void setLineColor(const String& color) { 00097 PlotLinePtr l = line(); 00098 l->setColor(color); 00099 setLine(*l); 00100 } 00101 virtual void setLineStyle(PlotLine::Style style) { 00102 PlotLinePtr l = line(); 00103 l->setStyle(style); 00104 setLine(*l); 00105 } 00106 virtual void setLineWidth(double width) { 00107 PlotLinePtr l = line(); 00108 l->setWidth(width); 00109 setLine(*l); 00110 } 00111 // </group> 00112 }; 00113 00114 00115 // Scatter plot abstract class. In addition to the abstract methods in Plot, a 00116 // scatter plot subclass should also have symbol customization for the points. 00117 // A scatter plot is expected to consist of PlotPointData. 00118 class ScatterPlot : public virtual Plot { 00119 public: 00120 ScatterPlot() { } 00121 00122 virtual ~ScatterPlot() { } 00123 00124 00125 // Overrides Plot::data(). 00126 virtual PlotDataPtr data() const { return pointData(); } 00127 00128 // Returns the data value at the given index. Just a thin layer over the 00129 // PlotPointData functionality. 00130 virtual ppoint_t pointAt(unsigned int i) const { 00131 PlotPointDataPtr data = pointData(); 00132 if(!data.null()) return ppoint_t(data->xAt(i), data->yAt(i)); 00133 else return ppoint_t(0, 0); 00134 } 00135 00136 // Implements PlotItem::drawCount(). Provides default implementation that 00137 // returns the number of plotted points. 00138 virtual unsigned int drawCount() const { return pointData()->size(); } 00139 00140 00141 // ABSTRACT METHODS // 00142 00143 // Returns the point data used for this plot. 00144 virtual PlotPointDataPtr pointData() const = 0; 00145 00146 // Returns true if symbols are shown, false otherwise. 00147 virtual bool symbolsShown() const = 0; 00148 00149 // Sets whether symbols are shown or not. If symbolsShown is true, the 00150 // implementation can decide whether to choose the symbol shown, or just 00151 // use the last set PlotSymbol. 00152 virtual void setSymbolsShown(bool symbolsShown = true) = 0; 00153 00154 // Returns a copy of the symbol for this plot. Note: if symbols are not 00155 // shown, then this behavior is undefined. The last shown symbol can be 00156 // returned, or a symbol with style NOSYMBOL, or a null pointer, or.... 00157 virtual PlotSymbolPtr symbol() const = 0; 00158 00159 // Sets the plot symbols to the given symbol. Implies setSymbolsShown(true) 00160 // unless the symbol's style is set to NOSYMBOL. 00161 virtual void setSymbol(const PlotSymbol& symbol) = 0; 00162 00163 00164 // IMPLEMENTED METHODS // 00165 00166 // Convenience methods for setting symbol. 00167 // <group> 00168 virtual void setSymbol(const PlotSymbolPtr symbol) { 00169 if(!symbol.null()) setSymbol(*symbol); 00170 else setSymbolsShown(false); 00171 } 00172 virtual void setSymbol(PlotSymbol::Symbol s) { 00173 PlotSymbolPtr sym = symbol(); 00174 sym->setSymbol(s); 00175 setSymbol(*sym); 00176 } 00177 virtual void setSymbol(char s) { 00178 PlotSymbolPtr sym = symbol(); 00179 sym->setSymbol(s); 00180 setSymbol(*sym); 00181 } 00182 virtual void setSymbolSize(double width, double height) { 00183 PlotSymbolPtr sym = symbol(); 00184 sym->setSize(width, height); 00185 setSymbol(*sym); 00186 } 00187 virtual void setSymbolLine(const PlotLine& line) { 00188 PlotSymbolPtr sym = symbol(); 00189 sym->setLine(line); 00190 setSymbol(*sym); 00191 } 00192 virtual void setSymbolLine(const PlotLinePtr line) { 00193 if(!line.null()) setSymbolLine(*line); } 00194 virtual void setSymbolLine(const String& color, 00195 PlotLine::Style style = PlotLine::SOLID, double width = 1.0) { 00196 PlotSymbolPtr sym = symbol(); 00197 sym->setLine(color, style, width); 00198 setSymbol(*sym); 00199 } 00200 virtual void setSymbolAreaFill(const PlotAreaFill& fill) { 00201 PlotSymbolPtr sym = symbol(); 00202 sym->setAreaFill(fill); 00203 setSymbol(*sym); 00204 } 00205 virtual void setSymbolAreaFill(const PlotAreaFillPtr fill) { 00206 if(!fill.null()) setSymbolAreaFill(*fill); } 00207 virtual void setSymbolAreaFill(const String& color, 00208 PlotAreaFill::Pattern pattern = PlotAreaFill::FILL) { 00209 PlotSymbolPtr sym = symbol(); 00210 sym->setAreaFill(color, pattern); 00211 setSymbol(*sym); 00212 } 00213 // </group> 00214 }; 00215 00216 00217 // Subclass of ScatterPlot that adds masking functionality. The ScatterPlot 00218 // customization methods (lines, symbols, etc.) are expected to apply only to 00219 // unmasked data, while additional methods are provided in MaskedScatterPlot 00220 // to customize the masked data (which is not shown by default). 00221 class MaskedScatterPlot : public virtual ScatterPlot { 00222 public: 00223 MaskedScatterPlot() { } 00224 00225 virtual ~MaskedScatterPlot() { } 00226 00227 00228 // Overrides ScatterPlot::pointData(). 00229 virtual PlotPointDataPtr pointData() const { return maskedData(); } 00230 00231 // Returns whether the data at the given index is masked or not. Just a 00232 // thin layer over the PlotMaskedPointData functionality. 00233 virtual bool maskedAt(unsigned int index) const { 00234 PlotMaskedPointDataPtr data = maskedData(); 00235 return !data.null() && data->maskedAt(index); 00236 } 00237 00238 00239 // ABSTRACT METHODS // 00240 00241 // Returns the masked data used for this plot. 00242 virtual PlotMaskedPointDataPtr maskedData() const = 0; 00243 00244 // Returns true if this plot has lines shown for masked points, false 00245 // otherwise. 00246 virtual bool maskedLinesShown() const = 0; 00247 00248 // Sets whether or not lines are shown for masked points. If linesShown is 00249 // true, the implementation can decide whether to choose the line style 00250 // shown, or just use the last set PlotLine. 00251 virtual void setMaskedLinesShown(bool linesShown = true) = 0; 00252 00253 // Returns a copy of the line used for masked points. Note: if lines are 00254 // not shown, then this behavior is undefined. The last shown line can be 00255 // returned, or a line with style NOLINE, or a null pointer, or.... 00256 virtual PlotLinePtr maskedLine() const = 0; 00257 00258 // Sets the lines for masked points to the given. Implies 00259 // setMaskedLinesShown(true) unless the given line's style is set to 00260 // NOLINE. 00261 virtual void setMaskedLine(const PlotLine& line) = 0; 00262 00263 // Convenience methods for setting line for masked points. 00264 // <group> 00265 virtual void setMaskedLine(const PlotLinePtr line) { 00266 if(!line.null()) setMaskedLine(*line); 00267 else setMaskedLinesShown(false); 00268 } 00269 virtual void setMaskedLine(const String& color, 00270 PlotLine::Style style = PlotLine::SOLID, 00271 double width = 1.0) { 00272 PlotLinePtr l = maskedLine(); 00273 l->setColor(color); 00274 l->setStyle(style); 00275 l->setWidth(width); 00276 setMaskedLine(*l); 00277 } 00278 virtual void setMaskedLineColor(const String& color) { 00279 PlotLinePtr l = maskedLine(); 00280 l->setColor(color); 00281 setMaskedLine(*l); 00282 } 00283 virtual void setMaskedLineStyle(PlotLine::Style style) { 00284 PlotLinePtr l = maskedLine(); 00285 l->setStyle(style); 00286 setMaskedLine(*l); 00287 } 00288 virtual void setMaskedLineWidth(double width) { 00289 PlotLinePtr l = maskedLine(); 00290 l->setWidth(width); 00291 setMaskedLine(*l); 00292 } 00293 // </group> 00294 00295 // Returns true if symbols are shown for masked points, false otherwise. 00296 virtual bool maskedSymbolsShown() const = 0; 00297 00298 // Sets whether symbols are shown or not for masked points. If 00299 // symbolsShown is true, the implementation can decide whether to choose 00300 // the symbol shown, or just use the last set PlotSymbol. 00301 virtual void setMaskedSymbolsShown(bool symbolsShown = true) = 0; 00302 00303 // Returns a copy of the symbol for masked points. Note: if symbols are 00305 // be returned, or a symbol with style NOSYMBOL, or a null pointer, or.... 00306 virtual PlotSymbolPtr maskedSymbol() const = 0; 00307 00308 // Sets the symbols for masked points to the given. Implies 00309 // setMaskedSymbolsShown(true) unless the symbol's style is set to 00310 // NOSYMBOL. 00311 virtual void setMaskedSymbol(const PlotSymbol& symbol) = 0; 00312 00313 // Convenience methods for setting symbol for masked points. 00314 // <group> 00315 virtual void setMaskedSymbol(const PlotSymbolPtr symbol) { 00316 if(!symbol.null()) setMaskedSymbol(*symbol); 00317 else setMaskedSymbolsShown(false); 00318 } 00319 virtual void setMaskedSymbol(PlotSymbol::Symbol s) { 00320 PlotSymbolPtr sym = maskedSymbol(); 00321 sym->setSymbol(s); 00322 setMaskedSymbol(*sym); 00323 } 00324 virtual void setMaskedSymbol(char s) { 00325 PlotSymbolPtr sym = maskedSymbol(); 00326 sym->setSymbol(s); 00327 setMaskedSymbol(*sym); 00328 } 00329 virtual void setMaskedSymbolSize(double width, double height) { 00330 PlotSymbolPtr sym = maskedSymbol(); 00331 sym->setSize(width, height); 00332 setMaskedSymbol(*sym); 00333 } 00334 virtual void setMaskedSymbolLine(const PlotLine& line) { 00335 PlotSymbolPtr sym = maskedSymbol(); 00336 sym->setLine(line); 00337 setMaskedSymbol(*sym); 00338 } 00339 virtual void setMaskedSymbolLine(const PlotLinePtr line) { 00340 if(!line.null()) setMaskedSymbolLine(*line); } 00341 virtual void setMaskedSymbolLine(const String& color, 00342 PlotLine::Style style = PlotLine::SOLID, double width = 1.0) { 00343 PlotSymbolPtr sym = maskedSymbol(); 00344 sym->setLine(color, style, width); 00345 setMaskedSymbol(*sym); 00346 } 00347 virtual void setMaskedSymbolAreaFill(const PlotAreaFill& fill) { 00348 PlotSymbolPtr sym = maskedSymbol(); 00349 sym->setAreaFill(fill); 00350 setMaskedSymbol(*sym); 00351 } 00352 virtual void setMaskedSymbolAreaFill(const PlotAreaFillPtr fill) { 00353 if(!fill.null()) setMaskedSymbolAreaFill(*fill); } 00354 virtual void setMaskedSymbolAreaFill(const String& color, 00355 PlotAreaFill::Pattern pattern = PlotAreaFill::FILL) { 00356 PlotSymbolPtr sym = maskedSymbol(); 00357 sym->setAreaFill(color, pattern); 00358 setMaskedSymbol(*sym); 00359 } 00360 // </group> 00361 }; 00362 00363 00364 // An error plot is a scatter plot with error bars drawn. It is expected to 00365 // consist of PlotErrorData. 00366 class ErrorPlot : public virtual ScatterPlot { 00367 public: 00368 ErrorPlot() { } 00369 00370 virtual ~ErrorPlot() { } 00371 00372 00373 // Overrides ScatterPlot::pointData(). 00374 virtual PlotPointDataPtr pointData() const { return errorData(); } 00375 00376 00377 // ABSTRACT METHODS // 00378 00379 // Returns the error data used for this plot. 00380 virtual PlotErrorDataPtr errorData() const = 0; 00381 00382 // Returns whether the error bar line is shown or not. 00383 virtual bool errorLineShown() const = 0; 00384 00385 // Sets whether the error bar line is shown. If show is true, the 00386 // implementation can decide whether to choose the line shown, or just 00387 // use the last set PlotLine. 00388 virtual void setErrorLineShown(bool show = true) = 0; 00389 00390 // Returns the line used to draw the error bars. 00391 virtual PlotLinePtr errorLine() const = 0; 00392 00393 // Sets the line used to draw the error bars. 00394 virtual void setErrorLine(const PlotLine& line) = 0; 00395 00396 // Convenience methods for setting error line. 00397 // <group> 00398 virtual void setErrorLine(const PlotLinePtr line) { 00399 if(!line.null()) setErrorLine(*line); 00400 else setErrorLineShown(false); 00401 } 00402 virtual void setErrorLine(const String& color, 00403 PlotLine::Style style = PlotLine::SOLID, 00404 double width = 1.0) { 00405 PlotLinePtr line = errorLine(); 00406 line->setColor(color); 00407 line->setStyle(style); 00408 line->setWidth(width); 00409 setErrorLine(*line); 00410 } 00411 virtual void setErrorLineColor(const String& color) { 00412 PlotLinePtr line = errorLine(); 00413 line->setColor(color); 00414 setErrorLine(*line); 00415 } 00416 virtual void setErrorLineStyle(PlotLine::Style style) { 00417 PlotLinePtr line = errorLine(); 00418 line->setStyle(style); 00419 setErrorLine(*line); 00420 } 00421 virtual void setErrorLineWidth(double width) { 00422 PlotLinePtr line = errorLine(); 00423 line->setWidth(width); 00424 setErrorLine(*line); 00425 } 00426 // </group> 00427 00428 // Returns the "cap" size of the error bar. The cap is the perpendicular 00429 // line at the end of the error bar (that makes the error bar look like an 00430 // I rather than a |). 00431 virtual unsigned int errorCapSize() const = 0; 00432 00433 // Sets the error bar cap size in pixels. 00434 virtual void setErrorCapSize(unsigned int capSize) = 0; 00435 }; 00436 00437 00438 // An color plot is a scatter plot with differentiated colors for points in 00439 // different bins. It is expected to consist of PlotBinnedData. 00440 class ColoredPlot : public virtual ScatterPlot { 00441 public: 00442 // Constructor. 00443 ColoredPlot() { } 00444 00445 // Destructor. 00446 virtual ~ColoredPlot() { } 00447 00448 00449 // Overrides ScatterPlot::pointData(). 00450 virtual PlotPointDataPtr pointData() const { return binnedColorData(); } 00451 00452 00453 // ABSTRACT METHODS // 00454 00455 // Returns the binned data used for this plot. 00456 virtual PlotBinnedDataPtr binnedColorData() const = 0; 00457 00458 // Returns the color to use for the bin at the given index. The color 00459 // applies to the symbol fill color. 00460 virtual PlotColorPtr colorForBin(unsigned int bin) const = 0; 00461 00462 // Sets the color to use for the bin at the given index. The color applies 00463 // to the symbol fill color. 00464 virtual void setColorForBin(unsigned int bin, const PlotColorPtr color) = 0; 00465 }; 00466 00467 00468 // Bar plot abstract class. It is expected to take data in the form of 00469 // PlotPointData. The line methods in Plot are used for the bar outlines. 00470 class BarPlot : public virtual Plot { 00471 public: 00472 BarPlot() { } 00473 00474 virtual ~BarPlot() { } 00475 00476 00477 // Overrides Plot::data(). 00478 PlotDataPtr data() const { return pointData(); } 00479 00480 // Returns the data value at the given index. Just a thin layer over the 00481 // PlotPointData functionality. 00482 virtual ppoint_t pointAt(unsigned int i) const { 00483 PlotPointDataPtr data = pointData(); 00484 if(!data.null()) return ppoint_t(data->xAt(i), data->yAt(i)); 00485 else return ppoint_t(0, 0); 00486 } 00487 00488 // Implements PlotItem::drawCount(). Provides default implementation that 00489 // returns the number of plotted points. 00490 virtual unsigned int drawCount() const { return pointData()->size(); } 00491 00492 00493 // ABSTRACT METHODS // 00494 00495 // Returns the data used for the plot. 00496 virtual PlotPointDataPtr pointData() const = 0; 00497 00498 // Returns whether or not the bars have an area fill or not. 00499 virtual bool areaIsFilled() const = 0; 00500 00501 // Sets whether the bars have an area fill. If fill is true, the 00502 // implementation can decide whether to choose the area fill, or just 00503 // use the last set PlotAreaFill. 00504 virtual void setAreaFilled(bool fill = true) = 0; 00505 00506 // Returns a copy of the area fill used for the bar interiors. 00507 virtual PlotAreaFillPtr areaFill() const = 0; 00508 00509 // Sets the area fill used for the bars to the given. 00510 virtual void setAreaFill(const PlotAreaFill& areaFill) = 0; 00511 00512 00513 // IMPLEMENTED METHODS // 00514 00515 // Convenience methods for setting area fill. 00516 // <group> 00517 virtual void setAreaFill(const PlotAreaFillPtr areaFill) { 00518 if(!areaFill.null()) setAreaFill(*areaFill); 00519 else setAreaFilled(false); 00520 } 00521 virtual void setAreaFill(const String& color, 00522 PlotAreaFill::Pattern pattern = PlotAreaFill::FILL) { 00523 PlotAreaFillPtr fill = areaFill(); 00524 fill->setColor(color); 00525 fill->setPattern(pattern); 00526 setAreaFill(*fill); 00527 } 00528 // </group> 00529 }; 00530 00531 00532 // Plot used to show raster (image-like) data. Expected to use PlotRasterData. 00533 // The line methods in Plot are used for contour lines (if any). A RasterPlot 00534 // can either use the data values directly as colors, or automatically pick 00535 // colors based on a range of values and a colorbar. (See PlotRasterData.) 00536 class RasterPlot : public virtual Plot { 00537 public: 00538 RasterPlot() { } 00539 00540 virtual ~RasterPlot() { } 00541 00542 00543 // Overrides Plot::data(). 00544 PlotDataPtr data() const { return rasterData(); } 00545 00546 // Returns the data at the given point. Just a thin layer over data 00547 // functionality. 00548 virtual double valueAt(double x, double y) const { 00549 PlotRasterDataPtr data = rasterData(); 00550 if(!data.null()) return data->valueAt(x, y); 00551 else return 0; 00552 } 00553 00554 00555 // ABSTRACT METHODS // 00556 00557 // Returns the data used. 00558 virtual PlotRasterDataPtr rasterData() const = 0; 00559 00560 // Sets the x range. 00561 virtual void setXRange(double from, double to) = 0; 00562 00563 // Sets the y range. 00564 virtual void setYRange(double from, double to) = 0; 00565 00566 // Returns the data format. See PlotRasterData. 00567 virtual PlotRasterData::Format dataFormat() const = 0; 00568 00569 // Sets the data format. See PlotRasterData. 00570 virtual void setDataFormat(PlotRasterData::Format f) = 0; 00571 00572 // Returns the data origin. See PlotRasterData. 00573 virtual PlotRasterData::Origin dataOrigin() const = 0; 00574 00575 // Sets the data origin. See PlotRasterData. 00576 virtual void setDataOrigin(PlotRasterData::Origin o) = 0; 00577 00578 // Returns the contour line levels, if any. 00579 virtual vector<double> contourLines() const = 0; 00580 00581 // Sets the contour line levels. 00582 virtual void setContourLines(const vector<double>& lines) = 0; 00583 }; 00584 00585 00586 /* Smart pointer definitions */ 00587 00588 INHERITANCE_POINTER2(Plot, PlotPtr, PlotItem, PlotItemPtr) 00589 INHERITANCE_POINTER(ScatterPlot, ScatterPlotPtr, Plot, PlotPtr, PlotItem, 00590 PlotItemPtr) 00591 INHERITANCE_POINTER(MaskedScatterPlot, MaskedScatterPlotPtr, ScatterPlot, 00592 ScatterPlotPtr, PlotItem, PlotItemPtr) 00593 INHERITANCE_POINTER(ErrorPlot, ErrorPlotPtr, ScatterPlot, ScatterPlotPtr, 00594 PlotItem, PlotItemPtr) 00595 INHERITANCE_POINTER(ColoredPlot, ColoredPlotPtr, ScatterPlot, ScatterPlotPtr, 00596 PlotItem, PlotItemPtr) 00597 INHERITANCE_POINTER(BarPlot, BarPlotPtr, Plot, PlotPtr, PlotItem, PlotItemPtr) 00598 INHERITANCE_POINTER(RasterPlot, RasterPlotPtr, Plot, PlotPtr, PlotItem, 00599 PlotItemPtr) 00600 00601 } 00602 00603 #endif /*PLOT_H_*/