vtkPlot.cxx 14.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkPlot.cxx

  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.

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

#include "vtkPlot.h"

18
#include "vtkAxis.h"
19
#include "vtkPen.h"
20
#include "vtkBrush.h"
21 22 23 24 25
#include "vtkTable.h"
#include "vtkDataObject.h"
#include "vtkIdTypeArray.h"
#include "vtkContextMapper2D.h"
#include "vtkObjectFactory.h"
26
#include "vtkStringArray.h"
27
#include "vtkNew.h"
28
#include <sstream>
29

30 31
vtkCxxSetObjectMacro(vtkPlot, XAxis, vtkAxis);
vtkCxxSetObjectMacro(vtkPlot, YAxis, vtkAxis);
32 33

//-----------------------------------------------------------------------------
34
vtkPlot::vtkPlot() : ShiftScale(0.0, 0.0, 1.0, 1.0)
35
{
36
  this->Pen = vtkSmartPointer<vtkPen>::New();
37
  this->Pen->SetWidth(2.0);
38
  this->Brush = vtkSmartPointer<vtkBrush>::New();
39 40 41 42 43 44 45

  this->SelectionPen = vtkSmartPointer<vtkPen>::New();
  this->SelectionPen->SetColor(255, 50, 0, 150);
  this->SelectionPen->SetWidth(4.0);
  this->SelectionBrush = vtkSmartPointer<vtkBrush>::New();
  this->SelectionBrush->SetColor(255, 50, 0, 150);

Keith Fieldhouse's avatar
Keith Fieldhouse committed
46
  this->Labels = NULL;
47
  this->UseIndexForXSeries = false;
48
  this->Data = vtkSmartPointer<vtkContextMapper2D>::New();
Julien Finet's avatar
Julien Finet committed
49
  this->Selectable = true;
50
  this->Selection = NULL;
51 52
  this->XAxis = NULL;
  this->YAxis = NULL;
53 54

  this->TooltipDefaultLabelFormat = "%l: %x,  %y";
55 56
  this->TooltipNotation = vtkAxis::STANDARD_NOTATION;
  this->TooltipPrecision = 6;
57 58

  this->LegendVisibility = true;
59 60 61 62 63
}

//-----------------------------------------------------------------------------
vtkPlot::~vtkPlot()
{
64 65 66 67 68
  if (this->Selection)
    {
    this->Selection->Delete();
    this->Selection = NULL;
    }
Keith Fieldhouse's avatar
Keith Fieldhouse committed
69
  this->SetLabels(NULL);
70 71
  this->SetXAxis(NULL);
  this->SetYAxis(NULL);
72 73
}

74
//-----------------------------------------------------------------------------
75
bool vtkPlot::PaintLegend(vtkContext2D*, const vtkRectf&, int)
76 77 78 79
{
  return false;
}

80
//-----------------------------------------------------------------------------
81 82
vtkIdType vtkPlot::GetNearestPoint(const vtkVector2f&, const vtkVector2f&,
                                   vtkVector2f*)
83
{
Keith Fieldhouse's avatar
Keith Fieldhouse committed
84
  return -1;
85 86
}

87
//-----------------------------------------------------------------------------
88
vtkStdString vtkPlot::GetTooltipLabel(const vtkVector2d &plotPos,
89 90
                                      vtkIdType seriesIndex,
                                      vtkIdType)
91
{
92 93 94
  vtkStdString tooltipLabel;
  vtkStdString &format = this->TooltipLabelFormat.empty() ?
        this->TooltipDefaultLabelFormat : this->TooltipLabelFormat;
95 96
  // Parse TooltipLabelFormat and build tooltipLabel
  bool escapeNext = false;
97
  for (size_t i = 0; i < format.length(); ++i)
98 99 100
    {
    if (escapeNext)
      {
101
      switch (format[i])
102 103
        {
        case 'x':
104
          tooltipLabel += this->GetNumber(plotPos.GetX(), this->XAxis);
105 106
          break;
        case 'y':
107
          tooltipLabel += this->GetNumber(plotPos.GetY(), this->YAxis);
108 109 110 111 112 113
          break;
        case 'i':
          if (this->IndexedLabels &&
              seriesIndex >= 0 &&
              seriesIndex < this->IndexedLabels->GetNumberOfTuples())
            {
114
            tooltipLabel += this->IndexedLabels->GetValue(seriesIndex);
115 116 117 118
            }
          break;
        case 'l':
          // GetLabel() is GetLabel(0) in this implementation
119
          tooltipLabel += this->GetLabel();
120 121
          break;
        default: // If no match, insert the entire format tag
122 123
          tooltipLabel += "%";
          tooltipLabel += format[i];
124 125 126 127 128 129
          break;
        }
      escapeNext = false;
      }
    else
      {
130
      if (format[i] == '%')
131 132 133 134 135
        {
        escapeNext = true;
        }
      else
        {
136
        tooltipLabel += format[i];
137 138 139
        }
      }
    }
140 141 142 143 144 145 146
  return tooltipLabel;
}

