AWind
ChartWindow.h
1 /*
2  AWind.h - Arduino window library support for Color TFT LCD Boards
3  Copyright (C)2015 Andrei Degtiarev. All right reserved
4 
5 
6  You can always find the latest version of the library at
7  https://github.com/AndreiDegtiarev/AWind
8 
9 
10  This library is free software; you can redistribute it and/or
11  modify it under the terms of the CC BY-NC-SA 3.0 license.
12  Please see the included documents for further information.
13 
14  Commercial use of this library requires you to buy a license that
15  will allow commercial use. This includes using the library,
16  modified or not, as a tool to sell products.
17 
18  The license applies to all part of the library including the
19  examples and tools supplied with the library.
20 */
21 #pragma once
22 
23 #include "Window.h"
24 #include "IDataBuffer.h"
25 #include "TextBoxNumber.h"
26 #include "ChartDC.h"
27 #include "DecoratorPrimitives.h"
28 
30 class ChartWindow : public Window
31 {
32  IDataBuffer *_buffer;
33  unsigned long _last_buffer_change;
34  ChartDC _dc;
35  DecoratorAxis *_xAxis;
36  DecoratorAxis *_yAxis;
37 public:
39 
47  ChartWindow(DecoratorAxis *xAxis,DecoratorAxis *yAxis,int left,int top,int width,int hight):Window(F("chart"),left,top,width,hight)
48  {
49  _buffer = NULL;
50  _xAxis=xAxis;
51  _yAxis=yAxis;
52  }
54  void SetBuffer(IDataBuffer *buffer)
55  {
56  _buffer=buffer;
57  _last_buffer_change=0;
58  }
61  {
62  if(_yAxis == NULL)
63  {
64  Invalidate();
65  }
66  else
67  {
68  DC dc;
69  PrepareDC(&dc);
70  OnDraw(&dc);
71  }
72  }
74  void OnDraw(DC *dc)
75  {
76  if(_buffer!=NULL && _buffer->StartIndex()<_buffer->Size()-1)
77  {
78  unsigned int size=_buffer->Size();
79  float min_x;
80  float max_x;
81  float min_y;
82  float max_y;
83  int xOffset=1;
84  _buffer->MinMax(min_x,max_x,min_y,max_y);
85  if(_yAxis!=NULL)
86  {
87  xOffset=_yAxis->EstimateRight(dc);
88  _yAxis->GetMinMax(min_y,max_y);
89  }
90  if(_last_buffer_change!=_buffer->X(size-1))
91  {
92  _dc.setScalingX(xOffset,Width()-xOffset-1,min_x,max_x);
93  _dc.setScalingY(Height()-1,Height()-2,min_y,max_y);
94  _last_buffer_change=max_x;
95  }
96  dc->SetBackColor(Color::Black);
97  dc->FillRect(xOffset+1,2+1,Width()-2-1,Height()-2-1);
98  dc->Rectangle3D(xOffset,2,Width()-2,Height()-2,Color::DarkGray,Color::White);
99 
100  dc->SetColor(Color::Yellow);
101  _dc.MoveTo(dc,min_x,max(min_y,0));
102  _dc.LineTo(dc,max_x,max(min_y,0));
103  if(_yAxis==NULL)
104  {
105  _dc.MoveTo(dc,max(min_x,0),min_y);
106  _dc.LineTo(dc,max(min_x,0),max_y);
107  }
108  dc->SetColor(Color::LightBlue);
109  dc->SetFont(SmallFont);
110  if(_yAxis == NULL)
111  {
112  dc->DrawNumber(_dc.MaxY(),1,1,1);
113  dc->DrawNumber(_dc.MinY(),1,1,Height()-15);
114  }
115  _dc.MoveTo(dc,_buffer->X(_buffer->StartIndex()),_buffer->Y(_buffer->StartIndex()));
116  for(int i=_buffer->StartIndex()+1;i<size;i++)
117  {
118  _dc.LineTo(dc,_buffer->X(i),_buffer->Y(i));
119  }
120  }
121  }
122 };
int EstimateRight(DC *dc)
Estimates decorator right coordinate.
Definition: DecoratorPrimitives.h:219
Base class for all window objects. Provides basic window functionality.
Definition: Window.h:34
void FillRect(int left, int top, int right, int bottom)
Fills rectangle. Input coordinates have to be defined in the window coordinate system.
Definition: DC.h:116
Device context for chart window. Implement transformation of logic coordinates into window coordinate...
Definition: ChartDC.h:23
void DrawNumber(int number, int x, int y)
Draws integer number. Input coordinates have to be defined in the window coordinate system...
Definition: DC.h:161
void InvalidateOnlyChartArea()
Forces to repain only chart arey (not axises) - reduces flickering.
Definition: ChartWindow.h:60
Axis decorator primitive. It is shared between gauge and chart objects. Overriden members description...
Definition: DecoratorPrimitives.h:145
void PrepareDC(DC *dc)
Setups window coordinate system. This function called by window manager right before window has to be...
Definition: Window.h:249
Device context. Abstraction layer to the device specific drawing code. Coordinates in drawing functio...
Definition: DC.h:29
void MoveTo(DC *dc, float x, float y)
Moves actual position in logic coordinates.
Definition: ChartDC.h:106
Chart window implement XY plots.
Definition: ChartWindow.h:30
int Height()
Returns window height.
Definition: Window.h:189
void Invalidate()
If function is called than the window manager updates the window.
Definition: Window.h:157
ChartWindow(DecoratorAxis *xAxis, DecoratorAxis *yAxis, int left, int top, int width, int hight)
Constructor.
Definition: ChartWindow.h:47
void SetBuffer(IDataBuffer *buffer)
Sets data buffer.
Definition: ChartWindow.h:54
void Rectangle3D(int left, int top, int right, int bottom, Color color1, Color color2)
Draws rectangle with 3D border. Input coordinates have to be defined in the window coordinate system...
Definition: DC.h:105
void GetMinMax(float &minVal, float &maxVal)
Return min and max label values.
Definition: DecoratorPrimitives.h:194
void LineTo(DC *dc, float x, float y)
Draws line from actual position to the new position.
Definition: ChartDC.h:114
int Width()
Returns window width.
Definition: Window.h:184
void OnDraw(DC *dc)
Implements drawing code. Please note axises are plotted as decorators.
Definition: ChartWindow.h:74