1 // Written in the D programming language.
2 
3 /**
4 This module contains charts widgets implementation.
5 Currently only SimpleBarChart.
6 
7 
8 Synopsis:
9 
10 ----
11 import dlangui.widgets.charts;
12 
13 // creation of simple bar chart
14 SimpleBarChart chart = new SimpleBarChart("chart");
15 
16 // add bars
17 chart.addBar(12.2, makeRGBA(255, 0, 0, 0), "new bar"c);
18 
19 // update bar with index 0
20 chart.updateBar(0, 10, makeRGBA(255, 255, 0, 0), "new bar updated"c);
21 chart.updateBar(0, 20);
22 
23 // remove bars with index 0
24 chart.removeBar(0, 20);
25 
26 // change title
27 chart.title = "new title"d;
28 
29 // change min axis ratio
30 chart.axisRatio = 0.3; // y axis length will be 0.3 of x axis
31 
32 ----
33 
34 Copyright: Andrzej Kilijański, 2017
35 License:   Boost License 1.0
36 Authors:   Andrzej Kilijański, and3md@gmail.com
37 */
38 
39 module dlangui.widgets.charts;
40 
41 import dlangui.widgets.widget;
42 import std.math;
43 import std.algorithm.comparison;
44 import std.algorithm : remove;
45 import std.conv;
46 
47 class SimpleBarChart : Widget {
48 
49     this(string ID = null) {
50         super(ID);
51         clickable = false;
52         focusable = false;
53         trackHover = false;
54         styleId = "SIMPLE_BAR_CHART";
55         _axisX.arrowSize = 1;
56         title =  UIString.fromId("TITLE_NEW_CHART"c);
57         measureTextToSetWidgetSize();
58     }
59 
60     this(string ID, string titleResourceId) {
61         this(ID);
62         title = UIString.fromId(titleResourceId);
63     }
64 
65     this(string ID, dstring title) {
66         this(ID);
67         this.title = UIString.fromRaw(title);
68     }
69 
70     this(string ID, UIString title) {
71         this(ID);
72         this.title = title;
73     }
74 
75     struct BarData {
76         double y;
77         UIString title;
78         private Point _titleSize;
79         uint color;
80 
81         this (double y, uint color, UIString title) {
82             this.y = y;
83             this.color = color;
84             this.title = title;
85         }
86     }
87 
88     protected BarData[] _bars;
89     protected double _maxY = 0;
90 
91     size_t barCount() {
92         return _bars.length;
93     }
94 
95     void addBar(double y, uint color, UIString barTitle) {
96         if (y < 0)
97             return; // current limitation only positive values
98         _bars ~= BarData(y, color, barTitle);
99         if (y > _maxY)
100             _maxY = y;
101         requestLayout();
102     }
103 
104     void addBar(double y, uint color, string barTitle) {
105         addBar(y, color, UIString.fromId(barTitle));
106     }
107 
108     void addBar(double y, uint color, dstring barTitle) {
109         addBar(y, color, UIString.fromRaw(barTitle));
110     }
111 
112     void removeBar(size_t index) {
113         _bars = remove(_bars, index);
114         // update _maxY
115         _maxY = 0;
116         foreach (ref bar ; _bars) {
117             if (bar.y > _maxY)
118                 _maxY = bar.y;
119         }
120         requestLayout();
121     }
122 
123     void updateBar(size_t index, double y, uint color, string barTitle) {
124         updateBar(index, y, color, UIString.fromId(barTitle));
125     }
126 
127     void updateBar(size_t index, double y, uint color, dstring barTitle) {
128         updateBar(index, y, color, UIString.fromRaw(barTitle));
129     }
130 
131     void updateBar(size_t index, double y, uint color, UIString barTitle) {
132         if (y < 0)
133             return; // current limitation only positive values
134         _bars[index].y = y;
135         _bars[index].color = color;
136         _bars[index].title = barTitle;
137 
138         // update _maxY
139         _maxY = 0;
140         foreach (ref bar ; _bars) {
141             if (bar.y > _maxY)
142                 _maxY = bar.y;
143         }
144         requestLayout();
145     }
146 
147     void updateBar(size_t index, double y) {
148         if (y < 0)
149             return; // curent limitation only positive values
150         _bars[index].y = y;
151 
152         // update _maxY
153         _maxY = 0;
154         foreach (ref bar ; _bars) {
155             if (bar.y > _maxY)
156                 _maxY = bar.y;
157         }
158         requestLayout();
159     }
160 
161     protected UIString _title;
162     protected bool _showTitle = true;
163     protected Point _titleSize;
164     protected int _marginAfterTitle = 2;
165 
166     /// set title to show
167     @property Widget title(string s) {
168         return title(UIString.fromId(s));
169     }
170 
171     @property Widget title(dstring s) {
172         return title(UIString.fromRaw(s));
173     }
174 
175     /// set title to show
176     @property Widget title(UIString s) {
177         _title = s;
178         measureTitleSize();
179         if (_showTitle)
180             requestLayout();
181         return this;
182     }
183 
184     /// get title value
185     @property dstring title() {
186         return _title;
187     }
188 
189     /// show title?
190     @property bool showTitle() {
191         return _showTitle;
192     }
193 
194     @property void showTitle(bool show) {
195         if (_showTitle != show) {
196             _showTitle = show;
197             requestLayout();
198         }
199     }
200 
201     override protected void handleFontChanged() {
202         measureTitleSize();
203         measureTextToSetWidgetSize();
204     }
205 
206     protected void measureTitleSize() {
207         FontRef font = font();
208         _titleSize = font.textSize(_title, MAX_WIDTH_UNSPECIFIED, 4, 0, textFlags); //todo: more than one line title support
209     }
210 
211     @property uint chartBackgroundColor() {return ownStyle.customColor("chart_background_color"); }
212 
213     @property Widget chartBackgroundColor(uint newColor) {
214         ownStyle.setCustomColor("chart_background_color",newColor);
215         invalidate();
216         return this;
217     }
218 
219     @property uint chartAxisColor() {return ownStyle.customColor("chart_axis_color"); }
220 
221     @property Widget chartAxisColor(uint newColor) {
222         ownStyle.setCustomColor("chart_axis_color",newColor);
223         invalidate();
224         return this;
225     }
226 
227     @property uint chartSegmentTagColor() {return ownStyle.customColor("chart_segment_tag_color"); }
228 
229     @property Widget chartSegmentTagColor(uint newColor) {
230         ownStyle.setCustomColor("chart_segment_tag_color",newColor);
231         invalidate();
232         return this;
233     }
234 
235     struct AxisData {
236         Point maxDescriptionSize = Point(30,20);
237         int thickness = 1;
238         int arrowSize = 20;
239         int segmentTagLength = 4;
240         int zeroValueDist = 3;
241         int lengthFromZeroToArrow = 200;
242     }
243 
244     AxisData _axisX;
245     AxisData _axisY;
246 
247     protected int _axisYMaxValueDescWidth = 30;
248     protected int _axisYAvgValueDescWidth = 30;
249 
250     protected double _axisRatio = 0.6;
251 
252     @property double axisRatio() {
253         return _axisRatio;
254     }
255 
256     @property void axisRatio(double newRatio) {
257         _axisRatio = newRatio;
258         requestLayout();
259     }
260 
261     protected int _minBarWidth = 10;
262     protected int _barWidth = 10;
263     protected int _barDistance = 3;
264 
265     protected int _axisXMinWfromZero = 150;
266     protected int _axisYMinDescWidth = 30;
267 
268     protected dstring _textToSetDescLineSize = "aaaaaaaaaa";
269     protected Point _measuredTextToSetDescLineSize;
270 
271     @property dstring textToSetDescLineSize() {
272         return _textToSetDescLineSize;
273     }
274 
275     @property void textToSetDescLineSize(dstring newText) {
276         _textToSetDescLineSize = newText;
277         measureTextToSetWidgetSize();
278         requestLayout();
279     }
280 
281     private int[] _charWidths;
282     protected Point measureTextToSetWidgetSize() {
283         FontRef font = font();
284         _charWidths.length = _textToSetDescLineSize.length;
285         int charsMeasured = font.measureText(_textToSetDescLineSize, _charWidths, MAX_WIDTH_UNSPECIFIED, 4);
286         _measuredTextToSetDescLineSize.x = charsMeasured > 0 ? _charWidths[charsMeasured - 1]: 0;
287         _measuredTextToSetDescLineSize.y = font.height;
288         return _measuredTextToSetDescLineSize;
289     }
290 
291     override void measure(int parentWidth, int parentHeight) {
292         FontRef font = font();
293 
294         int mWidth = minWidth;
295         int mHeight = minHeight;
296 
297         int chartW = 0;
298         int chartH = 0;
299 
300         _axisY.maxDescriptionSize = measureAxisYDesc();
301 
302         int usedWidth = _axisY.maxDescriptionSize.x + _axisY.thickness + _axisY.segmentTagLength + _axisX.zeroValueDist + margins.left + padding.left + margins.right + padding.right + _axisX.arrowSize;
303 
304         int currentMinBarWidth = max(_minBarWidth, _measuredTextToSetDescLineSize.x);
305         _axisX.maxDescriptionSize.y = _measuredTextToSetDescLineSize.y;
306 
307         // axis length
308         _axisX.lengthFromZeroToArrow = cast(uint) barCount * (currentMinBarWidth + _barDistance);
309 
310         if (_axisX.lengthFromZeroToArrow < _axisXMinWfromZero) {
311             _axisX.lengthFromZeroToArrow = _axisXMinWfromZero;
312             if (barCount > 0)
313                 _barWidth = cast (int) ((_axisX.lengthFromZeroToArrow - (_barDistance * barCount)) / barCount);
314         }
315 
316         // minWidth and minHeight check
317 
318         if (minWidth > _axisX.lengthFromZeroToArrow + usedWidth) {
319             _axisX.lengthFromZeroToArrow = minWidth-usedWidth;
320             if (barCount > 0)
321                 _barWidth = cast (int) ((_axisX.lengthFromZeroToArrow - (_barDistance * barCount)) / barCount);
322         }
323 
324         // width FILL_PARENT support
325         if (parentWidth != SIZE_UNSPECIFIED && layoutWidth == FILL_PARENT) {
326             if (_axisX.lengthFromZeroToArrow < parentWidth - usedWidth) {
327                 _axisX.lengthFromZeroToArrow = parentWidth - usedWidth;
328             if (barCount > 0)
329                 _barWidth = cast (int) ((_axisX.lengthFromZeroToArrow - (_barDistance * barCount)) / barCount);
330             }
331         }
332 
333 
334         // initialize axis y length
335         _axisY.lengthFromZeroToArrow = cast(int) round(_axisRatio * _axisX.lengthFromZeroToArrow);
336 
337         // is axis Y enought long
338         int usedHeight = _axisX.maxDescriptionSize.y + _axisX.thickness + _axisX.segmentTagLength + _axisY.zeroValueDist + ((_showTitle) ? _titleSize.y + _marginAfterTitle : 0) + margins.top + padding.top + margins.bottom + padding.bottom + _axisY.arrowSize;
339         if (minHeight > _axisY.lengthFromZeroToArrow + usedHeight) {
340             _axisY.lengthFromZeroToArrow = minHeight - usedHeight;
341             _axisX.lengthFromZeroToArrow = cast (int) round(_axisY.lengthFromZeroToArrow / _axisRatio);
342         }
343 
344         // height FILL_PARENT support
345         if (parentHeight != SIZE_UNSPECIFIED && layoutHeight == FILL_PARENT) {
346             if (_axisY.lengthFromZeroToArrow < parentHeight - usedHeight)
347                 _axisY.lengthFromZeroToArrow = parentHeight - usedHeight;
348         }
349 
350         if (barCount > 0)
351             _barWidth = cast (int) ((_axisX.lengthFromZeroToArrow - (_barDistance * barCount)) / barCount);
352 
353         // compute X axis max description height
354         _axisX.maxDescriptionSize = measureAxisXDesc();
355 
356         // compute chart size
357         chartW = _axisY.maxDescriptionSize.x + _axisY.thickness + _axisY.segmentTagLength + _axisX.zeroValueDist + _axisX.lengthFromZeroToArrow + _axisX.arrowSize;
358         if (_showTitle && chartW < _titleSize.y)
359             chartW = _titleSize.y;
360 
361         chartH = _axisX.maxDescriptionSize.y + _axisX.thickness + _axisX.segmentTagLength + _axisY.zeroValueDist + _axisY.lengthFromZeroToArrow + ((_showTitle) ? _titleSize.y + _marginAfterTitle : 0) + _axisY.arrowSize;
362         measuredContent(parentWidth, parentHeight, chartW, chartH);
363     }
364 
365 
366     protected Point measureAxisXDesc() {
367         Point sz;
368         foreach (ref bar ; _bars) {
369             bar._titleSize = font.measureMultilineText(bar.title, 0, _barWidth, 4, 0, textFlags);
370             if (sz.y < bar._titleSize.y)
371                 sz.y = bar._titleSize.y;
372             if (sz.x < bar._titleSize.x)
373                 sz.x = bar._titleSize.y;
374         }
375         return sz;
376     }
377 
378     protected Point measureAxisYDesc() {
379         int maxDescWidth = _axisYMinDescWidth;
380         double currentMaxValue = _maxY;
381         if (approxEqual(_maxY, 0, 0.0000001, 0.0000001))
382             currentMaxValue = 100;
383 
384         Point sz = font.textSize(to!dstring(currentMaxValue), MAX_WIDTH_UNSPECIFIED, 4, 0, textFlags);
385         if (maxDescWidth<sz.x)
386             maxDescWidth=sz.x;
387         _axisYMaxValueDescWidth = sz.x;
388         sz = font.textSize(to!dstring(currentMaxValue / 2), MAX_WIDTH_UNSPECIFIED, 4, 0, textFlags);
389         if (maxDescWidth<sz.x)
390             maxDescWidth=sz.x;
391         _axisYAvgValueDescWidth = sz.x;
392         return Point(maxDescWidth, sz.y);
393     }
394 
395     protected int barYValueToPixels(int axisInPixels, double barYValue ) {
396         double currentMaxValue = _maxY;
397         if (approxEqual(_maxY, 0, 0.0000001, 0.0000001))
398             currentMaxValue = 100;
399 
400         double pixValue = axisInPixels / currentMaxValue;
401         return cast(int) round(barYValue * pixValue);
402     }
403 
404     override void onDraw(DrawBuf buf) {
405         if (visibility != Visibility.Visible)
406             return;
407         super.onDraw(buf);
408 
409         Rect rc = _pos;
410         applyMargins(rc);
411         applyPadding(rc);
412 
413         auto saver = ClipRectSaver(buf, rc, alpha);
414 
415         FontRef font = font();
416         if (_showTitle)
417             font.drawText(buf, rc.left+ (_measuredWidth - _titleSize.x)/2  , rc.top, _title, textColor, 4, 0, textFlags);
418 
419         // draw axises and
420         int x1 = rc.left + _axisY.maxDescriptionSize.x + _axisY.segmentTagLength;
421         int x2 = rc.left + _axisY.maxDescriptionSize.x + _axisY.segmentTagLength + _axisY.thickness + _axisX.zeroValueDist + _axisX.lengthFromZeroToArrow + _axisX.arrowSize;
422         int y1 = rc.bottom - _axisX.maxDescriptionSize.y - _axisX.segmentTagLength - _axisX.thickness - _axisY.zeroValueDist - _axisY.lengthFromZeroToArrow - _axisY.arrowSize;
423         int y2 = rc.bottom - _axisX.maxDescriptionSize.y - _axisX.segmentTagLength;
424 
425         buf.fillRect(Rect(x1, y1, x2, y2), chartBackgroundColor);
426 
427         // y axis
428         buf.drawLine(Point(x1 + 1, y1), Point(x1 + 1, y2), chartAxisColor);
429 
430         // x axis
431         buf.drawLine(Point(x1, y2 - 1), Point(x2, y2 - 1), chartAxisColor);
432 
433         // top line - will be optional in the future
434         buf.drawLine(Point(x1, y1), Point(x2, y1), chartAxisColor);
435 
436         // right line - will be optional in the future
437         buf.drawLine(Point(x2, y1), Point(x2, y2), chartAxisColor);
438 
439         // draw bars
440 
441         int firstBarX = x1 + _axisY.thickness + _axisX.zeroValueDist;
442         int firstBarY = y2 - _axisX.thickness - _axisY.zeroValueDist;
443 
444         SimpleTextFormatter fmt;
445         foreach (ref bar ; _bars) {
446             // draw bar
447             buf.fillRect(Rect(firstBarX, firstBarY - barYValueToPixels(_axisY.lengthFromZeroToArrow, bar.y), firstBarX + _barWidth, firstBarY), bar.color);
448 
449             // draw x axis segment under bar
450             buf.drawLine(Point(firstBarX + _barWidth / 2, y2), Point(firstBarX + _barWidth / 2, rc.bottom - _axisX.maxDescriptionSize.y), chartSegmentTagColor);
451 
452             // draw x axis description
453             fmt.format(bar.title, font, 0, _barWidth, 4, 0, textFlags);
454             fmt.draw(buf, firstBarX + (_barWidth - bar._titleSize.x) / 2, rc.bottom -  _axisX.maxDescriptionSize.y + (_axisX.maxDescriptionSize.y - bar._titleSize.y) / 2, font, textColor, Align.HCenter);
455 
456             firstBarX += _barWidth + _barDistance;
457         }
458 
459         // segments on y axis and values (now only max and max/2)
460         double currentMaxValue = _maxY;
461         if (approxEqual(_maxY, 0, 0.0000001, 0.0000001))
462             currentMaxValue = 100;
463 
464         int yZero = rc.bottom - _axisX.maxDescriptionSize.y - _axisX.segmentTagLength - _axisX.thickness - _axisY.zeroValueDist;
465         int yMax = yZero - _axisY.lengthFromZeroToArrow;
466         int yAvg = (yZero + yMax) / 2;
467 
468         buf.drawLine(Point(rc.left + _axisY.maxDescriptionSize.x, yMax), Point(rc.left + _axisY.maxDescriptionSize.x + _axisY.segmentTagLength, yMax), chartSegmentTagColor);
469         buf.drawLine(Point(rc.left + _axisY.maxDescriptionSize.x, yAvg), Point(rc.left + _axisY.maxDescriptionSize.x + _axisY.segmentTagLength, yAvg), chartSegmentTagColor);
470 
471         font.drawText(buf, rc.left + (_axisY.maxDescriptionSize.x - _axisYMaxValueDescWidth), yMax - _axisY.maxDescriptionSize.y / 2, to!dstring(currentMaxValue), textColor, 4, 0, textFlags);
472         font.drawText(buf, rc.left + (_axisY.maxDescriptionSize.x - _axisYAvgValueDescWidth), yAvg - _axisY.maxDescriptionSize.y / 2, to!dstring(currentMaxValue / 2), textColor, 4, 0, textFlags);
473 
474     }
475 
476     override void onThemeChanged() {
477         super.onThemeChanged();
478         handleFontChanged();
479     }
480 }
481