From 948a98905f439d2d961424380684ada577fc9b98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Fri, 30 Dec 2016 12:22:18 +0100 Subject: [PATCH] [apps//regression] Create a graph view Change-Id: I434e3647f7acf7b1358203f894d5847b787a2375 --- apps/regression/Makefile | 1 + apps/regression/graph_view.cpp | 56 ++++++++++++++++++++++++++++++++++ apps/regression/graph_view.h | 32 +++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 apps/regression/graph_view.cpp create mode 100644 apps/regression/graph_view.h diff --git a/apps/regression/Makefile b/apps/regression/Makefile index 0ead1f201..bac4096f2 100644 --- a/apps/regression/Makefile +++ b/apps/regression/Makefile @@ -4,6 +4,7 @@ app_objs += $(addprefix apps/regression/,\ calculation_controller.o\ data.o\ graph_controller.o\ + graph_view.o\ ) app_images += apps/regression/regression_icon.png diff --git a/apps/regression/graph_view.cpp b/apps/regression/graph_view.cpp new file mode 100644 index 000000000..c866b19b0 --- /dev/null +++ b/apps/regression/graph_view.cpp @@ -0,0 +1,56 @@ +#include "graph_view.h" +#include +#include + +namespace Regression { + +GraphView::GraphView(Data * data) : + CurveViewWithBanner(data, 0.2f, 0.2f, 0.4f, 0.2f), + m_data(data), + m_bannerView(BannerView(data)) +{ +} + +void GraphView::reloadSelection() { + float pixelXSelection = roundf(floatToPixel(Axis::Horizontal, m_data->xCursorPosition())); + float pixelYSelection = roundf(floatToPixel(Axis::Vertical, m_data->yCursorPosition())); + KDRect dirtyZone(KDRect(pixelXSelection - k_dotSize/2, pixelYSelection - k_dotSize/2, k_dotSize, k_dotSize)); + markRectAsDirty(dirtyZone); + m_bannerView.reload(); +} + +void GraphView::reload() { + markRectAsDirty(bounds()); + computeLabels(Axis::Horizontal); + computeLabels(Axis::Vertical); + m_bannerView.reload(); +} + +void GraphView::drawRect(KDContext * ctx, KDRect rect) const { + ctx->fillRect(rect, KDColorWhite); + drawAxes(ctx, rect, Axis::Horizontal); + drawAxes(ctx, rect, Axis::Vertical); + drawLabels(ctx, rect, Axis::Horizontal, true); + drawLabels(ctx, rect, Axis::Vertical, true); + drawCurve(ctx, rect, nullptr, KDColorRed); + for (int index = 0; index < m_data->numberOfPairs(); index++) { + drawDot(ctx, rect, m_data->xValueAtIndex(index), m_data->yValueAtIndex(index), KDColorBlue, KDSize(k_dotSize, k_dotSize)); + } +} + +char * GraphView::label(Axis axis, int index) const { + if (axis == Axis::Vertical) { + return (char *)m_yLabels[index]; + } + return (char *)m_xLabels[index]; +} + +View * GraphView::bannerView() { + return &m_bannerView; +} + +float GraphView::evaluateModelWithParameter(Model * curve, float t) const { + return m_data->yValueForXValue(t); +} + +} diff --git a/apps/regression/graph_view.h b/apps/regression/graph_view.h new file mode 100644 index 000000000..bc7f6e92d --- /dev/null +++ b/apps/regression/graph_view.h @@ -0,0 +1,32 @@ +#ifndef REGRESSION_GRAPH_VIEW_H +#define REGRESSION_GRAPH_VIEW_H + +#include +#include "data.h" +#include "banner_view.h" +#include "../constant.h" +#include "../curve_view_with_banner.h" + +namespace Regression { + +class GraphView : public CurveViewWithBanner { +public: + GraphView(Data * m_data); + void reloadSelection() override; + void reload(); + void drawRect(KDContext * ctx, KDRect rect) const override; +private: + constexpr static KDCoordinate k_dotSize = 4; + char * label(Axis axis, int index) const override; + View * bannerView() override; + float evaluateModelWithParameter(Model * curve, float t) const override; + Data * m_data; + char m_xLabels[k_maxNumberOfXLabels][Constant::FloatBufferSizeInScientificMode]; + char m_yLabels[k_maxNumberOfYLabels][Constant::FloatBufferSizeInScientificMode]; + BannerView m_bannerView; +}; + +} + + +#endif