vtkPlotStacked.cxx 24.2 KB
Newer Older
Keith Fieldhouse's avatar
Keith Fieldhouse committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkPlotStacked.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 "vtkPlotStacked.h"

#include "vtkChartXY.h"
#include "vtkContext2D.h"
#include "vtkPen.h"
#include "vtkBrush.h"
#include "vtkAxis.h"
#include "vtkContextMapper2D.h"
#include "vtkPoints2D.h"
#include "vtkTable.h"
#include "vtkDataArray.h"
Keith Fieldhouse's avatar
Keith Fieldhouse committed
27 28
#include "vtkFloatArray.h"
#include "vtkStringArray.h"
Keith Fieldhouse's avatar
Keith Fieldhouse committed
29 30 31 32
#include "vtkIdTypeArray.h"
#include "vtkImageData.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
Keith Fieldhouse's avatar
Keith Fieldhouse committed
33 34
#include "vtkColorSeries.h"
#include "vtkSmartPointer.h"
35
#include "vtkNew.h"
Keith Fieldhouse's avatar
Keith Fieldhouse committed
36

37 38 39
#include <vector>
#include <algorithm>
#include <map>
Keith Fieldhouse's avatar
Keith Fieldhouse committed
40

Keith Fieldhouse's avatar
Keith Fieldhouse committed
41 42 43 44 45 46
//-----------------------------------------------------------------------------
namespace {

// Compare the two vectors, in X component only
bool compVector2fX(const vtkVector2f& v1, const vtkVector2f& v2)
{
47
  if (v1.GetX() < v2.GetX())
Keith Fieldhouse's avatar
Keith Fieldhouse committed
48 49 50 51 52 53 54 55 56 57 58
    {
    return true;
    }
  else
    {
    return false;
    }
}

// Copy the two arrays into the points array
template<class A, class B>
59
void CopyToPoints(vtkPoints2D *points, vtkPoints2D *previous_points, A *a, B *b,
60
                  int n, double bds[4])
Keith Fieldhouse's avatar
Keith Fieldhouse committed
61
{
Keith Fieldhouse's avatar
Keith Fieldhouse committed
62 63 64 65 66 67
  points->SetNumberOfPoints(n);
  for (int i = 0; i < n; ++i)
    {
    double prev[] = {0.0,0.0};
    if (previous_points)
      previous_points->GetPoint(i,prev);
68 69 70 71 72 73 74 75
    double yi = b[i] + prev[1];
    points->SetPoint(i, a[i], yi);

    bds[0] = bds[0] < a[i] ? bds[0] : a[i];
    bds[1] = bds[1] > a[i] ? bds[1] : a[i];

    bds[2] = bds[2] < yi ? bds[2] : yi;
    bds[3] = bds[3] > yi ? bds[3] : yi;
Keith Fieldhouse's avatar
Keith Fieldhouse committed
76 77 78 79 80
    }
}

// Copy one array into the points array, use the index of that array as x
template<class A>
81 82
void CopyToPoints(
  vtkPoints2D *points, vtkPoints2D *previous_points, A *a, int n, double bds[4])
Keith Fieldhouse's avatar
Keith Fieldhouse committed
83
{
84 85
  bds[0] = 0.;
  bds[1] = n - 1.;
Keith Fieldhouse's avatar
Keith Fieldhouse committed
86 87 88 89 90 91
  points->SetNumberOfPoints(n);
  for (int i = 0; i < n; ++i)
    {
    double prev[] = {0.0,0.0};
    if (previous_points)
      previous_points->GetPoint(i,prev);
92 93 94 95 96
    double yi = a[i] + prev[1];
    points->SetPoint(i, i, yi);

    bds[2] = bds[2] < yi ? bds[2] : yi;
    bds[3] = bds[3] > yi ? bds[3] : yi;
Keith Fieldhouse's avatar
Keith Fieldhouse committed
97 98 99
    }
}

100 101 102
// Copy the two arrays into the points array
template<class A>
void CopyToPointsSwitch(vtkPoints2D *points, vtkPoints2D *previous_points, A *a,
103
                        vtkDataArray *b, int n, double bds[4])
104 105 106 107 108
{
  switch(b->GetDataType())
    {
    vtkTemplateMacro(
        CopyToPoints(points,previous_points, a,
109
                     static_cast<VTK_TT*>(b->GetVoidPointer(0)), n, bds));
110 111 112
    }
}

Keith Fieldhouse's avatar
Keith Fieldhouse committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
} // namespace

