1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkChart.h
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 
16 // .NAME vtkChart - Factory class for drawing 2D charts
17 //
18 // .SECTION Description
19 // This defines the interface for a chart.
20 
21 #ifndef vtkChart_h
22 #define vtkChart_h
23 
24 #include "vtkChartsCoreModule.h" // For export macro
25 #include "vtkContextItem.h"
26 #include "vtkRect.h"         // For vtkRectf
27 #include "vtkStdString.h"    // For vtkStdString ivars
28 #include "vtkSmartPointer.h" // For SP ivars
29 
30 class vtkTransform2D;
31 class vtkContextScene;
32 class vtkPlot;
33 class vtkAxis;
34 class vtkBrush;
35 class vtkTextProperty;
36 class vtkChartLegend;
37 
38 class vtkInteractorStyle;
39 class vtkAnnotationLink;
40 
41 class VTKCHARTSCORE_EXPORT vtkChart : public vtkContextItem
42 {
43 public:
44   vtkTypeMacro(vtkChart, vtkContextItem);
45   virtual void PrintSelf(ostream &os, vtkIndent indent);
46 
47 //BTX
48   // Description:
49   // Enum of the available chart types
50   enum {
51     LINE,
52     POINTS,
53     BAR,
54     STACKED,
55     BAG,
56     FUNCTIONALBAG};
57 
58   // Description:
59   // Enum of valid chart action types.
60   //
61   // PAN - moves the axis range
62   // ZOOM - zooms to a selected rectangle
63   // ZOOM_AXIS - zooms the x and y axis range
64   // SELECT_RECTANGLE - selects points within a rectangle
65   // SELECT_POLYGON - selects points within a polygon
66   // SELECT - alias for SELECT_RECTANGLE
67   // NOTIFY - Post vtkCommand::InteractionEvent on selection of a point
68   enum {
69     PAN = 0,
70     ZOOM,
71     ZOOM_AXIS,
72     SELECT,
73     SELECT_RECTANGLE = SELECT,
74     SELECT_POLYGON,
75     NOTIFY
76     };
77 
78   // Description:
79   // Enum of event type that are triggered by the charts
80   enum EventIds {
81     UpdateRange = 1002
82   };
83 //ETX
84 
85   // Description:
86   // Paint event for the chart, called whenever the chart needs to be drawn
87   virtual bool Paint(vtkContext2D *painter) = 0;
88 
89   // Description:
90   // Add a plot to the chart, defaults to using the name of the y column
91   virtual vtkPlot* AddPlot(int type);
92 
93   // Description:
94   // Add a plot to the chart. Return the index of the plot, -1 if it failed.
95   virtual vtkIdType AddPlot(vtkPlot* plot);
96 
97   // Description:
98   // Remove the plot at the specified index, returns true if successful,
99   // false if the index was invalid.
100   virtual bool RemovePlot(vtkIdType index);
101 
102   // Description:
103   // Remove the given plot.  Returns true if successful, false if the plot
104   // was not contained in this chart.  Note, the base implementation of
105   // this method performs a linear search to locate the plot.
106   virtual bool RemovePlotInstance(vtkPlot* plot);
107 
108   // Description:
109   // Remove all plots from the chart.
110   virtual void ClearPlots();
111 
112   // Description:
113   // Get the plot at the specified index, returns null if the index is invalid.
114   virtual vtkPlot* GetPlot(vtkIdType index);
115 
116   // Description:
117   // Get the number of plots the chart contains.
118   virtual vtkIdType GetNumberOfPlots();
119 
120   // Description:
121   // Get the axis specified by axisIndex. 0 is x, 1 is y. This should probably
122   // be improved either using a string or enum to select the axis.
123   virtual vtkAxis* GetAxis(int axisIndex);
124 
125   // Description:
126   // Get the number of axes in the current chart.
127   virtual vtkIdType GetNumberOfAxes();
128 
129   // Description:
130   // Request that the chart recalculates the range of its axes. Especially
131   // useful in applications after the parameters of plots have been modified.
132   virtual void RecalculateBounds();
133 
134   // Description:
135   // Enumeration of the possible selection methods in a chart. SELECTION_ROWS
136   // is the default and simply selects the row in a table in all plots showing
137   // that table. SELECTION_PLOTS will make a selection in each plot, and that
138   // selection remains specific to the plot object. SELECTION_COLUMNS selects
139   // the plots that use as input the selected columns of a table.
140   enum {
141     SELECTION_ROWS,
142     SELECTION_PLOTS,
143     SELECTION_COLUMNS
144   };
145 
146   // Description:
147   // Set the selection method, which controls how selections are handled by the
148   // chart. The default is SELECTION_ROWS which selects all points in all plots
149   // in a chart that have values in the rows selected. SELECTION_PLOTS allows
150   // for finer-grained selections specific to each plot, and so to each XY
151   // column pair. SELECTION_COLUMNS selects all points of plots that
152   // correspond to selected columns.
153   virtual void SetSelectionMethod(int method);
154   virtual int GetSelectionMethod();
155 
156   // Description:
157   // Set the vtkAnnotationLink for the chart.
158   virtual void SetAnnotationLink(vtkAnnotationLink *link);
159 
160   // Description:
161   // Get the vtkAnnotationLink for the chart.
162   vtkGetObjectMacro(AnnotationLink, vtkAnnotationLink);
163 
164   // Description:
165   // Set/get the width and the height of the chart.
166   vtkSetVector2Macro(Geometry, int);
167   vtkGetVector2Macro(Geometry, int);
168 
169   // Description:
170   // Set/get the first point in the chart (the bottom left).
171   vtkSetVector2Macro(Point1, int);
172   vtkGetVector2Macro(Point1, int);
173 
174   // Description:
175   // Set/get the second point in the chart (the top right).
176   vtkSetVector2Macro(Point2, int);
177   vtkGetVector2Macro(Point2, int);
178 
179   // Description:
180   // Set/get whether the chart should draw a legend.
181   virtual void SetShowLegend(bool visible);
182   virtual bool GetShowLegend();
183 
184   // Description:
185   // Get the legend for the chart, if available. Can return NULL if there is no
186   // legend.
187   virtual vtkChartLegend * GetLegend();
188 
189   // Description:
190   // Get/set the title text of the chart.
191   virtual void SetTitle(const vtkStdString &title);
192   virtual vtkStdString GetTitle();
193 
194   // Description:
195   // Get the vtkTextProperty that governs how the chart title is displayed.
196   vtkGetObjectMacro(TitleProperties, vtkTextProperty);
197 
198   // Description:
199   // Set/get the borders of the chart (space in pixels around the chart).
200   void SetBottomBorder(int border);
201   void SetTopBorder(int border);
202   void SetLeftBorder(int border);
203   void SetRightBorder(int border);
204 
205   // Description:
206   // Set/get the borders of the chart (space in pixels around the chart).
207   void SetBorders(int left, int bottom, int right, int top);
208 
209   // Description:
210   // Set the size of the chart. The rect argument specifies the bottom corner,
211   // width and height of the chart. The borders will be laid out within the
212   // specified rectangle.
213   void SetSize(const vtkRectf &rect);
214 
215   // Description:
216   // Get the current size of the chart.
217   vtkRectf GetSize();
218 
219   // Description:
220   // Enum of the available layout strategies for the charts.
221   enum {
222     FILL_SCENE,  // Attempt to fill the entire scene.
223     FILL_RECT,   // Attempt to supply the supplied vtkRectf in Size.
224     AXES_TO_RECT // Put the corners of the axes on the vtkRectf in Size.
225   };
226 
227   // Description:
228   // Set/get the layout strategy that should be used by the chart. As we don't
229   // support enums this can take any value in the integer range, but the only
230   // valid enums are FILL_SCENE, FILL_RECT and AXES_TO_RECT.
231   vtkSetMacro(LayoutStrategy, int);
232   vtkGetMacro(LayoutStrategy, int);
233 
234   // Description:
235   // Set/get whether the chart should automatically resize to fill the current
236   // render window. Default is true.
SetAutoSize(bool isAutoSized)237   virtual void SetAutoSize(bool isAutoSized)
238   {
239     this->LayoutStrategy = isAutoSized ? vtkChart::FILL_SCENE :
240                                          vtkChart::FILL_RECT;
241   }
GetAutoSize()242   virtual bool GetAutoSize()
243   {
244     return this->LayoutStrategy == vtkChart::FILL_SCENE ? true : false;
245   }
246 
247   // Description:
248   // Set/get whether the chart should still render its axes and decorations
249   // even if the chart has no visible plots. Default is false (do not render
250   // an empty plot).
251   //
252   // Note that if you wish to render axes for an empty plot you should also
253   // set AutoSize to false, as that will hide all axes for an empty plot.
254   vtkSetMacro(RenderEmpty, bool);
255   vtkGetMacro(RenderEmpty, bool);
256 
257   // Description:
258   // Assign action types to mouse buttons. Available action types are PAN, ZOOM
259   // and SELECT in the chart enum, the default assigns the LEFT_BUTTON to
260   // PAN, MIDDLE_BUTTON to ZOOM and RIGHT_BUTTON to SELECT. Valid mouse enums
261   // are in the vtkContextMouseEvent class.
262   //
263   // Note that only one mouse button can be assigned to each action, an action
264   // will have -1 (invalid button) assigned if it had the same button as the one
265   // assigned to a different action.
266   virtual void SetActionToButton(int action, int button);
267 
268   // Description:
269   // Get the mouse button associated with the supplied action. The mouse button
270   // enum is from vtkContextMouseEvent, and the action enum is from vtkChart.
271   virtual int GetActionToButton(int action);
272 
273   // Description:
274   // Assign action types to single mouse clicks. Available action types are
275   // SELECT and NOTIFY in the chart enum. The default assigns the LEFT_BUTTON
276   // to NOTIFY, and the RIGHT_BUTTON to SELECT.
277   virtual void SetClickActionToButton(int action, int button);
278 
279   // Description:
280   // Get the mouse button associated with the supplied click action. The mouse
281   // button enum is from vtkContextMouseEvent, and the action enum is from
282   // vtkChart.
283   virtual int GetClickActionToButton(int action);
284 
285   // Description:
286   // Set/Get the brush to use for the background color.
287   void SetBackgroundBrush(vtkBrush *brush);
288   vtkBrush* GetBackgroundBrush();
289 
290   // Description:
291   // Set/get the Selection Mode that will be used by the chart while doing
292   // selection. The only valid enums are vtkContextScene::SELECTION_NONE,
293   // SELECTION_DEFAULT, SELECTION_ADDITION, SELECTION_SUBTRACTION, SELECTION_TOGGLE
294   virtual void SetSelectionMode(int);
295   vtkGetMacro(SelectionMode, int);
296 
297 protected:
298   vtkChart();
299   ~vtkChart();
300 
301   // Description:
302   // Given the x and y vtkAxis, and a transform, calculate the transform that
303   // the points in a chart would need to be drawn within the axes. This assumes
304   // that the axes have the correct start and end positions, and that they are
305   // perpendicular.
306   bool CalculatePlotTransform(vtkAxis *x, vtkAxis *y,
307                               vtkTransform2D *transform);
308 
309   // Description:
310   // Calculate the unshifted, and unscaled plot transform for the x and y axis.
311   bool CalculateUnscaledPlotTransform(vtkAxis *x, vtkAxis *y,
312                                       vtkTransform2D *transform);
313 
314   // Description:
315   // Attach axis range listener so we can forward those events at the chart level
316   void AttachAxisRangeListener(vtkAxis*);
317 
318   void AxisRangeForwarderCallback(vtkObject*,unsigned long, void*);
319 
320   // Description:
321   // Our annotation link, used for sharing selections etc.
322   vtkAnnotationLink *AnnotationLink;
323 
324   // Description:
325   // The width and the height of the chart.
326   int Geometry[2];
327 
328   // Description:
329   // The position of the lower left corner of the chart.
330   int Point1[2];
331 
332   // Description:
333   // The position of the upper right corner of the chart.
334   int Point2[2];
335 
336   // Description:
337   // Display the legend?
338   bool ShowLegend;
339 
340   // Description:
341   // The title of the chart
342   vtkStdString Title;
343 
344   // Description:
345   // The text properties associated with the chart
346   vtkTextProperty* TitleProperties;
347 
348   vtkRectf Size;
349   // The layout strategy to employ when fitting the chart into the space.
350   int LayoutStrategy;
351   bool RenderEmpty;
352 
353   // Description:
354   // Brush to use for drawing the background.
355   vtkSmartPointer<vtkBrush> BackgroundBrush;
356 
357   // The mode when the chart is doing selection.
358   int SelectionMode;
359 
360   // How plot selections are handled, SELECTION_ROWS (default) or
361   // SELECTION_PLOTS - based on the plot that created the selection.
362   int SelectionMethod;
363 
364   // Description:
365   // Hold mouse action mappings.
366   class MouseActions
367     {
368   public:
369     MouseActions();
370     enum { MaxAction = 5 };
Pan()371     short& Pan() { return Data[0]; }
Zoom()372     short& Zoom() { return Data[1]; }
ZoomAxis()373     short& ZoomAxis() { return Data[2]; }
Select()374     short& Select() { return Data[3]; }
SelectPolygon()375     short& SelectPolygon() { return Data[4]; }
376     short& operator[](int index) { return Data[index]; }
377     short Data[MaxAction];
378     };
379   class MouseClickActions
380     {
381   public:
382     MouseClickActions();
Notify()383     short& Notify() { return Data[0]; }
Select()384     short& Select() { return Data[1]; }
385     short& operator[](int index) { return Data[index]; }
386     short Data[2];
387     };
388 
389   MouseActions Actions;
390   MouseClickActions ActionsClick;
391 
392 private:
393   vtkChart(const vtkChart &); // Not implemented.
394   void operator=(const vtkChart &);   // Not implemented.
395 };
396 
397 #endif //vtkChart_h
398