Skip to content

Commit 98efd39

Browse files
authored
Merge pull request #241 from simzer/drawingcontext
Legend derived from DrawingContext.
2 parents 3a6253b + ac2a201 commit 98efd39

File tree

10 files changed

+79
-98
lines changed

10 files changed

+79
-98
lines changed

src/chart/main/chart.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,28 +105,24 @@ void Chart::draw(Gfx::ICanvas &canvas) const
105105
if (actPlot
106106
&& (!events.draw.begin
107107
|| events.draw.begin->invoke(
108-
Util::EventDispatcher::Params{}))) {
108+
Util::EventDispatcher::Params{})))
109+
{
110+
Draw::DrawingContext context(canvas, layout, events.draw, *actPlot);
111+
109112
Draw::drawBackground(
110113
layout.boundary.outline(Geom::Size::Square(1)),
111114
canvas,
112115
actPlot->getStyle(),
113116
events.draw.background,
114117
Events::OnRectDrawParam(""));
115118

116-
Draw::drawPlot(layout.plot,
117-
*actPlot,
118-
canvas,
119-
actPlot->getStyle(),
120-
events.draw);
119+
Draw::drawPlot drawPlot(context);
121120

122121
actPlot->getOptions()->legend.visit(
123122
[&](int, const auto &legend)
124123
{
125124
if (legend.value)
126-
Draw::drawLegend(layout.legend,
127-
*actPlot,
128-
events.draw.legend,
129-
canvas,
125+
Draw::drawLegend(context,
130126
*legend.value,
131127
legend.weight);
132128
});

src/chart/rendering/drawaxes.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void drawAxes::drawAxis(Gen::ChannelId axisIndex)
5959
const char *element =
6060
axisIndex == Gen::ChannelId::x ? "plot.xAxis" : "plot.yAxis";
6161

62-
auto lineBaseColor = *style.plot.getAxis(axisIndex).color
62+
auto lineBaseColor = *rootStyle.plot.getAxis(axisIndex).color
6363
* static_cast<double>(plot.anyAxisSet);
6464

6565
if (lineBaseColor.alpha <= 0) return;
@@ -73,7 +73,7 @@ void drawAxes::drawAxis(Gen::ChannelId axisIndex)
7373
canvas.setLineColor(lineColor);
7474
canvas.setLineWidth(1.0);
7575

76-
if (events.plot.axis.base->invoke(
76+
if (rootEvents.plot.axis.base->invoke(
7777
Events::OnLineDrawParam(element, line))) {
7878
painter.drawLine(line);
7979
}
@@ -86,7 +86,7 @@ Geom::Point drawAxes::getTitleBasePos(Gen::ChannelId axisIndex,
8686
typedef Styles::AxisTitle::Position Pos;
8787
typedef Styles::AxisTitle::VPosition VPos;
8888

89-
const auto &titleStyle = style.plot.getAxis(axisIndex).title;
89+
const auto &titleStyle = rootStyle.plot.getAxis(axisIndex).title;
9090

9191
double orthogonal;
9292

@@ -117,7 +117,7 @@ Geom::Point drawAxes::getTitleOffset(Gen::ChannelId axisIndex,
117117
int index,
118118
bool fades) const
119119
{
120-
const auto &titleStyle = style.plot.getAxis(axisIndex).title;
120+
const auto &titleStyle = rootStyle.plot.getAxis(axisIndex).title;
121121

122122
auto calcSide = [](int, auto side)
123123
{
@@ -161,7 +161,7 @@ void drawAxes::drawTitle(Gen::ChannelId axisIndex)
161161
? "plot.xAxis.title"
162162
: "plot.yAxis.title";
163163

164-
const auto &titleStyle = style.plot.getAxis(axisIndex).title;
164+
const auto &titleStyle = rootStyle.plot.getAxis(axisIndex).title;
165165

166166
auto fades = titleStyle.position->interpolates()
167167
|| titleStyle.vposition->interpolates()
@@ -240,7 +240,7 @@ void drawAxes::drawTitle(Gen::ChannelId axisIndex)
240240
drawLabel(Geom::Rect(Geom::Point(), size),
241241
title.value,
242242
titleStyle,
243-
events.plot.axis.title,
243+
rootEvents.plot.axis.title,
244244
std::move(param),
245245
canvas,
246246
drawLabel::Options(false, 1.0, upsideDown));
@@ -254,7 +254,7 @@ void drawAxes::drawDimensionLabels(bool horizontal)
254254
{
255255
auto axisIndex = horizontal ? Gen::ChannelId::x : Gen::ChannelId::y;
256256

257-
const auto &labelStyle = style.plot.getAxis(axisIndex).label;
257+
const auto &labelStyle = rootStyle.plot.getAxis(axisIndex).label;
258258

259259
auto textColor = *labelStyle.color;
260260
if (textColor.alpha == 0.0) return;
@@ -281,7 +281,7 @@ void drawAxes::drawDimensionLabel(bool horizontal,
281281
horizontal ? "plot.xAxis.label" : "plot.yAxis.label";
282282
auto &enabled = horizontal ? plot.guides.x : plot.guides.y;
283283
auto axisIndex = horizontal ? Gen::ChannelId::x : Gen::ChannelId::y;
284-
const auto &labelStyle = style.plot.getAxis(axisIndex).label;
284+
const auto &labelStyle = rootStyle.plot.getAxis(axisIndex).label;
285285
auto textColor = *labelStyle.color;
286286

287287
auto text = it->second.label;
@@ -331,7 +331,7 @@ void drawAxes::drawDimensionLabel(bool horizontal,
331331
text,
332332
posDir,
333333
labelStyle,
334-
events.plot.axis.label,
334+
rootEvents.plot.axis.label,
335335
std::move(Events::Events::OnTextDrawParam(element)),
336336
0,
337337
textColor * weight * position.weight,

src/chart/rendering/drawguides.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ void drawGuides::draw(bool horizontal)
1717
{
1818
auto axisId = horizontal ? Gen::ChannelId::x : Gen::ChannelId::y;
1919

20-
const auto &guideStyle = style.plot.getAxis(axisId).guides;
20+
const auto &guideStyle = rootStyle.plot.getAxis(axisId).guides;
2121

2222
auto baseColor = *guideStyle.color;
2323
if (baseColor.alpha == 0) return;
@@ -61,7 +61,7 @@ void drawGuides::drawGuide(bool horizontal,
6161

6262
canvas.setLineColor(color);
6363
Geom::Line line(relMax, relMax + normal);
64-
if (events.plot.axis.guide->invoke(
64+
if (rootEvents.plot.axis.guide->invoke(
6565
Events::OnLineDrawParam(element, line)))
6666
painter.drawLine(line);
6767
}

src/chart/rendering/drawingcontext.h

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "chart/generator/plot.h"
66
#include "chart/main/events.h"
77
#include "chart/main/style.h"
8+
#include "chart/main/layout.h"
89
#include "painter/coordinatesystem.h"
910
#include "painter/painter.h"
1011

@@ -16,24 +17,28 @@ namespace Draw
1617
class DrawingContext
1718
{
1819
public:
19-
DrawingContext(const Geom::Rect &rect,
20-
const Gen::Plot &plot,
20+
DrawingContext(
2121
Gfx::ICanvas &canvas,
22-
const Styles::Chart &style,
23-
const Events::Draw &events) :
22+
const Layout &layout,
23+
const Events::Draw &events,
24+
const Gen::Plot &plot) :
2425
plot(plot),
2526
canvas(canvas),
2627
painter(*static_cast<Painter *>(canvas.getPainter())),
2728
options(*plot.getOptions()),
28-
style(style),
29-
events(events),
30-
boundingRect(rect)
29+
rootStyle(plot.getStyle()),
30+
rootEvents(events),
31+
layout(layout)
3132
{
33+
auto plotArea = rootStyle.plot.contentRect
34+
(layout.plot, rootStyle.calculatedSize());
35+
3236
coordSys = CoordinateSystem(
33-
style.plot.contentRect(rect, style.calculatedSize()),
37+
plotArea,
3438
options.angle,
3539
options.polar,
36-
plot.keepAspectRatio);
40+
plot.keepAspectRatio
41+
);
3742

3843
painter.setCoordSys(coordSys);
3944
}
@@ -43,9 +48,9 @@ class DrawingContext
4348
Gfx::ICanvas &canvas;
4449
Painter &painter;
4550
const Gen::Options &options;
46-
const Styles::Chart &style;
47-
const Events::Draw &events;
48-
Geom::Rect boundingRect;
51+
const Styles::Chart &rootStyle;
52+
const Events::Draw &rootEvents;
53+
const Layout &layout;
4954
};
5055

5156
}

src/chart/rendering/drawinterlacing.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void drawInterlacing::draw(bool horizontal, bool text)
2424
auto axisIndex = horizontal ? Gen::ChannelId::y : Gen::ChannelId::x;
2525

2626
auto interlacingColor =
27-
*style.plot.getAxis(axisIndex).interlacing.color;
27+
*rootStyle.plot.getAxis(axisIndex).interlacing.color;
2828

2929
if (!text && interlacingColor.alpha <= 0.0) return;
3030

@@ -93,7 +93,7 @@ void drawInterlacing::draw(
9393

9494
auto axisIndex = horizontal ? Gen::ChannelId::y : Gen::ChannelId::x;
9595

96-
auto &axisStyle = style.plot.getAxis(axisIndex);
96+
auto &axisStyle = rootStyle.plot.getAxis(axisIndex);
9797

9898
const auto &axis = plot.axises.at(axisIndex);
9999

@@ -208,7 +208,7 @@ void drawInterlacing::draw(
208208
horizontal ? "plot.yAxis.interlacing"
209209
: "plot.xAxis.interlacing";
210210

211-
if (events.plot.axis.interlacing->invoke(
211+
if (rootEvents.plot.axis.interlacing->invoke(
212212
Events::OnRectDrawParam(element, rect))) {
213213
painter.drawPolygon(points);
214214
}
@@ -229,7 +229,7 @@ void drawInterlacing::drawDataLabel(
229229
const char *element =
230230
horizontal ? "plot.yAxis.label" : "plot.xAxis.label";
231231
auto axisIndex = horizontal ? Gen::ChannelId::y : Gen::ChannelId::x;
232-
auto &labelStyle = style.plot.getAxis(axisIndex).label;
232+
auto &labelStyle = rootStyle.plot.getAxis(axisIndex).label;
233233

234234
auto str = Text::SmartString::fromNumber(value,
235235
*labelStyle.numberFormat,
@@ -279,7 +279,7 @@ void drawInterlacing::drawDataLabel(
279279
str,
280280
posDir,
281281
labelStyle,
282-
events.plot.axis.label,
282+
rootEvents.plot.axis.label,
283283
std::move(Events::Events::OnTextDrawParam(element)),
284284
0,
285285
textColor * position.weight,
@@ -294,7 +294,7 @@ void drawInterlacing::drawSticks(double tickIntensity,
294294
const char *element =
295295
horizontal ? "plot.yAxis.tick" : "plot.xAxis.tick";
296296
auto axisIndex = horizontal ? Gen::ChannelId::y : Gen::ChannelId::x;
297-
auto &axisStyle = style.plot.getAxis(axisIndex);
297+
auto &axisStyle = rootStyle.plot.getAxis(axisIndex);
298298
const auto &tickStyle = axisStyle.ticks;
299299

300300
auto tickLength = tickStyle.length->get(
@@ -333,7 +333,7 @@ void drawInterlacing::drawSticks(double tickIntensity,
333333
}
334334
});
335335

336-
if (events.plot.axis.tick->invoke(
336+
if (rootEvents.plot.axis.tick->invoke(
337337
Events::OnLineDrawParam(element, tickLine))) {
338338
canvas.line(tickLine);
339339
}

src/chart/rendering/drawitem.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ void drawItem::drawLines(const Styles::Guide &style,
4242
canvas.setLineColor(lineColor);
4343
auto axisPoint = blended.center.xComp() + origo.yComp();
4444
Geom::Line line(axisPoint, blended.center);
45-
if (events.plot.marker.guide->invoke(
45+
if (rootEvents.plot.marker.guide->invoke(
4646
Events::OnLineDrawParam("plot.marker.guide.x",
4747
line,
4848
marker.idx))) {
@@ -58,7 +58,7 @@ void drawItem::drawLines(const Styles::Guide &style,
5858
canvas.setLineColor(lineColor);
5959
auto axisPoint = blended.center.yComp() + origo.xComp();
6060
Geom::Line line(blended.center, axisPoint);
61-
if (events.plot.marker.guide->invoke(
61+
if (rootEvents.plot.marker.guide->invoke(
6262
Events::OnLineDrawParam("plot.marker.guide.y",
6363
line,
6464
marker.idx))) {
@@ -181,7 +181,7 @@ void drawItem::draw(const DrawItem &drawItem,
181181
auto colors = getColor(drawItem, factor);
182182

183183
canvas.setLineColor(colors.first);
184-
canvas.setLineWidth(*style.plot.marker.borderWidth);
184+
canvas.setLineWidth(*rootStyle.plot.marker.borderWidth);
185185
canvas.setBrushColor(colors.second);
186186

187187
auto boundary = drawItem.getBoundary();
@@ -196,7 +196,7 @@ void drawItem::draw(const DrawItem &drawItem,
196196
auto p0 = coordSys.convert(line.begin);
197197
auto p1 = coordSys.convert(line.end);
198198

199-
if (events.plot.marker.base->invoke(
199+
if (rootEvents.plot.marker.base->invoke(
200200
Events::OnLineDrawParam("plot.marker",
201201
Geom::Line(p0, p1),
202202
drawItem.marker.idx))) {
@@ -208,7 +208,7 @@ void drawItem::draw(const DrawItem &drawItem,
208208
}
209209
}
210210
else {
211-
if (events.plot.marker.base->invoke(
211+
if (rootEvents.plot.marker.base->invoke(
212212
Events::OnRectDrawParam("plot.marker",
213213
rect,
214214
drawItem.marker.idx))) {
@@ -230,7 +230,7 @@ void drawItem::drawLabel(const DrawItem &drawItem, size_t index)
230230
auto text = getLabelText(index);
231231
if (text.empty()) return;
232232

233-
auto &labelStyle = style.plot.marker.label;
233+
auto &labelStyle = rootStyle.plot.marker.label;
234234

235235
auto labelPos = labelStyle.position->combine<Geom::Line>(
236236
[&](int, const auto &position)
@@ -250,7 +250,7 @@ void drawItem::drawLabel(const DrawItem &drawItem, size_t index)
250250
text,
251251
labelPos,
252252
labelStyle,
253-
events.plot.marker.label,
253+
rootEvents.plot.marker.label,
254254
std::move(param),
255255
centered,
256256
textColor,
@@ -259,7 +259,7 @@ void drawItem::drawLabel(const DrawItem &drawItem, size_t index)
259259

260260
std::string drawItem::getLabelText(size_t index) const
261261
{
262-
auto &labelStyle = style.plot.marker.label;
262+
auto &labelStyle = rootStyle.plot.marker.label;
263263
auto &values = marker.label.values;
264264

265265
auto needsInterpolation = marker.label.count == 2
@@ -318,14 +318,14 @@ std::pair<Gfx::Color, Gfx::Color> drawItem::getColor(
318318
{
319319
auto selectedColor = getSelectedColor();
320320

321-
auto borderAlpha = *style.plot.marker.borderOpacity;
322-
auto fillAlpha = *style.plot.marker.fillOpacity;
321+
auto borderAlpha = *rootStyle.plot.marker.borderOpacity;
322+
auto fillAlpha = *rootStyle.plot.marker.fillOpacity;
323323

324324
auto fakeBgColor =
325-
(*style.backgroundColor + *style.plot.backgroundColor)
325+
(*rootStyle.backgroundColor + *rootStyle.plot.backgroundColor)
326326
.transparent(1.0);
327327

328-
auto borderColor = style.plot.marker.borderOpacityMode->combine<
328+
auto borderColor = rootStyle.plot.marker.borderOpacityMode->combine<
329329
Gfx::Color>(
330330
[&](int, const auto &mode)
331331
{

src/chart/rendering/drawlegend.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,22 @@
88
using namespace Vizzu;
99
using namespace Vizzu::Draw;
1010

11-
drawLegend::drawLegend(const Geom::Rect &rect,
12-
const Gen::Plot &plot,
13-
const Events::Draw::Legend &events,
14-
Gfx::ICanvas &canvas,
11+
drawLegend::drawLegend(const DrawingContext &context,
1512
Gen::ChannelId channelType,
1613
double weight) :
17-
plot(plot),
18-
events(events),
19-
canvas(canvas),
14+
DrawingContext(context),
15+
events(context.rootEvents.legend),
16+
style(context.rootStyle.legend),
2017
type(channelType),
21-
weight(weight),
22-
style(plot.getStyle().legend)
18+
weight(weight)
2319
{
2420
contentRect =
25-
style.contentRect(rect, plot.getStyle().calculatedSize());
21+
style.contentRect(layout.legend,
22+
context.rootStyle.calculatedSize());
2623
itemHeight = drawLabel::getHeight(style.label, canvas);
2724
titleHeight = drawLabel::getHeight(style.title, canvas);
2825

29-
drawBackground(rect,
26+
drawBackground(layout.legend,
3027
canvas,
3128
style,
3229
events.background,

0 commit comments

Comments
 (0)