class vtkPlotStackedSegment : public vtkObject {
  public:
    vtkTypeMacro(vtkPlotStackedSegment,vtkObject);
    static vtkPlotStackedSegment *New();

    vtkPlotStackedSegment()
      {
      this->Stacked = 0;
      this->Points = 0;
      this->BadPoints = 0;
      this->Previous = 0;
      this->Sorted = false;
      }

129 130 131 132
    void Configure(
      vtkPlotStacked *stacked, vtkDataArray *x_array,
      vtkDataArray *y_array,vtkPlotStackedSegment *prev,
      double bds[4])
Keith Fieldhouse's avatar
Keith Fieldhouse committed
133 134 135 136 137 138 139 140 141 142
      {
      this->Stacked = stacked;
      this->Sorted = false;
      this->Previous = prev;

      if (!this->Points)
        {
        this->Points = vtkSmartPointer<vtkPoints2D>::New();
        }

143
      if (x_array)
Keith Fieldhouse's avatar
Keith Fieldhouse committed
144 145 146 147 148 149
        {
        switch (x_array->GetDataType())
          {
            vtkTemplateMacro(
              CopyToPointsSwitch(this->Points,this->Previous ? this->Previous->Points : 0,
                                 static_cast<VTK_TT*>(x_array->GetVoidPointer(0)),
150
                                 y_array,x_array->GetNumberOfTuples(), bds));
Keith Fieldhouse's avatar
Keith Fieldhouse committed
151 152 153 154 155 156 157 158 159
          }
        }
      else
        { // Using Index for X Series
        switch (y_array->GetDataType())
          {
          vtkTemplateMacro(
            CopyToPoints(this->Points, this->Previous ? this->Previous->Points : 0,
                         static_cast<VTK_TT*>(y_array->GetVoidPointer(0)),
160
                         y_array->GetNumberOfTuples(), bds));
Keith Fieldhouse's avatar
Keith Fieldhouse committed
161 162 163 164 165 166 167
          }
        }

      // Nothing works if we're not sorted on the X access
      vtkIdType n = this->Points->GetNumberOfPoints();
      vtkVector2f* data =
          static_cast<vtkVector2f*>(this->Points->GetVoidPointer(0));
168 169
      std::vector<vtkVector2f> v(data, data+n);
      std::sort(v.begin(), v.end(), compVector2fX);
Keith Fieldhouse's avatar
Keith Fieldhouse committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

      this->CalculateLogSeries();
      this->FindBadPoints();
      }

    void CalculateLogSeries()
      {
      vtkAxis *xAxis = this->Stacked->GetXAxis();
      vtkAxis *yAxis = this->Stacked->GetYAxis();

      if (!xAxis || !yAxis)
        {
        return;
        }

David Thompson's avatar
David Thompson committed
185 186
      bool logX = xAxis->GetLogScaleActive();
      bool logY = yAxis->GetLogScaleActive();
Keith Fieldhouse's avatar
Keith Fieldhouse committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 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 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 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

      float* data = static_cast<float*>(this->Points->GetVoidPointer(0));

      vtkIdType n = this->Points->GetNumberOfPoints();
      if (logX)
        {
        for (vtkIdType i = 0; i < n; ++i)
          {
          data[2*i] = log10(data[2*i]);
          }
        }
      if (logY)
        {
        for (vtkIdType i = 0; i < n; ++i)
          {
          data[2*i+1] = log10(data[2*i+1]);
          }
        }
      }

    void FindBadPoints()
      {
      // This should be run after CalculateLogSeries as a final step.
      float* data = static_cast<float*>(this->Points->GetVoidPointer(0));
      vtkIdType n = this->Points->GetNumberOfPoints();
      if (!this->BadPoints)
        {
        this->BadPoints = vtkSmartPointer<vtkIdTypeArray>::New();
        }
      else
        {
        this->BadPoints->SetNumberOfTuples(0);
        }

      // Scan through and find any bad points.
      for (vtkIdType i = 0; i < n; ++i)
        {
        vtkIdType p = 2*i;
        if (vtkMath::IsInf(data[p]) || vtkMath::IsInf(data[p+1]) ||
            vtkMath::IsNan(data[p]) || vtkMath::IsNan(data[p+1]))
          {
          this->BadPoints->InsertNextValue(i);
          }
        }

      if (this->BadPoints->GetNumberOfTuples() == 0)
        {
        this->BadPoints = 0;
        }
      }

