Skip to content

Commit

Permalink
feat: Add log axes (#18)
Browse files Browse the repository at this point in the history
* feat: Add log axes

* Remove dead file

* Fix c++ formatting
  • Loading branch information
rprospero authored Jun 3, 2024
1 parent 80e6f82 commit 1a5e73f
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ qt_add_qml_module(
axisTickLabels.cpp
lineGeometry.cpp
lineGeometry.h
logAxis.h
logAxis.cpp
scatterGeometry.cpp
scatterGeometry.h
QML_FILES
main.qml
AxisModel.qml
LineModel.qml
ScatterModel.qml)

Expand Down
7 changes: 6 additions & 1 deletion src/axis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void Axis::updateData()
update();
}

std::vector<float> Axis::convert(QList<double> points)
std::vector<float> Axis::convert(QList<double> points) const
{
std::vector<float> result(points.length());

Expand All @@ -91,3 +91,8 @@ std::vector<float> Axis::convert(QList<double> points)

return result;
}

double Axis::tick(int index, int count) const
{
return minimum_ + (double)index / ((double)count - 1.0) * (maximum_ - minimum_);
}
3 changes: 2 additions & 1 deletion src/axis.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ class Axis : public QQuick3DGeometry

public:
Axis();
std::vector<float> convert(QList<double> values);
virtual std::vector<float> convert(QList<double> values) const;
AxisTickLabels *tickLabels();
bool direction() const;
double minimum() const;
double maximum() const;
int tickCount() const;
void setTickCount(const int count);
virtual double tick(int index, int count) const;

Q_SIGNALS:
void dataChanged();
Expand Down
3 changes: 1 addition & 2 deletions src/axisTickLabels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ QVariant AxisTickLabels::data(const QModelIndex &index, int role) const
switch (role)
{
case Qt::UserRole:
return QString("%1").arg(parent_.minimum() +
index.row() / ((double)N_ - 1) * (parent_.maximum() - parent_.minimum()));
return QString("%1").arg(parent_.tick(index.row(), N_));
case (Qt::UserRole + 1):
return parent_.direction() ? index.row() : 0;
case (Qt::UserRole + 2):
Expand Down
28 changes: 28 additions & 0 deletions src/logAxis.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "logAxis.h"
#include <algorithm>

#include <cmath>
#include <iostream>

LogAxis::LogAxis() : Axis() {}

std::vector<float> LogAxis::convert(QList<double> points) const
{
std::vector<float> result(points.length());

auto min = log(minimum());
auto max = log(maximum());

std::transform(points.begin(), points.end(), result.begin(),
[min, max](const auto x) { return -1.0f + 2 * (log(x) - min) / (max - min); });

return result;
}

double LogAxis::tick(int index, int count) const
{
auto min = log(minimum());
auto max = log(maximum());

return exp(min + (double)index / ((double)count - 1.0) * (max - min));
}
20 changes: 20 additions & 0 deletions src/logAxis.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include "axis.h"
#include "axisTickLabels.h"
#include <QQuick3DGeometry>
#include <QVector3D>

class LogAxis : public Axis
{
Q_OBJECT
QML_NAMED_ELEMENT(LogAxis)

public:
LogAxis();
std::vector<float> convert(QList<double> values) const override;
double tick(int index, int count) const override;

Q_SIGNALS:
void dataChanged();
};
9 changes: 5 additions & 4 deletions src/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ ApplicationWindow {
axis: Axis {
id: xAxis

direction: false
direction: true
maximum: 1.0
minimum: -1.0
thickness: 0.01
Expand All @@ -60,13 +60,14 @@ ApplicationWindow {
color: "black"
scl: root.scale

axis: Axis {
axis: LogAxis {
id: yAxis

direction: true
direction: false
maximum: 1.0
minimum: -1.0
minimum: 0.01
thickness: 0.01
tickCount: ticCount.value
}
}
}
Expand Down

0 comments on commit 1a5e73f

Please sign in to comment.