Skip to content

Commit dc5aaf1

Browse files
authored
Merge pull request #637 from vizzuhq/stretch_markerlabel_percent
Stretch markerlabel percent
2 parents f8d8d69 + 70847e7 commit dc5aaf1

File tree

9 files changed

+156
-10
lines changed

9 files changed

+156
-10
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
## [0.16.3] - 2025-04-28
6+
7+
- Added new (development) style parameter: 'plot.marker.label.unit' with options 'original' (default) and 'percent'.
8+
59
## [0.16.2] - 2025-04-03
610

711
### Fixed

src/chart/animator/styles.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ void StyleMorphFactory::operator()(const T &source,
6262
template <typename T, typename PT>
6363
requires(std::is_same_v<PT, Text::NumberFormat>
6464
|| std::is_same_v<PT, Text::NumberScale>
65-
|| std::is_same_v<PT, Styles::MarkerLabel::Format>)
65+
|| std::is_same_v<PT, Styles::MarkerLabel::Format>
66+
|| std::is_same_v<PT, Styles::MarkerLabel::Unit>)
6667
void StyleMorphFactory::operator()(const T &, const T &, T &) const
6768
{}
6869

src/chart/animator/styles.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ class StyleMorphFactory
8484
template <typename T, typename PT = Style::ParamT<T>>
8585
requires(std::is_same_v<PT, Text::NumberFormat>
8686
|| std::is_same_v<PT, Text::NumberScale>
87-
|| std::is_same_v<PT, Styles::MarkerLabel::Format>)
87+
|| std::is_same_v<PT, Styles::MarkerLabel::Format>
88+
|| std::is_same_v<PT, Styles::MarkerLabel::Unit>)
8889
void operator()(const T &, const T &, T &) const;
8990

9091
private:

src/chart/generator/plotbuilder.cpp

+34-6
Original file line numberDiff line numberDiff line change
@@ -411,11 +411,22 @@ void PlotBuilder::calcLegendAndLabel(const Data::DataTable &dataTable)
411411
if (auto &&meas = plot->getOptions()
412412
->getChannels()
413413
.at(ChannelId::label)
414-
.measure())
414+
.measure()) {
415+
auto markerLabelsUnitPercent =
416+
plot->getStyle().plot.marker.label.unit
417+
== Styles::MarkerLabel::Unit::percent
418+
&& plot->getOptions()->align == Base::Align::Type::stretch
419+
&& plot->getOptions()->labelSeries(
420+
plot->getOptions()->subAxisType())
421+
== *meas
422+
&& !plot->getOptions()->isSplit();
415423
plot->axises.label = {
416-
::Anim::String{
417-
std::string{dataTable.getUnit(meas->getColIndex())}},
424+
::Anim::String{std::string{
425+
markerLabelsUnitPercent
426+
? "%"
427+
: dataTable.getUnit(meas->getColIndex())}},
418428
::Anim::String{meas->getColIndex()}};
429+
}
419430
}
420431

421432
void PlotBuilder::calcAxis(const Data::DataTable &dataTable,
@@ -500,6 +511,19 @@ void PlotBuilder::addAlignment(const Buckets &subBuckets) const
500511
}
501512

502513
auto &&subAxis = plot->getOptions()->subAxisType();
514+
515+
auto &&subAxisLabel = plot->getOptions()->labelSeries(subAxis);
516+
auto markerLabelsUnitPercent =
517+
plot->getStyle().plot.marker.label.unit
518+
== Styles::MarkerLabel::Unit::percent
519+
&& plot->getOptions()->align == Base::Align::Type::stretch
520+
&& subAxisLabel.has_value()
521+
&& subAxisLabel
522+
== plot->getOptions()
523+
->getChannels()
524+
.at(ChannelId::label)
525+
.measure();
526+
503527
const Base::Align align{plot->getOptions()->align, {0.0, 1.0}};
504528
for (auto &&bucket : subBuckets) {
505529
Math::Range<> range;
@@ -510,9 +534,13 @@ void PlotBuilder::addAlignment(const Buckets &subBuckets) const
510534

511535
auto &&transform = align.getAligned(range) / range;
512536

513-
for (auto &&[marker, idx] : bucket)
514-
marker.setSizeBy(subAxis,
515-
marker.getSizeBy(subAxis) * transform);
537+
for (auto &&[marker, idx] : bucket) {
538+
auto &&newRange = marker.getSizeBy(subAxis) * transform;
539+
marker.setSizeBy(subAxis, newRange);
540+
if (markerLabelsUnitPercent)
541+
marker.label->value.value.emplace(
542+
newRange.size() * 100);
543+
}
516544
}
517545
}
518546