      void GetBounds(double bounds[4])
        {
        bounds[0] = bounds[1] = bounds[2] = bounds[3] = 0.0;
        if (this->Points)
          {
          if (!this->BadPoints)
            {
            this->Points->GetBounds(bounds);
            }
          else
            {
            // There are bad points in the series - need to do this ourselves.
            this->CalculateBounds(bounds);
            }
          }
        }


      void CalculateBounds(double bounds[4])
        {
        // We can use the BadPoints array to skip the bad points
        if (!this->Points || !this->BadPoints)
          {
          return;
          }
        vtkIdType start = 0;
        vtkIdType end = 0;
        vtkIdType i = 0;
        vtkIdType nBad = this->BadPoints->GetNumberOfTuples();
        vtkIdType nPoints = this->Points->GetNumberOfPoints();
        if (this->BadPoints->GetValue(i) == 0)
          {
          while (i < nBad && i == this->BadPoints->GetValue(i))
            {
            start = this->BadPoints->GetValue(i++) + 1;
            }
          if (start < nPoints)
            {
            end = nPoints;
            }
          else
            {
            // They are all bad points
            return;
            }
          }
        if (i < nBad)
          {
          end = this->BadPoints->GetValue(i++);
          }
        else
          {
          end = nPoints;
          }
        vtkVector2f* pts = static_cast<vtkVector2f*>(this->Points->GetVoidPointer(0));

        // Initialize our min/max
295 296
        bounds[0] = bounds[1] = pts[start].GetX();
        bounds[2] = bounds[3] = pts[start++].GetY();
Keith Fieldhouse's avatar
Keith Fieldhouse committed
297 298 299 300 301 302

        while (start < nPoints)
          {
          // Calculate the min/max in this range
          while (start < end)
            {
303 304
            float x = pts[start].GetX();
            float y = pts[start++].GetY();
Keith Fieldhouse's avatar
Keith Fieldhouse committed
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
            if (x < bounds[0])
              {
              bounds[0] = x;
              }
            else if (x > bounds[1])
              {
              bounds[1] = x;
              }
            if (y < bounds[2])
              {
              bounds[2] = y;
              }
            else if (y > bounds[3])
              {
              bounds[3] = y;
              }
            }
          // Now figure out the next range
          start = end + 1;
          if (++i < nBad)
            {
            end = this->BadPoints->GetValue(i);
            }
          else
            {
            end = nPoints;
            }
          }
        }


