Prevent crash on Y-axis autoscale when all trace values are identical

This commit is contained in:
Jan Käberich 2020-11-27 16:53:41 +01:00
parent d81b8168ad
commit 6893af1386

View File

@ -462,6 +462,7 @@ void TraceXYPlot::updateAxisTicks()
}; };
auto createAutomaticTicks = [](vector<double>& ticks, double start, double stop, int minDivisions) -> double { auto createAutomaticTicks = [](vector<double>& ticks, double start, double stop, int minDivisions) -> double {
Q_ASSERT(stop > start);
ticks.clear(); ticks.clear();
double max_div_step = (stop - start) / minDivisions; double max_div_step = (stop - start) / minDivisions;
int zeros = floor(log10(max_div_step)); int zeros = floor(log10(max_div_step));
@ -561,13 +562,28 @@ void TraceXYPlot::updateAxisTicks()
} }
} }
} }
// add 5% overrange if(max >= min) {
auto range = max - min; auto range = max - min;
min -= range * 0.05; if(range == 0.0) {
max += range * 0.05; // this could happen if all values in a trace are identical (e.g. imported ideal touchstone files)
YAxis[i].rangeMin = min; if(max == 0.0) {
YAxis[i].rangeMax = max; // simply use +/-1 range
YAxis[i].rangeDiv = createAutomaticTicks(YAxis[i].ticks, min, max, 8); max = 1.0;
min = -1.0;
} else {
// +/-5% around value
max += abs(max * 0.05);
min -= abs(max * 0.05);
}
} else {
// add 5% of range at both ends
min -= range * 0.05;
max += range * 0.05;
}
YAxis[i].rangeMin = min;
YAxis[i].rangeMax = max;
YAxis[i].rangeDiv = createAutomaticTicks(YAxis[i].ticks, min, max, 8);
}
} }
} }
} }