/
Visualizer.cs
1085 lines (900 loc) · 40.4 KB
/
Visualizer.cs
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using static Statistics;
/// <summary>
/// This helps to visualize statistics. X-values can only be numbers or DateTime. (Time will be shown in hours.)
/// </summary>
public class Visualizer : MonoBehaviour
{
// the overall panel object, which the visualization is shown up in
[SerializeField] private GameObject panel;
// the distance between max value and the frame of the panel
[SerializeField] private float yDistance = 20;
// the line, which indicates the average y value
[SerializeField] private Image averageLine;
// the statistics this visualizer is using to show
[SerializeField] private Statistics statistics;
// the statistics viewer that helps the user to visualize only a part of the data in the entire graph view
[SerializeField] private StatisticsViewer statisticsViewer;
// indicates whether the visualization is drawn / shown or not
private bool shown;
// list containing all lines connecting the points
private List<Image> lines;
// list containing all circles representing points
private List<RectTransform> circles;
// the thickness of the lines
[SerializeField] private float thickness = 2;
// the radius of the circles representing points
[SerializeField] private float circleRadius = 15f;
// the game object which is a parent to all lines and circles, so when this is moved, all lines and circles are moved as well simultaneously
private GameObject graph;
// the parents of the line and circle game objects in the hierarchy
private GameObject graphLines, graphCircles;
// allows the user to scroll (autoscrolls if on the very right of graph)
private GameObject scrollView;
// the gameobject that is moved to scroll
private GameObject scrollContent;
// the color of the lines
[SerializeField] private Color lineColor = new Color(0.7f, 0.7f, 0.7f, 1f);
// the color of the circles
[SerializeField] private Color circleColor = new Color(1f, 0.5f, 0f);
// the color of the x and y axis
[SerializeField] private Color ledgerLineColor = new Color(1f, 1f, 1f, 0.1f);
// the color of the x and y axis
[SerializeField] private Color axisColor = new Color(1f, 1f, 1f, 0.25f);
// the color of the average line
[SerializeField] private Color averageLineColor = new Color(1f, 1f, 1f, 0.4f);
// the dynamic and minimum scale of the x axis
private float widthPerUnit, minWidthPerUnit;
// the x and y axis panel
private GameObject xAxisPanel, yAxisPanel, xLedgerPanel;
// the amount of ledger lines on the y axis
[SerializeField] private int yLedgerLines = 5;
private List<Text> yTexts;
// the amount of ledger lines on the x axis that can be seen on the screen at once
[SerializeField] private int xLedgerLines = 5;
private List<Text> xTexts;
private List<RectTransform> xLines;
// the height of each ledger line on the x axis
[SerializeField] private float xLedgerLineHeight = 15;
// the amount of decimal places to round to
[Range(1, 4)] [SerializeField] private int roundDecimalPlaces = 1;
// the size of the info text on the x and y axis
[SerializeField] private int fontSize = 200;
// the maximum unit value shown on the x axis (is neglected if xAxisIsLimited == false)
[SerializeField] private int xMax;
// indicates whether the user is able to scroll or not, as the graph is greater than it is shown on the screen
private bool maxXReached;
// indicates whether the graph should fill the content panel or can escape from it to the right
private bool keepEntireGraph;
// the unit of the x values (needed to know how to treat the x axis ledger line info)
[SerializeField] private xUnit.Units unit = xUnit.Units.HOURS;
// is true if the the graph is bigger than the ui and the first x info texts has been updated for the last time, otherwise it is set to false
private bool maxReachedFirstUpdated;
// the currently shown information panel
private PointInformationPanel currentPointInformationPanel;
/// <summary>
/// This methods is used to set all important values that the visualizer needs to show the statistics
/// </summary>
public void SetValues(Statistics statistics)
{
// assign the statistics to the corresponding instance variable
this.statistics = statistics;
// set the visualizer of the statistics to this one
statistics.SetVisualizer(this);
// instantiate empty lists
lines = new List<Image>();
circles = new List<RectTransform>();
// assign the scroll view
scrollView = panel.transform.Find("Scroll View").gameObject;
// assign the content panel
scrollContent = scrollView.transform.Find("Viewport").transform.Find("Content").gameObject;
// assign the empty graph game object
graph = scrollContent.transform.Find("Graph").gameObject;
// assign the line parent
graphLines = graph.transform.Find("Lines").gameObject;
// assign the circle parent
graphCircles = graph.transform.Find("Circles").gameObject;
// assign the axis panels
xAxisPanel = panel.transform.Find("x-Axis Panel").gameObject;
yAxisPanel = panel.transform.Find("y-Axis Panel").gameObject;
// assign the x axis info panel
xLedgerPanel = graph.transform.Find("xLedgerLines").gameObject;
// set the distance that corresponds to one unit of x and the ui coordinate system
minWidthPerUnit = scrollContent.GetComponent<RectTransform>().sizeDelta.x / xMax;
// set the width scale on the x axis to the width at beginning (is changing over the time)
widthPerUnit = scrollContent.GetComponent<RectTransform>().sizeDelta.x;
// update the width per unit on the x axis
UpdateWidthPerUnit(false);
}
/// <summary>
/// This method make the visualizer draw all lines and circles (dynamically).
/// </summary>
public void Show()
{
// If the visualizer is already showing the graph, do not show the graph again.
if (shown) return;
shown = true;
// update the width per unit on the x axis
UpdateWidthPerUnit(false);
// show the x and y axis panel and draw its ui
DrawInfoUI();
// draw all connecting lines
for (int i = 0; i < statistics.GetPoints().Count - 1; i++)
{
// draw a line between this and the next point
DrawLine(statistics.GetPoints()[i], statistics.GetPoints()[i + 1]);
}
// draw all circles
for (int i = 0; i < statistics.GetPoints().Count; i++)
{
// a circle at the point
DrawCircle(statistics.GetPoints()[i]);
}
}
/// <summary>
/// This method draws the legder lines and shows its values.
/// </summary>
private void DrawInfoUI()
{
// draw the y axis
DrawYAxis();
// the size of the panel where the graph can be seen in
Vector2 viewSize = scrollView.GetComponent<RectTransform>().sizeDelta;
// the size of the y panel
Vector2 yPanelSize = yAxisPanel.GetComponent<RectTransform>().sizeDelta;
Vector2 xPanelSize = xAxisPanel.GetComponent<RectTransform>().sizeDelta;
// instantiate empty lists
yTexts = new List<Text>();
xTexts = new List<Text>();
xLines = new List<RectTransform>();
// the distance between each y ledger line
float yPart = (viewSize.y - yDistance) / (float)(yLedgerLines - 1);
// create all ledger lines and texts on the y axis
for (int i = 0; i < yLedgerLines; i++)
{
// create a new ledger line object
GameObject ledgerObj = Object.Instantiate(UIManager.GetInstance().imagePrefab, yAxisPanel.transform);
// rename it
ledgerObj.name = "LedgerLine" + yAxisPanel.transform.childCount;
// calculate its position
Vector2 pos = new Vector2(viewSize.x / 2 + yPanelSize.x / 2, yPart * i - yPanelSize.y / 2);
// set its position and size
RectTransform rt = ledgerObj.GetComponent<RectTransform>();
rt.localPosition = pos;
rt.sizeDelta = new Vector2(viewSize.x, thickness);
// set its color
Image img = ledgerObj.GetComponent<Image>();
img.color = ledgerLineColor;
// create text
GameObject textObj = Object.Instantiate(UIManager.GetInstance().textPrefab, yAxisPanel.transform);
// rename the text object
textObj.name = "Info" + yAxisPanel.transform.childCount;
// set the position
RectTransform textRt = textObj.GetComponent<RectTransform>();
textRt.localPosition = new Vector2(0, pos.y);
// get and set the text, font size and color
Text text = textObj.GetComponent<Text>();
text.text = "";
text.fontSize = fontSize;
text.color = axisColor;
// add the text to the list
yTexts.Add(text);
}
float xStep = viewSize.x / (float)(xLedgerLines + 1);
for (int i = 0; i < xLedgerLines; i++)
{
// calculate the position of the text info and the ledger line
Vector2 pos = new Vector2(xStep * (i + 1) - xPanelSize.x / 2, 0);
// create the info text and ledger line
AddXInfo(pos.x);
}
// set the y and x info texts properly
UpdateYInfoText();
UpdateXInfoText();
}
/// <summary>
/// This method creates a info text and ledger line on the x axis at the particular x position. At the end both object are added to the lists 'xLists' and 'xTexts'.
/// </summary>
private void AddXInfo(float xPos, object xObj = null)
{
// convert the x object to a readable string
string infoText = "";
if (xObj != null)
{
infoText = GetNameFromXObject(xObj);
}
RectTransform scrollRt = scrollContent.GetComponent<RectTransform>();
// create a new ledger line object
GameObject ledgerObj = Object.Instantiate(UIManager.GetInstance().imagePrefab, xLedgerPanel.transform);
// rename the object
ledgerObj.name = "LedgerLine" + xLedgerPanel.transform.childCount;
Vector2 pos = new Vector2(xPos, -scrollRt.sizeDelta.y / 2 + xLedgerLineHeight / 2 + thickness / 2);
// set its position and size
RectTransform rt = ledgerObj.GetComponent<RectTransform>();
rt.localPosition = pos;
rt.sizeDelta = new Vector2(thickness, xLedgerLineHeight);
// set its color
Image img = ledgerObj.GetComponent<Image>();
img.color = ledgerLineColor;
// add the ledger line to the list
xLines.Add(rt);
// create text
GameObject textObj = Instantiate(UIManager.GetInstance().textPrefab, xLedgerPanel.transform);
// rename the object
textObj.name = "Info" + xLedgerPanel.transform.childCount;
// set the position
RectTransform textRt = textObj.GetComponent<RectTransform>();
textRt.localPosition = new Vector2(pos.x, -scrollRt.sizeDelta.y / 2 - fontSize / 10);
// get and set the text, font size and color
Text text = textObj.GetComponent<Text>();
text.text = infoText;
text.fontSize = fontSize;
text.color = axisColor;
// add the text to the list
xTexts.Add(text);
// reparent it to the x info object
textObj.transform.SetParent(xAxisPanel.transform);
// make the x info text follow the x ledger line which is still a child of the content
ObjectFollower objFollower = textObj.AddComponent<ObjectFollower>();
objFollower.Follow(ledgerObj);
}
/// <summary>
/// This method draws the y axis.
/// </summary>
private void DrawYAxis()
{
// If the x and y axis line already exist, do not draw them again.
if (yAxisPanel.transform.Find("Line")) return;
//****************** y axis ******************\\
GameObject yLine = Object.Instantiate(UIManager.GetInstance().imagePrefab, yAxisPanel.transform);
// rename the instantiated object
yLine.name = "Line";
RectTransform yRt = yLine.GetComponent<RectTransform>();
// resize the line
yRt.sizeDelta = new Vector2(thickness, yAxisPanel.GetComponent<RectTransform>().sizeDelta.y);
// position the line on the right side
yRt.localPosition = new Vector2(yAxisPanel.GetComponent<RectTransform>().sizeDelta.x / 2 - yRt.sizeDelta.x / 2, -thickness / 2);
// set the color
Image yImg = yLine.GetComponent<Image>();
yImg.color = ledgerLineColor;
}
/// <summary>
/// This method deletes and stops drawing all lines and circles.
/// </summary>
public void Close()
{
// If the visualizer is not showing the graph any more, do not delete the graph again.
if (shown == false) return;
shown = false;
// delete all circles and lines
for (int i = 0; i < graphLines.transform.childCount; i++)
{
Object.Destroy(graphLines.transform.GetChild(i).gameObject);
}
for (int i = 0; i < graphCircles.transform.childCount; i++)
{
Object.Destroy(graphCircles.transform.GetChild(i).gameObject);
}
circles.Clear();
lines.Clear();
}
/// <summary>
/// This method translates the statistics data to ui coordinates.
/// </summary>
private Vector2 GetPositionFromPoint(Point point)
{
// get the minimum and the maximum value of the statistics
float yMin = statistics.GetYMin();
float yMax = statistics.GetYMax();
// If the statistics has neither yMin nor yMax calculated, ...
if (statistics.GetPoints().Count < 2)
{
// ... return this default position.
return new Vector2(0, 0);
}
// get the x object of the point
object xObj = point.GetX();
// the x value that is computed next
float x = GetXValue(xObj);
// get the y value of the point
float y = point.GetY();
// get the size of the panel
RectTransform rt = scrollView.GetComponent<RectTransform>();
float height = rt.sizeDelta.y;
float width = rt.sizeDelta.x;
// compute the ui coordinate and return it
float percentageOfHeight = (y - yMin) / (yMax - yMin);
float uiX = x * widthPerUnit - width / 2;
float uiY = -height / 2 + percentageOfHeight * (height - yDistance);
return new Vector2(uiX, uiY);
}
/// <summary>
/// This method calculates the Point from the particular ui position.
/// </summary>
private Point GetPointFromPosition(Vector2 pos)
{
// get the size of the content view
Vector2 contentSize = scrollContent.GetComponent<RectTransform>().sizeDelta;
// calculate what percentage the x and y pos are of the width and height
float percX = (pos.x + scrollView.GetComponent<RectTransform>().sizeDelta.x / 2) / contentSize.x;
float percY = pos.y / (contentSize.y - yDistance);
// now multiply these percentages width the spans in the 'Point coordinate system'
float y = (statistics.GetYMax() - statistics.GetYMin()) * percY + statistics.GetYMin();
object x;
// If the current unit is a time unit, ...
if (unit != xUnit.Units.NUMBER)
{
// ... calculate the x value, as if it is time unit.
x = ((System.DateTime)statistics.GetPoints()[0].GetX()).AddHours(GetXDifference() * percX);
}
// Otherwise ...
else
{
// ... calculate the x value, as if it is a common number.
x = (float)System.Convert.ToDouble(statistics.GetLastPoint().GetX()) + GetXDifference() * percX;
}
return new Point(x, y);
}
/// <summary>
/// This method converts an x object to a float.
/// </summary>
private float GetXValue(object xObj)
{
// If x is type of 'DateTime', ...
if (xObj.GetType() == System.DateTime.Now.GetType())
{
// ... convert this DateTime object to an amount of hours.
return DateToHours((System.DateTime)xObj);
}
// Otherwise ...
else
{
// ... convert x to a float.
return (float)System.Convert.ToDouble(xObj);
}
}
/// <summary>
/// This method calculates the differnce between the very first and last x value.
/// </summary>
private float GetXDifference()
{
if (statistics.GetPoints().Count <= 1) return 1;
return GetXValue(statistics.GetPoints()[statistics.GetPoints().Count - 1].GetX()) - GetXValue(statistics.GetPoints()[0].GetX());
}
/// <summary>
/// This method converts DateTime to a float, which represents the hours since data collection started.
/// </summary>
public float DateToHours(System.DateTime dateTime)
{
// subtract the very first DateTime from the the given DateTime
System.TimeSpan timeSpan = dateTime.Subtract((System.DateTime)statistics.GetPoints()[0].GetX());
// round to three decimal places and return it
return Mathf.Round((float)(timeSpan.TotalHours * 1000f)) / 1000f;
}
/// <summary>
/// This method draws a circle at the particular point.
/// </summary>
private void DrawCircle(Point point)
{
// create a circle
GameObject pointImage = Object.Instantiate(UIManager.GetInstance().pointPrefab, graphCircles.transform);
// rename this object
pointImage.name = "Circle_" + graphCircles.transform.childCount;
// set the information for the point
PointInfo pointInfo = pointImage.GetComponent<PointInfo>();
pointInfo.SetVisualizer(this);
pointInfo.SetIndex(point.GetIndexOfList(statistics.GetPoints()));
RectTransform rt = pointImage.GetComponent<RectTransform>();
// position it right there, where the point is located in the visualization
rt.localPosition = GetPositionFromPoint(point);
// resize the circle
rt.sizeDelta = new Vector2(circleRadius, circleRadius);
Image img = pointImage.transform.GetChild(0).GetComponent<Image>();
// set the shape to a circular shape
img.sprite = UIManager.GetInstance().circleSprite;
// set the color
img.color = circleColor;
// add the RectTransform component to the circle list
circles.Add(rt);
}
/// <summary>
/// This method draws a line between two particular points. The line shows up in the visualization immediately.
/// </summary>
private void DrawLine(Point point1, Point point2)
{
// create image object
GameObject lineObject = Object.Instantiate(UIManager.GetInstance().imagePrefab, graphLines.transform);
// rename this object
lineObject.name = "Line_" + graphLines.transform.childCount;
// get and store its image component
Image img = lineObject.GetComponent<Image>();
lines.Add(img);
// set the color of the image
img.color = lineColor;
// set the rotation, position and size of the image correctly
SetLineTransform(img.GetComponent<RectTransform>(), point1, point2);
}
/// <summary>
/// This sets the rotation, position and length of the line between two particular points.
/// </summary>
private void SetLineTransform(RectTransform rt, Point point1, Point point2)
{
// set the thickness of the image
rt.sizeDelta = new Vector2(thickness, thickness);
// translate the statistics points to points that meet the ui coordinate system
Vector2 pos1 = GetPositionFromPoint(point1);
Vector2 pos2 = GetPositionFromPoint(point2);
// set its position (right between point1 and point2)
Vector2 middlePos = (pos1 + pos2) / 2;
rt.localPosition = middlePos;
// set the thickness and length of the line
rt.sizeDelta = new Vector2(Vector2.Distance(pos1, pos2), thickness);
// rotate the line, so point1 and point2 are connected
float angle = Mathf.Atan((pos2.y - pos1.y) / (pos2.x - pos1.x)) * 57.2958f;
rt.localEulerAngles = new Vector3(0, 0, angle);
}
/// <summary>
/// This method is called, when a new point is added to the statistics.
/// </summary>
/// <param name="changed"> is true if yMin or yMax changed </param>
public void OnPointAdded(bool changed)
{
// draw and update all lines and circles only if the visualizer is supposed to
if (shown == false) return;
// update the width per unit on the x axis if necessary
UpdateWidthPerUnit(changed);
// If the statistics has more than 1 point, ...
if (statistics.GetPoints().Count > 1)
{
// ... draw a line between the last two points.
DrawLine(statistics.GetPoints()[statistics.GetPoints().Count - 2], statistics.GetLastPoint());
}
// draw a circle at the ui positition of the latest added point
DrawCircle(statistics.GetLastPoint());
// update the size and the position of the scroll view
UpdateScrollView();
// update the x info texts
UpdateXInfoText();
// update the y position of the average line
UpdateAverageLine();
}
/// <summary>
/// This method is called when more than one point has been added to the statistics.
/// </summary>
/// <param name="amount"> the amount of new points </param>
public void OnPointsAdded(int amount)
{
// draw and update all lines and circles only if the visualizer is supposed to
if (shown == false) return;
// update the width per unit on the x axis if necessary
UpdateWidthPerUnit(true);
// draw all circles and lines, until the second last point of the list has been reached
// then just call the 'OnPointAdded' method, which finishes it up by drawing the last point
for (int i = statistics.GetPoints().Count - amount; i < statistics.GetPoints().Count - 1; i++)
{
// If the current point is not the very first one, ...
if (i > 0)
{
// ... draw a line between the last two points.
DrawLine(statistics.GetPoints()[i - 1], statistics.GetPoints()[i]);
}
// draw a circle at the ui positition of the latest added point
DrawCircle(statistics.GetPoints()[i]);
}
OnPointAdded(true);
}
/// <summary>
/// This method resizes and repositions the scroll view, so the user is able to scroll to the right and
/// to the left to see the entire graph.
/// </summary>
private void UpdateScrollView()
{
// If the graph can be larger than the width of the content panel, ...
if (keepEntireGraph == false)
{
// ... and if the maximum x value is reached, ...
if (maxXReached)
{
// ... resize the scroll view, so scrolling can be done.
RectTransform rt = scrollContent.GetComponent<RectTransform>();
float scrollViewWidth = scrollView.GetComponent<RectTransform>().sizeDelta.x;
float x = Mathf.Abs(GetPositionFromPoint(statistics.GetPoints()[0]).x
- GetPositionFromPoint(statistics.GetLastPoint()).x);
float onTheVeryRightX = -1 * rt.sizeDelta.x + scrollViewWidth / 2;
// If the scroll view is not on the very right, ...
if ((int)onTheVeryRightX >= (int)rt.localPosition.x - 10)
{
// ... reposition the scroll view to the very right.
rt.localPosition = new Vector2(-(1.5f * x - scrollViewWidth), rt.localPosition.y);
}
// adapt the width of the content panel, so that the previous added point cann be seen
// on the very right
rt.sizeDelta = new Vector2(x, rt.sizeDelta.y);
}
}
}
/// <summary>
/// This method sets the bool 'keepEntireGraph' to true or false. This bool indicates whether the whole graph should be seen in the content panel or not and scrolling is allowed.
/// </summary>
public void KeepEntireGraph(bool keep)
{
keepEntireGraph = keep;
UpdateWidthPerUnit(false);
}
/// <summary>
/// This method recalculates the width per unit on the x axis if necessary and updates the whole
/// visualization afterwards.
/// </summary>
private void UpdateWidthPerUnit(bool changed)
{
// If the minimum width scale has not been reached yet or the graph should be kept in the content panel, ...
if (xMaxIsReached() == false || keepEntireGraph)
{
// ... calculate this scale.
float calculatedScale = scrollView.GetComponent<RectTransform>().sizeDelta.x / GetXDifference();
// If the calculated scale is less than the minimum width, ...
if (calculatedScale <= minWidthPerUnit && keepEntireGraph == false)
{
// ... alter the calculated scale value then.
calculatedScale = minWidthPerUnit;
maxXReached = true;
}
// assign the new scale
widthPerUnit = calculatedScale;
// update the entire graph, as the width per unit on the x axis has changed
UpdateGraph();
return;
}
// If yMin or yMax changed, update the graph.
if (changed) UpdateGraph();
}
/// <summary>
/// This method checks whether the graph can be seen in the scroll view entirely or not.
/// </summary>
private bool xMaxIsReached()
{
return widthPerUnit <= minWidthPerUnit;
}
/// <summary>
/// This method updates position, rotation and thickness of all lines and circles. It is called, when yMin or yMax of the statistics changed.
/// </summary>
public void UpdateGraph()
{
// If the statistics has more than one point, ...
if (statistics.GetPoints().Count <= 1) return;
// ... go on and update the graph.
// If the visualization does not have any circles or lines, an update is not necesarry.
if (circles == null || lines == null) return;
// Iterate over all circles
for (int i = 0; i < circles.Count; i++)
{
// update the position of the current circle
RectTransform rt = circles[i];
rt.localPosition = GetPositionFromPoint(statistics.GetPoints()[i]);
// If i is 1 or greater, ...
if (i > 0)
{
// ... update the RectTransform component of the line connecting this and the previous point.
SetLineTransform(lines[i - 1].GetComponent<RectTransform>(), statistics.GetPoints()[i - 1], statistics.GetPoints()[i]);
}
}
// update the info values on the y axis
UpdateYInfoText();
}
/// <summary>
/// This method updates the info texts on the y axis.
/// </summary>
private void UpdateYInfoText()
{
// the y value steps between each ledger line
float steps = (statistics.GetYMax() - statistics.GetYMin()) / (float)(yTexts.Count - 1);
for (int i = 0; i < yTexts.Count; i++)
{
// update the values
yTexts[i].text = Round(statistics.GetYMin() + steps * i) + "";
}
}
/// <summary>
/// This method updates the info texts on the x axis, when xMax has not been reached yet.
/// </summary>
private void UpdateXInfoText()
{
if (statistics == null) return;
// If the statistics does not have any points, stop.
if (statistics.GetPoints() == null) return;
if (statistics.GetPoints().Count == 0) return;
// If the width per unit is adjusting, ...
if (xMaxIsReached() == false)
{
// update the x info texts
UpdateXInfoTextFullShown();
}
// Otherwise ...
else
{
// If the graph is about to become bigger than the ui, update the x info texts for the last time.
if (maxReachedFirstUpdated == false)
{
// update the first x info texts
UpdateXInfoTextFullShown(xMax / (float)(xLedgerLines + 1));
// do not update the first x info texts not any more
maxReachedFirstUpdated = true;
}
// ... add a ledger line and info text to the x axis, if the distance between the last
// added ledger line and the right edge of the content view is greater than the distance between each
// ledger line on the x axis:
// the width of the scroll view
float scrollViewWidth = scrollView.GetComponent<RectTransform>().sizeDelta.x;
// the x pos of the last added ledger line
float lastXInfoPosX = xLines[xLines.Count - 1].GetComponent<RectTransform>().localPosition.x;
// the distance between the this ledger line and the right edge
float diff = scrollContent.GetComponent<RectTransform>().sizeDelta.x - lastXInfoPosX - scrollViewWidth / 2;
// the distance between each ledger line
float xStep = scrollViewWidth / (float)(xLedgerLines + 1);
// fill the right gap with ledger lines
for (int i = 0; i < (int)(diff / xStep); i++)
{
// the (x) pos of the info text and ledger line
Vector2 pos = new Vector2(lastXInfoPosX + xStep * (i + 1), 0);
// create the info text and ledger line
AddXInfo(pos.x, GetPointFromPosition(pos).GetX());
}
}
}
/// <summary>
/// This method updates the x info texts. This should only be called, if the graph is entirely shown.
/// </summary>
private void UpdateXInfoTextFullShown()
{
UpdateXInfoTextFullShown(GetXDifference() / (float)(xLedgerLines + 1));
}
/// <summary>
/// This method updates the x info texts. This should only be called, if the graph is entirely shown.
/// </summary>
private void UpdateXInfoTextFullShown(float xStep)
{
// ... and if the unit is a time unit, ...
if (unit != xUnit.Units.NUMBER)
{
// ... set the x info texts.
System.DateTime startDt = (System.DateTime)statistics.GetPoints()[0].GetX();
// ... update the x info text.
for (int i = 0; i < xTexts.Count; i++)
{
xTexts[i].text = GetNameFromXObject(startDt.AddHours(xStep * (i + 1)));
}
}
// Otherwise ...
else
{
float startValue = (float)System.Convert.ToDouble(statistics.GetPoints()[0].GetX());
// ... just update the x info text with common numbers.
for (int i = 0; i < xTexts.Count; i++)
{
xTexts[i].text = Round(startValue + xStep * (i + 1)) + "";
}
}
}
/// <summary>
/// This method gives the name of an x object corresponding to the current unit on the x axis back.
/// </summary>
private string GetNameFromXObject(object xObj)
{
// If the unit of the x object is a time unit, ...
if (unit != xUnit.Units.NUMBER)
{
// ... get the name of the date time corresponding to the current unit.
return xUnit.GetName(unit, (System.DateTime)xObj);
}
// Otherwise, return the object as a string.
return Round((float)System.Convert.ToDouble(xObj)).ToString();
}
/// <summary>
/// This method updates the average line position.
/// </summary>
private void UpdateAverageLine()
{
// If the average line image is not assigned, do not calculate anything and stop right here.
if (averageLine == null) return;
// If this visualizer does not have a statistics assigned, stop.
if (statistics == null) return;
// If the statistics does not have any points, stop.
if (statistics.GetPoints() == null) return;
if (statistics.GetPoints().Count == 0) return;
// get the rect transform component of the average line image
RectTransform rt = averageLine.GetComponent<RectTransform>();
// If the average line is still hidden, ...
if (averageLine.gameObject.activeSelf == false)
{
// ... show it, ...
averageLine.gameObject.SetActive(true);
// ... set its thickness ...
rt.sizeDelta = new Vector2(rt.sizeDelta.x, thickness);
// ... and set its color.
averageLine.color = averageLineColor;
}
// the y pos of the average line corresponding to the average of the y values of the statistics
float yPos = GetPositionFromPoint(new Point(0, GetAverageYValue())).y;
// update the position
averageLine.transform.localPosition = new Vector2(averageLine.transform.localPosition.x, yPos);
}
/// <summary>
/// This method calculates the average y value.
/// </summary>
private float GetAverageYValue()
{
// the sum of all y values
float sum = 0;
// add all y values to the sum variable
for (int i = 0; i < statistics.GetPoints().Count; i++)
{
sum += statistics.GetPoints()[i].GetY();
}
// divide the sum with the amount of points and return it
return sum / (float)statistics.GetPoints().Count;
}
/// <summary>
/// This method repaints the entire graph.
/// </summary>
public void Repaint()
{
HideCurrentPointInformationPanel();
// delete all circles and lines
for (int i = 0; i < graphLines.transform.childCount; i++)
{
Object.Destroy(graphLines.transform.GetChild(i).gameObject);
}
for (int i = 0; i < graphCircles.transform.childCount; i++)
{
Object.Destroy(graphCircles.transform.GetChild(i).gameObject);
}
lines = new List<Image>();
circles = new List<RectTransform>();
RectTransform rt = scrollContent.GetComponent<RectTransform>();
rt.sizeDelta = scrollView.GetComponent<RectTransform>().sizeDelta;
// reset the distance that corresponds to one unit of x and the ui coordinate system
minWidthPerUnit = scrollContent.GetComponent<RectTransform>().sizeDelta.x / xMax;
// reset the width scale on the x axis to the width at beginning (is changing over the time)
widthPerUnit = scrollContent.GetComponent<RectTransform>().sizeDelta.x;
// update the width per unit on the x axis
UpdateWidthPerUnit(false);
// delete all x info texts and legder lines
for (int i = 0; i < xLedgerPanel.transform.childCount; i++)
{
Destroy(xLedgerPanel.transform.GetChild(i).gameObject);
}
for (int i = 0; i < xAxisPanel.transform.childCount; i++)
{
Destroy(xAxisPanel.transform.GetChild(i).gameObject);
}
xTexts = new List<Text>();
xLines = new List<RectTransform>();
// create all the x ledger line and info texts again
float xStep = scrollView.GetComponent<RectTransform>().sizeDelta.x / (float)(xLedgerLines + 1);
for (int i = 0; i < xLedgerLines; i++)
{
// calculate the position of the text info and the ledger line
Vector2 pos = new Vector2(xStep * (i + 1) - xAxisPanel.GetComponent<RectTransform>().sizeDelta.x / 2, 0);
// create the info text and ledger line
AddXInfo(pos.x);
}
// reset the y info texts
for (int i = 0; i < yTexts.Count; i++)
{
yTexts[i].GetComponent<Text>().text = "";
}
// reset the average line
averageLine.gameObject.SetActive(false);
// get the points of the statistics
Point[] points = new Point[statistics.GetPoints().Count];
statistics.GetPoints().CopyTo(points);
// make the statistics not to store the points, as it already has them
statistics.SetRecoveringState(true);
// delete all the points from the statistics
statistics.Clear();
// add the points to the statistics again, so OnPointAdded is called again and the graph is repainted
statistics.AddPoints(new List<Point>(points));
// make the graph store all the data again
statistics.SetRecoveringState(false);
// reposition the first circle, as this circle is positioned wrong at first
circles[0].localPosition = GetPositionFromPoint(statistics.GetPoints()[0]);
UpdateYInfoText();
// update the first x info texts
UpdateXInfoTextFullShown(xMax / (float)(xLedgerLines + 1));
// reposition the scroll content to the very right
float scrollViewWidth = scrollView.GetComponent<RectTransform>().sizeDelta.x;
float x = circles[circles.Count - 1].localPosition.x + scrollViewWidth / 2 + circleRadius / 2;
LeanTween.moveLocalX(scrollContent.gameObject, -(1.5f * x - scrollViewWidth), 0.1f);
// stop the content view if it is moving
scrollView.GetComponent<ScrollRect>().velocity = Vector2.zero;
}
/// <summary>
/// This method is called, when the user presses a finger onto the scroll view.
/// </summary>
public void OnFingerDown()
{
HideCurrentPointInformationPanel();
}
/// <summary>
/// This method shows the information of the point that has been clicked on by the user.
/// </summary>
/// <param name="pointObject"> the game object that has been clicked on </param>
public void ShowInfoOfPoint(GameObject pointObject, int index)
{