Ejemplo n.º 1
0
    public void Init()
    {
        if (hasInit) return;
        hasInit = true;

        pieGraph = theGraph.GetComponent<WMG_Pie_Graph>();
        axisGraph = theGraph.GetComponent<WMG_Axis_Graph>();

        changeObjs.Add(legendC);

        setOriginalPropertyValues();

        legendC.OnChange += LegendChanged;

        PauseCallbacks();
    }
Ejemplo n.º 2
0
 // constructor method.
 public GraphPositioning(WMG_Graph_Manager graph)
 {
     graphrt = graph.GetComponent <RectTransform>();
 }
Ejemplo n.º 3
0
    public void updateLegend()
    {
        if (legendTypeChanged || legendChanged)
        {
            if (!hideLegend && showBackground && !theGraph.activeInHierarchy(background))
            {
                theGraph.SetActive(background, true);
            }
            if ((hideLegend || !showBackground) && theGraph.activeInHierarchy(background))
            {
                theGraph.SetActive(background, false);
            }
            if (!hideLegend && !theGraph.activeInHierarchy(entriesParent))
            {
                theGraph.SetActive(entriesParent, true);
            }
            if (hideLegend && theGraph.activeInHierarchy(entriesParent))
            {
                theGraph.SetActive(entriesParent, false);
            }
            if (hideLegend)
            {
                return;
            }

            // Swap parent offsets when changing the legend type
            if (legendTypeChanged)
            {
                theGraph.SwapVals <float>(ref offsetX, ref offsetY);
            }

            WMG_Axis_Graph axisGraph    = theGraph.GetComponent <WMG_Axis_Graph>();
            WMG_Pie_Graph  pieGraph     = theGraph.GetComponent <WMG_Pie_Graph>();
            float          graphY       = 0;
            float          graphX       = 0;
            float          maxPointSize = 0;
            if (axisGraph != null)
            {
                graphY       = axisGraph.yAxisLength;
                graphX       = axisGraph.xAxisLength;
                maxPointSize = axisGraph.getMaxPointSize();
            }
            if (pieGraph != null)
            {
                graphY       = pieGraph.pieSize + pieGraph.explodeLength;
                graphX       = graphY;
                maxPointSize = pieSwatchSize;
            }
            int numEntries = legendEntries.Count;
            for (int j = 0; j < legendEntries.Count; j++)
            {
                if (!activeInHierarchy(legendEntries[j].gameObject))
                {
                    numEntries--;
                }
            }
            int maxInRowOrColumn = Mathf.CeilToInt(1f * numEntries / numRowsOrColumns);             // Max elements in a row for horizontal legends

            float oppositeSideOffset = 0;
            if (legendType == legendTypes.Bottom)
            {
                if (autoCenterLegend)
                {
                    offsetX = (-maxInRowOrColumn * legendEntryWidth) / 2f + legendEntryLinkSpacing + 5;
                }
                if (oppositeSideLegend)
                {
                    oppositeSideOffset = 2 * offsetY + graphY;
                }
                changeSpritePositionTo(this.gameObject, new Vector3(graphX / 2 + offsetX, -offsetY + oppositeSideOffset, 0));
            }
            else if (legendType == legendTypes.Right)
            {
                if (autoCenterLegend)
                {
                    offsetY = (-(maxInRowOrColumn - 1) * legendEntryHeight) / 2f;
                }
                if (oppositeSideLegend)
                {
                    oppositeSideOffset = -2 * offsetX - graphX - legendEntryWidth + 2 * legendEntryLinkSpacing;
                }
                changeSpritePositionTo(this.gameObject, new Vector3(graphX + offsetX + oppositeSideOffset, graphY / 2 - offsetY, 0));
            }
            if (pieGraph != null)
            {
                Vector2 offset = pieGraph.getPaddingOffset();
                changeSpritePositionRelativeToObjBy(this.gameObject, this.gameObject, new Vector3(-graphX / 2f - offset.x, -graphY / 2f - offset.y));
            }

            int numRows = maxInRowOrColumn;
            int numCols = numRowsOrColumns;

            if (legendType == legendTypes.Right)
            {
                theGraph.SwapVals <int>(ref numRows, ref numCols);
            }

            changeSpriteWidth(background, Mathf.RoundToInt(legendEntryWidth * numRows + 2 * backgroundPadding + legendEntryLinkSpacing));
            changeSpriteHeight(background, Mathf.RoundToInt(legendEntryHeight * numCols + 2 * backgroundPadding));
            changeSpritePositionTo(background, new Vector3(-backgroundPadding - legendEntryLinkSpacing - maxPointSize / 2f, backgroundPadding + legendEntryHeight / 2f));

            if (numRowsOrColumns < 1)
            {
                numRowsOrColumns = 1;                                   // Ensure not less than 1
            }
            if (numRowsOrColumns > numEntries)
            {
                numRowsOrColumns = numEntries;                                            // Ensure cannot exceed number series
            }
            int extras = 0;
            if (numEntries > 0)
            {
                extras = numEntries % numRowsOrColumns; // When the number series does not divide evenly by the num rows setting, then this is the number of extras
            }
            int  origExtras       = extras;             // Save the original extras, since we will need to decrement extras in the loop
            int  cumulativeOffset = 0;                  // Used to offset the other dimension, for example, elements moved to a lower row (y), need to also move certain distance (x) left
            int  previousI        = 0;                  // Used to determine when the i (row for horizontal) has changed from the previous i, which is used to increment the cumulative offset
            bool useSmaller       = false;              // Used to determine whether we need to subtract 1 from maxInRowOrColumn when calculating the cumulative offset

            if (maxInRowOrColumn == 0)
            {
                return;                                    // Legend hidden / all entries deactivated
            }
            // Calculate the position of the legend entry for each line series
            for (int j = 0; j < legendEntries.Count; j++)
            {
                WMG_Legend_Entry legendEntry = legendEntries[j];

                if (axisGraph != null)
                {
                    theGraph.changeSpritePositionRelativeToObjBy(legendEntry.nodeLeft, legendEntry.swatchNode, new Vector3(-legendEntryLinkSpacing, 0, 0));
                    theGraph.changeSpritePositionRelativeToObjBy(legendEntry.nodeRight, legendEntry.swatchNode, new Vector3(legendEntryLinkSpacing, 0, 0));

                    WMG_Link theLine = legendEntry.line.GetComponent <WMG_Link>();
                    theLine.Reposition();
                }
                else
                {
                    changeSpriteWidth(legendEntry.swatchNode, Mathf.RoundToInt(pieSwatchSize));
                    changeSpriteHeight(legendEntry.swatchNode, Mathf.RoundToInt(pieSwatchSize));
                }

                theGraph.changeSpritePositionToX(legendEntry.label, legendEntrySpacing);

                // Legend text
                if (axisGraph != null)
                {
                    string theText = legendEntry.seriesRef.seriesName;

                    if (labelType == WMG_Enums.labelTypes.None)
                    {
                        theText = "";
                    }
                    changeLabelText(legendEntry.label, theText);
                }
                legendEntry.label.transform.localScale = new Vector3(legendEntryFontSize, legendEntryFontSize, 1);

                // i is the row for horizontal legends, and the column for vertical
                int i = Mathf.FloorToInt(j / maxInRowOrColumn);
                if (origExtras > 0)
                {
                    i = Mathf.FloorToInt((j + 1) / maxInRowOrColumn);
                }

                // If there were extras, but no longer any more extras, then need to subtract 1 from the maxInRowOrColumn, and recalculate i
                if (extras == 0 && origExtras > 0)
                {
                    i = origExtras + Mathf.FloorToInt((j - origExtras * maxInRowOrColumn) / (maxInRowOrColumn - 1));
                    if ((j - origExtras * maxInRowOrColumn) > 0)
                    {
                        useSmaller = true;
                    }
                }

                // When there are extras decrease i for the last element in the row
                if (extras > 0)
                {
                    if ((j + 1) % maxInRowOrColumn == 0)
                    {
                        extras--;
                        i--;
                    }
                }

                // Increment cumulative offset when i changes, use offset to position other dimension correctly.
                if (previousI != i)
                {
                    previousI = i;
                    if (useSmaller)
                    {
                        cumulativeOffset += (maxInRowOrColumn - 1);
                    }
                    else
                    {
                        cumulativeOffset += maxInRowOrColumn;
                    }
                }

                // Set the position based on the series index (j), i (row index for horizontal), and cumulative offset
                if (legendType == legendTypes.Bottom)
                {
                    theGraph.changeSpritePositionTo(legendEntry.gameObject, new Vector3(j * legendEntryWidth - legendEntryWidth * cumulativeOffset, -i * legendEntryHeight, 0));
                }
                else if (legendType == legendTypes.Right)
                {
                    theGraph.changeSpritePositionTo(legendEntry.gameObject, new Vector3(i * legendEntryWidth, -j * legendEntryHeight + legendEntryHeight * cumulativeOffset, 0));
                }
            }
        }
    }