Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Axis refactor v12c - Add sort 'byLabel' #620

Open
wants to merge 2 commits into
base: axis_refactor_v12b
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
- Separate Channel properties to AxisChannel properties at config.
- Channels 'set' rewrite doesn't clear AxisChannel properties.

### Added

- Add new sorting strategy: 'byLabel'.

## [0.16.0] - 2024-11-28

### Fixed
Expand Down
5 changes: 4 additions & 1 deletion src/apps/weblib/typeschema-api/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,11 @@ definitions:
appear in the data set.
- 'byValue': markers will be sorted by the corresponding measure (if present)
in decreasing order.
- 'byLabel': markers will be sorted by the corresponding shown categories
(if present) in natural order (ASCII case-insensitive, space-insensitive
alphabetical order and numbers are compared by value).
type: string
enum: [none, byValue]
enum: [none, byValue, byLabel]
reverse:
description: Reverts the order of the markers if set.
type: boolean
Expand Down
36 changes: 29 additions & 7 deletions src/chart/generator/plotbuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "base/math/floating.h"
#include "base/math/range.h"
#include "base/refl/auto_enum.h"
#include "base/text/naturalcmp.h"
#include "chart/main/style.h"
#include "chart/options/align.h"
#include "chart/options/channel.h"
Expand Down Expand Up @@ -135,19 +136,40 @@ std::vector<PlotBuilder::BucketInfo> PlotBuilder::sortedBuckets(
{},
&BucketInfo::index);
if (it == sorted.end() || it->index != idx.itemId)
it = sorted.emplace(it, idx.itemId, 0.0);
it = sorted.emplace(it,
idx.itemId,
0.0,
(marker.*buckets.marker_id_get).label
? &(marker.*buckets.marker_id_get)
.label->value
: nullptr);

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

if (plot->getOptions()->getChannels().axisPropsAt(axisIndex).sort
== Sort::byValue)
switch (plot->getOptions()
->getChannels()
.axisPropsAt(axisIndex)
.sort) {
case Sort::byValue:
std::ranges::stable_sort(sorted,
[](const BucketInfo &lhs, const BucketInfo &rhs)
{
return Math::Floating::less(lhs.size, rhs.size);
});
break;
case Sort::byLabel:
std::ranges::stable_sort(sorted,
[](const BucketInfo &lhs, const BucketInfo &rhs)
{
if (rhs.label == nullptr || lhs.label == nullptr)
return lhs.label != nullptr;
return Text::NaturalCmp{}(*lhs.label, *rhs.label);
});
break;
default: break;
}

if (plot->getOptions()
->getChannels()
Expand Down Expand Up @@ -440,10 +462,10 @@ void PlotBuilder::calcAxis(const Data::DataTable &dataTable,
}
else {
for (auto merge =
plot->getOptions()->dimLabelIndex(+type) == 0
&& (type != plot->getOptions()->mainAxisType()
|| axisProps.sort != Sort::byValue
|| scale.dimensions().size() == 1);
axisProps.sort == Sort::byLabel
|| (plot->getOptions()->dimLabelIndex(+type) == 0
&& (axisProps.sort == Sort::none
|| scale.dimensions().size() == 1));
const auto &marker : plot->markers) {
if (!marker.enabled) continue;

Expand Down
1 change: 1 addition & 0 deletions src/chart/generator/plotbuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class PlotBuilder
{
std::size_t index{};
double size{};
const std::string *label{};
};

void initDimensionTrackers();
Expand Down
2 changes: 1 addition & 1 deletion src/chart/options/sort.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Vizzu::Gen
{

enum class Sort : std::uint8_t { none, byValue };
enum class Sort : std::uint8_t { none, byValue, byLabel };

}

Expand Down
2 changes: 1 addition & 1 deletion test/e2e/tests/fixes.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"refs": ["00f01d9"]
},
"319": {
"refs": ["7991f1b"]
"refs": ["f237567"]
},
"320": {
"refs": ["e4b8a2f"]
Expand Down
7 changes: 4 additions & 3 deletions test/e2e/tests/fixes/319.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ const testSteps = [
series: [
{
name: 'Foo',
values: ['Alice', 'Bob', 'Ted', 'Alice', 'Bob', 'Ted', 'Alice']
values: ['Ted', 'Alice', 'Bob', 'Ted', 'Alice', 'Bob', 'Ted']
},
{ name: 'Foo2', values: ['A', 'A', 'A', 'B', 'B', 'B', 'A'] },
{ name: 'Foo3', values: ['X', 'Y', 'Z', 'X', 'Y', 'Z', 'Y'] },
{ name: 'Bar', values: [15, 32, 12, 23, 41, 31, 1] }
{ name: 'Bar', values: [23, 32, 12, 15, 41, 31, 1] }
]
}
}),
Expand All @@ -30,7 +30,8 @@ const testSteps = [
}),
(chart) =>
chart.animate({
x: { sort: 'none' }
x: { set: ['Foo2', 'Foo'], labelLevel: 1, sort: 'byLabel' },
y: { reverse: true }
})
]

Expand Down
2 changes: 1 addition & 1 deletion tools/ci/type/gen-presets.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function genSchema(presets) {
},
sort: {
type: 'string',
enum: ['none', 'byValue']
enum: ['none', 'byValue', 'byLabel']
}
}
}
Expand Down