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