    void Paint(vtkContext2D *painter, vtkPen *pen, vtkBrush *brush)
      {
      painter->ApplyPen(pen);
      painter->ApplyBrush(brush);
      int n = this->Points->GetNumberOfPoints();
341
      float *data_extent = vtkArrayDownCast<vtkFloatArray>(this->Points->GetData())->GetPointer(0);
Keith Fieldhouse's avatar
Keith Fieldhouse committed
342 343
      float *data_base = 0;
      if (this->Previous)
344
        data_base = vtkArrayDownCast<vtkFloatArray>(this->Previous->Points->GetData())->GetPointer(0);
Keith Fieldhouse's avatar
Keith Fieldhouse committed
345 346 347 348 349 350 351

      if (n >= 2)
        {
        float poly_points[8];

        for (int i = 0; i < (n - 1); ++i)
          {
352
          if (data_base)
Keith Fieldhouse's avatar
Keith Fieldhouse committed
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
            {
            poly_points[0] = data_base[2*i];
            poly_points[1] = data_base[2*i+1];
            poly_points[2] = data_base[2*i+2];
            poly_points[3] = data_base[2*i+3];
            }
          else
            {
            poly_points[0] = data_extent[2*i];  // Use the same X as extent
            poly_points[1] = 0.0;
            poly_points[2] = data_extent[2*i+2]; // Use the same X as extent
            poly_points[3] = 0.0;
            }
          poly_points[4] = data_extent[2*i+2];
          poly_points[5] = data_extent[2*i+3];
          poly_points[6] = data_extent[2*i];
          poly_points[7] = data_extent[2*i+1];

          painter->DrawQuad(poly_points);
          }
        }
      }

376 377 378
    bool GetNearestPoint(const vtkVector2f& point,
                         const vtkVector2f& tol,
                         vtkVector2f* location)
Keith Fieldhouse's avatar
Keith Fieldhouse committed
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
      {
      // Right now doing a simple bisector search of the array. This should be
      // revisited. Assumes the x axis is sorted, which should always be true for
      // line plots.
      if (!this->Points)
        {
        return false;
        }
      vtkIdType n = this->Points->GetNumberOfPoints();
      if (n < 2)
        {
        return false;
        }

      // Set up our search array, use the STL lower_bound algorithm
      // When searching, invert the behavior of the offset and
      // compensate for the half width overlap.
396
      std::vector<vtkVector2f>::iterator low;
397
      vtkVector2f lowPoint(point.GetX()-tol.GetX(), 0.0f);
Keith Fieldhouse's avatar
Keith Fieldhouse committed
398 399 400

      vtkVector2f* data =
          static_cast<vtkVector2f*>(this->Points->GetVoidPointer(0));
401
      std::vector<vtkVector2f> v(data, data+n);
Keith Fieldhouse's avatar
Keith Fieldhouse committed
402

403
      low = std::lower_bound(v.begin(), v.end(), lowPoint, compVector2fX);
Keith Fieldhouse's avatar
Keith Fieldhouse committed
404 405

      // Now consider the y axis.  We only worry about our extent
Kyle Lutz's avatar
Kyle Lutz committed
406
      // to the base because each segment is called in order and the
Keith Fieldhouse's avatar
Keith Fieldhouse committed
407 408 409
      // first positive wins.
      while (low != v.end())
        {
410
        if (low->GetX() - tol.GetX() > point.GetX())
Keith Fieldhouse's avatar
Keith Fieldhouse committed
411 412 413
          {
          break;
          }
414 415
        else if (low->GetX()-tol.GetX() < point.GetX() &&
                 low->GetX()+tol.GetX() > point.GetX())
Keith Fieldhouse's avatar
Keith Fieldhouse committed
416
          {
417 418
          if ((point.GetY() >= 0 && point.GetY() < low->GetY()) ||
              (point.GetY() < 0 && point.GetY() > low->GetY()))
Keith Fieldhouse's avatar
Keith Fieldhouse committed
419 420 421 422 423 424 425 426 427 428
              {
              *location = *low;
              return true;
              }
          }
        ++low;
        }
      return false;
      }

429 430
    void SelectPoints(const vtkVector2f& min,
                      const vtkVector2f& max,
Keith Fieldhouse's avatar
Keith Fieldhouse committed
431 432 433 434 435 436 437 438 439 440 441 442 443
                      vtkIdTypeArray *selection)
      {
      if (!this->Points)
        {
        return;
        }

      // Iterate through all points and check whether any are in range
      vtkVector2f* data = static_cast<vtkVector2f*>(this->Points->GetVoidPointer(0));
      vtkIdType n = this->Points->GetNumberOfPoints();

      for (vtkIdType i = 0; i < n; ++i)
        {
444 445
        if (data[i].GetX() >= min.GetX() && data[i].GetX() <= max.GetX() &&
            data[i].GetY() >= min.GetY() && data[i].GetY() <= max.GetY())
Keith Fieldhouse's avatar
Keith Fieldhouse committed
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
          {
          selection->InsertNextValue(i);
          }
        }
      }

    vtkSmartPointer<vtkPlotStackedSegment> Previous;
    vtkSmartPointer<vtkPoints2D> Points;
    vtkSmartPointer<vtkIdTypeArray> BadPoints;
    vtkPlotStacked *Stacked;
    bool Sorted;
};

vtkStandardNewMacro(vtkPlotStackedSegment);

//-----------------------------------------------------------------------------

class vtkPlotStackedPrivate {
  public:
    vtkPlotStackedPrivate(vtkPlotStacked *stacked) : Stacked(stacked) {}
    void Update()
      {
      this->Segments.clear();
469 470
      this->UnscaledInputBounds[0] = this->UnscaledInputBounds[2] = vtkMath::Inf();
      this->UnscaledInputBounds[1] = this->UnscaledInputBounds[3] = -vtkMath::Inf();
Keith Fieldhouse's avatar
Keith Fieldhouse committed
471 472 473 474 475
      }

    vtkPlotStackedSegment *AddSegment(vtkDataArray *x_array, vtkDataArray *y_array, vtkPlotStackedSegment *prev=0)
      {
      vtkSmartPointer<vtkPlotStackedSegment> segment = vtkSmartPointer<vtkPlotStackedSegment>::New();
476
      segment->Configure(this->Stacked,x_array,y_array,prev,this->UnscaledInputBounds);
Keith Fieldhouse's avatar
Keith Fieldhouse committed
477 478 479 480 481 482 483 484 485
      this->Segments.push_back(segment);
      return segment;
      }

