Skip to content

Commit

Permalink
Histogram/Line meters: Fixed issue with MinValue not being included i…
Browse files Browse the repository at this point in the history
…n the drawing calculations correctly
  • Loading branch information
brianferguson committed Sep 17, 2022
1 parent f751215 commit 12beca8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 27 deletions.
28 changes: 12 additions & 16 deletions Library/MeterHistogram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,10 +432,9 @@ bool MeterHistogram::Draw(Gfx::Canvas& canvas)
{
const FLOAT startStep = (FLOAT)(startValue + (step * i));

double value = (m_MaxPrimaryValue == 0.0) ?
0.0
: m_PrimaryValues[(i + (m_MeterPos % displayH)) % displayH] / m_MaxPrimaryValue;
value -= m_MinPrimaryValue;
double range = m_MaxPrimaryValue - m_MinPrimaryValue;
double value = (range <= 0.0) ? 0.0 :
(m_PrimaryValues[(i + (m_MeterPos % displayH)) % displayH] - m_MinPrimaryValue) / range;

int primaryBarHeight = (int)(displayW * value);
primaryBarHeight = min(displayW, primaryBarHeight);
Expand All @@ -445,10 +444,9 @@ bool MeterHistogram::Draw(Gfx::Canvas& canvas)

if (secondaryMeasure)
{
value = (m_MaxSecondaryValue == 0.0) ?
0.0
: m_SecondaryValues[(i + m_MeterPos) % displayH] / m_MaxSecondaryValue;
value -= m_MinSecondaryValue;
range = m_MaxSecondaryValue - m_MinSecondaryValue;
value = (range <= 0.0) ? 0.0 :
(m_SecondaryValues[(i + m_MeterPos) % displayH] - m_MinSecondaryValue) / range;

int secondaryBarHeight = (int)(displayW * value);
secondaryBarHeight = min(displayW, secondaryBarHeight);
Expand Down Expand Up @@ -497,10 +495,9 @@ bool MeterHistogram::Draw(Gfx::Canvas& canvas)
{
const FLOAT startStep = (FLOAT)(startValue + (step * i));

double value = (m_MaxPrimaryValue == 0.0) ?
0.0
: m_PrimaryValues[(i + m_MeterPos) % displayW] / m_MaxPrimaryValue;
value -= m_MinPrimaryValue;
double range = m_MaxPrimaryValue - m_MinPrimaryValue;
double value = (range <= 0.0) ? 0.0 :
(m_PrimaryValues[((i + m_MeterPos) % displayW)] - m_MinPrimaryValue) / range;

int primaryBarHeight = (int)(displayH * value);
primaryBarHeight = min(displayH, primaryBarHeight);
Expand All @@ -510,10 +507,9 @@ bool MeterHistogram::Draw(Gfx::Canvas& canvas)

if (secondaryMeasure)
{
value = (m_MaxSecondaryValue == 0.0) ?
0.0
: m_SecondaryValues[(i + m_MeterPos) % displayW] / m_MaxSecondaryValue;
value -= m_MinSecondaryValue;
range = m_MaxSecondaryValue - m_MinSecondaryValue;
value = (range <= 0.0) ? 0.0 :
(m_SecondaryValues[(i + m_MeterPos) % displayW] - m_MinSecondaryValue) / range;

int secondaryBarHeight = (int)(displayH * value);
secondaryBarHeight = min(displayH, secondaryBarHeight);
Expand Down
37 changes: 26 additions & 11 deletions Library/MeterLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,9 @@ bool MeterLine::Draw(Gfx::Canvas& canvas)
int maxSize = m_GraphHorizontalOrientation ? m_H : m_W;
if (!Meter::Draw(canvas) || maxSize <= 0) return false;

double maxValue = 0.0;
double maxValue = 0.0, minValue = 0.0;

// Find the maximum value
// Find the maximum / minimum value
if (m_Autoscale)
{
double newValue = 0.0;
Expand Down Expand Up @@ -248,21 +248,35 @@ bool MeterLine::Draw(Gfx::Canvas& canvas)
maxValue *= 2.0;
}
}

for (auto i = m_Measures.cbegin(); i != m_Measures.cend(); ++i)
{
double val = (*i)->GetMinValue();
minValue = max(minValue, val);
}
}
else
{
for (auto i = m_Measures.cbegin(); i != m_Measures.cend(); ++i)
{
double val = (*i)->GetMaxValue();
maxValue = max(maxValue, val);
}

if (maxValue == 0.0)
{
maxValue = 1.0;
val = (*i)->GetMinValue();
minValue = max(minValue, val);
}
}

if (maxValue <= 0.0)
{
maxValue = 1.0;
}

if (minValue <= 0.0)
{
minValue = 0.0;
}

D2D1_RECT_F meterRect = GetMeterRectPadding();
int drawW = (int)(meterRect.right - meterRect.left);
int drawH = (int)(meterRect.bottom - meterRect.top);
Expand Down Expand Up @@ -310,18 +324,19 @@ bool MeterLine::Draw(Gfx::Canvas& canvas)
canvas.DrawGeometry(path, 0, 0);
};

const double range = maxValue - minValue;
if (m_GraphHorizontalOrientation)
{
const FLOAT W = (FLOAT)(drawW - 1);
int counter = 0;
for (auto i = m_AllValues.cbegin(); i != m_AllValues.cend(); ++i)
{
const double scale = m_ScaleValues[counter] * W / maxValue;
const double scale = (m_ScaleValues[counter] * W) / range;
int pos = m_CurrentPos;

auto calcX = [&](FLOAT& _x)
{
_x = ((FLOAT)((*i)[pos] * scale) + offset);
_x = ((FLOAT)(((*i)[pos] - minValue) * scale) + offset);
_x = min(_x, W + offset);
_x = max(_x, offset);
_x = meterRect.left + (m_GraphStartLeft ? _x : W - _x + 1.0f);
Expand Down Expand Up @@ -372,18 +387,18 @@ bool MeterLine::Draw(Gfx::Canvas& canvas)
++counter;
}
}
else
else // GraphOrientation=Vertical
{
const FLOAT H = (FLOAT)(drawH - 1);
int counter = 0;
for (auto i = m_AllValues.cbegin(); i != m_AllValues.cend(); ++i)
{
const double scale = m_ScaleValues[counter] * H / maxValue;
const double scale = (m_ScaleValues[counter] * H) / range;
int pos = m_CurrentPos;

auto calcY = [&](FLOAT& _y)
{
_y = ((FLOAT)((*i)[pos] * scale) + offset);
_y = ((FLOAT)(((*i)[pos] - minValue) * scale) + offset);
_y = min(_y, H + offset);
_y = max(_y, offset);
_y = meterRect.top + (m_Flip ? _y : H - _y + 1.0f);
Expand Down

0 comments on commit 12beca8

Please sign in to comment.