Skip to content

Commit e46e2e3

Browse files
authored
Merge pull request #593 from vizzuhq/expose_data_points
Add marker top position to draw event
2 parents 9550dd3 + 3182d97 commit e46e2e3

File tree

12 files changed

+104
-35
lines changed

12 files changed

+104
-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 and center position to draw event
1720

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

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,19 @@ 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+
center: Point
145+
}
146+
140147
/** Plot marker element of the chart representing a data point. */
141148
export interface Marker extends Element {
142149
tagName: 'plot-marker'
143150
categories: Data.Record
144151
values: Data.Record
152+
position: MarkerPosition
145153
/** Unique index of the marker. */
146154
index: string
147155
}

src/chart/main/events.h

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

251-
explicit Marker(const Gen::Marker &marker) :
256+
explicit Marker(const Gen::Marker &marker,
257+
const DataPosition &position) :
252258
Element("plot-marker"),
253-
marker(marker)
259+
marker(marker),
260+
position(position)
254261
{}
255262

256263
void appendToJSON(Conv::JSONObj &&jsonObj) const override
257264
{
258-
Element::appendToJSON(
259-
marker.appendToJSON(std::move(jsonObj)));
265+
Element::appendToJSON(marker.appendToJSON(
266+
std::move(jsonObj))("position", position));
260267
}
261268
};
262269

@@ -268,8 +275,10 @@ class Events
268275
{
269276
bool horizontal;
270277

271-
MarkerGuide(const Gen::Marker &marker, bool horizontal) :
272-
MarkerChild("guide", marker),
278+
MarkerGuide(const Gen::Marker &marker,
279+
const Marker::DataPosition &position,
280+
bool horizontal) :
281+
MarkerChild("guide", marker, position),
273282
horizontal(horizontal)
274283
{}
275284

@@ -313,15 +322,19 @@ class Events
313322
return std::make_unique<Legend>(properties);
314323
}
315324

316-
static auto marker(const Gen::Marker &marker)
325+
static auto marker(const Gen::Marker &marker,
326+
const Marker::DataPosition &position)
317327
{
318-
return std::make_unique<Marker>(marker);
328+
return std::make_unique<Marker>(marker, position);
319329
}
320330

321331
static auto markerGuide(const Gen::Marker &marker,
332+
const Marker::DataPosition &position,
322333
bool horizontal)
323334
{
324-
return std::make_unique<MarkerGuide>(marker, horizontal);
335+
return std::make_unique<MarkerGuide>(marker,
336+
position,
337+
horizontal);
325338
}
326339

327340
static auto root()
@@ -362,11 +375,13 @@ class Events
362375
}
363376

364377
static auto markerLabel(const std::string &label,
365-
const Gen::Marker &marker)
378+
const Gen::Marker &marker,
379+
const Marker::DataPosition &position)
366380
{
367381
return std::make_unique<Text<MarkerChild>>(label,
368382
"label",
369-
marker);
383+
marker,
384+
position);
370385
}
371386

372387
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: 12 additions & 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:
@@ -132,6 +133,17 @@ AbstractMarker AbstractMarker::createInterpolated(
132133
return aMarker;
133134
}
134135

136+
void AbstractMarker::setDataPosition(const CoordinateSystem &coordSys)
137+
{
138+
dataPosition = {
139+
this->getLabelPos(Styles::MarkerLabel::Position::top,
140+
coordSys)
141+
.end,
142+
this->getLabelPos(Styles::MarkerLabel::Position::center,
143+
coordSys)
144+
.begin};
145+
}
146+
135147
Geom::Rect AbstractMarker::getBoundary() const
136148
{
137149
return Geom::Rect::Boundary(points);

src/chart/rendering/markers/abstractmarker.h

Lines changed: 3 additions & 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;
@@ -51,6 +52,8 @@ class AbstractMarker
5152
const Gen::Marker &marker,
5253
const Gen::ShapeType &shapeType,
5354
::Anim::InterpolateIndex lineIndex);
55+
56+
void setDataPosition(const CoordinateSystem &coordSys);
5457
};
5558

5659
class SingleDrawMarker : public AbstractMarker

src/chart/rendering/markers/circlemarker.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ CircleMarker::CircleMarker(const Gen::Marker &marker,
3838
dataRect.pos = pos;
3939
dataRect.size = Geom::Size();
4040
radius = fabs(coordSys.verConvert(r));
41+
42+
setDataPosition(coordSys);
4143
}
4244

4345
}

src/chart/rendering/markers/connectingmarker.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ 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+
setDataPosition(ctx.coordSys);
161163
}
162164

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

src/chart/rendering/markers/rectanglemarker.cpp

Lines changed: 3 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,8 @@ 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+
setDataPosition(coordSys);
8689
}
8790

8891
}

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)