    void PaintSegments(vtkContext2D *painter, vtkColorSeries *colorSeries,
                       vtkPen *pen, vtkBrush *brush)
      {
      int colorInSeries = 0;
      bool useColorSeries = this->Segments.size() > 1;
486
      for (std::vector<vtkSmartPointer<vtkPlotStackedSegment> >::iterator it = this->Segments.begin();
Keith Fieldhouse's avatar
Keith Fieldhouse committed
487 488 489 490 491 492 493 494 495
           it != this->Segments.end(); ++it)
        {
        if (useColorSeries && colorSeries)
          brush->SetColor(colorSeries->GetColorRepeating(colorInSeries++).GetData());
        (*it)->Paint(painter,pen,brush);
        }
      }


496 497 498
    vtkIdType GetNearestPoint(const vtkVector2f& point,
                              const vtkVector2f& tol,
                              vtkVector2f* location)
Keith Fieldhouse's avatar
Keith Fieldhouse committed
499 500 501 502
      {
      // Depends on the fact that we check the segments in order. Each
      // Segment only worrys about its own total extent from the base.
      int index = 0;
503
      for (std::vector<vtkSmartPointer<vtkPlotStackedSegment> >::iterator it = this->Segments.begin();
Keith Fieldhouse's avatar
Keith Fieldhouse committed
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
           it != this->Segments.end(); ++it)
        {
        if ((*it)->GetNearestPoint(point,tol,location))
          {
          return index;
          }
        ++index;
        }
      return -1;
      }

    void GetBounds(double bounds[4])
      {
      // Depends on the fact that we check the segments in order. Each
      // Segment only worrys about its own total extent from the base.
      double segment_bounds[4];
520
      for (std::vector<vtkSmartPointer<vtkPlotStackedSegment> >::iterator it = this->Segments.begin();
Keith Fieldhouse's avatar
Keith Fieldhouse committed
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
           it != this->Segments.end(); ++it)
        {
        (*it)->GetBounds(segment_bounds);
        if (segment_bounds[0] < bounds[0])
          {
          bounds[0] = segment_bounds[0];
          }
        if (segment_bounds[1] > bounds[1])
          {
          bounds[1] = segment_bounds[1];
          }
        if (segment_bounds[2] < bounds[2])
          {
          bounds[2] = segment_bounds[2];
          }
        if (segment_bounds[3] > bounds[3])
          {
          bounds[3] = segment_bounds[3];
          }
        }
      }


544 545
    void SelectPoints(const vtkVector2f& min,
                      const vtkVector2f& max,
Keith Fieldhouse's avatar
Keith Fieldhouse committed
546 547
                      vtkIdTypeArray *selection)
      {
548
      for (std::vector<vtkSmartPointer<vtkPlotStackedSegment> >::iterator it = this->Segments.begin();
Keith Fieldhouse's avatar
Keith Fieldhouse committed
549 550 551 552 553 554
           it != this->Segments.end(); ++it)
        {
        (*it)->SelectPoints(min,max,selection);
        }
      }

555
    std::vector<vtkSmartPointer<vtkPlotStackedSegment> > Segments;
Keith Fieldhouse's avatar
Keith Fieldhouse committed
556
    vtkPlotStacked *Stacked;
557
    std::map<int,std::string> AdditionalSeries;
558
    double UnscaledInputBounds[4];
Keith Fieldhouse's avatar
Keith Fieldhouse committed
559 560 561 562 563 564 565 566
};

//-----------------------------------------------------------------------------
vtkStandardNewMacro(vtkPlotStacked);

//-----------------------------------------------------------------------------
vtkPlotStacked::vtkPlotStacked()
{
Keith Fieldhouse's avatar
Keith Fieldhouse committed
567
  this->Private = new vtkPlotStackedPrivate(this);
Keith Fieldhouse's avatar
Keith Fieldhouse committed
568 569
  this->BaseBadPoints = NULL;
  this->ExtentBadPoints = NULL;
Keith Fieldhouse's avatar
Keith Fieldhouse committed
570
  this->AutoLabels = NULL;
Keith Fieldhouse's avatar
Keith Fieldhouse committed
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
  this->Pen->SetColor(0,0,0,0);
  this->LogX = false;
  this->LogY = false;
}

//-----------------------------------------------------------------------------
vtkPlotStacked::~vtkPlotStacked()
{
  if (this->BaseBadPoints)
    {
    this->BaseBadPoints->Delete();
    this->BaseBadPoints = NULL;
    }
  if (this->ExtentBadPoints)
    {
    this->ExtentBadPoints->Delete();
    this->ExtentBadPoints = NULL;
    }
Keith Fieldhouse's avatar
Keith Fieldhouse committed
589 590

  delete this->Private;
Keith Fieldhouse's avatar
Keith Fieldhouse committed
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
}

