vtkChart.h 12.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkChart.h

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/

// .NAME vtkChart - Factory class for drawing 2D charts
//
// .SECTION Description
// This defines the interface for a chart.

21 22
#ifndef vtkChart_h
#define vtkChart_h
23

24
#include "vtkChartsCoreModule.h" // For export macro
25
#include "vtkContextItem.h"
26 27 28
#include "vtkRect.h"         // For vtkRectf
#include "vtkStdString.h"    // For vtkStdString ivars
#include "vtkSmartPointer.h" // For SP ivars
29

30
class vtkTransform2D;
31 32
class vtkContextScene;
class vtkPlot;
33
class vtkAxis;
34
class vtkBrush;
35
class vtkTextProperty;
36
class vtkChartLegend;
37 38 39 40

class vtkInteractorStyle;
class vtkAnnotationLink;

41
class VTKCHARTSCORE_EXPORT vtkChart : public vtkContextItem
42 43
{
public:
44
  vtkTypeMacro(vtkChart, vtkContextItem);
45 46 47 48
  virtual void PrintSelf(ostream &os, vtkIndent indent);

  // Description:
  // Enum of the available chart types
49
  enum {
50 51 52
    LINE,
    POINTS,
    BAR,
53
    STACKED,
54
    BAG,
55 56
    FUNCTIONALBAG,
    AREA};
57 58

  // Description:
59 60 61 62 63 64 65 66 67
  // Enum of valid chart action types.
  //
  // PAN - moves the axis range
  // ZOOM - zooms to a selected rectangle
  // ZOOM_AXIS - zooms the x and y axis range
  // SELECT_RECTANGLE - selects points within a rectangle
  // SELECT_POLYGON - selects points within a polygon
  // SELECT - alias for SELECT_RECTANGLE
  // NOTIFY - Post vtkCommand::InteractionEvent on selection of a point
68
  enum {
69
    PAN = 0,
70
    ZOOM,
71
    ZOOM_AXIS,
72
    SELECT,
73 74
    SELECT_RECTANGLE = SELECT,
    SELECT_POLYGON,
75
    NOTIFY
76
    };
77 78 79 80 81 82

  // Description:
  // Enum of event type that are triggered by the charts
  enum EventIds {
    UpdateRange = 1002
  };
83 84 85 86 87 88 89

  // Description:
  // Paint event for the chart, called whenever the chart needs to be drawn
  virtual bool Paint(vtkContext2D *painter) = 0;

  // Description:
  // Add a plot to the chart, defaults to using the name of the y column
90
  virtual vtkPlot* AddPlot(int type);
91

92 93 94 95
  // Description:
  // Add a plot to the chart. Return the index of the plot, -1 if it failed.
  virtual vtkIdType AddPlot(vtkPlot* plot);

96 97 98 99 100
  // Description:
  // Remove the plot at the specified index, returns true if successful,
  // false if the index was invalid.
  virtual bool RemovePlot(vtkIdType index);

101 102 103 104 105 106
  // Description:
  // Remove the given plot.  Returns true if successful, false if the plot
  // was not contained in this chart.  Note, the base implementation of
  // this method performs a linear search to locate the plot.
  virtual bool RemovePlotInstance(vtkPlot* plot);

107 108 109 110 111 112 113
  // Description:
  // Remove all plots from the chart.
  virtual void ClearPlots();

  // Description:
  // Get the plot at the specified index, returns null if the index is invalid.
  virtual vtkPlot* GetPlot(vtkIdType index);
114 115 116

  // Description:
  // Get the number of plots the chart contains.
117
  virtual vtkIdType GetNumberOfPlots();
118

119 120 121 122 123
  // Description:
  // Get the axis specified by axisIndex. 0 is x, 1 is y. This should probably
  // be improved either using a string or enum to select the axis.
  virtual vtkAxis* GetAxis(int axisIndex);

124 125 126 127
  // Description:
  // Get the number of axes in the current chart.
  virtual vtkIdType GetNumberOfAxes();

128 129 130 131 132
  // Description:
  // Request that the chart recalculates the range of its axes. Especially
  // useful in applications after the parameters of plots have been modified.
  virtual void RecalculateBounds();

133 134 135 136
  // Description:
  // Enumeration of the possible selection methods in a chart. SELECTION_ROWS
  // is the default and simply selects the row in a table in all plots showing
  // that table. SELECTION_PLOTS will make a selection in each plot, and that
137 138
  // selection remains specific to the plot object. SELECTION_COLUMNS selects
  // the plots that use as input the selected columns of a table.
139 140
  enum {
    SELECTION_ROWS,
141 142
    SELECTION_PLOTS,
    SELECTION_COLUMNS
143 144 145 146 147 148 149
  };

  // Description:
  // Set the selection method, which controls how selections are handled by the
  // chart. The default is SELECTION_ROWS which selects all points in all plots
  // in a chart that have values in the rows selected. SELECTION_PLOTS allows
  // for finer-grained selections specific to each plot, and so to each XY
150 151
  // column pair. SELECTION_COLUMNS selects all points of plots that
  // correspond to selected columns.
152 153 154
  virtual void SetSelectionMethod(int method);
  virtual int GetSelectionMethod();

155 156 157 158 159 160 161 162 163
  // Description:
  // Set the vtkAnnotationLink for the chart.
  virtual void SetAnnotationLink(vtkAnnotationLink *link);

  // Description:
  // Get the vtkAnnotationLink for the chart.
  vtkGetObjectMacro(AnnotationLink, vtkAnnotationLink);

  // Description:
164 165 166 167 168 169 170 171 172 173 174 175 176 177
  // Set/get the width and the height of the chart.
  vtkSetVector2Macro(Geometry, int);
  vtkGetVector2Macro(Geometry, int);

  // Description:
  // Set/get the first point in the chart (the bottom left).
  vtkSetVector2Macro(Point1, int);
  vtkGetVector2Macro(Point1, int);

  // Description:
  // Set/get the second point in the chart (the top right).
  vtkSetVector2Macro(Point2, int);
  vtkGetVector2Macro(Point2, int);

178 179
  // Description:
  // Set/get whether the chart should draw a legend.
180 181
  virtual void SetShowLegend(bool visible);
  virtual bool GetShowLegend();
182

183 184 185 186 187
  // Description:
  // Get the legend for the chart, if available. Can return NULL if there is no
  // legend.
  virtual vtkChartLegend * GetLegend();

188 189
  // Description:
  // Get/set the title text of the chart.
190 191
  virtual void SetTitle(const vtkStdString &title);
  virtual vtkStdString GetTitle();
192

193 194 195 196
  // Description:
  // Get the vtkTextProperty that governs how the chart title is displayed.
  vtkGetObjectMacro(TitleProperties, vtkTextProperty);

197 198 199 200 201 202 203 204 205
  // Description:
  // Set/get the borders of the chart (space in pixels around the chart).
  void SetBottomBorder(int border);
  void SetTopBorder(int border);
  void SetLeftBorder(int border);
  void SetRightBorder(int border);

  // Description:
  // Set/get the borders of the chart (space in pixels around the chart).
206
  void SetBorders(int left, int bottom, int right, int top);
207

208 209 210 211 212 213 214 215 216 217
  // Description:
  // Set the size of the chart. The rect argument specifies the bottom corner,
  // width and height of the chart. The borders will be laid out within the
  // specified rectangle.
  void SetSize(const vtkRectf &rect);

  // Description:
  // Get the current size of the chart.
  vtkRectf GetSize();

218 219 220 221 222
  // Description:
  // Enum of the available layout strategies for the charts.
  enum {
    FILL_SCENE,  // Attempt to fill the entire scene.
    FILL_RECT,   // Attempt to supply the supplied vtkRectf in Size.
223
    AXES_TO_RECT // Put the corners of the axes on the vtkRectf in Size.
224 225 226 227 228 229 230 231 232
  };

  // Description:
  // Set/get the layout strategy that should be used by the chart. As we don't
  // support enums this can take any value in the integer range, but the only
  // valid enums are FILL_SCENE, FILL_RECT and AXES_TO_RECT.
  vtkSetMacro(LayoutStrategy, int);
  vtkGetMacro(LayoutStrategy, int);

233 234 235
  // Description:
  // Set/get whether the chart should automatically resize to fill the current
  // render window. Default is true.
236 237 238 239 240 241 242 243 244
  virtual void SetAutoSize(bool isAutoSized)
  {
    this->LayoutStrategy = isAutoSized ? vtkChart::FILL_SCENE :
                                         vtkChart::FILL_RECT;
  }
  virtual bool GetAutoSize()
  {
    return this->LayoutStrategy == vtkChart::FILL_SCENE ? true : false;
  }
245

246 247 248 249 250 251 252 253 254 255
  // Description:
  // Set/get whether the chart should still render its axes and decorations
  // even if the chart has no visible plots. Default is false (do not render
  // an empty plot).
  //
  // Note that if you wish to render axes for an empty plot you should also
  // set AutoSize to false, as that will hide all axes for an empty plot.
  vtkSetMacro(RenderEmpty, bool);
  vtkGetMacro(RenderEmpty, bool);

256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
  // Description:
  // Assign action types to mouse buttons. Available action types are PAN, ZOOM
  // and SELECT in the chart enum, the default assigns the LEFT_BUTTON to
  // PAN, MIDDLE_BUTTON to ZOOM and RIGHT_BUTTON to SELECT. Valid mouse enums
  // are in the vtkContextMouseEvent class.
  //
  // Note that only one mouse button can be assigned to each action, an action
  // will have -1 (invalid button) assigned if it had the same button as the one
  // assigned to a different action.
  virtual void SetActionToButton(int action, int button);

  // Description:
  // Get the mouse button associated with the supplied action. The mouse button
  // enum is from vtkContextMouseEvent, and the action enum is from vtkChart.
  virtual int GetActionToButton(int action);

272 273 274 275 276 277 278 279 280 281 282 283
  // Description:
  // Assign action types to single mouse clicks. Available action types are
  // SELECT and NOTIFY in the chart enum. The default assigns the LEFT_BUTTON
  // to NOTIFY, and the RIGHT_BUTTON to SELECT.
  virtual void SetClickActionToButton(int action, int button);

  // Description:
  // Get the mouse button associated with the supplied click action. The mouse
  // button enum is from vtkContextMouseEvent, and the action enum is from
  // vtkChart.
  virtual int GetClickActionToButton(int action);

284 285 286 287 288
  // Description:
  // Set/Get the brush to use for the background color.
  void SetBackgroundBrush(vtkBrush *brush);
  vtkBrush* GetBackgroundBrush();

289 290 291 292 293 294 295
  // Description:
  // Set/get the Selection Mode that will be used by the chart while doing
  // selection. The only valid enums are vtkContextScene::SELECTION_NONE,
  // SELECTION_DEFAULT, SELECTION_ADDITION, SELECTION_SUBTRACTION, SELECTION_TOGGLE
  virtual void SetSelectionMode(int);
  vtkGetMacro(SelectionMode, int);

296 297 298 299
protected:
  vtkChart();
  ~vtkChart();

300 301 302 303 304 305 306 307
  // Description:
  // Given the x and y vtkAxis, and a transform, calculate the transform that
  // the points in a chart would need to be drawn within the axes. This assumes
  // that the axes have the correct start and end positions, and that they are
  // perpendicular.
  bool CalculatePlotTransform(vtkAxis *x, vtkAxis *y,
                              vtkTransform2D *transform);

308 309 310 311 312
  // Description:
  // Calculate the unshifted, and unscaled plot transform for the x and y axis.
  bool CalculateUnscaledPlotTransform(vtkAxis *x, vtkAxis *y,
                                      vtkTransform2D *transform);

313 314 315 316 317 318
  // Description:
  // Attach axis range listener so we can forward those events at the chart level
  void AttachAxisRangeListener(vtkAxis*);

  void AxisRangeForwarderCallback(vtkObject*,unsigned long, void*);

319 320 321 322
  // Description:
  // Our annotation link, used for sharing selections etc.
  vtkAnnotationLink *AnnotationLink;

323 324 325 326 327 328 329 330 331 332 333
  // Description:
  // The width and the height of the chart.
  int Geometry[2];

  // Description:
  // The position of the lower left corner of the chart.
  int Point1[2];

  // Description:
  // The position of the upper right corner of the chart.
  int Point2[2];
334

335 336 337 338
  // Description:
  // Display the legend?
  bool ShowLegend;

339 340
  // Description:
  // The title of the chart
341
  vtkStdString Title;
342

343 344 345 346
  // Description:
  // The text properties associated with the chart
  vtkTextProperty* TitleProperties;

347
  vtkRectf Size;
348 349
  // The layout strategy to employ when fitting the chart into the space.
  int LayoutStrategy;
350
  bool RenderEmpty;
351

352 353 354 355
  // Description:
  // Brush to use for drawing the background.
  vtkSmartPointer<vtkBrush> BackgroundBrush;

356 357 358
  // The mode when the chart is doing selection.
  int SelectionMode;

359 360 361 362
  // How plot selections are handled, SELECTION_ROWS (default) or
  // SELECTION_PLOTS - based on the plot that created the selection.
  int SelectionMethod;

363 364 365 366 367 368
  // Description:
  // Hold mouse action mappings.
  class MouseActions
    {
  public:
    MouseActions();
369
    enum { MaxAction = 5 };
370 371
    short& Pan() { return Data[0]; }
    short& Zoom() { return Data[1]; }
372 373 374
    short& ZoomAxis() { return Data[2]; }
    short& Select() { return Data[3]; }
    short& SelectPolygon() { return Data[4]; }
375
    short& operator[](int index) { return Data[index]; }
376
    short Data[MaxAction];
377
    };
378 379 380 381 382 383 384 385 386
  class MouseClickActions
    {
  public:
    MouseClickActions();
    short& Notify() { return Data[0]; }
    short& Select() { return Data[1]; }
    short& operator[](int index) { return Data[index]; }
    short Data[2];
    };
387 388

  MouseActions Actions;
389
  MouseClickActions ActionsClick;
390

391
private:
392
  vtkChart(const vtkChart &) VTK_DELETE_FUNCTION;
393
  void operator=(const vtkChart &) VTK_DELETE_FUNCTION;
394 395
};

396
#endif //vtkChart_h