// ReSharper disable once CognitiveComplexity
        public void ShowGraph(List <float> valueList, bool makeDotsVisible = true, bool makeLinksVisible              = true, bool makeOriginZero = false, int maxVisibleValueAmount = -1,
                              Func <float, string>?getAxisLabelX           = null, Func <float, string>?getAxisLabelY = null, Color?linkColor     = null)
        {
            getAxisLabelX ??= i => i.ToString(CultureInfo.InvariantCulture);

            getAxisLabelY ??= f => Mathf.RoundToInt(f).ToString();

            if (maxVisibleValueAmount <= 0)
            {
                maxVisibleValueAmount = valueList.Count;
            }

            ClearOldData();

            var graphSizeDelta = GraphContainer.sizeDelta;
            var graphWidth     = graphSizeDelta.x;
            var graphHeight    = graphSizeDelta.y;

            var yMaximum = valueList[0];
            var yMinimum = valueList[0];

            for (var i = Mathf.Max(valueList.Count - maxVisibleValueAmount, 0); i < valueList.Count; i++)
            {
                var value = valueList[i];
                if (value > yMaximum)
                {
                    yMaximum = value;
                }

                if (value < yMinimum)
                {
                    yMinimum = value;
                }
            }

            var yDifference = yMaximum - yMinimum;

            if (yDifference <= 0)
            {
                yDifference = 5f;
            }

            yMaximum += (yDifference * 0.2f);
            yMinimum -= (yDifference * 0.2f);

            if (makeOriginZero)
            {
                yMinimum = 0f;                 // Start the graph at zero
            }

            var xSize  = graphWidth / (maxVisibleValueAmount + 1);
            var xIndex = 0;

            linkColor = linkColor == null ? DefaultLinkColor : new Color(linkColor.Value.r, linkColor.Value.g, linkColor.Value.b, .5f);

            GameObject?lastCircleGameObject = null;

            for (var i = Mathf.Max(valueList.Count - maxVisibleValueAmount, 0); i < valueList.Count; i++)
            {
                var xPosition        = xSize + xIndex * xSize;
                var yPosition        = (valueList[i] - yMinimum) / (yMaximum - yMinimum) * graphHeight;
                var circleGameObject = CreateCircle(new Vector2(xPosition, yPosition), makeDotsVisible);
                DotObjects.Add(circleGameObject);
                if (lastCircleGameObject != null)
                {
                    var dotConnectionGameObject = CreateDotConnection(lastCircleGameObject.GetComponent <RectTransform>().anchoredPosition,
                                                                      circleGameObject.GetComponent <RectTransform>().anchoredPosition,
                                                                      makeLinksVisible,
                                                                      linkColor.Value);
                    LinkObjects.Add(dotConnectionGameObject);
                }

                lastCircleGameObject = circleGameObject;

                var labelX   = Instantiate(_labelTemplateX, GraphContainer, false);
                var labelXGo = labelX.gameObject;
                labelXGo.SetActive(true);
                labelX.anchoredPosition           = new Vector2(xPosition, -7f);
                labelX.GetComponent <Text>().text = getAxisLabelX(i);
                LabelXObjects.Add(labelXGo);

                var dashX   = Instantiate(_dashTemplateX, GraphContainer, false);
                var dashXGo = dashX.gameObject;
                dashXGo.SetActive(true);
                dashX.anchoredPosition = new Vector2(yPosition, -3);
                DashXObjects.Add(dashXGo);

                xIndex++;
            }

            const int separatorCount = 10;

            for (var i = 0; i <= separatorCount; i++)
            {
                var labelY   = Instantiate(_labelTemplateY, GraphContainer, false);
                var labelYGo = labelY.gameObject;
                labelYGo.SetActive(true);
                var normalizedValue = i * 1f / separatorCount;
                labelY.anchoredPosition           = new Vector2(-7f, normalizedValue * graphHeight);
                labelY.GetComponent <Text>().text = getAxisLabelY(yMinimum + (normalizedValue * (yMaximum - yMinimum)));
                LabelYObjects.Add(labelYGo);

                var dashY   = Instantiate(_dashTemplateY, GraphContainer, false);
                var dashYGo = dashY.gameObject;
                dashYGo.SetActive(true);
                dashY.anchoredPosition = new Vector2(-4f, normalizedValue * graphHeight);
                DashYObjects.Add(dashYGo);
            }
        }
