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 v11e - Cross interlacing, NaN handling, off dimLabel out #617

Merged
merged 7 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
- Enable dimension axis ticks and interlacing.
- Enable measure axis guides.
- Fix dimension axis guides on sorted chart.
- Fix NaN handling on axes and size measures other aggregators than sum.
- Add meaning to crossing interlacing.
- Do not draw dimension axis labels when the middle of the text is off the plot.

## [0.15.0] - 2024-10-28

Expand Down
4 changes: 3 additions & 1 deletion src/chart/generator/axis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,9 @@ bool DimensionAxis::setLabels(double step)
auto currStep = 0.0;

for (auto curr = int{}; auto &&item : sortedItems()) {
if (++curr <= currStep) continue;
if (auto mid = item.get().range.middle();
std::signbit(mid) || mid > 1.0 || ++curr <= currStep)
continue;
currStep += step;
item.get().label = true;
hasLabel = true;
Expand Down
3 changes: 2 additions & 1 deletion src/chart/generator/axis.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ struct ChannelStats

void track(ChannelId at, const double &value)
{
std::get<0>(tracked[at]).include(value);
if (std::isfinite(value))
std::get<0>(tracked[at]).include(value);
}

template <ChannelIdLike Id>
Expand Down
3 changes: 3 additions & 0 deletions src/chart/generator/marker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ Marker::Marker(const Options &options,
data.joinDimensionValues(labelChannel.dimensions(),
index)};
}

if (!std::isfinite(position.x) || !std::isfinite(position.y))
enabled = false;
}

bool Marker::connectMarkers(bool first,
Expand Down
16 changes: 11 additions & 5 deletions src/chart/generator/plotbuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,16 @@ PlotBuilder::PlotBuilder(const Data::DataTable &dataTable,
std::size_t mainBucketSize{};
auto &&subBuckets = generateMarkers(mainBucketSize);

if (!plot->getOptions()->getChannels().anyAxisSet())
if (!plot->getOptions()->getChannels().anyAxisSet()) {
addSpecLayout(subBuckets);
else
normalizeSizes();
}
else {
normalizeSizes();
addAxisLayout(subBuckets, mainBucketSize, dataTable);
}

normalizeColors();
normalizeSizes();
calcLegendAndLabel(dataTable);
}

Expand Down Expand Up @@ -551,8 +554,11 @@ void PlotBuilder::normalizeSizes()
|| plot->getOptions()->geometry == ShapeType::line) {
Math::Range<> size;

for (auto &marker : plot->markers)
if (marker.enabled) size.include(marker.sizeFactor);
for (auto &marker : plot->markers) {
if (std::isnan(marker.sizeFactor)) marker.enabled = false;
if (!marker.enabled) continue;
size.include(marker.sizeFactor);
}

size = plot->getOptions()
->getChannels()
Expand Down
3 changes: 2 additions & 1 deletion src/chart/rendering/drawaxes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ const DrawAxes &&DrawAxes::init() &&
!item.startPos.isAuto() && *item.startPos,
!item.endPos.isAuto() && *item.endPos,
axis.dimension.factor);
needSeparators && sepWeight > 0)
needSeparators && sepWeight > 0
&& item.range.getMin() > 0)
separators.emplace_back(item.range.getMin(),
sepWeight);
}
Expand Down
131 changes: 108 additions & 23 deletions src/chart/rendering/drawinterlacing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,37 @@ void DrawInterlacing::drawGeometries(Gen::AxisId axisIndex) const
|| guides.interlacings == false)
return;

std::map<double, double> othWeights{{0.0, 0.0}, {1.0, 0.0}};
schaumb marked this conversation as resolved.
Show resolved Hide resolved

const auto &othGuides = parent.plot->guides.at(!axisIndex);
const auto &othAxisStyle =
parent.rootStyle.plot.getAxis(!axisIndex);
if (!othAxisStyle.interlacing.color->isTransparent()
&& othGuides.interlacings != false)
for (const auto &othInterval :
parent.getIntervals(!axisIndex)) {
if (Math::Floating::is_zero(othInterval.isSecond))
continue;
auto min = std::max(othInterval.range.getMin(), 0.0);
auto max = std::min(othInterval.range.getMax(), 1.0);
auto mprev = std::prev(othWeights.upper_bound(min));
auto mnext = othWeights.lower_bound(max);

if (mprev->first < min)
mprev =
othWeights.try_emplace(mprev, min, mprev->second);
if (mnext->first > max)
mnext = othWeights.try_emplace(mnext,
max,
std::prev(mnext)->second);

while (mprev != mnext)
mprev++->second +=
Math::FuzzyBool::And<double>(othInterval.weight,
othInterval.isSecond,
othGuides.interlacings);
}

auto orientation = !+axisIndex;