//-----------------------------------------------------------------------------
vtkStdString vtkPlot::GetNumber(double position, vtkAxis *axis)
{
  // Determine and format the X and Y position in the chart
147
  std::ostringstream ostr;
148
  ostr.imbue(std::locale::classic());
149
  ostr.precision(this->GetTooltipPrecision());
150

151
  if(this->GetTooltipNotation() == vtkAxis::SCIENTIFIC_NOTATION)
152
    {
153 154 155 156 157
    ostr.setf(ios::scientific, ios::floatfield);
    }
  else if(this->GetTooltipNotation() == vtkAxis::FIXED_NOTATION)
    {
    ostr.setf(ios::fixed, ios::floatfield);
158 159
    }

David Thompson's avatar
David Thompson committed
160
  if (axis && axis->GetLogScaleActive())
161 162 163 164 165 166 167 168 169 170
    {
    // If axes are set to logarithmic scale we need to convert the
    // axis value using 10^(axis value)
    ostr << pow(double(10.0), double(position));
    }
  else
    {
    ostr << position;
    }
  return ostr.str();
171 172
}

173 174 175
//-----------------------------------------------------------------------------
bool vtkPlot::SelectPoints(const vtkVector2f&, const vtkVector2f&)
{
176 177 178 179
  if (this->Selection)
    {
    this->Selection->SetNumberOfTuples(0);
    }
180 181 182
  return false;
}

183 184 185
//-----------------------------------------------------------------------------
bool vtkPlot::SelectPointsInPolygon(const vtkContextPolygon &)
{
186 187 188 189
  if (this->Selection)
    {
    this->Selection->SetNumberOfTuples(0);
    }
190 191 192
  return false;
}

193
//-----------------------------------------------------------------------------
194 195 196
void vtkPlot::SetColor(unsigned char r, unsigned char g, unsigned char b,
                       unsigned char a)
{
197
  this->Pen->SetColor(r, g, b, a);
198 199
}

200 201 202
//-----------------------------------------------------------------------------
void vtkPlot::SetColor(double r, double g, double b)
{
203 204 205
  this->Pen->SetColorF(r, g, b);
}

206 207 208 209 210
//-----------------------------------------------------------------------------
void vtkPlot::GetColor(double rgb[3])
{
  this->Pen->GetColorF(rgb);
}
211

212 213 214 215 216 217 218 219 220 221
//-----------------------------------------------------------------------------
void vtkPlot::GetColor(unsigned char rgb[3])
{
  double rgbF[3];
  this->GetColor(rgbF);
  rgb[0] = static_cast<unsigned char>(255. * rgbF[0] + 0.5);
  rgb[1] = static_cast<unsigned char>(255. * rgbF[1] + 0.5);
  rgb[2] = static_cast<unsigned char>(255. * rgbF[2] + 0.5);
}

222 223 224 225 226 227 228 229 230 231
//-----------------------------------------------------------------------------
void vtkPlot::SetWidth(float width)
{
  this->Pen->SetWidth(width);
}

//-----------------------------------------------------------------------------
float vtkPlot::GetWidth()
{
  return this->Pen->GetWidth();
232 233
}

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
//-----------------------------------------------------------------------------
void vtkPlot::SetPen(vtkPen *pen)
{
  if (this->Pen != pen)
    {
    this->Pen = pen;
    this->Modified();
    }
}

//-----------------------------------------------------------------------------
vtkPen* vtkPlot::GetPen()
{
  return this->Pen.GetPointer();
}

//-----------------------------------------------------------------------------
void vtkPlot::SetBrush(vtkBrush *brush)
{
  if (this->Brush != brush)
    {
    this->Brush = brush;
    this->Modified();
    }
}

//-----------------------------------------------------------------------------
vtkBrush* vtkPlot::GetBrush()
{
  return this->Brush.GetPointer();
}

266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
//-----------------------------------------------------------------------------
void vtkPlot::SetSelectionPen(vtkPen *pen)
{
  if (this->SelectionPen != pen)
    {
    this->SelectionPen = pen;
    this->Modified();
    }
}

