Skip to content

Commit edf5951

Browse files
committed
Add sort 'byLabel'
1 parent a405f53 commit edf5951

File tree

8 files changed

+42
-14
lines changed

8 files changed

+42
-14
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
### Added
3535

36+
- Add new sorting strategy: 'byLabel'.
3637
- Add spacing property for plot axis style structure.
3738

3839
### Changed

src/apps/weblib/typeschema-api/config.yaml

+4-1
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,11 @@ definitions:
114114
appear in the data set.
115115
- 'byValue': markers will be sorted by the corresponding measure (if present)
116116
in decreasing order.
117+
- 'byLabel': markers will be sorted by the corresponding shown categories
118+
(if present) in natural order (ASCII case-insensitive, space-insensitive
119+
alphabetical order and numbers are compared by value).
117120
type: string
118-
enum: [none, byValue]
121+
enum: [none, byValue, byLabel]
119122
reverse:
120123
description: Reverts the order of the markers if set.
121124
type: boolean

src/chart/generator/plotbuilder.cpp

+29-7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "base/math/floating.h"
1919
#include "base/math/range.h"
2020
#include "base/refl/auto_enum.h"
21+
#include "base/text/naturalcmp.h"
2122
#include "chart/main/style.h"
2223
#include "chart/options/align.h"
2324
#include "chart/options/channel.h"
@@ -135,19 +136,40 @@ std::vector<PlotBuilder::BucketInfo> PlotBuilder::sortedBuckets(
135136
{},
136137
&BucketInfo::index);
137138
if (it == sorted.end() || it->index != idx.itemId)
138-
it = sorted.emplace(it, idx.itemId, 0.0);
139+
it = sorted.emplace(it,
140+
idx.itemId,
141+
0.0,
142+
(marker.*buckets.marker_id_get).label
143+
? &(marker.*buckets.marker_id_get)
144+
.label->value
145+
: nullptr);
139146

140147
it->size += marker.size.getCoord(
141148
!plot->getOptions()->getOrientation());
142149
}
143150

144-
if (plot->getOptions()->getChannels().axisPropsAt(axisIndex).sort
145-
== Sort::byValue)
151+
switch (plot->getOptions()
152+
->getChannels()
153+
.axisPropsAt(axisIndex)
154+
.sort) {
155+
case Sort::byValue:
146156
std::ranges::stable_sort(sorted,
147157
[](const BucketInfo &lhs, const BucketInfo &rhs)
148158
{
149159
return Math::Floating::less(lhs.size, rhs.size);
150160
});
161+
break;
162+
case Sort::byLabel:
163+
std::ranges::stable_sort(sorted,
164+
[](const BucketInfo &lhs, const BucketInfo &rhs)
165+
{
166+
if (rhs.label == nullptr || lhs.label == nullptr)
167+
return lhs.label != nullptr;
168+
return Text::NaturalCmp{}(*lhs.label, *rhs.label);
169+
});
170+
break;
171+
default: break;
172+
}
151173

152174
if (plot->getOptions()
153175
->getChannels()
@@ -440,10 +462,10 @@ void PlotBuilder::calcAxis(const Data::DataTable &dataTable,
440462
}
441463
else {
442464
for (auto merge =
443-
plot->getOptions()->dimLabelIndex(+type) == 0
444-
&& (type != plot->getOptions()->mainAxisType()
445-
|| axisProps.sort != Sort::byValue
446-
|| scale.dimensions().size() == 1);
465+
axisProps.sort == Sort::byLabel
466+
|| (plot->getOptions()->dimLabelIndex(+type) == 0
467+
&& (axisProps.sort == Sort::none
468+
|| scale.dimensions().size() == 1));
447469
const auto &marker : plot->markers) {
448470
if (!marker.enabled) continue;
449471

src/chart/generator/plotbuilder.h

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class PlotBuilder
3131
{
3232
std::size_t index{};
3333
double size{};
34+
const std::string *label{};
3435
};
3536

3637
void initDimensionTrackers();

src/chart/options/sort.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace Vizzu::Gen
77
{
88

9-
enum class Sort : std::uint8_t { none, byValue };
9+
enum class Sort : std::uint8_t { none, byValue, byLabel };
1010

1111
}
1212

test/e2e/tests/fixes.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"refs": ["00f01d9"]
3030
},
3131
"319": {
32-
"refs": ["7991f1b"]
32+
"refs": ["f237567"]
3333
},
3434
"320": {
3535
"refs": ["e4b8a2f"]

test/e2e/tests/fixes/319.mjs

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ const testSteps = [
55
series: [
66
{
77
name: 'Foo',
8-
values: ['Alice', 'Bob', 'Ted', 'Alice', 'Bob', 'Ted', 'Alice']
8+
values: ['Ted', 'Alice', 'Bob', 'Ted', 'Alice', 'Bob', 'Ted']
99
},
1010
{ name: 'Foo2', values: ['A', 'A', 'A', 'B', 'B', 'B', 'A'] },
1111
{ name: 'Foo3', values: ['X', 'Y', 'Z', 'X', 'Y', 'Z', 'Y'] },
12-
{ name: 'Bar', values: [15, 32, 12, 23, 41, 31, 1] }
12+
{ name: 'Bar', values: [23, 32, 12, 15, 41, 31, 1] }
1313
]
1414
}
1515
}),
@@ -30,7 +30,8 @@ const testSteps = [
3030
}),
3131
(chart) =>
3232
chart.animate({
33-
x: { sort: 'none' }
33+
x: { set: ['Foo2', 'Foo'], labelLevel: 1, sort: 'byLabel' },
34+
y: { reverse: true }
3435
})
3536
]
3637

tools/ci/type/gen-presets.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ function genSchema(presets) {
100100
},
101101
sort: {
102102
type: 'string',
103-
enum: ['none', 'byValue']
103+
enum: ['none', 'byValue', 'byLabel']
104104
}
105105
}
106106
}

0 commit comments

Comments
 (0)