parent.painter.setPolygonToCircleFactor(0);
Expand All @@ -48,30 +79,64 @@ void DrawInterlacing::drawGeometries(Gen::AxisId axisIndex) const
1.0,
Math::Floating::less)
- clippedBottom;
auto rect = Geom::Rect{
Geom::Point::Coord(orientation, 0.0, clippedBottom),
{Geom::Size::Coord(orientation, 1.0, clippedSize)}};

auto interlacingColor =
*axisStyle.interlacing.color
* Math::FuzzyBool::And<double>(interval.weight,
interval.isSecond,
guides.interlacings);

auto &canvas = parent.canvas;
canvas.save();
canvas.setLineColor(Gfx::Color::Transparent());
canvas.setBrushColor(interlacingColor);
if (auto &&eventTarget =
Events::Targets::axisInterlacing(axisIndex);
parent.rootEvents.draw.plot.axis.interlacing->invoke(
Events::OnRectDrawEvent(*eventTarget,
{rect, true}))) {
parent.painter.drawPolygon(rect.points());
parent.renderedChart.emplace(Rect{rect, true},
std::move(eventTarget));

auto rect = [&](const double &from, const double &to)
{
return Geom::Rect{
Geom::Point::Coord(orientation, from, clippedBottom),
{Geom::Size::Coord(orientation,
to - from,
clippedSize)}};
};

auto weight = Math::FuzzyBool::And<double>(interval.weight,
interval.isSecond,
guides.interlacings);
auto interlacingColor = *axisStyle.interlacing.color * weight;

for (auto first = othWeights.begin(),
next = std::next(first),
last = othWeights.end();
next != last;
++next, ++first) {
if (Math::Floating::is_zero(first->second))
drawInterlacing(axisIndex,
interlacingColor,
rect(first->first, next->first));
else if (axisIndex == Gen::AxisId::y) {
auto color =
interlacingColor
+ *othAxisStyle.interlacing.color * first->second;

color.alpha =
1
- (1
- axisStyle.interlacing.color->alpha
* weight)
* (1
- othAxisStyle.interlacing.color->alpha
* first->second);

if (weight + first->second > 1.0)
color = Math::Niebloid::interpolate(
color
* std::max({std::abs(axisStyle.interlacing
.color->red
- color.red),
std::abs(
axisStyle.interlacing.color->green
- color.green),
std::abs(
axisStyle.interlacing.color->blue
- color.blue)}),
color,
2.0 - (weight + first->second));
drawInterlacing(first->second > weight ? !axisIndex
: axisIndex,
color,
rect(first->first, next->first));
}
schaumb marked this conversation as resolved.
Show resolved Hide resolved
}
canvas.restore();
}
}

Expand Down Expand Up @@ -119,6 +184,26 @@ void DrawInterlacing::drawTexts(Gen::AxisId axisIndex) const
}
}

void DrawInterlacing::drawInterlacing(Gen::AxisId axisIndex,
const Gfx::Color &interlacingColor,
const Geom::Rect &rect) const
{
auto &canvas = parent.canvas;
canvas.save();
canvas.setLineColor(Gfx::Color::Transparent());
canvas.setLineWidth(0);
canvas.setBrushColor(interlacingColor);
if (auto &&eventTarget =
Events::Targets::axisInterlacing(axisIndex);
parent.rootEvents.draw.plot.axis.interlacing->invoke(
Events::OnRectDrawEvent(*eventTarget, {rect, true}))) {
parent.painter.drawPolygon(rect.points());
parent.renderedChart.emplace(Rect{rect, true},
std::move(eventTarget));
}
canvas.restore();
}

void DrawInterlacing::drawDataLabel(
const ::Anim::Interpolated<bool> &axisEnabled,
Gen::AxisId axisIndex,
Expand Down
4 changes: 4 additions & 0 deletions src/chart/rendering/drawinterlacing.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class DrawInterlacing
const DrawAxes &parent;

private:
void drawInterlacing(Gen::AxisId axisIndex,
const Gfx::Color &interlacingColor,
const Geom::Rect &rect) const;

void drawDataLabel(const ::Anim::Interpolated<bool> &enabled,
Gen::AxisId axisIndex,
const Geom::Point &tickPos,
Expand Down
9 changes: 7 additions & 2 deletions src/dataframe/impl/aggregators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,19 @@ get_aggregators() noexcept

auto &&aggrs = Refl::enum_names<aggregator_type>;
return {{{{aggrs[static_cast<std::size_t>(aggregator_type::sum)],
empty_double,
init_nan,
[](custom_aggregator::id_type &id,
cell_reference const &cell) -> double
{
auto &ref = *std::get_if<double>(&id);
const double &value =
*std::get_if<double>(&cell);
if (std::isfinite(value)) ref += value;
if (std::isfinite(value)) {
if (std::isnan(ref))
ref = value;
else
ref += value;
}

return ref;
}},
Expand Down
Loading