//-----------------------------------------------------------------------------
void vtkPlotStacked::SetColor(unsigned char r, unsigned char g, unsigned char b,
                       unsigned char a)
{
  this->Brush->SetColor(r, g, b, a);
}

//-----------------------------------------------------------------------------
void vtkPlotStacked::SetColor(double r, double g, double b)
{
  this->Brush->SetColorF(r, g, b);
}

//-----------------------------------------------------------------------------
void vtkPlotStacked::GetColor(double rgb[3])
{
  this->Brush->GetColorF(rgb);
}


//-----------------------------------------------------------------------------
void vtkPlotStacked::Update()
{
  if (!this->Visible)
    {
    return;
    }
  // Check if we have an input
  vtkTable *table = this->Data->GetInput();
  if (!table)
    {
    vtkDebugMacro(<< "Update event called with no input table set.");
    return;
    }
  else if(this->Data->GetMTime() > this->BuildTime ||
          table->GetMTime() > this->BuildTime ||
          this->MTime > this->BuildTime)
    {
    vtkDebugMacro(<< "Updating cached values.");
    this->UpdateTableCache(table);
    }
634 635
  else if ((this->XAxis->GetMTime() > this->BuildTime) ||
           (this->YAxis->GetMTime() > this->BuildTime))
Keith Fieldhouse's avatar
Keith Fieldhouse committed
636
    {
David Thompson's avatar
David Thompson committed
637 638
    if (this->LogX != this->XAxis->GetLogScaleActive() ||
        this->LogY != this->YAxis->GetLogScaleActive())
Keith Fieldhouse's avatar
Keith Fieldhouse committed
639 640 641 642 643 644 645 646 647 648 649 650
      {
      this->UpdateTableCache(table);
      }
    }
}

//-----------------------------------------------------------------------------
bool vtkPlotStacked::Paint(vtkContext2D *painter)
{
  // This is where everything should be drawn, or dispatched to other methods.
  vtkDebugMacro(<< "Paint event called in vtkPlotStacked.");

Keith Fieldhouse's avatar
Keith Fieldhouse committed
651
  if (!this->Visible)
Keith Fieldhouse's avatar
Keith Fieldhouse committed
652 653 654 655 656 657 658 659 660 661 662 663 664 665
    {
    return false;
    }

  // Now add some decorations for our selected points...
  if (this->Selection)
    {
    vtkDebugMacro(<<"Selection set " << this->Selection->GetNumberOfTuples());
    }
  else
    {
    vtkDebugMacro("No selection set.");
    }

Keith Fieldhouse's avatar
Keith Fieldhouse committed
666
  this->Private->PaintSegments(painter,this->ColorSeries,this->Pen,this->Brush);
Keith Fieldhouse's avatar
Keith Fieldhouse committed
667 668 669 670 671

  return true;
}

//-----------------------------------------------------------------------------
672
bool vtkPlotStacked::PaintLegend(vtkContext2D *painter, const vtkRectf& rect,
673
                                 int legendIndex)
Keith Fieldhouse's avatar
Keith Fieldhouse committed
674
{
675 676 677 678 679 680 681 682 683 684 685 686 687 688
  if (this->ColorSeries)
    {
    vtkNew<vtkPen> pen;
    vtkNew<vtkBrush> brush;
    pen->SetColor(this->ColorSeries->GetColorRepeating(legendIndex).GetData());
    brush->SetColor(pen->GetColor());
    painter->ApplyPen(pen.GetPointer());
    painter->ApplyBrush(brush.GetPointer());
    }
  else
    {
    painter->ApplyPen(this->Pen);
    painter->ApplyBrush(this->Brush);
    }
Keith Fieldhouse's avatar
Keith Fieldhouse committed
689
  painter->DrawRect(rect[0], rect[1], rect[2], rect[3]);
Keith Fieldhouse's avatar
Keith Fieldhouse committed
690 691 692 693 694 695
  return true;
}

//-----------------------------------------------------------------------------
void vtkPlotStacked::GetBounds(double bounds[4])
{
Keith Fieldhouse's avatar
Keith Fieldhouse committed
696
  this->Private->GetBounds(bounds);
Keith Fieldhouse's avatar
Keith Fieldhouse committed
697 698
}

699 700 701 702 703 704 705 706 707
//-----------------------------------------------------------------------------
void vtkPlotStacked::GetUnscaledInputBounds(double bounds[4])
{
  for (int i = 0; i < 4; ++i)
    {
    bounds[i] = this->Private->UnscaledInputBounds[i];
    }
}

