Skip to content

Commit fedb777

Browse files
committed
Add marker top position to draw event
1 parent 9550dd3 commit fedb777

File tree

12 files changed

+91
-35
lines changed

12 files changed

+91
-35
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
- Changed MarkerId to be a string instead of a number.
1616

17+
### Added
18+
19+
- Add marker top position to draw event
1720

1821
## [0.14.0] - 2024-10-03
1922

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,18 @@ export interface Logo extends Element {
137137
export interface Area extends Element {
138138
tagName: 'plot-area'
139139
}
140+
141+
/** Marker element position structure helper for tooltip */
142+
export interface MarkerPosition {
143+
top: Point
144+
}
145+
140146
/** Plot marker element of the chart representing a data point. */
141147
export interface Marker extends Element {
142148
tagName: 'plot-marker'
143149
categories: Data.Record
144150
values: Data.Record
151+
position: MarkerPosition
145152
/** Unique index of the marker. */
146153
index: string
147154
}

src/chart/main/events.h

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -247,16 +247,22 @@ class Events
247247
struct Marker : Element
248248
{
249249
const Gen::Marker ▮
250+
struct DataPosition
251+
{
252+
Geom::Point top;
253+
} position;
250254

251-
explicit Marker(const Gen::Marker &marker) :
255+
explicit Marker(const Gen::Marker &marker,
256+
const DataPosition &position) :
252257
Element("plot-marker"),
253-
marker(marker)
258+
marker(marker),
259+
position(position)
254260
{}
255261

256262
void appendToJSON(Conv::JSONObj &&jsonObj) const override
257263
{
258-
Element::appendToJSON(
259-
marker.appendToJSON(std::move(jsonObj)));
264+
Element::appendToJSON(marker.appendToJSON(
265+
std::move(jsonObj))("position", position));
260266
}
261267
};
262268

@@ -268,8 +274,10 @@ class Events
268274
{
269275
bool horizontal;
270276

271-
MarkerGuide(const Gen::Marker &marker, bool horizontal) :
272-
MarkerChild("guide", marker),
277+
MarkerGuide(const Gen::Marker &marker,
278+
const Marker::DataPosition &position,
279+
bool horizontal) :
280+
MarkerChild("guide", marker, position),
273281
horizontal(horizontal)
274282
{}
275283

@@ -313,15 +321,19 @@ class Events
313321
return std::make_unique<Legend>(properties);
314322
}
315323

316-
static auto marker(const Gen::Marker &marker)
324+
static auto marker(const Gen::Marker &marker,
325+
const Marker::DataPosition &position)
317326
{
318-
return std::make_unique<Marker>(marker);
327+
return std::make_unique<Marker>(marker, position);
319328
}
320329

321330
static auto markerGuide(const Gen::Marker &marker,
331+
const Marker::DataPosition &position,
322332
bool horizontal)
323333
{
324-
return std::make_unique<MarkerGuide>(marker, horizontal);
334+
return std::make_unique<MarkerGuide>(marker,
335+
position,
336+
horizontal);
325337
}
326338

327339
static auto root()
@@ -362,11 +374,13 @@ class Events
362374
}
363375

364376
static auto markerLabel(const std::string &label,
365-
const Gen::Marker &marker)
377+
const Gen::Marker &marker,
378+
const Marker::DataPosition &position)
366379
{
367380
return std::make_unique<Text<MarkerChild>>(label,
368381
"label",
369-
marker);
382+
marker,
383+
position);
370384
}
371385