//-----------------------------------------------------------------------------
vtkPen* vtkPlot::GetSelectionPen()
{
  return this->SelectionPen.GetPointer();
}

//-----------------------------------------------------------------------------
void vtkPlot::SetSelectionBrush(vtkBrush *brush)
{
  if (this->SelectionBrush != brush)
    {
    this->SelectionBrush = brush;
    this->Modified();
    }
}

//-----------------------------------------------------------------------------
vtkBrush* vtkPlot::GetSelectionBrush()
{
  return this->SelectionBrush.GetPointer();
}

298
//-----------------------------------------------------------------------------
299
void vtkPlot::SetLabel(const vtkStdString& label)
300
{
301
  vtkNew<vtkStringArray> labels;
302
  labels->InsertNextValue(label);
303
  this->SetLabels(labels.GetPointer());
304 305 306
}

//-----------------------------------------------------------------------------
307
vtkStdString vtkPlot::GetLabel()
308 309 310 311 312
{
  return this->GetLabel(0);
}

//-----------------------------------------------------------------------------
Keith Fieldhouse's avatar
Keith Fieldhouse committed
313 314 315 316 317 318 319 320 321 322 323
void vtkPlot::SetLabels(vtkStringArray *labels)
{
  if (this->Labels == labels)
    {
    return;
    }

  this->Labels = labels;
  this->Modified();
}

324
//-----------------------------------------------------------------------------
325
vtkStringArray * vtkPlot::GetLabels()
326 327
{
  // If the label string is empty, return the y column name
Keith Fieldhouse's avatar
Keith Fieldhouse committed
328 329 330 331 332
  if (this->Labels)
    {
    return this->Labels;
    }
  else if (this->AutoLabels)
333
    {
Keith Fieldhouse's avatar
Keith Fieldhouse committed
334
    return this->AutoLabels;
335 336 337 338
    }
  else if (this->Data->GetInput() &&
           this->Data->GetInputArrayToProcess(1, this->Data->GetInput()))
    {
Keith Fieldhouse's avatar
Keith Fieldhouse committed
339 340 341
    this->AutoLabels = vtkSmartPointer<vtkStringArray>::New();
    this->AutoLabels->InsertNextValue(this->Data->GetInputArrayToProcess(1, this->Data->GetInput())->GetName());
    return this->AutoLabels;
342 343 344 345 346 347
    }
  else
    {
    return NULL;
    }
}
Keith Fieldhouse's avatar
Keith Fieldhouse committed
348 349 350 351 352 353 354 355 356 357 358 359 360
//-----------------------------------------------------------------------------
int vtkPlot::GetNumberOfLabels()
{
  vtkStringArray *labels = this->GetLabels();
  if (labels)
    {
    return labels->GetNumberOfValues();
    }
  else
    {
    return 0;
    }
}
361

362 363 364 365 366 367 368 369
//-----------------------------------------------------------------------------
void vtkPlot::SetIndexedLabels(vtkStringArray *labels)
{
  if (this->IndexedLabels == labels)
    {
    return;
    }

370 371 372 373 374 375 376 377 378
  if (labels)
    {
    this->TooltipDefaultLabelFormat = "%i: %x,  %y";
    }
  else
    {
    this->TooltipDefaultLabelFormat = "%l: %x,  %y";
    }

379 380 381 382 383 384 385 386 387 388
  this->IndexedLabels = labels;
  this->Modified();
}

//-----------------------------------------------------------------------------
vtkStringArray * vtkPlot::GetIndexedLabels()
{
  return this->IndexedLabels.GetPointer();
}

389 390 391 392 393 394
//-----------------------------------------------------------------------------
vtkContextMapper2D * vtkPlot::GetData()
{
  return this->Data.GetPointer();
}

395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
//-----------------------------------------------------------------------------
void vtkPlot::SetTooltipLabelFormat(const vtkStdString &labelFormat)
{
  if (this->TooltipLabelFormat == labelFormat)
    {
    return;
    }

  this->TooltipLabelFormat = labelFormat;
  this->Modified();
}

//-----------------------------------------------------------------------------
vtkStdString vtkPlot::GetTooltipLabelFormat()
{
  return this->TooltipLabelFormat;
}

413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
//-----------------------------------------------------------------------------
void vtkPlot::SetTooltipNotation(int notation)
{
  this->TooltipNotation = notation;
  this->Modified();
}

//-----------------------------------------------------------------------------
int vtkPlot::GetTooltipNotation()
{
  return this->TooltipNotation;
}

