Ejemplo n.º 1
0
    private void DrawBarChart()
    {
        if (controller.SeriesCount < 1)
        {
            return;
        }


        float mBarHeight = chartHolder.rect.height;
        float mBarSector = chartHolder.rect.width / controller.SeriesCount;
        float mBarWidth  = mBarSector * 0.67f;

        for (int idx = 0; idx < controller.SeriesCount; idx++)
        {
            float     x        = (idx + 0.5f) * mBarSector;
            float     y        = controller.values[idx] / controller.GetTotalMaxValue() * mBarHeight;
            Vector3[] lines    = new Vector3[] { new Vector3(x, 0), new Vector3(x, y) };
            Mesh      lineMesh = ChartUtils.GenerateLineMesh(lines, mBarWidth);

            string         name     = ChartUtils.NameGenerator(chartChildString, idx);
            GameObject     obj      = chartHolder.Find(name).gameObject;
            CanvasRenderer renderer = obj.GetComponent <CanvasRenderer>();

            renderer.Clear();
            renderer.SetMaterial(controller.materials[idx], null);
            renderer.SetMesh(lineMesh);

            RectTransform rt;
            if (obj.transform.childCount > 0)
            {
                rt = obj.transform.GetChild(0) as RectTransform;
            }
            else
            {
                var go = new GameObject();
                go.transform.SetParent(obj.transform);

                var t = go.AddComponent <Text>();
                t.alignment = TextAnchor.MiddleCenter;
                t.color     = Color.black;
                t.font      = font;

                rt = go.transform as RectTransform;
            }

            rt.localPosition = new Vector3(x, -7);

            var text = obj.GetComponentInChildren <Text>();
            if (text != null)
            {
                text.text = controller.values[idx].ToString();
            }
        }
    }