Example #1
0
    public override void DrawSeries()
    {
        if (mGameObject == null || mData == null)
        {
            return;
        }

        base.DrawSeries();
        clearMarkers();
        int meshCount = mData.Count;

        if (mPlotStyle == Style.Line)
        {
            meshCount = 1;
        }
        clearMeshes(meshCount);
        clearCanvasRenderers(meshCount);

        List <Vector3> pVertices  = new List <Vector3>();
        List <Vector2> pUvs       = new List <Vector2>();
        List <int>     pTriangles = new List <int>();

        Mesh           pMesh;
        CanvasRenderer pCanvasRenderer;

        if (!mGraph.UnityGui && mMeshes == null)
        {
            mMeshes = new List <KeyValuePair <GameObject, Mesh> >();
        }
        else if (mGraph.UnityGui && mCanvasRenderers == null)
        {
            mCanvasRenderers = new List <KeyValuePair <GameObject, CanvasRenderer> >();
        }

        Vector2 pZero = mGraph.adjustPoint(Vector2.zero);

        for (int i = 0; i < mData.Count; ++i)
        {
            Vector2 pDataPoint = mData[i];
            pDataPoint = mGraph.adjustPoint(pDataPoint);

            if (MarkersStyle != MarkerStyle.None)
            {
                drawMarker(pDataPoint, i);
            }

            if (mPlotStyle == Style.Line && i == 0)
            {
                continue;
            }

            if (mPlotStyle == Style.Line)
            {
                Vector2 pPrevDataPoint = mData[i - 1];
                NGraphUtils.addSegment(mGraph.adjustPoint(pPrevDataPoint), pDataPoint, PlotThickness, pVertices, pUvs, pTriangles, mGraph.UnityGui);
            }
            else if (mPlotStyle == Style.Bar)
            {
                if (mGraph.UnityGui)
                {
                    if (i >= mCanvasRenderers.Count)
                    {
                        GameObject pChildBarGo = NGraphUtils.AddGameObject(mGameObject, 0, "Bar - " + i);
                        pCanvasRenderer = NGraphUtils.AddCanvasRenderer(pChildBarGo);
                        mCanvasRenderers.Add(new KeyValuePair <GameObject, CanvasRenderer>(pChildBarGo, pCanvasRenderer));
                    }
                    else
                    {
                        pCanvasRenderer = mCanvasRenderers[i].Value;
                    }

                    Material pMat = new Material(PlotMaterial);
                    pMat.SetColor("_TintColor", PlotColor);
                    pMat.SetVector("_Clipping", new Vector4(mClipping.x, mClipping.y, mClipping.z, mClipping.w));

                    pCanvasRenderer.SetMaterial(pMat, null);

                    float h   = pDataPoint.y - pZero.y;
                    float top = pZero.y;
                    NGraphUtils.DrawRect(new Rect(-mPlotThickness / 2 + pDataPoint.x, top, mPlotThickness, h), pCanvasRenderer);
                }
                else
                {
                    if (i >= mMeshes.Count)
                    {
                        GameObject pChildBarGo = NGraphUtils.AddGameObject(mGameObject, 0, "Bar - " + i);
                        NGraphUtils.AddMesh(pChildBarGo, out mMeshRenderer, out pMesh);
                        mMeshes.Add(new KeyValuePair <GameObject, Mesh>(pChildBarGo, pMesh));
                        mMeshRenderer.material = new Material(PlotMaterial);
                        mMeshRenderer.material.SetColor("_TintColor", PlotColor);
                        mMeshRenderer.material.SetVector("_Clipping", new Vector4(mClipping.x, mClipping.y, mClipping.z, mClipping.w));
                    }
                    else
                    {
                        pMesh = mMeshes[i].Value;
                    }

                    pMesh.Clear();
                    NGraphUtils.DrawRect(new Rect(-mPlotThickness / 2 + pDataPoint.x, pZero.y, mPlotThickness, pDataPoint.y - pZero.y), pMesh);
                }
            }
        }

        if (mPlotStyle == Style.Line)
        {
            if (mGraph.UnityGui)
            {
                if (mCanvasRenderers.Count == 0)
                {
                    GameObject pChildBarGo = NGraphUtils.AddGameObject(mGameObject, 0, "Line");
                    pCanvasRenderer = NGraphUtils.AddCanvasRenderer(pChildBarGo);
                    mCanvasRenderers.Add(new KeyValuePair <GameObject, CanvasRenderer>(pChildBarGo, pCanvasRenderer));
                }
                else
                {
                    pCanvasRenderer = mCanvasRenderers[0].Value;
                }

                Vector3[] e = new Vector3[4];
                mGraph.GetComponent <RectTransform>().GetWorldCorners(e);

                Material pMat = new Material(PlotMaterial);
                pMat.SetColor("_TintColor", PlotColor);
                pMat.SetVector("_Clipping", new Vector4(mClipping.x, mClipping.y, mClipping.z, mClipping.w));

                pCanvasRenderer.SetMaterial(pMat, null);
                List <UIVertex> vertices = new List <UIVertex>(pVertices.Count);
                for (int i = 0; i < pVertices.Count; i++)
                {
                    UIVertex pVertex = new UIVertex();
                    pVertex.position = pVertices[i];
                    pVertex.uv0      = pUvs[i];
                    vertices.Add(pVertex);
                }
                pCanvasRenderer.SetVertices(vertices);
            }
            else
            {
                if (mMeshes.Count == 0)
                {
                    GameObject pChildBarGo = NGraphUtils.AddGameObject(mGameObject, 0, "Line");
                    NGraphUtils.AddMesh(pChildBarGo, out mMeshRenderer, out pMesh);
                    mMeshes.Add(new KeyValuePair <GameObject, Mesh>(pChildBarGo, pMesh));
                    mMeshRenderer.material = new Material(PlotMaterial);
                    mMeshRenderer.material.SetColor("_TintColor", PlotColor);
                    mMeshRenderer.material.SetVector("_Clipping", new Vector4(mClipping.x, mClipping.y, mClipping.z, mClipping.w));
                }
                else
                {
                    pMesh = mMeshes[0].Value;
                }

                pMesh.Clear();
                pMesh.vertices  = pVertices.ToArray();
                pMesh.uv        = pUvs.ToArray();
                pMesh.triangles = pTriangles.ToArray();
            }
        }

        // Draw data labels if available
        foreach (dataLabelStruct labelInfo in mDataLabels)
        {
            drawDataLabel(labelInfo);
        }
    }