372386
static auto dimLegendLabel(

src/chart/rendering/markerrenderer.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ void MarkerRenderer::drawLines(Gfx::ICanvas &canvas,
6767
const Geom::Line line(axisPoint, blended.center);
6868

6969
auto guideElement =
70-
Events::Targets::markerGuide(blended.marker, false);
70+
Events::Targets::markerGuide(blended.marker,
71+
blended.dataPosition,
72+
false);
7173

7274
if (rootEvents.draw.plot.marker.guide->invoke(
7375
Events::OnLineDrawEvent(*guideElement,
@@ -88,7 +90,9 @@ void MarkerRenderer::drawLines(Gfx::ICanvas &canvas,
8890
const Geom::Line line(center, axisPoint);
8991

9092
auto guideElement =
91-
Events::Targets::markerGuide(blended.marker, true);
93+
Events::Targets::markerGuide(blended.marker,
94+
blended.dataPosition,
95+
true);
9296

9397
if (rootEvents.draw.plot.marker.guide->invoke(
9498
Events::OnLineDrawEvent(*guideElement,
@@ -262,7 +266,8 @@ void MarkerRenderer::draw(Gfx::ICanvas &canvas,
262266
canvas.setLineWidth(*rootStyle.plot.marker.borderWidth);
263267

264268
auto markerElement =
265-
Events::Targets::marker(abstractMarker.marker);
269+
Events::Targets::marker(abstractMarker.marker,
270+
abstractMarker.dataPosition);
266271

267272
auto colorAlpha =
268273
Math::FuzzyBool::And<double>(abstractMarker.enabled, factor);
@@ -363,7 +368,9 @@ void MarkerRenderer::drawLabel(Gfx::ICanvas &canvas,
363368
Gfx::ColorTransform::OverrideColor(
364369
(*labelStyle.filter)(color)*colorAlpha),
365370
*rootEvents.draw.plot.marker.label,
366-
Events::Targets::markerLabel(text, marker));
371+
Events::Targets::markerLabel(text,
372+
marker,
373+
abstractMarker.dataPosition));
367374
}
368375

369376
std::string MarkerRenderer::getLabelText(

src/chart/rendering/markers/abstractmarker.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ AbstractMarker AbstractMarker::create(const DrawingContext &ctx,
4747
switch (shapeType) {
4848
case Gen::ShapeType::rectangle:
4949
return RectangleMarker(marker,
50+
ctx.coordSys,
5051
ctx.getOptions(),
5152
ctx.rootStyle);
5253
case Gen::ShapeType::circle:

src/chart/rendering/markers/abstractmarker.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class AbstractMarker
3535
Geom::Point center;
3636
Geom::Rect dataRect;
3737
double radius{};
38+
Events::Targets::Marker::DataPosition dataPosition;
3839

3940
[[nodiscard]] Geom::Rect getBoundary() const;
4041
[[nodiscard]] Geom::Line getLine() const;

src/chart/rendering/markers/circlemarker.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ CircleMarker::CircleMarker(const Gen::Marker &marker,
3838
dataRect.pos = pos;
3939
dataRect.size = Geom::Size();
4040
radius = fabs(coordSys.verConvert(r));
41+
42+
dataPosition = {
43+
this->getLabelPos(Styles::MarkerLabel::Position::top,
44+
coordSys)
45+
.end};
4146
}
4247

4348
}

src/chart/rendering/markers/connectingmarker.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ ConnectingMarker::ConnectingMarker(const DrawingContext &ctx,
158158

159159
dataRect.pos = isLine ? points[2] : points[1];
160160
dataRect.size = Geom::Size{points[2] - dataRect.pos};
161+
162+
dataPosition = {
163+
this->getLabelPos(Styles::MarkerLabel::Position::top,
164+
ctx.coordSys)
165+
.end};
161166
}
162167

163168
const Gen::Marker *ConnectingMarker::getPrev(

src/chart/rendering/markers/rectanglemarker.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace Vizzu::Draw
1616
{
1717

1818
RectangleMarker::RectangleMarker(const Gen::Marker &marker,
19+
const CoordinateSystem &coordSys,
1920
const Gen::Options &options,
2021
const Styles::Chart &style) :
2122
SingleDrawMarker(marker, options, Gen::ShapeType::rectangle)
@@ -83,6 +84,11 @@ RectangleMarker::RectangleMarker(const Gen::Marker &marker,
8384
dataRect.pos = points[0];
8485
dataRect.size = Geom::Size{points[2] - points[0]};
8586
radius = 0;
87+
88+
dataPosition = {
89+
this->getLabelPos(Styles::MarkerLabel::Position::top,
90+
coordSys)
91+
.end};
8692
}
8793

8894
}

src/chart/rendering/markers/rectanglemarker.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class RectangleMarker : public SingleDrawMarker
1010
{
1111
public:
1212
RectangleMarker(const Gen::Marker &marker,
13+
const CoordinateSystem &coordSys,
1314
const Gen::Options &options,
1415
const Styles::Chart &style);
1516
};

0 commit comments

Comments
 (0)