Ejemplo n.º 1
0
    public void refreshGraph(float animateDuration)
    {
        // Find the total number of slices
        bool isOtherSlice = false;

        numSlices = sliceValues.Count;
        if (limitNumberSlices)
        {
            if (numSlices > maxNumberSlices)
            {
                numSlices = maxNumberSlices;
                if (includeOthers)
                {
                    isOtherSlice = true;
                    numSlices++;
                }
            }
        }
        // Create pie slices based on sliceValues data
        for (int i = 0; i < numSlices; i++)
        {
            if (sliceLabels.Count <= i)
            {
                sliceLabels.Add("");
            }
            if (sliceColors.Count <= i)
            {
                sliceColors.Add(Color.white);
            }
            if (slices.Count <= i)
            {
                GameObject curObj = theGraph.CreateNode(nodePrefab, slicesParent);
//				curObj.transform.parent = slicesParent.transform;
                slices.Add(curObj);
            }
            if (sliceLegendEntries.Count <= i)
            {
                GameObject curObj = theGraph.CreateNode(legendEntryPrefab, legendParent);
//				curObj.transform.parent = legendParent.transform;
                sliceLegendEntries.Add(curObj);
            }
            if (slicePercents.Count <= i)
            {
                slicePercents.Add(0);
            }
            if (sliceExplodeAngles.Count <= i)
            {
                sliceExplodeAngles.Add(0);
            }
        }
        // Find Other Slice Value and Total Value
        float otherSliceValue = 0;
        float totalVal        = 0;

        for (int i = 0; i < sliceValues.Count; i++)
        {
            totalVal += sliceValues[i];
            if (isOtherSlice && i >= maxNumberSlices)
            {
                otherSliceValue += sliceValues[i];
            }
            if (limitNumberSlices && !isOtherSlice && i >= maxNumberSlices)
            {
                totalVal -= sliceValues[i];
            }
        }

        // If there are more sliceLegendEntries or slices than sliceValues data, delete the extras
        for (int i = sliceLegendEntries.Count - 1; i >= 0; i--)
        {
            if (sliceLegendEntries[i] != null && i >= numSlices)
            {
                WMG_Node theEntry = sliceLegendEntries[i].GetComponent <WMG_Node>();
                theGraph.DeleteNode(theEntry);
                sliceLegendEntries.RemoveAt(i);
            }
        }
        for (int i = slices.Count - 1; i >= 0; i--)
        {
            if (slices[i] != null && i >= numSlices)
            {
                WMG_Node theSlice = slices[i].GetComponent <WMG_Node>();
                theGraph.DeleteNode(theSlice);
                slices.RemoveAt(i);
            }
        }

        // Update Legend Background
        if (legendType != labelTypes.None && !theGraph.activeInHierarchy(legendParent))
        {
            theGraph.SetActive(legendParent, true);
        }
        if (legendType == labelTypes.None && theGraph.activeInHierarchy(legendParent))
        {
            theGraph.SetActive(legendParent, false);
        }
        changeSpriteWidth(legendBackground, Mathf.RoundToInt(legendWidth));
        changeSpriteHeight(legendBackground, Mathf.RoundToInt(10 + legendRowHeight * numSlices));
        WMG_Node legendSlice = slices[0].GetComponent <WMG_Node>();

        if (legendLocation == legendLocations.Right)
        {
            legendParent.transform.localPosition = new Vector3(getSpriteWidth(legendBackground) / 2 + getSpriteWidth(legendSlice.objectToColor) / 2 + 20, 0, legendParent.transform.localPosition.z);
        }
        else if (legendLocation == legendLocations.Left)
        {
            legendParent.transform.localPosition = new Vector3(-getSpriteWidth(legendBackground) / 2 - getSpriteWidth(legendSlice.objectToColor) / 2 - 20, 0, legendParent.transform.localPosition.z);
        }
        else if (legendLocation == legendLocations.Below)
        {
            legendParent.transform.localPosition = new Vector3(0, -getSpriteHeight(legendBackground) / 2 - getSpriteHeight(legendSlice.objectToColor) / 2 - 20, legendParent.transform.localPosition.z);
        }

        float curTotalRot = 0;

        for (int i = 0; i < numSlices; i++)
        {
            // Update Legend Entries
            sliceLegendEntries[i].transform.localPosition = new Vector3(-getSpriteWidth(legendBackground) / 2 + 30, getSpriteHeight(legendBackground) / 2 - 25 - i * 40, 0);
            WMG_Node sliceLegend       = sliceLegendEntries[i].GetComponent <WMG_Node>();
            UISprite sliceLegendSprite = sliceLegend.objectToColor.GetComponent <UISprite>();
            float    numberToMult      = Mathf.Pow(10f, numberDecimalsInPercents + 2);

            // Update Pie Slices
            float newAngle = -1 * curTotalRot;
            if (newAngle < 0)
            {
                newAngle += 360;
            }
            WMG_Node pieSlice = slices[i].GetComponent <WMG_Node>();
            if (sliceLabelType != labelTypes.None && !theGraph.activeInHierarchy(pieSlice.objectToLabel))
            {
                theGraph.SetActive(pieSlice.objectToLabel, true);
            }
            if (sliceLabelType == labelTypes.None && theGraph.activeInHierarchy(pieSlice.objectToLabel))
            {
                theGraph.SetActive(pieSlice.objectToLabel, false);
            }

            // Set Slice Data and maybe Other Slice Data
            float slicePercent = sliceValues[i] / totalVal;
            if (isOtherSlice && i == numSlices - 1)
            {
                slicePercent = otherSliceValue / totalVal;
                StartCoroutine(AnimateSpriteFill(i, animateDuration, slicePercent, newAngle, numSlices - 1, includeOthersColor));                 // Animate fill and rotation of slice sprites
                setLabelData(sliceLegend.objectToLabel, legendType, includeOthersLabel, slicePercent, otherSliceValue, numberToMult);
                setLabelData(pieSlice.objectToLabel, sliceLabelType, includeOthersLabel, slicePercent, otherSliceValue, numberToMult);
                sliceLegendSprite.color = includeOthersColor;
            }
            else
            {
                StartCoroutine(AnimateSpriteFill(i, animateDuration, slicePercent, newAngle, numSlices - 1, sliceColors[i]));                 // Animate fill and rotation of slice sprites
                setLabelData(sliceLegend.objectToLabel, legendType, sliceLabels[i], slicePercent, sliceValues[i], numberToMult);
                setLabelData(pieSlice.objectToLabel, sliceLabelType, sliceLabels[i], slicePercent, sliceValues[i], numberToMult);
                sliceLegendSprite.color = sliceColors[i];
            }

            curTotalRot += slicePercent * 360;
        }
    }