cabana/chart: use different colors to distinguish similar signals (#29890)
* Use different colors to distinguish similar signals * check hueF value * lighter old-commit-hash: 34f5cad4e7a115826167d44ee48446e7e157b79f
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
#include <QMimeData>
|
||||
#include <QOpenGLWidget>
|
||||
#include <QPropertyAnimation>
|
||||
#include <QRandomGenerator>
|
||||
#include <QRubberBand>
|
||||
#include <QScreen>
|
||||
#include <QtMath>
|
||||
@@ -151,6 +152,11 @@ void ChartView::removeIf(std::function<bool(const SigItem &s)> predicate) {
|
||||
|
||||
void ChartView::signalUpdated(const cabana::Signal *sig) {
|
||||
if (std::any_of(sigs.cbegin(), sigs.cend(), [=](auto &s) { return s.sig == sig; })) {
|
||||
for (const auto &s : sigs) {
|
||||
if (s.sig == sig && s.series->color() != sig->color) {
|
||||
setSeriesColor(s.series, sig->color);
|
||||
}
|
||||
}
|
||||
updateTitle();
|
||||
updateSeries(sig);
|
||||
}
|
||||
@@ -281,7 +287,6 @@ void ChartView::updateSeries(const cabana::Signal *sig, bool clear) {
|
||||
s.step_vals.clear();
|
||||
s.last_value_mono_time = 0;
|
||||
}
|
||||
s.series->setColor(s.sig->color);
|
||||
|
||||
const auto &msgs = can->events(s.msg_id);
|
||||
s.vals.reserve(msgs.capacity());
|
||||
@@ -789,6 +794,7 @@ QXYSeries *ChartView::createSeries(SeriesType type, QColor color) {
|
||||
}
|
||||
|
||||
void ChartView::addSeries(QXYSeries *series) {
|
||||
setSeriesColor(series, series->color());
|
||||
chart()->addSeries(series);
|
||||
series->attachAxis(axis_x);
|
||||
series->attachAxis(axis_y);
|
||||
@@ -801,6 +807,21 @@ void ChartView::addSeries(QXYSeries *series) {
|
||||
}
|
||||
}
|
||||
|
||||
void ChartView::setSeriesColor(QXYSeries *series, QColor color) {
|
||||
auto existing_series = chart()->series();
|
||||
for (auto s : existing_series) {
|
||||
if (s != series && std::abs(color.hueF() - qobject_cast<QXYSeries *>(s)->color().hueF()) < 0.1) {
|
||||
// use different color to distinguish it from others.
|
||||
auto last_color = qobject_cast<QXYSeries *>(existing_series.back())->color();
|
||||
color.setHsvF(std::fmod(last_color.hueF() + 60 / 360.0, 1.0),
|
||||
QRandomGenerator::global()->bounded(35, 100) / 100.0,
|
||||
QRandomGenerator::global()->bounded(85, 100) / 100.0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
series->setColor(color);
|
||||
}
|
||||
|
||||
void ChartView::setSeriesType(SeriesType type) {
|
||||
if (type != series_type) {
|
||||
series_type = type;
|
||||
|
||||
@@ -89,6 +89,7 @@ private:
|
||||
std::tuple<double, double, int> getNiceAxisNumbers(qreal min, qreal max, int tick_count);
|
||||
qreal niceNumber(qreal x, bool ceiling);
|
||||
QXYSeries *createSeries(SeriesType type, QColor color);
|
||||
void setSeriesColor(QXYSeries *, QColor color);
|
||||
void updateSeriesPoints();
|
||||
void removeIf(std::function<bool(const SigItem &)> predicate);
|
||||
inline void clearTrackPoints() { for (auto &s : sigs) s.track_pt = {}; }
|
||||
|
||||
@@ -277,6 +277,10 @@ void ChartsWidget::splitChart(ChartView *src_chart) {
|
||||
for (auto it = src_chart->sigs.begin() + 1; it != src_chart->sigs.end(); /**/) {
|
||||
auto c = createChart();
|
||||
src_chart->chart()->removeSeries(it->series);
|
||||
|
||||
// Restore to the original color
|
||||
it->series->setColor(it->sig->color);
|
||||
|
||||
c->addSeries(it->series);
|
||||
c->sigs.push_back(*it);
|
||||
c->updateAxisY();
|
||||
|
||||
Reference in New Issue
Block a user