forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvtkCategoryLegend.cxx
286 lines (238 loc) · 8.55 KB
/
vtkCategoryLegend.cxx
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkCategoryLegend.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 "vtkBrush.h"
#include "vtkCategoryLegend.h"
#include "vtkContext2D.h"
#include "vtkObjectFactory.h"
#include "vtkScalarsToColors.h"
#include "vtkTextProperty.h"
#include "vtkVariantArray.h"
//-----------------------------------------------------------------------------
vtkStandardNewMacro(vtkCategoryLegend);
//-----------------------------------------------------------------------------
vtkCategoryLegend::vtkCategoryLegend()
{
this->SetInline(false);
this->SetHorizontalAlignment(vtkChartLegend::RIGHT);
this->SetVerticalAlignment(vtkChartLegend::BOTTOM);
this->ScalarsToColors = nullptr;
this->Values = nullptr;
this->TitleProperties->SetColor(this->LabelProperties->GetColor());
this->TitleProperties->SetFontSize(this->LabelProperties->GetFontSize());
this->TitleProperties->SetFontFamily(this->LabelProperties->GetFontFamily());
this->TitleProperties->SetJustificationToCentered();
this->TitleProperties->SetVerticalJustificationToTop();
this->TitleProperties->SetBold(1);
this->TitleWidthOffset = 0.0;
this->HasOutliers = false;
this->OutlierLabel = "outliers";
}
//-----------------------------------------------------------------------------
vtkCategoryLegend::~vtkCategoryLegend() = default;
//-----------------------------------------------------------------------------
bool vtkCategoryLegend::Paint(vtkContext2D* painter)
{
if (!this->Visible || this->ScalarsToColors == nullptr || this->Values == nullptr)
{
return true;
}
// Draw a box around the legend.
painter->ApplyPen(this->Pen);
painter->ApplyBrush(this->Brush);
this->GetBoundingRect(painter);
painter->DrawRect(this->Rect.GetX(), this->Rect.GetY(),
this->Rect.GetWidth(), this->Rect.GetHeight());
// Draw the title (if any)
vtkVector2f stringBounds[2];
float titleHeight = 0.0;
if (!this->Title.empty())
{
painter->ApplyTextProp(this->TitleProperties);
painter->ComputeStringBounds(this->Title, stringBounds->GetData());
titleHeight = stringBounds[1].GetY() + this->Padding;
float x = this->Rect.GetX() + this->Rect.GetWidth() / 2.0;
float y = this->Rect.GetY() + this->Rect.GetHeight() - this->Padding;
painter->DrawString(x, y, this->Title);
}
painter->ApplyTextProp(this->LabelProperties);
// compute the height of a sample string.
// The height of this string will also be used as the size of
// the color marks.
painter->ComputeStringBounds("Tgyf", stringBounds->GetData());
float stringHeight = stringBounds[1].GetY();
// the starting X positions of our marks & labels
float markX = this->Rect.GetX() + this->TitleWidthOffset + this->Padding;
float labelX = markX + stringHeight + this->Padding;
// the Y value of the row that we're currently drawing
float y = this->Rect.GetY() + this->Rect.GetHeight() -
this->Padding - floor(stringHeight) - titleHeight;
// draw all of the marks & labels
for (vtkIdType l = 0; l < this->Values->GetNumberOfTuples(); ++l)
{
vtkStdString currentString = this->Values->GetValue(l).ToString();
if (currentString.empty())
{
continue;
}
if (this->ScalarsToColors->GetAnnotatedValueIndex(
this->Values->GetValue(l)) == -1)
{
continue;
}
// paint the color mark for this category
double color[4];
this->ScalarsToColors->GetAnnotationColor(this->Values->GetValue(l), color);
painter->GetBrush()->SetColorF(color[0], color[1], color[2]);
painter->DrawRect(markX, y, stringHeight, stringHeight);
// draw this category's label
painter->DrawString(labelX, y, this->Values->GetValue(l).ToString());
// Move y position down another row
y -= stringHeight + this->Padding;
}
if (this->HasOutliers)
{
// paint the outlier color mark
double color[4];
this->ScalarsToColors->GetAnnotationColor(
this->ScalarsToColors->GetAnnotatedValue(-1), color);
painter->GetBrush()->SetColorF(color[0], color[1], color[2]);
painter->DrawRect(markX, y, stringHeight, stringHeight);
// draw the outlier label
painter->DrawString(labelX, y, this->OutlierLabel);
}
return true;
}
//-----------------------------------------------------------------------------
void vtkCategoryLegend::SetScalarsToColors(vtkScalarsToColors* stc)
{
this->ScalarsToColors = stc;
}
//-----------------------------------------------------------------------------
vtkScalarsToColors * vtkCategoryLegend::GetScalarsToColors()
{
return this->ScalarsToColors;
}
//-----------------------------------------------------------------------------
vtkRectf vtkCategoryLegend::GetBoundingRect(vtkContext2D *painter)
{
if (this->CacheBounds && this->RectTime > this->GetMTime() &&
this->RectTime > this->PlotTime &&
this->RectTime > this->ScalarsToColors->GetMTime() &&
this->RectTime > this->Values->GetMTime())
{
return this->Rect;
}
painter->ApplyTextProp(this->LabelProperties);
vtkVector2f stringBounds[2];
painter->ComputeStringBounds("Tgyf", stringBounds->GetData());
float height = stringBounds[1].GetY();
// programmatically set Padding here. This results in better
// appearance when we zoom in or out on the legend.
this->Padding = static_cast<int>(height / 4.0);
if (this->Padding < 1)
{
this->Padding = 1;
}
// Calculate size of title (if any)
float titleHeight = 0.0f;
float titleWidth = 0.0f;
if (!this->Title.empty())
{
painter->ApplyTextProp(this->TitleProperties);
painter->ComputeStringBounds(this->Title, stringBounds->GetData());
titleWidth = stringBounds[1].GetX();
titleHeight = stringBounds[1].GetY() + this->Padding;
painter->ApplyTextProp(this->LabelProperties);
}
// Calculate the widest legend label
float maxWidth = 0.0;
int numSkippedValues = 0;
this->TitleWidthOffset = 0.0;
this->HasOutliers = false;
for (vtkIdType l = 0; l < this->Values->GetNumberOfTuples(); ++l)
{
if (this->Values->GetValue(l).ToString().empty())
{
++numSkippedValues;
continue;
}
if (this->ScalarsToColors->GetAnnotatedValueIndex(
this->Values->GetValue(l)) == -1)
{
++numSkippedValues;
this->HasOutliers = true;
continue;
}
painter->ComputeStringBounds(this->Values->GetValue(l).ToString(),
stringBounds->GetData());
if (stringBounds[1].GetX() > maxWidth)
{
maxWidth = stringBounds[1].GetX();
}
}
// Calculate size of outlier label (if necessary)
if (this->HasOutliers)
{
painter->ComputeStringBounds(this->OutlierLabel,
stringBounds->GetData());
if (stringBounds[1].GetX() > maxWidth)
{
maxWidth = stringBounds[1].GetX();
}
}
if (titleWidth > maxWidth)
{
this->TitleWidthOffset = (titleWidth - maxWidth) / 2.0;
maxWidth = titleWidth;
}
int numLabels = this->Values->GetNumberOfTuples() - numSkippedValues;
if (this->HasOutliers)
{
++numLabels;
}
// 3 paddings: one on the left, one on the right, and one between the
// color mark and its label.
float w = ceil(maxWidth + 3 * this->Padding + height);
float h = ceil((numLabels * (height + this->Padding)) + this->Padding
+ titleHeight);
float x = floor(this->Point[0]);
float y = floor(this->Point[1]);
// Compute bottom left point based on current alignment.
if (this->HorizontalAlignment == vtkChartLegend::CENTER)
{
x -= w / 2.0;
}
else if (this->HorizontalAlignment == vtkChartLegend::RIGHT)
{
x -= w;
}
if (this->VerticalAlignment == vtkChartLegend::CENTER)
{
y -= h / 2.0;
}
else if (this->VerticalAlignment == vtkChartLegend::TOP)
{
y -= h;
}
this->Rect = vtkRectf(x, y, w, h);
this->RectTime.Modified();
return this->Rect;
}
//-----------------------------------------------------------------------------
void vtkCategoryLegend::SetTitle(const vtkStdString &title)
{
this->Title = title;
}
//-----------------------------------------------------------------------------
vtkStdString vtkCategoryLegend::GetTitle()
{
return this->Title;
}