vtkChartXY.h 13.4 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:    vtkChartXY.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 vtkChartXY - Factory class for drawing XY charts
//
// .SECTION Description
// This class implements an XY chart.

21 22 23
// .SECTION See Also
// vtkBarChartActor

24 25
#ifndef vtkChartXY_h
#define vtkChartXY_h
26

27
#include "vtkChartsCoreModule.h" // For export macro
28
#include "vtkChart.h"
29
#include "vtkSmartPointer.h" // For SP ivars
30
#include "vtkVector.h" // For vtkVector2f in struct
31
#include "vtkContextPolygon.h" // For vtkContextPolygon
32 33 34 35

class vtkPlot;
class vtkAxis;
class vtkPlotGrid;
36
class vtkChartLegend;
37
class vtkTooltipItem;
38 39
class vtkChartXYPrivate; // Private class to keep my STL vector in...

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

  // Description:
  // Creates a 2D Chart object.
  static vtkChartXY *New();

50 51 52 53 54 55
  // Description:
  // Perform any updates to the item that may be necessary before rendering.
  // The scene should take care of calling this on all items before their
  // Paint function is invoked.
  virtual void Update();

56 57 58 59
  // Description:
  // Paint event for the chart, called whenever the chart needs to be drawn
  virtual bool Paint(vtkContext2D *painter);

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

64 65 66 67
  // Description:
  // Adds a plot to the chart
  virtual vtkIdType AddPlot(vtkPlot* plot);

68 69 70 71 72 73 74 75 76 77 78 79 80
  // Description:
  // Remove the plot at the specified index, returns true if successful,
  // false if the index was invalid.
  virtual bool RemovePlot(vtkIdType index);

  // 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);

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  // Description:
  // Get the index of the specified plot, returns -1 if the plot does not
  // belong to the chart.
  virtual vtkIdType GetPlotIndex(vtkPlot*);

  // Description:
  // Raises the \a plot to the top of the plot's stack.
  // \return The new index of the plot
  // \sa StackPlotAbove(), LowerPlot(), StackPlotUnder()
  vtkIdType RaisePlot(vtkPlot* plot);

  // Description:
  // Raises the \a plot above the \a under plot. If \a under is null,
  // the plot is raised to the top of the plot's stack.
  // \return The new index of the plot
  // \sa RaisePlot(), LowerPlot(), StackPlotUnder()
  virtual vtkIdType StackPlotAbove(vtkPlot* plot, vtkPlot* under);

  // Description:
  // Lowers the \a plot to the bottom of the plot's stack.
  // \return The new index of the plot
  // \sa StackPlotUnder(), RaisePlot(), StackPlotAbove()
  vtkIdType LowerPlot(vtkPlot* plot);

  // Description:
  // Lowers the \a plot under the \a above plot. If \a above is null,
  // the plot is lowered to the bottom of the plot's stack
  // \return The new index of the plot
  // \sa StackPlotUnder(), RaisePlot(), StackPlotAbove()
  virtual vtkIdType StackPlotUnder(vtkPlot* plot, vtkPlot* above);

112 113
  // Description:
  // Get the number of plots the chart contains.
114
  virtual vtkIdType GetNumberOfPlots();
115

116 117 118 119 120 121 122 123
  // Description:
  // Figure out which quadrant the plot is in.
  int GetPlotCorner(vtkPlot *plot);

  // Description:
  // Figure out which quadrant the plot is in.
  void SetPlotCorner(vtkPlot *plot, int corner);

124
  // Description:
125 126 127
  // Get the axis specified by axisIndex. This is specified with the vtkAxis
  // position enum, valid values are vtkAxis::LEFT, vtkAxis::BOTTOM,
  // vtkAxis::RIGHT and vtkAxis::TOP.
128 129
  virtual vtkAxis* GetAxis(int axisIndex);

130 131 132 133
  // Description:
  // Set whether the chart should draw a legend.
  virtual void SetShowLegend(bool visible);

134 135
  // Description:
  // Get the vtkChartLegend object that will be displayed by the chart.
136
  virtual vtkChartLegend* GetLegend();
137

138 139 140 141
  // Description:
  // Set the vtkTooltipItem object that will be displayed by the chart.
  virtual void SetTooltip(vtkTooltipItem *tooltip);

142 143 144 145
  // Description:
  // Get the vtkTooltipItem object that will be displayed by the chart.
  virtual vtkTooltipItem* GetTooltip();

146 147 148 149
  // Description:
  // Get the number of axes in the current chart.
  virtual vtkIdType GetNumberOfAxes();

150 151 152 153 154
  // 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();

155 156 157 158 159 160 161 162
  // 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
  // column pair.
  virtual void SetSelectionMethod(int method);

163
  // Description:
164
  // If true then the axes will be drawn at the origin (scientific style).
