public IGraphVisualObject CreateGraphVisualObject(Vector2 graphPos, float graphPosWidth, string toolTipTxt, bool isEnd = false) { List <GameObject> gameObjectList = new List <GameObject>(); GameObject dotObject = CreateDot(graphPos); gameObjectList.Add(dotObject); GameObject dotConnectionObject = null; if (lastLineObject != null) { dotConnectionObject = CreateDotConnection( lastLineObject.GetGraphPos(), dotObject.GetComponent <RectTransform>().anchoredPosition); gameObjectList.Add(dotConnectionObject); } LineGraphVisualObject lineGraphVisualObject = new LineGraphVisualObject(dotObject, dotConnectionObject, lastLineObject); IGraphVisualObject iLineGraphVisualObject = lineGraphVisualObject; iLineGraphVisualObject.SetGraphVisualObjectInfo(graphPos, graphPosWidth, toolTipTxt); lastLineObject = !isEnd ? lineGraphVisualObject : null; return(lineGraphVisualObject); }
public void ShowGraph(IList valueList, IGraphVisual graphVisual = null, int maxVisibleValueAmount = -1, Func <int, string> getAxisLabelX = null, Func <float, string> getAxisLabelY = null) { Setup(); if (valueList == null) { valueList = new List <object> { 0 }; //Debug.LogError("valueList is null!"); //return; } this.valueList = valueList; if (graphVisual == null) { graphVisual = barChartVisual; } this.graphVisual = graphVisual; if (maxVisibleValueAmount <= 0) { // Show all if no amount specified maxVisibleValueAmount = valueList.Count; } if (maxVisibleValueAmount > valueList.Count) { // Validate the amount to show the maximum maxVisibleValueAmount = valueList.Count; } this.maxVisibleValueAmount = maxVisibleValueAmount; // Test for label defaults if (getAxisLabelX == null) { if (this.getAxisLabelX != null) { getAxisLabelX = this.getAxisLabelX; } else { getAxisLabelX = delegate(int _i) { return(_i.ToString()); }; } } if (getAxisLabelY == null) { if (this.getAxisLabelY != null) { getAxisLabelY = this.getAxisLabelY; } else { getAxisLabelY = delegate(float _f) { return(Mathf.RoundToInt(_f).ToString()); }; } } this.getAxisLabelX = getAxisLabelX; this.getAxisLabelY = getAxisLabelY; // Clean up previous graph foreach (GameObject gameObject in gameObjectList) { Destroy(gameObject); } gameObjectList.Clear(); yLabelList.Clear(); foreach (IGraphVisualObject graphVisualObject in graphVisualObjectList) { graphVisualObject.CleanUp(); } graphVisualObjectList.Clear(); graphVisual.CleanUp(); // Grab the width and height from the container float graphWidth = graphContainer.sizeDelta.x; float graphHeight = graphContainer.sizeDelta.y; float yMinimum, yMaximum; CalculateYScale(out yMinimum, out yMaximum); // Set the distance between each point on the graph xSize = graphWidth / (maxVisibleValueAmount + 1); // Cycle through all visible data points int xIndex = 0; for (int i = Mathf.Max(valueList.Count - maxVisibleValueAmount, 0); i < valueList.Count; i++) { float xPosition = xSize + xIndex * xSize; float yPosition = ((GetValue(valueList[i]) - yMinimum) / (yMaximum - yMinimum)) * graphHeight; // Add data point visual string tooltipText; if (getTooltipLabel != null) { tooltipText = getTooltipLabel(GetValue(valueList[i])); } else { tooltipText = getAxisLabelY(GetValue(valueList[i])); } IGraphVisualObject graphVisualObject = graphVisual.CreateGraphVisualObject(new Vector2(xPosition, yPosition), xSize, tooltipText); graphVisualObjectList.Add(graphVisualObject); // Duplicate the x label template RectTransform labelX = Instantiate(labelTemplateX); labelX.SetParent(graphContainer, false); labelX.gameObject.SetActive(true); labelX.anchoredPosition = new Vector2(xPosition, -7f); labelX.GetComponent <Text>().text = getAxisLabelX(i); gameObjectList.Add(labelX.gameObject); // Duplicate the x dash template RectTransform dashX = Instantiate(dashTemplateX); dashX.SetParent(dashContainer, false); dashX.gameObject.SetActive(true); dashX.anchoredPosition = new Vector2(xPosition, -3f); dashX.sizeDelta = new Vector2(graphHeight, dashX.sizeDelta.y); gameObjectList.Add(dashX.gameObject); xIndex++; } // Set up separators on the y axis int separatorCount = 10; for (int i = 0; i <= separatorCount; i++) { // Duplicate the label template RectTransform labelY = Instantiate(labelTemplateY); labelY.SetParent(graphContainer, false); labelY.gameObject.SetActive(true); float normalizedValue = i * 1f / separatorCount; labelY.anchoredPosition = new Vector2(-7f, normalizedValue * graphHeight); labelY.GetComponent <Text>().text = getAxisLabelY(yMinimum + (normalizedValue * (yMaximum - yMinimum))); yLabelList.Add(labelY); gameObjectList.Add(labelY.gameObject); // Duplicate the dash template RectTransform dashY = Instantiate(dashTemplateY); dashY.SetParent(dashContainer, false); dashY.gameObject.SetActive(true); dashY.anchoredPosition = new Vector2(-4f, normalizedValue * graphHeight); dashY.sizeDelta = new Vector2(graphWidth, dashY.sizeDelta.y); gameObjectList.Add(dashY.gameObject); } }
private void ShowGraph(List <int> valueList, IGraphVisual graphVisual, int maxVisibleValueAmount = -1, Func <int, string> getAxisLabelX = null, Func <float, string> getAxisLabelY = null) { this.valueList = valueList; this.graphVisual = graphVisual; this.getAxisLabelX = getAxisLabelX; this.getAxisLabelY = getAxisLabelY; if (maxVisibleValueAmount <= 0) { maxVisibleValueAmount = valueList.Count; } if (maxVisibleValueAmount > valueList.Count) { maxVisibleValueAmount = valueList.Count; } this.maxVisibleValueAmount = maxVisibleValueAmount; if (getAxisLabelX == null) { getAxisLabelX = delegate(int _i) { return(_i.ToString()); }; } if (getAxisLabelY == null) { getAxisLabelY = delegate(float _f) { return(Mathf.RoundToInt(_f).ToString()); }; } foreach (GameObject gameObject in gameObjectList) { Destroy(gameObject); } gameObjectList.Clear(); yLabelList.Clear(); foreach (IGraphVisualObject graphVisualObject in graphVisualObjectList) { graphVisualObject.CleanUp(); } graphVisualObjectList.Clear(); graphVisual.CleanUp(); float graphHeight = graphContainer.sizeDelta.y; float graphWidth = graphContainer.sizeDelta.x; float yMinimum, yMaximum; CalculateYScale(out yMinimum, out yMaximum); xSize = graphWidth / (maxVisibleValueAmount + 1); int xIndex = 0; for (int i = Mathf.Max(valueList.Count - maxVisibleValueAmount, 0); i < valueList.Count; ++i) { float xposition = xSize + xIndex * xSize; float yposition = ((valueList[i] - yMinimum) / (yMaximum - yMinimum)) * graphHeight; string toolTipText = getAxisLabelY(valueList[i]); IGraphVisualObject graphVisualObject = graphVisual.CreateGraphVisualObject(new Vector2(xposition, yposition), xSize, toolTipText); graphVisualObjectList.Add(graphVisualObject); RectTransform labelX = Instantiate(labelTemplateX); labelX.SetParent(graphContainer); labelX.gameObject.SetActive(true); labelX.anchoredPosition = new Vector2(xposition, -20f); labelX.GetComponent <Text>().text = getAxisLabelX(i); gameObjectList.Add(labelX.gameObject); RectTransform dashX = Instantiate(dashTemplateX); dashX.SetParent(graphContainer); dashX.gameObject.SetActive(true); dashX.anchoredPosition = new Vector2(xposition, 0f); gameObjectList.Add(dashX.gameObject); xIndex++; } int sepraterCount = 10; for (int i = 0; i <= sepraterCount; i++) { RectTransform labelY = Instantiate(lableTemplateY); labelY.SetParent(graphContainer, false); labelY.gameObject.SetActive(true); float normalisedValue = i * 1f / sepraterCount; labelY.anchoredPosition = new Vector2(-80f, normalisedValue * graphHeight); labelY.GetComponent <Text>().text = getAxisLabelY(yMinimum + (normalisedValue * (yMaximum - yMinimum))); yLabelList.Add(labelY); gameObjectList.Add(labelY.gameObject); RectTransform dashY = Instantiate(dashTemplateY); dashY.SetParent(dashContainer, false); dashY.gameObject.SetActive(true); dashY.anchoredPosition = new Vector2(0f, normalisedValue * graphHeight); gameObjectList.Add(dashY.gameObject); } }
private void ShowGraph(List <int> valueList, IGraphVisual graphVisual, int maxVisibleValueAmount = -1, Func <int, string> getAxisLabelX = null, Func <float, string> getAxisLabelY = null) { this.valueList = valueList; this.graphVisual = graphVisual; this.getAxisLabelX = getAxisLabelX; this.getAxisLabelY = getAxisLabelY; if (valueList.Count == 0) { return; } if (maxVisibleValueAmount <= 0) { // Show all if no amount specified maxVisibleValueAmount = valueList.Count; } if (maxVisibleValueAmount > valueList.Count) { // Validate the amount to show the maximum maxVisibleValueAmount = valueList.Count; } this.maxVisibleValueAmount = maxVisibleValueAmount; // Test for label defaults if (getAxisLabelX == null) { getAxisLabelX = delegate(int _i) { return(_i.ToString()); }; } if (getAxisLabelY == null) { getAxisLabelY = delegate(float _f) { return(Mathf.RoundToInt(_f).ToString()); }; } // Clean up previous graph foreach (GameObject gameObject in gameObjectList) { Destroy(gameObject); } gameObjectList.Clear(); foreach (IGraphVisualObject graphVisualObject in graphVisualObjectList) { graphVisualObject.CleanUp(); } graphVisualObjectList.Clear(); graphVisual.CleanUp(); // Grab the width and height from the container float graphWidth = graphContainer.sizeDelta.x; float graphHeight = graphContainer.sizeDelta.y; // Identify y Min and Max values float yMaximum = valueList[0]; float yMinimum = valueList[0]; for (int i = Mathf.Max(valueList.Count - maxVisibleValueAmount, 0); i < valueList.Count; i++) { int value = valueList[i]; if (value > yMaximum) { yMaximum = value; } if (value < yMinimum) { yMinimum = value; } } float yDifference = yMaximum - yMinimum; if (yDifference <= 0) { yDifference = 5f; } yMaximum = yMaximum + (yDifference * 0.2f); yMinimum = yMinimum - (yDifference * 0.2f); yMinimum = 0f; // Start the graph at zero // Set the distance between each point on the graph float xSize = graphWidth / (maxVisibleValueAmount); // Cycle through all visible data points int xIndex = 0; for (int i = Mathf.Max(valueList.Count - maxVisibleValueAmount, 0); i < valueList.Count; i++) { float xPosition = xIndex * xSize; float yPosition = ((valueList[i] - yMinimum) / (yMaximum - yMinimum)) * graphHeight; // Add data point visual string tooltipText = getAxisLabelY(valueList[i]); IGraphVisualObject graphVisualObject = graphVisual.CreateGraphVisualObject(new Vector2(xPosition, yPosition), xSize, tooltipText); graphVisualObjectList.Add(graphVisualObject); // Duplicate the x label template RectTransform labelX = Instantiate(labelTemplateX); labelX.SetParent(graphContainer, false); labelX.gameObject.SetActive(true); labelX.anchoredPosition = new Vector2(xPosition, -7f); labelX.GetComponent <Text>().text = getAxisLabelX(i); gameObjectList.Add(labelX.gameObject); // Duplicate the x dash template RectTransform dashX = Instantiate(dashTemplateX); dashX.SetParent(dashContainer, false); dashX.gameObject.SetActive(true); dashX.anchoredPosition = new Vector2(xPosition, -3f); gameObjectList.Add(dashX.gameObject); xIndex++; //graphVisualObjectList[0] = (graphVisualObject = graphVisual.CreateGraphVisualObject(new Vector2(25, 25), xSize, "F**K")); } // Set up separators on the y axis int separatorCount = 10; for (int i = 0; i <= separatorCount; i++) { // Duplicate the label template RectTransform labelY = Instantiate(labelTemplateY); labelY.SetParent(graphContainer, false); labelY.gameObject.SetActive(true); float normalizedValue = i * 1f / separatorCount; labelY.anchoredPosition = new Vector2(-7f, normalizedValue * graphHeight); labelY.GetComponent <Text>().text = getAxisLabelY(yMinimum + (normalizedValue * (yMaximum - yMinimum))); gameObjectList.Add(labelY.gameObject); // Duplicate the dash template RectTransform dashY = Instantiate(dashTemplateY); dashY.SetParent(dashContainer, false); dashY.gameObject.SetActive(true); dashY.anchoredPosition = new Vector2(-4f, normalizedValue * graphHeight); gameObjectList.Add(dashY.gameObject); } }
private void ShowGraph(List <int> valueList, IGraphVisual graphVisual, int maxVisibleValueAmount = -1, Func <int, string> getAxisLabelX = null, Func <float, string> getAxisLabelY = null) { this.valueList = valueList; this.graphVisual = graphVisual; this.getAxisLabelX = getAxisLabelX; this.getAxisLabelY = getAxisLabelY; if (maxVisibleValueAmount <= 0) { // Show all if no amount specified maxVisibleValueAmount = valueList.Count; } if (maxVisibleValueAmount >= 30) { // Show all if no amount specified maxVisibleValueAmount = 30; } this.maxVisibleValueAmount = maxVisibleValueAmount; // Test for label defaults if (getAxisLabelX == null) { getAxisLabelX = delegate(int _i) { return(_i.ToString()); }; } if (getAxisLabelY == null) { getAxisLabelY = delegate(float _f) { return(Mathf.RoundToInt(_f).ToString()); }; } // Clean up previous graph foreach (GameObject gameObject in gameObjectList) { Destroy(gameObject); } gameObjectList.Clear(); yLabelList.Clear(); foreach (IGraphVisualObject graphVisualObject in graphVisualObjectList) { graphVisualObject.CleanUp(); } graphVisualObjectList.Clear(); graphVisual.CleanUp(); // Grab the width and height from the container float graphWidth = graphContainer.sizeDelta.x; float graphHeight = graphContainer.sizeDelta.y; float yMinimum, yMaximum; CalculateYScale(out yMinimum, out yMaximum); // Set the distance between each point on the graph xSize = graphWidth / (maxVisibleValueAmount + 1); // Cycle through all visible data points int xIndex = 0; for (int i = Mathf.Max(valueList.Count - maxVisibleValueAmount, 0); i < valueList.Count; i++) { float xPosition = xSize + xIndex * xSize; float yPosition = ((valueList[i] - yMinimum) / (yMaximum - yMinimum)) * graphHeight; // Add data point visual IGraphVisualObject graphVisualObject = graphVisual.CreateGraphVisualObject(new Vector2(xPosition, yPosition), xSize); graphVisualObjectList.Add(graphVisualObject); xIndex++; } // Set up separators on the y axis int separatorCount = 5; for (int i = 0; i <= separatorCount; i++) { // Duplicate the label template RectTransform labelY = Instantiate(labelTemplateY); labelY.SetParent(graphContainer, false); labelY.gameObject.SetActive(true); float normalizedValue = i * 1f / separatorCount; labelY.anchoredPosition = new Vector2(-7f, normalizedValue * graphHeight); labelY.GetComponent <Text>().text = getAxisLabelY(yMinimum + (normalizedValue * (yMaximum - yMinimum))); yLabelList.Add(labelY); gameObjectList.Add(labelY.gameObject); } }
private void ShowGraph(List <ScoreItem> valueList, int maxVisibleValueAmount = -1) { this.valueList = valueList; IGraphVisual graphVisual = new BarChartVisual(graphContainer, Color.white, .8f); getAxisLabelY = (float _f) => "" + (_f / 100); if (maxVisibleValueAmount <= 0) { // Show all if no amount specified maxVisibleValueAmount = valueList.Count; } if (maxVisibleValueAmount > valueList.Count) { // Validate the amount to show the maximum maxVisibleValueAmount = valueList.Count; } this.maxVisibleValueAmount = maxVisibleValueAmount; // Clean up previous graph foreach (GameObject gameObject in gameObjectList) { Destroy(gameObject); } gameObjectList.Clear(); yLabelList.Clear(); foreach (IGraphVisualObject graphVisualObject in graphVisualObjectList) { graphVisualObject.CleanUp(); } graphVisualObjectList.Clear(); graphVisual.CleanUp(); // Grab the width and height from the container float graphWidth = graphContainer.sizeDelta.x; float graphHeight = graphContainer.sizeDelta.y; float yMinimum, yMaximum; CalculateYScale(out yMinimum, out yMaximum); // Set the distance between each point on the graph xSize = graphWidth / (maxVisibleValueAmount + 1); // Cycle through all visible data points int xIndex = 0; for (int i = 0; i < valueList.Count; i++) { float xPosition = xSize + xIndex * xSize; float yPosition = ((valueList[i].Score - yMinimum) / (yMaximum - yMinimum)) * graphHeight; // Add data point visual string tooltipText = getAxisLabelY(valueList[i].Score); IGraphVisualObject graphVisualObject = graphVisual.CreateGraphVisualObject(new Vector2(xPosition, yPosition), xSize, tooltipText); graphVisualObjectList.Add(graphVisualObject); // Duplicate the x label template RectTransform labelX = Instantiate(labelTemplateX); labelX.SetParent(graphContainer, false); labelX.gameObject.SetActive(true); labelX.anchoredPosition = new Vector2(xPosition - 40, -7f); labelX.GetComponent <Text>().text = valueList[i].Name; gameObjectList.Add(labelX.gameObject); xIndex++; } // Set up separators on the y axis int separatorCount = 10; for (int i = 0; i <= separatorCount; i++) { // Duplicate the label template RectTransform labelY = Instantiate(labelTemplateY); labelY.SetParent(graphContainer, false); labelY.gameObject.SetActive(true); float normalizedValue = i * 1f / separatorCount; labelY.anchoredPosition = new Vector2(-7f, normalizedValue * graphHeight); labelY.GetComponent <Text>().text = getAxisLabelY(yMinimum + (normalizedValue * (yMaximum - yMinimum))); yLabelList.Add(labelY); gameObjectList.Add(labelY.gameObject); } }
//public static void ShowTooltip_Static(string tooltipText, Vector2 anchoredPosition) //{ // instance.ShowTooltip(tooltipText, anchoredPosition); //} //private void ShowTooltip(string tooltipText, Vector2 anchoredPosition) //{ // tooltipGameObject.SetActive(true); // tooltipGameObject.GetComponent<RectTransform>().anchoredPosition = anchoredPosition; // TextMeshProUGUI tooltipUIText = tooltipGameObject.transform.Find("Text").GetComponent<TextMeshProUGUI>(); // tooltipUIText.text = tooltipText; // float textPaddingSize = 4f; // Vector2 backgroundSize = new Vector2( // tooltipUIText.preferredWidth + textPaddingSize * 2f, // tooltipUIText.preferredHeight + textPaddingSize * 2f); // tooltipGameObject.transform.Find("Background").GetComponent<RectTransform>().sizeDelta = backgroundSize; // tooltipGameObject.transform.SetAsLastSibling(); //} //public static void HideTooltip_Static() //{ // instance.HideTooltip(); //} //private void HideTooltip() //{ // tooltipGameObject.SetActive(false); //} private void ShowGraph(List <float> valueList, IGraphVisual graphVisual, RectTransform performancePanel, List <IGraphVisualObject> graphVisualObjectList, List <GameObject> gameObjectList, int startX, int endX, Func <int, string> getAxisLabelX = null, Func <float, string> getAxisLabelY = null) { this.graphVisual = graphVisual; this.startX = startX; this.endX = endX; this.getAxisLabelX = getAxisLabelX; this.getAxisLabelY = getAxisLabelY; if (getAxisLabelX == null) { getAxisLabelX = delegate(int _i) { return(_i.ToString()); }; } if (getAxisLabelY == null) { getAxisLabelY = delegate(float _f) { return(Mathf.RoundToInt(_f).ToString()); }; } if (endX <= 0) { endX = valueList.Count; } foreach (GameObject gameObject in gameObjectList) { Destroy(gameObject); } gameObjectList.Clear(); yLabelList.Clear(); foreach (IGraphVisualObject graphVisualObject in graphVisualObjectList) { graphVisualObject.CleanUp(); } graphVisualObjectList.Clear(); graphVisual.CleanUp(); float graphWidth = redPerformance.sizeDelta.x; float graphHeight = redPerformance.sizeDelta.y; float yMax = valueList[0]; float yMin = valueList[0]; for (int i = Mathf.Max(startX, 0); i < endX; i++) { float value = valueList[i]; if (value > yMax) { yMax = value; } if (value < yMin) { yMin = value; } } float yDifference = yMax - yMin; if (yDifference <= 0) { yDifference = 5f; } yMax = yMax + (yDifference * 0.2f); yMin = yMin - (yDifference * 0.2f); if (startYScaleAtZero) { yMin = 0f; // Start the graph at zero } xSize = graphWidth / ((endX - startX) + 1); int index = 0; int gap = 0; if (ScaleXLabel(valueList) != 0) { gap = valueList.Count / ScaleXLabel(valueList); } for (int i = Mathf.Max(startX, 0); i < endX; i++) { float xPosition = xSize + index * xSize; float yPosition = ((valueList[i] - yMin) / (yMax - yMin)) * graphHeight; string tooltipText = valueList[i].ToString(); IGraphVisualObject graphVisualObject = graphVisual.CreateGraphVisualObject(new Vector2(xPosition, yPosition), xSize, tooltipText); graphVisualObjectList.Add(graphVisualObject); if (i % gap == 0) { RectTransform labelX = Instantiate(lableTemplateX); labelX.SetParent(performancePanel, false); labelX.gameObject.SetActive(true); labelX.anchoredPosition = new Vector2(xPosition, -20f); labelX.GetComponent <TextMeshProUGUI>().text = getAxisLabelX(i); gameObjectList.Add(labelX.gameObject); } index++; } int separatorCount = 5; for (int i = 0; i <= separatorCount; i++) { RectTransform labelY = Instantiate(lableTemplateY); labelY.SetParent(performancePanel, false); labelY.gameObject.SetActive(true); float normalizedValue = i * 1f / separatorCount; labelY.anchoredPosition = new Vector2(-20f, normalizedValue * graphHeight); labelY.GetComponent <TextMeshProUGUI>().text = getAxisLabelY(yMin + normalizedValue * (yMax - yMin)); yLabelList.Add(labelY); gameObjectList.Add(labelY.gameObject); // //RectTransform dashY = Instantiate(dashTemplateX); // //dashY.SetParent(performance, false); // //dashY.gameObject.SetActive(true); // //dashY.anchoredPosition = new Vector2(-4f, normalizedValue * graphHeight); // //gameObjectList.Add(dashY.gameObject); } }