Keith Fieldhouse's avatar
Keith Fieldhouse committed
708
//-----------------------------------------------------------------------------
709
vtkIdType vtkPlotStacked::GetNearestPoint(const vtkVector2f& point,
Keith Fieldhouse's avatar
Keith Fieldhouse committed
710 711 712
                                    const vtkVector2f& tol,
                                    vtkVector2f* location)
{
Keith Fieldhouse's avatar
Keith Fieldhouse committed
713
  return this->Private->GetNearestPoint(point,tol,location);
Keith Fieldhouse's avatar
Keith Fieldhouse committed
714 715 716 717 718 719 720 721 722 723 724
}

//-----------------------------------------------------------------------------
bool vtkPlotStacked::SelectPoints(const vtkVector2f& min, const vtkVector2f& max)
{
  if (!this->Selection)
    {
    this->Selection = vtkIdTypeArray::New();
    }
  this->Selection->SetNumberOfTuples(0);

Keith Fieldhouse's avatar
Keith Fieldhouse committed
725
  this->Private->SelectPoints(min,max,this->Selection);
Keith Fieldhouse's avatar
Keith Fieldhouse committed
726 727 728 729 730

  return this->Selection->GetNumberOfTuples() > 0;
}

//-----------------------------------------------------------------------------
Keith Fieldhouse's avatar
Keith Fieldhouse committed
731
vtkStringArray *vtkPlotStacked::GetLabels()
Keith Fieldhouse's avatar
Keith Fieldhouse committed
732
{
Keith Fieldhouse's avatar
Keith Fieldhouse committed
733 734
  // If the label string is empty, return the y column name
  if (this->Labels)
Keith Fieldhouse's avatar
Keith Fieldhouse committed
735
    {
Keith Fieldhouse's avatar
Keith Fieldhouse committed
736
    return this->Labels;
Keith Fieldhouse's avatar
Keith Fieldhouse committed
737
    }
Keith Fieldhouse's avatar
Keith Fieldhouse committed
738
  else if (this->AutoLabels)
Keith Fieldhouse's avatar
Keith Fieldhouse committed
739
    {
Keith Fieldhouse's avatar
Keith Fieldhouse committed
740
    return this->AutoLabels;
Keith Fieldhouse's avatar
Keith Fieldhouse committed
741
    }
Keith Fieldhouse's avatar
Keith Fieldhouse committed
742 743
  else if (this->Data->GetInput() &&
           this->Data->GetInputArrayToProcess(1, this->Data->GetInput()))
Keith Fieldhouse's avatar
Keith Fieldhouse committed
744
    {
Keith Fieldhouse's avatar
Keith Fieldhouse committed
745 746
    this->AutoLabels = vtkSmartPointer<vtkStringArray>::New();
    this->AutoLabels->InsertNextValue(this->Data->GetInputArrayToProcess(1, this->Data->GetInput())->GetName());
Keith Fieldhouse's avatar
Keith Fieldhouse committed
747

748
    std::map< int, std::string >::iterator it;
Keith Fieldhouse's avatar
Keith Fieldhouse committed
749 750 751 752 753 754 755 756 757
    for ( it = this->Private->AdditionalSeries.begin(); it != this->Private->AdditionalSeries.end(); ++it )
      {
      this->AutoLabels->InsertNextValue((*it).second);
      }
    return this->AutoLabels;
    }
  else
    {
    return NULL;
Keith Fieldhouse's avatar
Keith Fieldhouse committed
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
    }
}