src/chart/main/style.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ Chart Chart::def()
185185
{
186186
.position = Anim::Interpolated<MarkerLabel::Position>(MarkerLabel::Position::center),
187187
.filter = Gfx::ColorTransform::Lightness(0),
188-
.format = MarkerLabel::Format::measureFirst
188+
.format = MarkerLabel::Format::measureFirst,
189+
.unit = MarkerLabel::Unit::original
189190
}
190191
}
191192
}

src/chart/main/style.h

+2
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,12 @@ struct MarkerLabelParams
261261
measureFirst,
262262
dimensionsFirst
263263
};
264+
enum class Unit : std::uint8_t { original, percent };
264265

265266
Param<::Anim::Interpolated<Position>> position;
266267
Param<Gfx::ColorTransform> filter;
267268
Param<Format> format;
269+
Param<Unit> unit;
268270
};
269271

270272
struct MarkerLabel : OrientedLabel, MarkerLabelParams

src/chart/main/version.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
#include "base/app/version.h"
44

5-
const App::Version Vizzu::Main::version(0, 16, 2);
5+
const App::Version Vizzu::Main::version(0, 16, 3);
66

77
const char *const Vizzu::Main::siteUrl = "https://vizzu.io/";

test/e2e/tests/style_tests.json

+3
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,9 @@
10201020
},
10211021
"legend/offsetY": {
10221022
"refs": ["b326287"]
1023+
},
1024+
"plot/markerLabelUnit": {
1025+
"refs": ["ae0f5f5"]
10231026
}
10241027
}
10251028
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
const testSteps = [
2+
(chart) => {
3+
const data = {
4+
series: [
5+
{
6+
name: 'Foo',
7+
values: [
8+
'A',
9+
'B',
10+
'C',
11+
'A',
12+
'B',
13+
'C',
14+
'A',
15+
'B',
16+
'C',
17+
'A',
18+
'B',
19+
'C',
20+
'A',
21+
'B',
22+
'C',
23+
'B',
24+
'B',
25+
'B',
26+
'B'
27+
]
28+
},
29+
{
30+
name: 'Foo2',
31+
values: [
32+
'1',
33+
'1',
34+
'1',
35+
'2',
36+
'2',
37+
'2',
38+
'3',
39+
'3',
40+
'3',
41+
'4',
42+
'4',
43+
'4',
44+
'5',
45+
'5',
46+
'5',
47+
'1',
48+
'2',
49+
'3',
50+
'4'
51+
]
52+
},
53+
{
54+
name: 'Foo3',
55+
values: [
56+
'0',
57+
'1',
58+
'0',
59+
'2',
60+
'3',
61+
'2',
62+
'0',
63+
'1',
64+
'0',
65+
'2',
66+
'3',
67+
'2',
68+
'0',
69+
'1',
70+
'0',
71+
'4',
72+
'4',
73+
'4',
74+
'4'
75+
]
76+
},
77+
{
78+
name: 'Bar',
79+
values: [NaN, 1, NaN, 1, 2, 1, NaN, 1, NaN, 1, 2, 1, NaN, 1, NaN, 0, 0, 0, 0]
80+
},
81+
{ name: 'Bar2', values: [2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 4, 2, 4, 2] }
82+
]
83+
}
84+
85+
chart.feature('tooltip', true)
86+
return chart.animate({ data })
87+
},
88+
(chart) =>
89+
chart.animate({
90+
config: {
91+
x: ['Foo2'],
92+
y: ['Bar2', 'Foo3'],
93+
color: ['Foo3'],
94+
label: ['Bar2'],
95+
align: 'stretch'
96+
}
97+
}),
98+
(chart) =>
99+
chart.animate({
100+
style: {
101+
'plot.marker.label.unit': 'percent'
102+
}
103+
})
104+
]
105+
106+
export default testSteps

0 commit comments

Comments
 (0)