Skip to content

Commit d2037f4

Browse files
committed
Add channel to LegendProperties
1 parent 5e56526 commit d2037f4

File tree

3 files changed

+47
-58
lines changed

3 files changed

+47
-58
lines changed

src/chart/main/events.h

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -207,27 +207,24 @@ class Events
207207

208208
struct LegendProperties
209209
{
210-
double scrollTop;
211-
double scrollHeight;
210+
Gen::ChannelId channel;
211+
double scrollTop{};
212+
double scrollHeight{};
212213
};
213214

214215
struct Legend : Element
215216
{
216-
Gen::ChannelId channel;
217217
LegendProperties properties;
218218

219-
explicit Legend(Gen::ChannelId channel,
220-
const LegendProperties &properties) :
219+
explicit Legend(const LegendProperties &properties) :
221220
Element("legend"),
222-
channel(channel),
223221
properties(properties)
224222
{}
225223

226224
void appendToJSON(Conv::JSONObj &&jsonObj) const override
227225
{
228226
Element::appendToJSON(
229-
std::move(jsonObj)("channel", channel)
230-
.mergeObj(properties));
227+
std::move(jsonObj).mergeObj(properties));
231228
}
232229
};
233230

@@ -311,10 +308,9 @@ class Events
311308
return std::make_unique<Axis>(horizontal);
312309
}
313310

314-
static auto legend(Gen::ChannelId channel,
315-
const LegendProperties &properties)
311+
static auto legend(const LegendProperties &properties)
316312
{
317-
return std::make_unique<Legend>(channel, properties);
313+
return std::make_unique<Legend>(properties);
318314
}
319315

320316
static auto marker(const Gen::Marker &marker)
@@ -377,56 +373,55 @@ class Events
377373
const std::string_view &categoryName,
378374
const std::string_view &categoryValue,
379375
const std::string &label,
380-
Gen::ChannelId channel,
376+
381377
const LegendProperties &properties)
382378
{
383379
return std::make_unique<CategoryInfo<Text<LegendChild>>>(
384380
categoryName,
385381
categoryValue,
386382
label,
387383
"label",
388-
channel,
384+
389385
properties);
390386
}
391387

392388
static auto measLegendLabel(const std::string &label,
393-
Gen::ChannelId channel,
389+
394390
const LegendProperties &properties)
395391
{
396392
return std::make_unique<Text<LegendChild>>(label,
397393
"label",
398-
channel,
394+
399395
properties);
400396
}
401397

402398
static auto legendTitle(const std::string &title,
403-
Gen::ChannelId channel,
399+
404400
const LegendProperties &properties)
405401
{
406402
return std::make_unique<Text<LegendChild>>(title,
407403
"title",
408-
channel,
404+
409405
properties);
410406
}
411407

412408
static auto legendMarker(const std::string_view &categoryName,
413409
const std::string_view &categoryValue,
414-
Gen::ChannelId channel,
410+
415411
const LegendProperties &properties)
416412
{
417413
return std::make_unique<CategoryInfo<LegendChild>>(
418414
categoryName,
419415
categoryValue,
420416
"marker",
421-
channel,
417+
422418
properties);
423419
}
424420

425-
static auto legendBar(Gen::ChannelId channel,
426-
const LegendProperties &properties)
421+
static auto legendBar(const LegendProperties &properties)
427422
{
428423
return std::make_unique<LegendChild>("bar",
429-
channel,
424+
430425
properties);
431426
}
432427

