Skip to content

Commit 5e56526

Browse files
committed
Add scrollTop/scrollHeight + some review
1 parent 9a24410 commit 5e56526

File tree

5 files changed

+64
-44
lines changed

5 files changed

+64
-44
lines changed

src/apps/weblib/ts-api/events.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ export interface Caption extends TextElement {
124124
export interface Legend extends Element {
125125
tagName: 'legend'
126126
channel: string
127-
/** Marker labels y overflow by pixel */
128-
yOverflow: number
127+
scrollTop: number
128+
scrollHeight: number
129129
}
130130

131131
/** Logo element of the chart. */

src/base/conv/auto_json.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,25 @@ struct JSONObj : protected JSONRepeat<'{', '}'>
325325
}
326326
return std::move(*this);
327327
}
328+
329+
template <class T> JSONObj &&mergeObj(T &&obj) &&
330+
{
331+
if (was) json.pop_back();
332+
auto pre_size = json.size();
333+
334+
staticObj(obj);
335+
336+
json.pop_back();
337+
338+
if (pre_size + 1 == json.size())
339+
json.pop_back();
340+
else if (was)
341+
json[pre_size] = ',';
342+
else
343+
was = true;
344+
345+
return std::move(*this);
346+
}
328347
};
329348

330349
struct JSONArr : protected JSONRepeat<'[', ']'>

src/chart/main/events.h

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -205,22 +205,29 @@ class Events
205205
}
206206
};
207207

208+
struct LegendProperties
209+
{
210+
double scrollTop;
211+
double scrollHeight;
212+
};
213+
208214
struct Legend : Element
209215
{
210216
Gen::ChannelId channel;
211-
double yOverflow;
217+
LegendProperties properties;
212218

213219
explicit Legend(Gen::ChannelId channel,
214-
double yOverflow) :
220+
const LegendProperties &properties) :
215221
Element("legend"),
216222
channel(channel),
217-
yOverflow(yOverflow)
223+
properties(properties)
218224
{}
219225

220226
void appendToJSON(Conv::JSONObj &&jsonObj) const override
221227
{
222-
Element::appendToJSON(std::move(jsonObj)("channel",
223-
channel)("yOverflow", yOverflow));
228+
Element::appendToJSON(
229+
std::move(jsonObj)("channel", channel)
230+
.mergeObj(properties));
224231
}
225232
};
226233

@@ -304,9 +311,10 @@ class Events
304311
return std::make_unique<Axis>(horizontal);
305312
}
306313

307-
static auto legend(Gen::ChannelId channel, double yOverflow)
314+
static auto legend(Gen::ChannelId channel,
315+
const LegendProperties &properties)
308316
{
309-
return std::make_unique<Legend>(channel, yOverflow);
317+
return std::make_unique<Legend>(channel, properties);
310318
}
311319

312320
static auto marker(const Gen::Marker &marker)
@@ -370,56 +378,56 @@ class Events
370378
const std::string_view &categoryValue,
371379
const std::string &label,
372380
Gen::ChannelId channel,
373-
double yOverflow)
381+
const LegendProperties &properties)
374382
{
375383
return std::make_unique<CategoryInfo<Text<LegendChild>>>(
376384
categoryName,
377385
categoryValue,
378386
label,
379387
"label",
380388
channel,
381-
yOverflow);
389+
properties);
382390
}
383391

384392
static auto measLegendLabel(const std::string &label,
385393
Gen::ChannelId channel,
386-
double yOverflow)
394+
const LegendProperties &properties)
387395
{
388396
return std::make_unique<Text<LegendChild>>(label,
389397
"label",
390398
channel,
391-
yOverflow);
399+
properties);
392400
}
393401

394402
static auto legendTitle(const std::string &title,
395403
Gen::ChannelId channel,
396-
double yOverflow)
404+
const LegendProperties &properties)
397405
{
398406
return std::make_unique<Text<LegendChild>>(title,
399407
"title",
400408
channel,
401-
yOverflow);
409+
properties);
402410
}
403411

404412
static auto legendMarker(const std::string_view &categoryName,
405413
const std::string_view &categoryValue,
406414
Gen::ChannelId channel,
407-
double yOverflow)
415+
const LegendProperties &properties)
408416
{
409417
return std::make_unique<CategoryInfo<LegendChild>>(
410418
categoryName,
411419
categoryValue,
412420
"marker",
413421
channel,
414-
yOverflow);
422+
properties);
415423
}
416424

417425
static auto legendBar(Gen::ChannelId channel,
418-
double yOverflow)
426+
const LegendProperties &properties)
419427
{
420428
return std::make_unique<LegendChild>("bar",
421429
channel,
422-
yOverflow);
430+
properties);
423431
}
424432

