Skip to content

Commit a3bca64

Browse files
authored
Merge pull request #527 from vizzuhq/spec_common_base
Move bubble chart and treemap builders to common base
2 parents 8ca16b1 + 29b971b commit a3bca64

File tree

7 files changed

+85
-73
lines changed

7 files changed

+85
-73
lines changed

src/chart/generator/plot.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ void Plot::addSpecLayout(Buckets &buckets)
237237
keepAspectRatio = true;
238238
}
239239
else
240-
Charts::TreeMap::setupVector(buckets);
240+
Charts::TreeMapBuilder::setupVector(buckets);
241241
}
242242
}
243243

src/chart/speclayout/bubblechart.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Vizzu::Charts
77
{
88

99
BubbleChart::BubbleChart(const std::vector<double> &circleAreas,
10-
const Geom::Rect &rect)
10+
const SpecMarker *parent)
1111
{
1212
markers.reserve(circleAreas.size());
1313

@@ -19,7 +19,8 @@ BubbleChart::BubbleChart(const std::vector<double> &circleAreas,
1919
std::sort(markers.begin(), markers.end(), SpecMarker::sizeOrder);
2020

2121
generate();
22-
normalize(rect);
22+
normalize(parent ? parent->circle().boundary()
23+
: Geom::Rect{{}, Geom::Size{1, 1}});
2324

2425
std::sort(markers.begin(), markers.end(), SpecMarker::indexOrder);
2526
}

src/chart/speclayout/bubblechart.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ class BubbleChart
1919
Markers markers;
2020

2121
explicit BubbleChart(const std::vector<double> &circleAreas,
22-
const Geom::Rect &rect = Geom::Rect(Geom::Point{0, 0},
23-
Geom::Size{1, 1}));
22+
const SpecMarker *parent = nullptr);
2423

2524
private:
2625
void generate();

src/chart/speclayout/bubblechartbuilder.h

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
#include <cmath>
66

77
#include "bubblechart.h"
8+
#include "sizedependentlayout.h"
89

