Fix x ticks display issue, adjust default values

This commit is contained in:
Jan Käberich 2021-10-25 20:41:58 +02:00
parent 6ef0d9e87a
commit a8aebfb216
3 changed files with 70 additions and 72 deletions

View File

@ -381,69 +381,6 @@ void TraceXYPlot::draw(QPainter &p)
font.setPixelSize(AxisLabelSize);
p.setFont(font);
p.drawText(QRect(0, w.height()-AxisLabelSize*1.5, w.width(), AxisLabelSize*1.5), Qt::AlignHCenter, AxisTypeToName(XAxis.type));
if(XAxis.ticks.size() >= 1) {
// draw X ticks
// this only works for evenly distributed ticks:
auto max = qMax(abs(XAxis.ticks.front()), abs(XAxis.ticks.back()));
auto minLabel = qMin(abs(XAxis.ticks.front()), abs(XAxis.ticks.back()));
double step;
if(XAxis.ticks.size() >= 2) {
step = abs(XAxis.ticks[0] - XAxis.ticks[1]);
} else {
// only one tick, set arbitrary number of digits
step = max / 1000;
}
if(minLabel > 0 && minLabel < step) {
step = minLabel;
}
int significantDigits = floor(log10(max)) - floor(log10(step)) + 1;
bool displayFullFreq = significantDigits <= 5;
constexpr int displayLastDigits = 4;
QString prefixes = "fpnum kMG";
QString commonPrefix = QString();
if(!displayFullFreq) {
auto fullFreq = Unit::ToString(XAxis.ticks.front(), "", prefixes, significantDigits);
commonPrefix = fullFreq.at(fullFreq.size() - 1);
auto front = fullFreq;
front.truncate(fullFreq.size() - displayLastDigits);
auto back = fullFreq;
back.remove(0, front.size());
back.append("..");
p.setPen(QPen(QColor("orange")));
QRect bounding;
p.drawText(QRect(2, plotAreaBottom + AxisLabelSize + 5, w.width(), AxisLabelSize), 0, front, &bounding);
p.setPen(pref.Graphs.Color.axis);
p.drawText(QRect(bounding.x() + bounding.width(), plotAreaBottom + AxisLabelSize + 5, w.width(), AxisLabelSize), 0, back);
}
for(auto t : XAxis.ticks) {
auto xCoord = Util::Scale<double>(t, XAxis.rangeMin, XAxis.rangeMax, plotAreaLeft, plotAreaLeft + plotAreaWidth);
auto tickValue = Unit::ToString(t, "", prefixes, significantDigits);
p.setPen(QPen(pref.Graphs.Color.axis, 1));
if(displayFullFreq) {
p.drawText(QRect(xCoord - 40, plotAreaBottom + 5, 80, AxisLabelSize), Qt::AlignHCenter, tickValue);
} else {
// check if the same prefix was used as in the fullFreq string
if(tickValue.at(tickValue.size() - 1) != commonPrefix) {
// prefix changed, we reached the next order of magnitude. Force same prefix as in fullFreq and add extra digit
tickValue = Unit::ToString(t, "", commonPrefix, significantDigits + 1);
}
tickValue.remove(0, tickValue.size() - displayLastDigits);
QRect bounding;
p.drawText(QRect(xCoord - 40, plotAreaBottom + 5, 80, AxisLabelSize), Qt::AlignHCenter, tickValue, &bounding);
p.setPen(QPen(QColor("orange")));
p.drawText(QRect(0, plotAreaBottom + 5, bounding.x() - 1, AxisLabelSize), Qt::AlignRight, "..");
p.setPen(QPen(pref.Graphs.Color.axis, 1));
}
p.drawLine(xCoord, plotAreaBottom, xCoord, plotAreaBottom + 2);
if(xCoord != plotAreaLeft && xCoord != plotAreaLeft + plotAreaWidth) {
p.setPen(QPen(pref.Graphs.Color.Ticks.divisions, 0.5, Qt::DashLine));
p.drawLine(xCoord, plotAreaTop, xCoord, plotAreaBottom);
}
}
}
for(int i=0;i<2;i++) {
if(YAxis[i].type == YAxisType::Disabled) {
continue;
@ -470,7 +407,6 @@ void TraceXYPlot::draw(QPainter &p)
int significantDigits = floor(log10(max)) - floor(log10(step)) + 1;
auto yCoordWidth = 0;
auto yCoordLast = 0;
for(unsigned int j = 0; j < YAxis[i].ticks.size(); j++) {
auto yCoord = Util::Scale<double>(YAxis[i].ticks[j], YAxis[i].rangeMax, YAxis[i].rangeMin, plotAreaTop, w.height() - xAxisSpace);
@ -493,19 +429,18 @@ void TraceXYPlot::draw(QPainter &p)
}
if(i == 0) {
// only draw tick lines for primary axis
p.setPen(QPen(pref.Graphs.Color.Ticks.divisions, 0.5, Qt::DashLine));
p.drawLine(plotAreaLeft, yCoord, plotAreaLeft + plotAreaWidth, yCoord);
if (pref.Graphs.Color.Ticks.Background.enabled) {
yCoordWidth = floor((w.height() - xAxisSpace - plotAreaTop)/(YAxis[i].ticks.size()-1));
if (j%2)
{
p.setBrush(pref.Graphs.Color.Ticks.Background.background);
auto rect = QRect(plotAreaLeft, yCoord, plotAreaWidth, yCoordWidth);
p.setPen(pref.Graphs.Color.Ticks.Background.background);
auto rect = QRect(plotAreaLeft+1, yCoord+1, plotAreaWidth-2, yCoordWidth-2);
p.drawRect(rect);
}
yCoordLast = yCoord;
}
p.setPen(QPen(pref.Graphs.Color.Ticks.divisions, 0.5, Qt::DashLine));
p.drawLine(plotAreaLeft, yCoord, plotAreaLeft + plotAreaWidth, yCoord);
}
}
}
@ -572,6 +507,69 @@ void TraceXYPlot::draw(QPainter &p)
p.setClipping(false);
}
if(XAxis.ticks.size() >= 1) {
// draw X ticks
// this only works for evenly distributed ticks:
auto max = qMax(abs(XAxis.ticks.front()), abs(XAxis.ticks.back()));
auto minLabel = qMin(abs(XAxis.ticks.front()), abs(XAxis.ticks.back()));
double step;
if(XAxis.ticks.size() >= 2) {
step = abs(XAxis.ticks[0] - XAxis.ticks[1]);
} else {
// only one tick, set arbitrary number of digits
step = max / 1000;
}
if(minLabel > 0 && minLabel < step) {
step = minLabel;
}
int significantDigits = floor(log10(max)) - floor(log10(step)) + 1;
bool displayFullFreq = significantDigits <= 5;
constexpr int displayLastDigits = 4;
QString prefixes = "fpnum kMG";
QString commonPrefix = QString();
if(!displayFullFreq) {
auto fullFreq = Unit::ToString(XAxis.ticks.front(), "", prefixes, significantDigits);
commonPrefix = fullFreq.at(fullFreq.size() - 1);
auto front = fullFreq;
front.truncate(fullFreq.size() - displayLastDigits);
auto back = fullFreq;
back.remove(0, front.size());
back.append("..");
p.setPen(QPen(QColor("orange")));
QRect bounding;
p.drawText(QRect(2, plotAreaBottom + AxisLabelSize + 5, w.width(), AxisLabelSize), 0, front, &bounding);
p.setPen(pref.Graphs.Color.axis);
p.drawText(QRect(bounding.x() + bounding.width(), plotAreaBottom + AxisLabelSize + 5, w.width(), AxisLabelSize), 0, back);
}
for(auto t : XAxis.ticks) {
auto xCoord = Util::Scale<double>(t, XAxis.rangeMin, XAxis.rangeMax, plotAreaLeft, plotAreaLeft + plotAreaWidth);
auto tickValue = Unit::ToString(t, "", prefixes, significantDigits);
p.setPen(QPen(pref.Graphs.Color.axis, 1));
if(displayFullFreq) {
p.drawText(QRect(xCoord - 40, plotAreaBottom + 5, 80, AxisLabelSize), Qt::AlignHCenter, tickValue);
} else {
// check if the same prefix was used as in the fullFreq string
if(tickValue.at(tickValue.size() - 1) != commonPrefix) {
// prefix changed, we reached the next order of magnitude. Force same prefix as in fullFreq and add extra digit
tickValue = Unit::ToString(t, "", commonPrefix, significantDigits + 1);
}
tickValue.remove(0, tickValue.size() - displayLastDigits);
QRect bounding;
p.drawText(QRect(xCoord - 40, plotAreaBottom + 5, 80, AxisLabelSize), Qt::AlignHCenter, tickValue, &bounding);
p.setPen(QPen(QColor("orange")));
p.drawText(QRect(0, plotAreaBottom + 5, bounding.x() - 1, AxisLabelSize), Qt::AlignRight, "..");
p.setPen(QPen(pref.Graphs.Color.axis, 1));
}
p.drawLine(xCoord, plotAreaBottom, xCoord, plotAreaBottom + 2);
if(xCoord != plotAreaLeft && xCoord != plotAreaLeft + plotAreaWidth) {
p.setPen(QPen(pref.Graphs.Color.Ticks.divisions, 0.5, Qt::DashLine));
p.drawLine(xCoord, plotAreaTop, xCoord, plotAreaBottom);
}
}
}
if(dropPending) {
p.setOpacity(0.5);
p.setBrush(Qt::white);

View File

@ -130,7 +130,7 @@ private:
{&Graphs.Color.background, "Graphs.Color.background", QColor(Qt::black)},
{&Graphs.Color.axis, "Graphs.Color.axis", QColor(Qt::white)},
{&Graphs.Color.Ticks.Background.enabled, "Graphs.Color.Ticks.Background.enabled", true},
{&Graphs.Color.Ticks.Background.background, "Graphs.Color.Ticks.Background.background", QColor(Qt::white)},
{&Graphs.Color.Ticks.Background.background, "Graphs.Color.Ticks.Background.background", QColor(20, 20, 20)},
{&Graphs.Color.Ticks.divisions, "Graphs.Color.Ticks.divisions", QColor(Qt::gray)},
{&Graphs.domainChangeBehavior, "Graphs.domainChangeBehavior", GraphDomainChangeBehavior::AdjustGraphs},
{&Graphs.markerBehavior.showDataOnGraphs, "Graphs.markerBehavior.ShowDataOnGraphs", true},

View File

@ -715,7 +715,7 @@
<item row="2" column="0">
<widget class="QLabel" name="label_17">
<property name="text">
<string>Divisions:</string>
<string>Tick lines:</string>
</property>
</widget>
</item>
@ -729,7 +729,7 @@
<item row="1" column="0">
<widget class="QCheckBox" name="GraphsColorTicksBackgroundEnabled">
<property name="text">
<string>Enable background</string>
<string>Different background for every 2nd Y tick</string>
</property>
<property name="checked">
<bool>true</bool>