425433
static auto dimAxisLabel(const std::string_view &categoryName,

src/chart/rendering/drawlegend.cpp

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ void DrawLegend::draw(Gfx::ICanvas &canvas,
5353
.titleRect = titleRect,
5454
.markerWindowRect = markerWindowRect,
5555
.fadeHeight = fadeHeight,
56-
.yOverflow = {},
57-
.yOffset = {},
5856
.type = channelType,
5957
.weight = weight,
6058
.itemHeight = itemHeight,
@@ -64,23 +62,23 @@ void DrawLegend::draw(Gfx::ICanvas &canvas,
6462
.colorGradientSetter = {markerWindowRect.leftSide(),
6563
Gfx::ColorGradient{{
6664
{0.0, {}},
67-
{fadeElementPercent / 2.0, {}},
6865
{fadeElementPercent, {}},
6966
{1.0 - fadeElementPercent, {}},
70-
{1.0 - fadeElementPercent / 2.0, {}},
7167
{1.0, {}},
7268
}}}};
7369

74-
info.yOverflow = markersLegendFullSize(info) - markerWindowHeight;
75-
if (std::signbit(info.yOverflow)) info.yOverflow = 0.0;
76-
info.yOffset =
77-
style.translateY->get(info.yOverflow, info.itemHeight);
70+
info.properties.scrollHeight = markersLegendFullSize(info);
71+
auto yOverflow =
72+
info.properties.scrollHeight - markerWindowHeight;
73+
if (std::signbit(yOverflow)) yOverflow = 0.0;
74+
info.properties.scrollTop =
75+
style.translateY->get(yOverflow, info.itemHeight);
7876

7977
DrawBackground{{ctx()}}.draw(canvas,
8078
legendLayout,
8179
style,
8280
*events.background,
83-
Events::Targets::legend(channelType, info.yOverflow));
81+
Events::Targets::legend(channelType, info.properties));
8482

8583
canvas.save();
8684

@@ -108,13 +106,9 @@ void DrawLegend::ColorGradientSetter::operator()(Gfx::ICanvas &canvas,
108106
for (auto &stop : modifiableStops) stop.value = color;
109107

110108
modifiableStops[0].value.alpha = 0.0;
111-
modifiableStops[1].value.alpha *= 0.27;
112-
modifiableStops[4].value.alpha *= 0.27;
113109
modifiableStops[5].value.alpha = 0.0;
114110

115-
canvas.setBrushGradient(
116-
{transform(line.begin), transform(line.end)},
117-
gradient);
111+
canvas.setBrushGradient(transform(line), gradient);
118112
}
119113

120114
void DrawLegend::drawTitle(const Info &info) const
@@ -136,7 +130,7 @@ void DrawLegend::drawTitle(const Info &info) const
136130
*events.title,
137131
Events::Targets::legendTitle(title.value,
138132
info.type,
139-
info.yOverflow),
133+
info.properties),
140134
{.alpha = title.weight * info.weight * mul});
141135
});
142136
}
@@ -187,7 +181,7 @@ void DrawLegend::drawDimension(const Info &info) const
187181
value.second.categoryValue,
188182
value.second.categoryValue,
189183
info.type,
190-
info.yOverflow),
184+
info.properties),
191185
{.alpha =
192186
double{
193187
alpha
@@ -204,8 +198,8 @@ void DrawLegend::drawDimension(const Info &info) const
204198
Geom::Rect DrawLegend::getItemRect(const Info &info, double index)
205199
{
206200
Geom::Rect res = info.markerWindowRect;
207-
res.pos.y +=
208-
info.fadeHeight + index * info.itemHeight - info.yOffset;
201+
res.pos.y += info.fadeHeight + index * info.itemHeight
202+
- info.properties.scrollTop;
209203
res.size.y = info.itemHeight;
210204
if (std::signbit(res.size.x)) res.size.x = 0;
211205
return res;
@@ -254,7 +248,7 @@ void DrawLegend::drawMarker(const Info &info,
254248
Events::Targets::legendMarker(info.dimension.category,
255249
categoryValue,
256250
info.type,
257-
info.yOverflow);
251+
info.properties);
258252

259253
if (events.marker->invoke(
260254
Events::OnRectDrawEvent(*markerElement, {rect, false}))) {
@@ -315,7 +309,7 @@ void DrawLegend::extremaLabel(const Info &info,
315309
*events.label,
316310
Events::Targets::measLegendLabel(text,
317311
info.type,
318-
info.yOverflow),
312+
info.properties),
319313
{.alpha = info.measureWeight * plusWeight});
320314
}
321315

@@ -354,7 +348,7 @@ void DrawLegend::colorBar(const Info &info,
354348

355349
auto barElement =
356350
Events::Targets::legendBar(Gen::ChannelId::color,
357-
info.yOverflow);
351+
info.properties);
358352

359353
if (events.bar->invoke(
360354
Events::OnRectDrawEvent(*barElement, {rect, false}))) {
@@ -387,7 +381,7 @@ void DrawLegend::lightnessBar(const Info &info,
387381

388382
auto barElement =
389383
Events::Targets::legendBar(Gen::ChannelId::lightness,
390-
info.yOverflow);
384+
info.properties);
391385

392386
if (events.bar->invoke(
393387
Events::OnRectDrawEvent(*barElement, {rect, false}))) {
@@ -409,7 +403,7 @@ void DrawLegend::sizeBar(const Info &info,
409403
info.canvas.setLineWidth(0);
410404

411405
auto barElement = Events::Targets::legendBar(Gen::ChannelId::size,
412-
info.yOverflow);
406+
info.properties);
413407

414408
if (events.bar->invoke(
415409
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,8 +42,6 @@ class DrawLegend : public DrawingContext
4242
Geom::Rect titleRect;
4343
Geom::Rect markerWindowRect;
4444
double fadeHeight{};
45-
double yOverflow{};
46-
double yOffset{};
4745
Gen::ChannelId type{};
4846
double weight{};
4947
double itemHeight{};
@@ -53,6 +51,7 @@ class DrawLegend : public DrawingContext
5351
double measureEnabled = measure.enabled.calculate<double>();
5452
bool dimensionEnabled = dimension.enabled;
5553
double measureWeight = weight * measureEnabled;
54+
Events::Targets::LegendProperties properties{};
5655
ColorGradientSetter colorGradientSetter;
5756
};
5857

0 commit comments

Comments
 (0)