165 166
  vtkSetMacro(DrawAxesAtOrigin, bool);
  vtkGetMacro(DrawAxesAtOrigin, bool);
167
  vtkBooleanMacro(DrawAxesAtOrigin, bool);
168

169 170 171 172 173
  // Description:
  // If true then the axes will be turned on and off depending upon whether
  // any plots are in that corner. Defaults to true.
  vtkSetMacro(AutoAxes, bool);
  vtkGetMacro(AutoAxes, bool);
174
  vtkBooleanMacro(AutoAxes, bool);
175

176 177 178 179 180
  // Description:
  // Border size of the axes that are hidden (vtkAxis::GetVisible())
  vtkSetMacro(HiddenAxisBorder, int);
  vtkGetMacro(HiddenAxisBorder, int);

181 182 183 184 185 186 187 188
  // Description
  // Force the axes to have their Minimum and Maximum properties inside the
  // plot boundaries. It constrains pan and zoom interaction.
  // False by default.
  vtkSetMacro(ForceAxesToBounds, bool);
  vtkGetMacro(ForceAxesToBounds, bool);
  vtkBooleanMacro(ForceAxesToBounds, bool);

189 190 191 192 193 194 195 196 197
  // Description:
  // Set the width fraction for any bar charts drawn in this chart. It is
  // assumed that all bar plots will use the same array for the X axis, and that
  // this array is regularly spaced. The delta between the first two x values is
  // used to calculated the width of the bars, and subdivided between each bar.
  // The default value is 0.8, 1.0 would lead to bars that touch.
  vtkSetMacro(BarWidthFraction, float);
  vtkGetMacro(BarWidthFraction, float);

198 199 200 201 202 203 204 205 206
  // Description:
  // Set the behavior of the mouse wheel.  If true, the mouse wheel zooms in/out
  // on the chart.  Otherwise, unless MouseWheelEvent is overridden by a subclass
  // the mouse wheel does nothing.
  // The default value is true.
  vtkSetMacro(ZoomWithMouseWheel, bool);
  vtkGetMacro(ZoomWithMouseWheel, bool);
  vtkBooleanMacro(ZoomWithMouseWheel, bool);

207 208 209 210 211 212 213 214
  // Description:
  // Adjust the minimum of a logarithmic axis to be greater than 0, regardless
  // of the minimum data value.
  // False by default.
  vtkSetMacro(AdjustLowerBoundForLogPlot, bool);
  vtkGetMacro(AdjustLowerBoundForLogPlot, bool);
  vtkBooleanMacro(AdjustLowerBoundForLogPlot, bool);

215
  // Description:
216
  // Set the information passed to the tooltip.
217
  virtual void SetTooltipInfo(const vtkContextMouseEvent &,
218
                              const vtkVector2d &,
219
                              vtkIdType, vtkPlot*,
220
                              vtkIdType segmentIndex = -1);
221

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
  // Description:
  // Return true if the supplied x, y coordinate is inside the item.
  virtual bool Hit(const vtkContextMouseEvent &mouse);

  // Description:
  // Mouse enter event.
  virtual bool MouseEnterEvent(const vtkContextMouseEvent &mouse);

  // Description:
  // Mouse move event.
  virtual bool MouseMoveEvent(const vtkContextMouseEvent &mouse);

  // Description:
  // Mouse leave event.
  virtual bool MouseLeaveEvent(const vtkContextMouseEvent &mouse);

  // Description:
  // Mouse button down event
  virtual bool MouseButtonPressEvent(const vtkContextMouseEvent &mouse);

  // Description:
  // Mouse button release event.
  virtual bool MouseButtonReleaseEvent(const vtkContextMouseEvent &mouse);

  // Description:
  // Mouse wheel event, positive delta indicates forward movement of the wheel.
  virtual bool MouseWheelEvent(const vtkContextMouseEvent &mouse, int delta);
249 250 251 252

  // Description:
  // Key press event.
  virtual bool KeyPressEvent(const vtkContextKeyEvent &key);
253

254 255 256 257
protected:
  vtkChartXY();
  ~vtkChartXY();

258
  // Description:
259 260 261
  // Recalculate the necessary transforms.
  void RecalculatePlotTransforms();

262 263 264 265 266
  // Description:
  // Calculate the optimal zoom level such that all of the points to be plotted
  // will fit into the plot area.
  void RecalculatePlotBounds();

267 268 269 270
  // Description:
  // Update the layout of the chart, this may require the vtkContext2D in order
  // to get font metrics etc. Initially this was added to resize the charts
  // according in response to the size of the axes.
271
  virtual bool UpdateLayout(vtkContext2D* painter);
272