Esempio n. 2
0
        public void ShowGraph(List <Vector2> valueList, bool makeDotsVisible = true, bool makeLinksVisible = true,
                              bool makeOriginZero = false, int maxVisibleValueAmount = -1,
                              Func <float, string> getAxisLabelX = null, Func <float, string> getAxisLabelY = null)
        {
            if (getAxisLabelX == null)
            {
                getAxisLabelX = delegate(float _i) { return(_i.ToString()); }
            }
            ;
            if (getAxisLabelY == null)
            {
                getAxisLabelY = delegate(float _f) { return(Mathf.RoundToInt(_f).ToString()); }
            }
            ;

            if (maxVisibleValueAmount <= 0)
            {
                maxVisibleValueAmount = valueList.Count;
            }

            if (DotObjects != null)
            {
                foreach (var go in DotObjects)
                {
                    Destroy(go);
                }
                DotObjects.Clear();
            }
            if (LinkObjects != null)
            {
                foreach (var go in LinkObjects)
                {
                    Destroy(go);
                }
                LinkObjects.Clear();
            }
            if (LabelXObjects != null)
            {
                foreach (var go in LabelXObjects)
                {
                    Destroy(go);
                }
                LabelXObjects.Clear();
            }
            if (LabelYObjects != null)
            {
                foreach (var go in LabelYObjects)
                {
                    Destroy(go);
                }
                LabelYObjects.Clear();
            }
            if (DashXObjects != null)
            {
                foreach (var go in DashXObjects)
                {
                    Destroy(go);
                }
                DashXObjects.Clear();
            }
            if (DashYObjects != null)
            {
                foreach (var go in DashYObjects)
                {
                    Destroy(go);
                }
                DashYObjects.Clear();
            }

            var graphWidth  = GraphContainer.sizeDelta.x;
            var graphHeight = GraphContainer.sizeDelta.y;

            var yMaximum = valueList[0].y;
            var yMinimum = valueList[0].y;

            var xMaximum = valueList[valueList.Count - 1].x;
            var xMinimum = valueList[0].x;

            for (var i = Mathf.Max(valueList.Count - maxVisibleValueAmount, 0); i < valueList.Count; i++)
            {
                var value = valueList[i];
                if (value.y > yMaximum)
                {
                    yMaximum = value.y;
                }
                if (value.y < yMinimum)
                {
                    yMinimum = value.y;
                }
            }

            var yDifference = yMaximum - yMinimum;

            if (yDifference <= 0)
            {
                yDifference = 5f;
            }
            yMaximum = yMaximum + (yDifference * 0.2f);
            yMinimum = yMinimum - (yDifference * 0.2f);

            if (makeOriginZero)
            {
                yMinimum = 0f; // Start the graph at zero
            }
            var xSize  = graphWidth / (maxVisibleValueAmount + 1);
            var xIndex = 0;

            GameObject lastCircleGameObject = null;

            for (var i = Mathf.Max(valueList.Count - maxVisibleValueAmount, 0); i < valueList.Count; i++)
            {
                var xPosition        = (valueList[i].x - xMinimum) / (xMaximum - xMinimum) * graphWidth;
                var yPosition        = (valueList[i].y - yMinimum) / (yMaximum - yMinimum) * graphHeight;
                var circleGameObject = CreateCircle(new Vector2(xPosition, yPosition), makeDotsVisible);
                DotObjects.Add(circleGameObject);
                if (lastCircleGameObject != null)
                {
                    var dotConnectionGameObject = CreateDotConnection(lastCircleGameObject.GetComponent <RectTransform>().anchoredPosition,
                                                                      circleGameObject.GetComponent <RectTransform>().anchoredPosition,
                                                                      makeLinksVisible);
                    LinkObjects.Add(dotConnectionGameObject);
                }
                lastCircleGameObject = circleGameObject;

                var labelX = Instantiate(_labelTemplateX);
                labelX.SetParent(GraphContainer, false);
                labelX.gameObject.SetActive(true);
                labelX.anchoredPosition = new Vector2(xPosition, -7f);
                labelX.GetComponent <UnityEngine.UI.Text>().text = getAxisLabelX(i);
                LabelXObjects.Add(labelX.gameObject);

                var dashX = Instantiate(_dashTemplateX);
                dashX.SetParent(GraphContainer, false);
                dashX.gameObject.SetActive(true);
                dashX.anchoredPosition = new Vector2(yPosition, -3);
                DashXObjects.Add(dashX.gameObject);

                xIndex++;
            }

            var separatorCount = 10;

            for (var i = 0; i <= separatorCount; i++)
            {
                var labelY = Instantiate(_labelTemplateY);
                labelY.SetParent(GraphContainer, false);
                labelY.gameObject.SetActive(true);
                var normalizedValue = i * 1f / separatorCount;
                labelY.anchoredPosition = new Vector2(-7f, normalizedValue * graphHeight);
                labelY.GetComponent <UnityEngine.UI.Text>().text = getAxisLabelY(yMinimum + (normalizedValue * (yMaximum - yMinimum)));
                LabelYObjects.Add(labelY.gameObject);

                var dashY = Instantiate(_dashTemplateY);
                dashY.SetParent(GraphContainer, false);
                dashY.gameObject.SetActive(true);
                dashY.anchoredPosition = new Vector2(-4f, normalizedValue * graphHeight);
                DashYObjects.Add(dashY.gameObject);
            }
        }