src/chart/rendering/drawlegend.cpp

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ void DrawLegend::draw(Gfx::ICanvas &canvas,
5353
.titleRect = titleRect,
5454
.markerWindowRect = markerWindowRect,
5555
.fadeHeight = fadeHeight,
56-
.type = channelType,
5756
.weight = weight,
5857
.itemHeight = itemHeight,
5958
.markerSize = markerSize,
6059
.measure = plot->axises.at(channelType).measure,
6160
.dimension = plot->axises.at(channelType).dimension,
61+
.properties = {.channel = channelType},
6262
.colorGradientSetter = {markerWindowRect.leftSide(),
6363
Gfx::ColorGradient{{
6464
{0.0, {}},
@@ -78,7 +78,7 @@ void DrawLegend::draw(Gfx::ICanvas &canvas,
7878
legendLayout,
7979
style,
8080
*events.background,
81-
Events::Targets::legend(channelType, info.properties));
81+
Events::Targets::legend(info.properties));
8282

8383
canvas.save();
8484

@@ -113,26 +113,26 @@ void DrawLegend::ColorGradientSetter::operator()(Gfx::ICanvas &canvas,
113113

114114
void DrawLegend::drawTitle(const Info &info) const
115115
{
116-
plot->axises.at(info.type).common.title.visit(
117-
[this,
118-
&info,
119-
&rect = info.titleRect,
120-
mul = std::max<double>(info.measureEnabled,
121-
info.dimensionEnabled)](::Anim::InterpolateIndex,
122-
const auto &title)
123-
{
124-
if (title.weight <= 0) return;
125-
126-
DrawLabel{{ctx()}}.draw(info.canvas,
127-
Geom::TransformedRect::fromRect(rect),
128-
title.value,
129-
style.title,
130-
*events.title,
131-
Events::Targets::legendTitle(title.value,
132-
info.type,
133-
info.properties),
134-
{.alpha = title.weight * info.weight * mul});
135-
});
116+
plot->axises.at(info.properties.channel)
117+
.common.title.visit(
118+
[this,
119+
&info,
120+
&rect = info.titleRect,
121+
mul = std::max<double>(info.measureEnabled,
122+
info.dimensionEnabled)](::Anim::InterpolateIndex,
123+
const auto &title)
124+
{
125+
if (title.weight <= 0) return;
126+
127+
DrawLabel{{ctx()}}.draw(info.canvas,
128+
Geom::TransformedRect::fromRect(rect),
129+
title.value,
130+
style.title,
131+
*events.title,
132+
Events::Targets::legendTitle(title.value,
133+
info.properties),
134+
{.alpha = title.weight * info.weight * mul});
135+
});
136136
}
137137

138138
void DrawLegend::drawDimension(const Info &info) const
@@ -180,7 +180,7 @@ void DrawLegend::drawDimension(const Info &info) const
180180
info.dimension.category,
181181
value.second.categoryValue,
182182
value.second.categoryValue,
183-
info.type,
183+
184184
info.properties),
185185
{.alpha =
186186
double{
@@ -247,7 +247,7 @@ void DrawLegend::drawMarker(const Info &info,
247247
auto markerElement =
248248
Events::Targets::legendMarker(info.dimension.category,
249249
categoryValue,
250-
info.type,
250+
251251
info.properties);
252252

253253
if (events.marker->invoke(
@@ -282,7 +282,7 @@ void DrawLegend::drawMeasure(const Info &info) const
282282
auto bar = getBarRect(info);
283283

284284
using ST = Gen::ChannelId;
285-
switch (info.type) {
285+
switch (info.properties.channel) {
286286
case ST::color: colorBar(info, bar); break;
287287
case ST::lightness: lightnessBar(info, bar); break;
288288
case ST::size: sizeBar(info, bar); break;
@@ -308,7 +308,7 @@ void DrawLegend::extremaLabel(const Info &info,
308308
style.label,
309309
*events.label,
310310
Events::Targets::measLegendLabel(text,
311-
info.type,
311+
312312
info.properties),
313313
{.alpha = info.measureWeight * plusWeight});
314314
}
@@ -346,9 +346,7 @@ void DrawLegend::colorBar(const Info &info,
346346
info.canvas.setLineColor(Gfx::Color::Transparent());
347347
info.canvas.setLineWidth(0);
348348

349-
auto barElement =
350-
Events::Targets::legendBar(Gen::ChannelId::color,
351-
info.properties);
349+
auto barElement = Events::Targets::legendBar(info.properties);
352350

353351
if (events.bar->invoke(
354352
Events::OnRectDrawEvent(*barElement, {rect, false}))) {
@@ -379,9 +377,7 @@ void DrawLegend::lightnessBar(const Info &info,
379377
info.canvas.setLineColor(Gfx::Color::Transparent());
380378
info.canvas.setLineWidth(0);
381379

382-
auto barElement =
383-
Events::Targets::legendBar(Gen::ChannelId::lightness,
384-
info.properties);
380+
auto barElement = Events::Targets::legendBar(info.properties);
385381

386382
if (events.bar->invoke(
387383
Events::OnRectDrawEvent(*barElement, {rect, false}))) {
@@ -402,8 +398,7 @@ void DrawLegend::sizeBar(const Info &info,
402398
Gfx::Color::Gray(0.8) * info.measureWeight);
403399
info.canvas.setLineWidth(0);
404400

405-
auto barElement = Events::Targets::legendBar(Gen::ChannelId::size,
406-
info.properties);
401+
auto barElement = Events::Targets::legendBar(info.properties);
407402

408403
if (events.bar->invoke(
409404
Events::OnRectDrawEvent(*barElement, {rect, false}))) {

src/chart/rendering/drawlegend.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ class DrawLegend : public DrawingContext
4242
Geom::Rect titleRect;
4343
Geom::Rect markerWindowRect;
4444
double fadeHeight{};
45-
Gen::ChannelId type{};
4645
double weight{};
4746
double itemHeight{};
4847
double markerSize{};
@@ -51,7 +50,7 @@ class DrawLegend : public DrawingContext
5150
double measureEnabled = measure.enabled.calculate<double>();
5251
bool dimensionEnabled = dimension.enabled;
5352
double measureWeight = weight * measureEnabled;
54-
Events::Targets::LegendProperties properties{};
53+
Events::Targets::LegendProperties properties;
5554
ColorGradientSetter colorGradientSetter;
5655
};
5756

0 commit comments

Comments
 (0)