Numworks Epsilon  1.4.1
Graphing Calculator Operating System
histogram_view.cpp
Go to the documentation of this file.
1 #include "histogram_view.h"
2 #include <assert.h>
3 #include <cmath>
4 
5 using namespace Shared;
6 
7 namespace Statistics {
8 
9 HistogramView::HistogramView(Store * store, BannerView * bannerView) :
10  CurveView(store, nullptr, bannerView, nullptr),
11  m_store(store),
12  m_labels{},
13  m_highlightedBarStart(NAN),
14  m_highlightedBarEnd(NAN)
15 {
16 }
17 
19  CurveView::reload();
20  float pixelLowerBound = floatToPixel(Axis::Horizontal, m_highlightedBarStart)-2;
21  float pixelUpperBound = floatToPixel(Axis::Horizontal, m_highlightedBarEnd)+2;
22  /* We deliberately do not mark as dirty the frame of the banner view to avoid
23  *unpleasant blinking of the drawing of the banner view. */
24  KDRect dirtyZone(KDRect(pixelLowerBound, 0, pixelUpperBound-pixelLowerBound,
25  bounds().height()-m_bannerView->bounds().height()));
26  markRectAsDirty(dirtyZone);
27 }
28 
29 void HistogramView::drawRect(KDContext * ctx, KDRect rect) const {
30  ctx->fillRect(rect, KDColorWhite);
31  drawAxes(ctx, rect, Axis::Horizontal);
32  drawLabels(ctx, rect, Axis::Horizontal, false);
33  /* We memoize the total size to avoid recomputing it in double precision at
34  * every call to EvaluateHistogramAtAbscissa() */
35  float totalSize = m_store->sumOfColumn(1);
36  if (isMainViewSelected()) {
37  drawHistogram(ctx, rect, EvaluateHistogramAtAbscissa, m_store, &totalSize, m_store->firstDrawnBarAbscissa(), m_store->barWidth(), true, Palette::Select, Palette::YellowDark, m_highlightedBarStart, m_highlightedBarEnd);
38  } else {
39  drawHistogram(ctx, rect, EvaluateHistogramAtAbscissa, m_store, &totalSize, m_store->firstDrawnBarAbscissa(), m_store->barWidth(), true, Palette::GreyMiddle, Palette::YellowDark);
40  }
41 }
42 
43 void HistogramView::setHighlight(float start, float end) {
44  if (m_highlightedBarStart != start || m_highlightedBarEnd != end) {
45  reload();
46  m_highlightedBarStart = start;
47  m_highlightedBarEnd = end;
48  reload();
49  }
50 }
51 
52 char * HistogramView::label(Axis axis, int index) const {
53  if (axis == Axis::Vertical) {
54  return nullptr;
55  }
56  return (char *)m_labels[index];
57 }
58 
59 float HistogramView::EvaluateHistogramAtAbscissa(float abscissa, void * model, void * context) {
60  Store * store = (Store *)model;
61  float * totalSize = (float *)context;
62  return store->heightOfBarAtValue(abscissa)/(*totalSize);
63 }
64 
65 }
static constexpr KDColor YellowDark
Definition: palette.h:8
bool isMainViewSelected() const
Definition: curve_view.cpp:42
#define NAN
Definition: math.h:30
View * m_bannerView
Definition: curve_view.h:56
const SettingsMessageTree model
void markRectAsDirty(KDRect rect)
Definition: view.cpp:39
double barWidth()
Definition: store.cpp:29
void drawAxes(KDContext *ctx, KDRect rect, Axis axis) const
Definition: curve_view.cpp:282
double firstDrawnBarAbscissa()
Definition: store.cpp:40
void drawRect(KDContext *ctx, KDRect rect) const override
constexpr KDColor KDColorWhite
Definition: color.h:42
void drawLabels(KDContext *ctx, KDRect rect, Axis axis, bool shiftOrigin) const
Definition: curve_view.cpp:146
static constexpr KDColor Select
Definition: palette.h:17
static constexpr KDColor GreyMiddle
Definition: palette.h:14
void start()
Definition: rt0.cpp:31
void drawHistogram(KDContext *ctx, KDRect rect, EvaluateModelWithParameter evaluation, void *model, void *context, float firstBarAbscissa, float barWidth, bool fillBar, KDColor defaultColor, KDColor highlightColor, float highlightLowerBound=INFINITY, float highlightUpperBound=-INFINITY) const
Definition: curve_view.cpp:382
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 setHighlight(float start, float end)
KDCoordinate height() const
Definition: rect.h:40
KDRect bounds() const
Definition: view.cpp:157