Change trace point value precision to be dynamic based on graph scale (#1148)

* Dynamic precision

* add comments

* feedback

* Update src/Calculator/Views/GraphingCalculator/GraphingCalculator.xaml.cpp

Co-Authored-By: Rudy Huyn <rudyhuyn@gmail.com>

* PR feedback

* Pr comment

Co-authored-by: Rudy Huyn <rudyhuyn@gmail.com>
This commit is contained in:
Pepe Rivera 2020-04-21 17:44:08 -07:00 committed by GitHub
parent 1ded2c57db
commit 0465dc8538
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 3 deletions

View File

@ -207,12 +207,44 @@ void GraphingCalculator::OnEquationsVectorChanged(IObservableVector<EquationView
GraphingControl->PlotGraph(false);
}
wstringstream GraphingCalculator::FormatTraceValue(double min, double max, float pointValue)
{
wstringstream traceValueString;
// Extract precision we will round to
auto precision = static_cast<int>(floor(log10(max - min)) - 3);
// Determine if we want to show scientific notation instead
if (precision <= -7 || precision >= 7)
{
traceValueString << scientific;
}
else
{
traceValueString << fixed;
}
// If we are rounding to a decimal place, set the precision
if (precision < 0)
{
traceValueString << setprecision(::min(7, abs(precision))) << pointValue;
}
else
{
traceValueString << setprecision(0) << pointValue;
}
return traceValueString;
}
void GraphingCalculator::OnTracePointChanged(Point newPoint)
{
wstringstream traceValueString;
// TODO: The below precision should ideally be dynamic based on the current scale of the graph.
traceValueString << "(" << fixed << setprecision(1) << newPoint.X << ", " << fixed << setprecision(1) << newPoint.Y << ")";
double xAxisMin, xAxisMax, yAxisMin, yAxisMax;
GraphingControl->GetDisplayRanges(&xAxisMin, &xAxisMax, &yAxisMin, &yAxisMax);
traceValueString << "(" << FormatTraceValue(xAxisMin, xAxisMax, newPoint.X).str() << ", " << FormatTraceValue(yAxisMin, yAxisMax, newPoint.Y).str() << ")";
TraceValue->Text = ref new String(traceValueString.str().c_str());

View File

@ -104,6 +104,7 @@ public ref class GraphingCalculator sealed : public Windows::UI::Xaml::Data::INo
void OnEquationFormatRequested(Platform::Object ^ sender, CalculatorApp::Controls::MathRichEditBoxFormatRequest ^ e);
void GraphMenuFlyoutItem_Click(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e);
void OnVisualStateChanged(Platform::Object ^ sender, Windows::UI::Xaml::VisualStateChangedEventArgs ^ e);
std::wstringstream FormatTraceValue(double min, double max, float pointValue);
void GraphViewButton_Click(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e);
};

View File

@ -28,6 +28,8 @@
#include <regex>
#include <string>
#include <tuple>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <WindowsNumerics.h>

View File

@ -232,10 +232,11 @@ public enum class GraphViewChangedReason
{
try
{
if (m_graph != nullptr)
if (m_graph != nullptr && m_renderMain != nullptr)
{
if (auto render = m_graph->GetRenderer())
{
Concurrency::critical_section::scoped_lock lock(m_renderMain->GetCriticalSection());
render->GetDisplayRanges(*xMin, *xMax, *yMin, *yMax);
}
}