//-----------------------------------------------------------------------------
void vtkPlot::SetTooltipPrecision(int precision)
{
  this->TooltipPrecision = precision;
  this->Modified();
}

//-----------------------------------------------------------------------------
int vtkPlot::GetTooltipPrecision()
{
  return this->TooltipPrecision;
}

Keith Fieldhouse's avatar
Keith Fieldhouse committed
439
//-----------------------------------------------------------------------------
440
vtkStdString vtkPlot::GetLabel(vtkIdType index)
Keith Fieldhouse's avatar
Keith Fieldhouse committed
441 442 443 444 445 446
{
  vtkStringArray *labels = this->GetLabels();
  if (labels && index >= 0 && index < labels->GetNumberOfValues())
    {
    return labels->GetValue(index);
    }
447
  else
Keith Fieldhouse's avatar
Keith Fieldhouse committed
448
    {
449
    return vtkStdString();
Keith Fieldhouse's avatar
Keith Fieldhouse committed
450 451
    }
}
452
//-----------------------------------------------------------------------------
453
void vtkPlot::SetInputData(vtkTable *table)
454
{
455
  this->Data->SetInputData(table);
Keith Fieldhouse's avatar
Keith Fieldhouse committed
456
  this->AutoLabels = 0;  // No longer valid
457 458 459
}

//-----------------------------------------------------------------------------
460 461
void vtkPlot::SetInputData(vtkTable *table, const vtkStdString &xColumn,
                           const vtkStdString &yColumn)
462
{
463 464
  vtkDebugMacro(<< "Setting input, X column = \"" << xColumn.c_str()
                << "\", " << "Y column = \"" << yColumn.c_str() << "\"");
465

466
  this->Data->SetInputData(table);
467 468
  this->Data->SetInputArrayToProcess(0, 0, 0,
                                     vtkDataObject::FIELD_ASSOCIATION_ROWS,
469
                                     xColumn.c_str());
470 471
  this->Data->SetInputArrayToProcess(1, 0, 0,
                                     vtkDataObject::FIELD_ASSOCIATION_ROWS,
472
                                     yColumn.c_str());
Keith Fieldhouse's avatar
Keith Fieldhouse committed
473
  this->AutoLabels = 0;  // No longer valid
474 475 476
}

//-----------------------------------------------------------------------------
477 478
void vtkPlot::SetInputData(vtkTable *table, vtkIdType xColumn,
                           vtkIdType yColumn)
479
{
480 481 482
  this->SetInputData(table,
                     table->GetColumnName(xColumn),
                     table->GetColumnName(yColumn));
483 484
}

485 486 487 488 489 490
//-----------------------------------------------------------------------------
vtkTable* vtkPlot::GetInput()
{
  return this->Data->GetInput();
}

491
//-----------------------------------------------------------------------------
492
void vtkPlot::SetInputArray(int index, const vtkStdString &name)
493 494 495
{
  this->Data->SetInputArrayToProcess(index, 0, 0,
                                     vtkDataObject::FIELD_ASSOCIATION_ROWS,
496
                                     name.c_str());
Keith Fieldhouse's avatar
Keith Fieldhouse committed
497
  this->AutoLabels = 0; // No longer valid
498 499
}

Julien Finet's avatar
Julien Finet committed
500 501 502 503 504 505 506 507 508 509
//-----------------------------------------------------------------------------
void vtkPlot::SetSelection(vtkIdTypeArray *id)
{
  if (!this->GetSelectable())
    {
    return;
    }
  vtkSetObjectBodyMacro(Selection,vtkIdTypeArray,id);
}

510
//-----------------------------------------------------------------------------
511
void vtkPlot::SetShiftScale(const vtkRectd &shiftScale)
512
{
513
  if (shiftScale != this->ShiftScale)
514 515
    {
    this->Modified();
516
    this->ShiftScale = shiftScale;
517 518 519 520
    }
}

//-----------------------------------------------------------------------------
521
vtkRectd vtkPlot::GetShiftScale()
522
{
523
  return this->ShiftScale;
524 525
}

526 527 528 529 530 531 532 533 534 535 536
//-----------------------------------------------------------------------------
void vtkPlot::SetProperty(const vtkStdString&, const vtkVariant&)
{
}

//-----------------------------------------------------------------------------
vtkVariant vtkPlot::GetProperty(const vtkStdString&)
{
  return vtkVariant();
}

537 538 539 540
//-----------------------------------------------------------------------------
void vtkPlot::PrintSelf(ostream &os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
541
  os << indent << "LegendVisibility: " << this->LegendVisibility << endl;
542
}