vtkChartHistogram2D.cxx 3.72 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkChart2DHistogram.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.

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

16
#include "vtkChartHistogram2D.h"
17 18 19 20 21 22 23 24

#include "vtkContext2D.h"
#include "vtkBrush.h"
#include "vtkPen.h"
#include "vtkContextScene.h"
#include "vtkContextMouseEvent.h"
#include "vtkTextProperty.h"
#include "vtkAxis.h"
25
#include "vtkPlotHistogram2D.h"
26
#include "vtkColorLegend.h"
27
#include "vtkTooltipItem.h"
28 29 30 31
#include "vtkSmartPointer.h"
#include "vtkObjectFactory.h"

//-----------------------------------------------------------------------------
32
vtkStandardNewMacro(vtkChartHistogram2D);
33 34

//-----------------------------------------------------------------------------
35
vtkChartHistogram2D::vtkChartHistogram2D()
36 37
{
  // Now for the 2D histogram
38
  this->Histogram = vtkSmartPointer<vtkPlotHistogram2D>::New();
39
  this->AddPlot(this->Histogram);
40

41
  this->RemoveItem(this->Legend);
42 43
  this->Legend = vtkSmartPointer<vtkColorLegend>::New();
  this->AddItem(this->Legend);
44 45 46 47

  // Re-add tooltip, making it the last ContextItem to be painted
  this->RemoveItem(this->Tooltip);
  this->AddItem(this->Tooltip);
48 49 50
}

//-----------------------------------------------------------------------------
51
vtkChartHistogram2D::~vtkChartHistogram2D() = default;
52 53

//-----------------------------------------------------------------------------
54
void vtkChartHistogram2D::Update()
55
{
56 57
  this->Histogram->Update();
  this->Legend->Update();
58
  this->vtkChartXY::Update();
59 60
}

61
//-----------------------------------------------------------------------------
62
void vtkChartHistogram2D::SetInputData(vtkImageData *data, vtkIdType z)
63
{
64
  this->Histogram->SetInputData(data, z);
65 66
}

67
//-----------------------------------------------------------------------------
68
void vtkChartHistogram2D::SetTransferFunction(vtkScalarsToColors *function)
69 70
{
  this->Histogram->SetTransferFunction(function);
71 72
  vtkColorLegend *legend = vtkColorLegend::SafeDownCast(this->Legend);
  if (legend)
73
  {
74
    legend->SetTransferFunction(function);
75
  }
76 77 78
}

//-----------------------------------------------------------------------------
79
bool vtkChartHistogram2D::UpdateLayout(vtkContext2D *painter)
80
{
81
  this->vtkChartXY::UpdateLayout(painter);
82 83
  vtkColorLegend *legend = vtkColorLegend::SafeDownCast(this->Legend);
  if (legend)
84
  {
85 86 87
    legend->SetPosition(vtkRectf(this->Point2[0] + 5, this->Point1[1],
                                 this->Legend->GetSymbolWidth(),
                                 this->Point2[1] - this->Point1[1]));
88
  }
89 90
  this->Legend->Update();
  return true;
91 92 93
}

//-----------------------------------------------------------------------------
94
bool vtkChartHistogram2D::Hit(const vtkContextMouseEvent &mouse)
95
{
96 97 98 99 100
  vtkVector2i pos(mouse.GetScreenPos());
  if (pos[0] > this->Point1[0] - 10 &&
      pos[0] < this->Point2[0] + 10 &&
      pos[1] > this->Point1[1] &&
      pos[1] < this->Point2[1])
101
  {
102
    return true;
103
  }
104
  else
105
  {
106
    return false;
107
  }
108 109
}

110 111 112 113
//-----------------------------------------------------------------------------
vtkPlot* vtkChartHistogram2D::GetPlot(vtkIdType index)
{
  if (index == 0)
114
  {
115
    return this->Histogram;
116
  }
117

118
  return nullptr;
119 120
}

121
//-----------------------------------------------------------------------------
122
void vtkChartHistogram2D::PrintSelf(ostream &os, vtkIndent indent)
123 124 125
{
  this->Superclass::PrintSelf(os, indent);
}