Numworks Epsilon  1.4.1
Graphing Calculator Operating System
box_view.cpp
Go to the documentation of this file.
1 #include "box_view.h"
2 #include <assert.h>
3 #include <cmath>
4 
5 using namespace Shared;
6 
7 namespace Statistics {
8 
9 BoxView::BoxView(Store * store, BannerView * bannerView, Quantile * selectedQuantile) :
10  CurveView(&m_boxRange, nullptr, bannerView, nullptr),
11  m_store(store),
12  m_boxRange(BoxRange(store)),
13  m_labels{},
14  m_selectedQuantile(selectedQuantile)
15 {
16 }
17 
19  CurveView::reload();
22  float calculation = (m_store->*calculationMethods[(int)*m_selectedQuantile])();
23  float pixelUpperBound = floatToPixel(Axis::Vertical, 0.2f)+1;
24  float pixelLowerBound = floatToPixel(Axis::Vertical, 0.8)-1;
25  float selectedValueInPixels = floatToPixel(Axis::Horizontal, calculation)-1;
26  KDRect dirtyZone(KDRect(selectedValueInPixels, pixelLowerBound, 4, pixelUpperBound - pixelLowerBound));
27  markRectAsDirty(dirtyZone);
28 }
29 
31  return *m_selectedQuantile;
32 }
33 
34 bool BoxView::selectQuantile(int selectedQuantile) {
35  if (selectedQuantile < 0 || selectedQuantile > 4) {
36  return false;
37  }
38  if ((int)*m_selectedQuantile != selectedQuantile) {
39  reload();
40  *m_selectedQuantile = (Quantile)selectedQuantile;
41  reload();
42  }
43  return true;
44 }
45 
46 void BoxView::drawRect(KDContext * ctx, KDRect rect) const {
47  ctx->fillRect(rect, KDColorWhite);
48  drawAxes(ctx, rect, Axis::Horizontal);
49  drawLabels(ctx, rect, Axis::Horizontal, false);
50  float lowBound = 0.35f;
51  float upBound = 0.65f;
52  // Draw the main box
53  KDCoordinate firstQuartilePixels = std::round(floatToPixel(Axis::Horizontal, m_store->firstQuartile()));
54  KDCoordinate thirdQuartilePixels = std::round(floatToPixel(Axis::Horizontal, m_store->thirdQuartile()));
55  KDCoordinate lowBoundPixel = std::round(floatToPixel(Axis::Vertical, upBound));
56  KDCoordinate upBoundPixel = std::round(floatToPixel(Axis::Vertical, lowBound));
57  ctx->fillRect(KDRect(firstQuartilePixels, lowBoundPixel, thirdQuartilePixels - firstQuartilePixels+2,
58  upBoundPixel-lowBoundPixel), Palette::GreyWhite);
59  // Draw the horizontal lines linking the box to the extreme bounds
60  drawSegment(ctx, rect, Axis::Horizontal, 0.5f, m_store->minValue(), m_store->firstQuartile(), Palette::GreyDark);
61  drawSegment(ctx, rect, Axis::Horizontal, 0.5f, m_store->thirdQuartile(), m_store->maxValue(), Palette::GreyDark);
62 
63  double calculations[5] = {m_store->minValue(), m_store->firstQuartile(), m_store->median(), m_store->thirdQuartile(), m_store->maxValue()};
64  /* We then draw all the vertical lines of the box and then recolor the
65  * the selected quantile (if there is one). As two quantiles can have the same
66  * value, we cannot choose line colors and then color only once the vertical
67  * lines. This solution could hide the highlighed line by coloring the next
68  * quantile if it has the same value. */
69  for (int k = 0; k < 5; k++) {
70  drawSegment(ctx, rect, Axis::Vertical, calculations[k], lowBound, upBound, Palette::GreyMiddle, 2);
71  }
72  if (isMainViewSelected()) {
73  drawSegment(ctx, rect, Axis::Vertical, calculations[(int)*m_selectedQuantile], lowBound, upBound, Palette::YellowDark, 2);
74  }
75 }
76 
77 char * BoxView::label(Axis axis, int index) const {
78  if (axis == Axis::Vertical) {
79  return nullptr;
80  }
81  return (char *)m_labels[index];
82 }
83 
84 }
static constexpr KDColor YellowDark
Definition: palette.h:8
bool isMainViewSelected() const
Definition: curve_view.cpp:42
void reload() override
Definition: box_view.cpp:18
int16_t KDCoordinate
Definition: coordinate.h:6
double maxValue()
Definition: store.cpp:98
static constexpr KDColor GreyDark
Definition: palette.h:15
void markRectAsDirty(KDRect rect)
Definition: view.cpp:39
void drawSegment(KDContext *ctx, KDRect rect, Axis axis, float coordinate, float lowerBound, float upperBound, KDColor color, KDCoordinate thickness=1) const
Definition: curve_view.cpp:196
void drawAxes(KDContext *ctx, KDRect rect, Axis axis) const
Definition: curve_view.cpp:282
double minValue()
Definition: store.cpp:108
double median()
Definition: store.cpp:155
constexpr KDColor KDColorWhite
Definition: color.h:42
void drawLabels(KDContext *ctx, KDRect rect, Axis axis, bool shiftOrigin) const
Definition: curve_view.cpp:146
#define round(x)
Definition: math.h:192
double firstQuartile()
Definition: store.cpp:141
double(Store::* CalculPointer)()
Definition: store.h:56
static constexpr KDColor GreyMiddle
Definition: palette.h:14
double thirdQuartile()
Definition: store.cpp:146
Definition: rect.h:26
void fillRect(KDRect rect, KDColor color)
Definition: context_rect.cpp:8
float floatToPixel(Axis axis, float f) const
Definition: curve_view.cpp:115
void drawRect(KDContext *ctx, KDRect rect) const override
Definition: box_view.cpp:46
Quantile selectedQuantile()
Definition: box_view.cpp:30
static constexpr KDColor GreyWhite
Definition: palette.h:12
bool selectQuantile(int selectedQuantile)
Definition: box_view.cpp:34