910
namespace Vizzu::Charts
1011
{
1112

12-
class BubbleChartBuilder
13+
class BubbleChartBuilder : SizeDependentLayout<BubbleChart>
1314
{
1415
public:
1516
template <typename Hierarchy>
@@ -21,36 +22,18 @@ template <typename Hierarchy>
2122
void BubbleChartBuilder::setupVector(double maxRadius,
2223
const Hierarchy &hierarchy)
2324
{
24-
if (hierarchy.empty()) return;
25-
26-
std::vector<double> sizes(hierarchy.size());
27-
for (std::size_t ix{}; const auto &level : hierarchy)
28-
for (auto &sum = sizes[ix++]; const auto &item : level)
29-
if (auto &&size = item->sizeFactor;
30-
std::isfinite(size) && size > 0)
31-
sum += size;
32-
33-
const BubbleChart chart(sizes);
34-
35-
std::vector<double> ssizes(hierarchy.inner_size());
36-
for (std::size_t cnt{}; const auto &level : hierarchy) {
37-
for (std::size_t ix{}; const auto &item : level)
38-
ssizes[ix++] = item->sizeFactor;
39-
40-
const BubbleChart subChart(ssizes,
41-
chart.markers[cnt++].circle().boundary());
42-
43-
for (std::size_t subCnt{}; const auto &item : level)
44-
if (const auto &[center, r] =
45-
subChart.markers[subCnt++].circle();
46-
std::isnan(r))
47-
item->enabled = false;
48-
else {
49-
item->position = center;
50-
item->size = Geom::Size{r, r};
51-
item->sizeFactor = r * r / (maxRadius * maxRadius);
52-
}
53-
}
25+
SizeDependentLayout::setupVector(hierarchy,
26+
[&maxRadius](auto *item, const SpecMarker &marker)
27+
{
28+
if (const auto &[center, r] = marker.circle();
29+
std::isnan(r))
30+
item->enabled = false;
31+
else {
32+
item->position = center;
33+
item->size = Geom::Size{r, r};
34+
item->sizeFactor = r * r / (maxRadius * maxRadius);
35+
}
36+
});
5437
}
5538

5639
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// Created by bela on 2024.05.17..
3+
//
4+
5+
#ifndef SIZEDEPENDENTLAYOUT_H
6+
#define SIZEDEPENDENTLAYOUT_H
7+
8+
namespace Vizzu::Charts
9+
{
10+
template <class ChartType> struct SizeDependentLayout
11+
{
12+
template <class Hierarchy, class AfterMarkerSetter>
13+
static void setupVector(const Hierarchy &hierarchy,
14+
AfterMarkerSetter &&afterMarkerSetter)
15+
{
16+
if (hierarchy.empty()) return;
17+
18+
std::vector<double> sizes(hierarchy.size());
19+
for (std::size_t ix{}; const auto &level : hierarchy)
20+
for (auto &sum = sizes[ix++]; const auto &item : level)
21+
if (auto &&size = item->sizeFactor;
22+
std::isfinite(size) && size > 0)
23+
sum += size;
24+
25+
const ChartType chart(sizes);
26+
27+
std::vector<double> ssizes(hierarchy.inner_size());
28+
29+
for (auto it = chart.markers.data();
30+
const auto &level : hierarchy) {
31+
for (std::size_t ix{}; const auto &item : level)
32+
ssizes[ix++] = item->sizeFactor;
33+
34+
const ChartType subChart(ssizes, it++);
35+
36+
for (std::size_t subCnt{}; const auto &item : level)
37+
afterMarkerSetter(item, subChart.markers[subCnt++]);
38+
}
39+
}
40+
};
41+
}
42+
43+
#endif // SIZEDEPENDENTLAYOUT_H

src/chart/speclayout/treemap.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ namespace Vizzu::Charts
88
{
99

1010
TreeMap::TreeMap(const std::vector<double> &sizes,
11-
const Geom::Point &p0,
12-
const Geom::Point &p1)
11+
const SpecMarker *parent)
1312
{
1413
markers.reserve(sizes.size());
1514

@@ -24,7 +23,11 @@ TreeMap::TreeMap(const std::vector<double> &sizes,
2423
while (start != markers.end() && !std::isfinite(start->size()))
2524
start++->emplaceRect({0, 0}, {0, 0});
2625

27-
divide(start, markers.end(), p0, p1);
26+
divide(start,
27+
markers.end(),
28+
parent ? parent->rect().pos : Geom::Point{0, 1},
29+
parent ? parent->rect().topRight() : Geom::Point{1, 0});
30+
2831
std::ranges::sort(markers, SpecMarker::indexOrder);
2932
}
3033

src/chart/speclayout/treemap.h

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "base/geom/rect.h"
99

10+
#include "sizedependentlayout.h"
1011
#include "specmarker.h"
1112

1213
namespace Vizzu::Charts
@@ -16,54 +17,36 @@ class TreeMap
1617
{
1718
public:
1819
explicit TreeMap(const std::vector<double> &sizes,
19-
const Geom::Point &p0 = Geom::Point{0, 1},
20-
const Geom::Point &p1 = Geom::Point{1, 0});
20+
const SpecMarker *parent = nullptr);
2121

22-
template <typename Hierarchy>
23-
static void setupVector(const Hierarchy &hierarchy);
22+
std::vector<SpecMarker> markers;
2423

2524
private:
2625
using It = std::vector<SpecMarker>::iterator;
2726

28-
std::vector<SpecMarker> markers;
29-
3027
static void divide(It begin,
3128
It end,
3229
const Geom::Point &p0,
3330
const Geom::Point &p1,
3431
bool horizontal = true);
3532
};
3633

37-
template <typename Hierarchy>
38-
void TreeMap::setupVector(const Hierarchy &hierarchy)
34+
struct TreeMapBuilder : SizeDependentLayout<TreeMap>
3935
{
40-
if (hierarchy.empty()) return;
41-
42-
std::vector<double> sizes(hierarchy.size());
43-
for (std::size_t ix{}; const auto &level : hierarchy)
44-
for (auto &sum = sizes[ix++]; const auto &item : level)
45-
if (auto &&size = item->sizeFactor;
46-
std::isfinite(size) && size > 0)
47-
sum += size;
48-
49-
TreeMap chart(sizes);
50-
51-
std::vector<double> in_sizes(hierarchy.inner_size());
52-
for (std::size_t cnt{}; const auto &level : hierarchy) {
53-
auto &&[pos, size] = chart.markers[cnt++].rect();
54-
55-
for (std::size_t ix{}; const auto &item : level)
56-
in_sizes[ix++] = item->sizeFactor;
57-
58-
TreeMap subChart(in_sizes, pos, pos + size);
36+
template <typename Hierarchy>
37+
static void setupVector(const Hierarchy &hierarchy);
38+
};
5939

60-
for (size_t subCnt{}; const auto &item : level) {
61-
auto &&[spos, ssize] =
62-
subChart.markers[subCnt++].rect().positive();
63-
item->position = spos + ssize;
64-
item->size = ssize;
65-
}
66-
}
40+
template <typename Hierarchy>
41+
void TreeMapBuilder::setupVector(const Hierarchy &hierarchy)
42+
{
43+
SizeDependentLayout::setupVector(hierarchy,
44+
[](auto *item, const SpecMarker &marker)
45+
{
46+
auto &&[spos, ssize] = marker.rect().positive();
47+
item->position = spos + ssize;
48+
item->size = ssize;
49+
});
6750
}
6851

6952
}

0 commit comments

Comments
 (0)