//-----------------------------------------------------------------------------
bool vtkPlotStacked::UpdateTableCache(vtkTable *table)
{
  // Get the x and ybase and yextent arrays (index 0 1 2 respectively)
  vtkDataArray* x = this->UseIndexForXSeries ?
                    0 : this->Data->GetInputArrayToProcess(0, table);
  vtkDataArray* y = this->Data->GetInputArrayToProcess(1, table);

  if (!x && !this->UseIndexForXSeries)
    {
    vtkErrorMacro(<< "No X column is set (index 0).");
    this->BuildTime.Modified();
    return false;
    }
  else if (!y)
    {
    vtkErrorMacro(<< "No Y column is set (index 1).");
    this->BuildTime.Modified();
    return false;
    }
  else if (!this->UseIndexForXSeries &&
           x->GetNumberOfTuples() != y->GetNumberOfTuples())
    {
    vtkErrorMacro("The x and y columns must have the same number of elements. "
                  << x->GetNumberOfTuples() << ", " << y->GetNumberOfTuples() << ", " <<
                  y->GetNumberOfTuples()
                  );
    this->BuildTime.Modified();
    return false;
    }
Keith Fieldhouse's avatar
Keith Fieldhouse committed
791
  this->Private->Update();
Keith Fieldhouse's avatar
Keith Fieldhouse committed
792

Keith Fieldhouse's avatar
Keith Fieldhouse committed
793
  vtkPlotStackedSegment *prev = this->Private->AddSegment(x,y);
Keith Fieldhouse's avatar
Keith Fieldhouse committed
794

795
  std::map< int, std::string >::iterator it;
Keith Fieldhouse's avatar
Keith Fieldhouse committed
796

Keith Fieldhouse's avatar
Keith Fieldhouse committed
797
  for ( it = this->Private->AdditionalSeries.begin(); it != this->Private->AdditionalSeries.end(); ++it )
Keith Fieldhouse's avatar
Keith Fieldhouse committed
798
    {
799
    y = vtkArrayDownCast<vtkDataArray>(table->GetColumnByName((*it).second.c_str()));
Keith Fieldhouse's avatar
Keith Fieldhouse committed
800
    prev = this->Private->AddSegment(x,y,prev);
Keith Fieldhouse's avatar
Keith Fieldhouse committed
801
    }
Keith Fieldhouse's avatar
Keith Fieldhouse committed
802 803

  // Record if this update was done with Log scale.
David Thompson's avatar
David Thompson committed
804 805
  this->LogX = this->XAxis ? this->XAxis->GetLogScaleActive(): false;
  this->LogY = this->YAxis ? this->YAxis->GetLogScaleActive(): false;
Keith Fieldhouse's avatar
Keith Fieldhouse committed
806

Keith Fieldhouse's avatar
Keith Fieldhouse committed
807 808 809 810 811
  this->BuildTime.Modified();
  return true;
}

//-----------------------------------------------------------------------------
Keith Fieldhouse's avatar
Keith Fieldhouse committed
812
void vtkPlotStacked::PrintSelf(ostream &os, vtkIndent indent)
Keith Fieldhouse's avatar
Keith Fieldhouse committed
813
{
Keith Fieldhouse's avatar
Keith Fieldhouse committed
814
  this->Superclass::PrintSelf(os, indent);
Keith Fieldhouse's avatar
Keith Fieldhouse committed
815 816 817 818
}

//-----------------------------------------------------------------------------

819
void vtkPlotStacked::SetInputArray(int index, const vtkStdString &name)
Keith Fieldhouse's avatar
Keith Fieldhouse committed
820
{
Keith Fieldhouse's avatar
Keith Fieldhouse committed
821
  if (index == 0 || index == 1)
Keith Fieldhouse's avatar
Keith Fieldhouse committed
822
    {
Keith Fieldhouse's avatar
Keith Fieldhouse committed
823
    vtkPlot::SetInputArray(index,name);
Keith Fieldhouse's avatar
Keith Fieldhouse committed
824 825 826
    }
  else
    {
827
    this->Private->AdditionalSeries[index] = name;
Keith Fieldhouse's avatar
Keith Fieldhouse committed
828
    }
Keith Fieldhouse's avatar
Keith Fieldhouse committed
829
  this->AutoLabels = 0; // No longer valid
Keith Fieldhouse's avatar
Keith Fieldhouse committed
830 831 832
}

//-----------------------------------------------------------------------------
Keith Fieldhouse's avatar
Keith Fieldhouse committed
833
void vtkPlotStacked::SetColorSeries(vtkColorSeries *colorSeries)
Keith Fieldhouse's avatar
Keith Fieldhouse committed
834
{
Keith Fieldhouse's avatar
Keith Fieldhouse committed
835
  if (this->ColorSeries == colorSeries)
Keith Fieldhouse's avatar
Keith Fieldhouse committed
836 837 838
    {
    return;
    }
Keith Fieldhouse's avatar
Keith Fieldhouse committed
839 840
  this->ColorSeries = colorSeries;
  this->Modified();
Keith Fieldhouse's avatar
Keith Fieldhouse committed
841 842 843
}

//-----------------------------------------------------------------------------
Keith Fieldhouse's avatar
Keith Fieldhouse committed
844
vtkColorSeries *vtkPlotStacked::GetColorSeries()
Keith Fieldhouse's avatar
Keith Fieldhouse committed
845
{
Keith Fieldhouse's avatar
Keith Fieldhouse committed
846
  return this->ColorSeries;
Keith Fieldhouse's avatar
Keith Fieldhouse committed
847
}