273 274 275 276 277 278 279 280 281 282 283
  // Description:
  // Layout for the legend if it is visible. This is run after the axes layout
  // and will adjust the borders to account for the legend position.
  // \return The required space in the specified border.
  virtual int GetLegendBorder(vtkContext2D* painter, int axisPosition);

  // Description:
  // Called after the edges of the chart are decided, set the position of the
  // legend, depends upon its alignment.
  virtual void SetLegendPosition(const vtkRectf& rect);

284 285
  // Description:
  // The legend for the chart.
286
  vtkSmartPointer<vtkChartLegend> Legend;
287

288 289
  // Description:
  // The tooltip item for the chart - can be used to display extra information.
290
  vtkSmartPointer<vtkTooltipItem> Tooltip;
291

292 293 294 295
  // Description:
  // Does the plot area transform need to be recalculated?
  bool PlotTransformValid;

296
  // Description:
297 298
  // The box created as the mouse is dragged around the screen.
  vtkRectf MouseBox;
299 300

  // Description:
301
  // Should the box be drawn (could be selection, zoom etc).
302 303
  bool DrawBox;

304 305 306 307 308 309 310 311 312
  // Description:
  // The polygon created as the mouse is dragged around the screen when in
  // polygonal selection mode.
  vtkContextPolygon SelectionPolygon;

  // Description:
  // Should the selection polygon be drawn.
  bool DrawSelectionPolygon;

313 314 315 316
  // Description:
  // Should we draw the location of the nearest point on the plot?
  bool DrawNearestPoint;

317 318 319 320 321 322
  // Description:
  // Keep the axes drawn at the origin? This will attempt to keep the axes drawn
  // at the origin, i.e. 0.0, 0.0 for the chart. This is often the preferred
  // way of drawing scientific/mathematical charts.
  bool DrawAxesAtOrigin;

323 324 325 326
  // Description:
  // Should axes be turned on and off automatically - defaults to on.
  bool AutoAxes;

327 328 329 330
  // Description:
  // Size of the border when an axis is hidden
  int HiddenAxisBorder;

331 332 333 334 335
  // Description:
  // The fraction of the interval taken up along the x axis by any bars that are
  // drawn on the chart.
  float BarWidthFraction;

336 337 338 339 340
  // Description:
  // Indicate if the layout has changed in some way that would require layout
  // code to be called.
  bool LayoutChanged;

341 342 343 344 345 346
  // Description:
  // Property to force the axes to have their Minimum and Maximum properties
  // inside the plot boundaries. It constrains pan and zoom interaction.
  // False by default.
  bool ForceAxesToBounds;

347
  // Description:
348 349 350 351
  // Property to enable zooming the chart with the mouse wheel.
  // True by default.
  bool ZoomWithMouseWheel;

352 353 354 355 356
  // Description:
  // Property to adjust the minimum of a logarithmic axis to be greater than 0,
  // regardless of the minimum data value.
  bool AdjustLowerBoundForLogPlot;

357
private:
358
  vtkChartXY(const vtkChartXY &) VTK_DELETE_FUNCTION;
359
  void operator=(const vtkChartXY &) VTK_DELETE_FUNCTION;
360 361 362

  vtkChartXYPrivate *ChartPrivate; // Private class where I hide my STL containers

363 364 365 366
  // Description:
  // Figure out the spacing between the bar chart plots, and their offsets.
  void CalculateBarPlots();

367
  // Description:
368 369 370 371 372
  // Try to locate a point within the plots to display in a tooltip.
  // If invokeEvent is greater than 0, then an event will be invoked if a point
  // is at that mouse position.
  bool LocatePointInPlots(const vtkContextMouseEvent &mouse,
                          int invokeEvent = -1);
373

374 375 376 377
  int LocatePointInPlot(const vtkVector2f &position,
                        const vtkVector2f &tolerance, vtkVector2f &plotPos,
                        vtkPlot *plot, vtkIdType &segmentIndex);

378 379
  // Description:
  // Remove the plot from the plot corners list.
380
  bool RemovePlotFromCorners(vtkPlot *plot);
381 382 383

  void ZoomInAxes(vtkAxis *x, vtkAxis *y, float *orign, float *max);

384 385 386 387 388 389 390
  // Description:
  // Transform the selection box or polygon.
  void TransformBoxOrPolygon(bool polygonMode, vtkTransform2D *transform,
                             const vtkVector2f &mousePosition,
                             vtkVector2f &min, vtkVector2f &max,
                             vtkContextPolygon &polygon);

391 392
};

393 394 395 396 397 398 399 400
// Description:
// Small struct used by InvokeEvent to send some information about the point
// that was clicked on. This is an experimental part of the API, subject to
// change.
struct vtkChartPlotData
{
  vtkStdString SeriesName;
  vtkVector2f Position;
401
  vtkVector2i ScreenPosition;
402
  int Index;
403 404
};

405
#endif //vtkChartXY_h