예제 #1
0
    private void ShowGraph(List <float> valueList, List <string> labelList)
    {
        float graphWidth           = graphContainer.sizeDelta.x;
        float graphHeight          = graphContainer.sizeDelta.y;
        int   maxVisibleValueAmout = 3;

        maxVisibleValueAmout = Mathf.Min(maxVisibleValueAmout, valueList.Count);
        float yMaximum = 55f;
        float xSize    = graphWidth / (maxVisibleValueAmout + 1) * 2;

        int xIndex = 0;

        for (int i = 0; i < valueList.Count; i++)
        {
            box = new GameObject("box", typeof(RectTransform));
            box.transform.SetParent(graphContainerBar, false);
            box.GetComponent <RectTransform>().sizeDelta = new Vector2(xSize, box.GetComponent <RectTransform>().sizeDelta.y);


            box.SetActive(true);
            float xPosition = xSize / 2;
            float yPosition = (valueList[i] / yMaximum) * graphHeight - 1;
            CreateBar(new Vector2(xPosition, yPosition), xSize / 2, labelList[i]);

            RectTransform labelX = Instantiate(labelXRect);
            labelX.SetParent(box.transform, false);
            labelX.gameObject.SetActive(true);
            labelX.anchoredPosition               = new Vector2(xPosition, -2.8f);
            labelX.GetComponent <Text>().text     = labelList[i].ToString();
            labelX.GetComponent <Text>().fontSize = 40;
            xIndex++;
        }
        if (!UIContainer.FindChild("labelY(Clone)"))
        {
            int separatorCount = 11;
            for (int i = 0; i < separatorCount; i++)
            {
                RectTransform labelY = Instantiate(labelYRect);
                labelY.gameObject.SetActive(true);
                labelY.SetParent(UIContainer, false);
                float normalizedValue = i * 1f / separatorCount;
                labelY.anchoredPosition                = new Vector2(6.5f, normalizedValue * (graphHeight - 20) + 20);
                labelY.GetComponent <Text>().text      = Mathf.RoundToInt(normalizedValue * yMaximum).ToString();
                labelY.GetComponent <Text>().color     = Color.black;
                labelY.GetComponent <Text>().fontStyle = FontStyle.Bold;
            }
        }
        graphContainerBar.GetComponent <ContentSizeFitter>().horizontalFit = ContentSizeFitter.FitMode.MinSize;
    }