예제 #1
0
    private void Awake()
    {
        // Grab base objects references
        graphContainer    = transform.Find("graphContainer").GetComponent <RectTransform>();
        labelTemplateX    = graphContainer.Find("labelTemplateX").GetComponent <RectTransform>();
        labelTemplateY    = graphContainer.Find("labelTemplateY").GetComponent <RectTransform>();
        dashContainer     = graphContainer.Find("dashContainer").GetComponent <RectTransform>();
        dashTemplateX     = dashContainer.Find("dashTemplateX").GetComponent <RectTransform>();
        dashTemplateY     = dashContainer.Find("dashTemplateY").GetComponent <RectTransform>();
        tooltipGameObject = graphContainer.Find("tooltip").gameObject;

        labelTemplateX.gameObject.SetActive(false);
        labelTemplateY.gameObject.SetActive(false);
        dashTemplateX.gameObject.SetActive(false);
        dashTemplateY.gameObject.SetActive(false);

        startYScaleAtZero     = true;
        gameObjectList        = new List <GameObject>();
        yLabelList            = new List <RectTransform>();
        graphVisualObjectList = new List <IGraphVisualObject>();

        lineGraphVisual = new LineGraphVisual(graphContainer, dotSprite, Color.green, new Color(1, 1, 1, .5f), this);
        barChartVisual  = new BarChartVisual(graphContainer, Color.white, .8f, this);

        valueList = new List <int> {
            0
        };

        HideTooltip();
    }
예제 #2
0
    private void Awake()
    {
        // Grab base objects references
        graphContainer    = transform.Find("graphContainer").GetComponent <RectTransform>();
        labelTemplateX    = graphContainer.Find("labelTemplateX").GetComponent <RectTransform>();
        labelTemplateY    = graphContainer.Find("labelTemplateY").GetComponent <RectTransform>();
        dashContainer     = graphContainer.Find("dashContainer").GetComponent <RectTransform>();
        dashTemplateX     = dashContainer.Find("dashTemplateX").GetComponent <RectTransform>();
        dashTemplateY     = dashContainer.Find("dashTemplateY").GetComponent <RectTransform>();
        tooltipGameObject = graphContainer.Find("tooltip").gameObject;

        labelTemplateX.gameObject.SetActive(false);
        labelTemplateY.gameObject.SetActive(false);
        dashTemplateX.gameObject.SetActive(false);
        dashTemplateY.gameObject.SetActive(false);

        startYScaleAtZero     = true;
        gameObjectList        = new List <GameObject>();
        yLabelList            = new List <RectTransform>();
        graphVisualObjectList = new List <IGraphVisualObject>();

        lineGraphVisual = new LineGraphVisual(graphContainer, dotSprite, color, color, this);
        barChartVisual  = new BarChartVisual(graphContainer, Color.white, .8f, this);

        valueList = new List <int>(30);
        for (int i = 0; i < valueList.Capacity; ++i)
        {
            valueList.Add(0);
        }
        ShowGraph(valueList, lineGraphVisual, 100);

        HideTooltip();
    }
    private void Awake()
    {
        //instance = this;
        templates      = GameObject.Find("Templates").GetComponent <RectTransform>();
        lableTemplateX = templates.Find("LableTemplateX").GetComponent <RectTransform>();
        lableTemplateY = templates.Find("LableTemplateY").GetComponent <RectTransform>();
        //tooltipGameObject = templates.Find("Tooltip").gameObject;

        redPerformance    = GameObject.Find("RedPerformance").GetComponent <RectTransform>();
        greenPerformance  = GameObject.Find("GreenPerformance").GetComponent <RectTransform>();
        bluePerformance   = GameObject.Find("BluePerformance").GetComponent <RectTransform>();
        bodyPerformance   = GameObject.Find("BodyPerformance").GetComponent <RectTransform>();
        drivePerformance  = GameObject.Find("DrivePerformance").GetComponent <RectTransform>();
        actionPerformance = GameObject.Find("ActionPerformance").GetComponent <RectTransform>();
        //allPerformance = GameObject.Find("AllPerformance").GetComponent<RectTransform>();

        redGraphVisualObjectList    = new List <IGraphVisualObject>();
        greenGraphVisualObjectList  = new List <IGraphVisualObject>();
        blueGraphVisualObjectList   = new List <IGraphVisualObject>();
        bodyGraphVisualObjectList   = new List <IGraphVisualObject>();
        driveGraphVisualObjectList  = new List <IGraphVisualObject>();
        actionGraphVisualObjectList = new List <IGraphVisualObject>();
        //allGraphVisualObjectList = new List<IGraphVisualObject>();

        redGameObjectList    = new List <GameObject>();
        greebGameObjectList  = new List <GameObject>();
        blueGameObjectList   = new List <GameObject>();
        bodyGameObjectList   = new List <GameObject>();
        driveGameObjectList  = new List <GameObject>();
        actionGameObjectList = new List <GameObject>();
        //allGameObjectList = new List<GameObject>();
        yLabelList = new List <RectTransform>();

        startYScaleAtZero = true;
        //valueList = new List<float>() {1,2,3,4,5 };
        //HideTooltip();

        for (int i = 0; i < 1200; i++)
        {
            GameObject dotGameObject = new GameObject("Dot", typeof(Image));
            dotGameObject.transform.SetParent(dotPooling, false);
            dotGameObject.SetActive(false);
            dotList.Add(dotGameObject);
            GameObject connectionGameObject = new GameObject("dotConnection", typeof(Image));
            connectionGameObject.transform.SetParent(connectionPooling, false);
            connectionGameObject.SetActive(false);
            connectionList.Add(connectionGameObject);
        }

        redLineGraphVisual    = new LineGraphVisual(redPerformance, dotSprite, Color.red, new Color(1, 1, 1, .5f), dotList, connectionList);
        greenLineGraphVisual  = new LineGraphVisual(greenPerformance, dotSprite, Color.green, new Color(1, 1, 1, .5f), dotList, connectionList);
        blueLineGraphVisual   = new LineGraphVisual(bluePerformance, dotSprite, Color.cyan, new Color(1, 1, 1, .5f), dotList, connectionList);
        bodyLineGraphVisual   = new LineGraphVisual(bodyPerformance, dotSprite, Color.white, new Color(1, 1, 1, .5f), dotList, connectionList);
        driveLineGraphVisual  = new LineGraphVisual(drivePerformance, dotSprite, Color.white, new Color(1, 1, 1, .5f), dotList, connectionList);
        actionLineGraphVisual = new LineGraphVisual(actionPerformance, dotSprite, Color.white, new Color(1, 1, 1, .5f), dotList, connectionList);
    }
    private void Start()
    {
        // Set Up graphVisual objects to use by Show Graph Method.
        IGraphVisual lineChartVisual = new LineGraphVisual(graphContainer, graphDotSprite, Color.white);

        lineGraphVisual = lineChartVisual;

        IGraphVisual barChartVisual = new BarChartVisual(graphContainer, colourOfBars);

        barGraphVisual = barChartVisual;

        graphGameObjectList = new List <GameObject>();
    }
예제 #5
0
 public WindowGraph(RectTransform graphContainer, IGraphVisual graphVisual)
 {
     GameObjectList        = new List <GameObject>();
     GraphVisualObjectList = new List <IGraphVisualObject>();
     startYScaleAtZero     = true;
     yLabelList            = new List <RectTransform>();
     GraphContainer        = graphContainer;
     LabelTemplateX        = GraphContainer.Find("LabelTemplateX").GetComponent <RectTransform>();
     LabelTemplateY        = GraphContainer.Find("LabelTemplateY").GetComponent <RectTransform>();
     DashTemplateX         = GraphContainer.Find("DashTemplateY").GetComponent <RectTransform>();
     DashTemplateY         = GraphContainer.Find("DashTemplateX").GetComponent <RectTransform>();
     GraphVisual           = graphVisual;
 }
    private void ShowGraph(
        List <int> valuesList, IGraphVisual graphVisual,
        int maxVisibleAmount = -1, Func <int, string> getAxisLabelX = null, Func <float, string> getAxisLabelY = null)
    {
        this.valuesList    = valuesList;
        this.graphVisual   = graphVisual;
        this.getAxisLabelX = getAxisLabelX;
        this.getAxisLabelY = getAxisLabelY;

        if (maxVisibleAmount <= 1)
        {
            maxVisibleAmount = valuesList.Count;
        }
        else if (maxVisibleAmount > valuesList.Count)
        {
            maxVisibleAmount = 2;
        }
        this.maxVisibleAmount = maxVisibleAmount;

        DestroyGraph();

        float graphHeight = graphContainer.sizeDelta.y;
        float graphWidth  = graphContainer.sizeDelta.x;

        xSize = graphWidth / (maxVisibleAmount + 1);

        int xIndex = 0;

        for (int i = Mathf.Max(valuesList.Count - maxVisibleAmount, 0); i < valuesList.Count; i++)
        {
            float  xPos       = xSize + xIndex * xSize;
            float  yPos       = valuesList[i] / 100f * graphHeight;
            string toolTipTxt = getAxisLabelY(valuesList[i]);
            bool   isEnd      = i == valuesList.Count - 1;

            graphVisualObjects.Add(graphVisual.CreateGraphVisualObject(new Vector2(xPos, yPos), xSize, toolTipTxt, isEnd));
            CreateSeparatorsX(i, xPos);

            xIndex++;
        }

        CreateSeparatorsY(graphHeight);
    }
예제 #7
0
    public void ShowGraph(List <int> valueList, GraphVisualType graphType)
    {
        IGraphVisual graphVisual = lineGraphVisual;

        switch (graphType)
        {
        case GraphVisualType.LineGraph:
            graphVisual = lineGraphVisual;
            break;

        case GraphVisualType.BarGraph:
            graphVisual = barChartVisual;
            break;

        default:
            break;
        }
        ShowGraph(valueList, graphVisual, -1, (int _i) => "" + (_i + 1), (int _f) => "" + _f);
    }
예제 #8
0
    private void HandleReferences(out IGraphVisual lineGraphVisual, out IGraphVisual barChartVisual)
    {
        statisticsManager = GameObject.FindObjectOfType <StatisticsManager> ();
        // Grab base objects references
        graphContainer    = transform.Find("graphContainer").GetComponent <RectTransform> ();
        labelTemplateX    = graphContainer.Find("labelTemplateX").GetComponent <RectTransform> ();
        labelTemplateY    = graphContainer.Find("labelTemplateY").GetComponent <RectTransform> ();
        dashContainer     = graphContainer.Find("dashContainer").GetComponent <RectTransform> ();
        dashTemplateX     = dashContainer.Find("dashTemplateX").GetComponent <RectTransform> ();
        dashTemplateY     = dashContainer.Find("dashTemplateY").GetComponent <RectTransform> ();
        tooltipGameObject = graphContainer.Find("tooltip").gameObject;

        startYScaleAtZero     = true;
        gameObjectList        = new List <GameObject> ();
        yLabelList            = new List <RectTransform> ();
        graphVisualObjectList = new List <IGraphVisualObject> ();

        lineGraphVisual = new LineGraphVisual(graphContainer, dotSprite, Color.green, new Color(1, 1, 1, .5f));
        barChartVisual  = new BarChartVisual(graphContainer, Color.white, .8f);
    }
예제 #9
0
    private void SetUpButtons(IGraphVisual lineGraphVisual, IGraphVisual barChartVisual)
    {
        transform.Find("barChartBtn").GetComponent <Button_UI> ().ClickFunc = () =>
        {
            SetGraphVisual(barChartVisual);
        };
        transform.Find("lineGraphBtn").GetComponent <Button_UI> ().ClickFunc = () =>
        {
            SetGraphVisual(lineGraphVisual);
        };

        transform.Find("decreaseVisibleAmountBtn").GetComponent <Button_UI> ().ClickFunc = () =>
        {
            DecreaseVisibleAmount();
        };
        transform.Find("increaseVisibleAmountBtn").GetComponent <Button_UI> ().ClickFunc = () =>
        {
            IncreaseVisibleAmount();
        };
    }
예제 #10
0
    public void CreateGraphVisuals(int predictionIndex)
    {
        //graphContainer = transform.Find("graphContainer").GetComponent<RectTransform>();
        //labelTemplateX = graphContainer.Find("labelTemplateX").GetComponent<RectTransform>();
        //labelTemplateY = graphContainer.Find("labelTemplateY").GetComponent<RectTransform>();
        //dashTemplateX = graphContainer.Find("dashTemplateX").GetComponent<RectTransform>();
        //dashTemplateY = graphContainer.Find("dashTemplateY").GetComponent<RectTransform>();
        //tooltipGameObject = graphContainer.Find("tooltip").gameObject;
        gameObjectList = new List <GameObject>();

        Color dataColor                    = new Color(0.4726771f, 0.8867924f, 0.4782762f, 1);
        Color dotConnectionColor           = new Color(1, 1, 1, .5f);
        Color predictionColor              = new Color(1, 0.3952115f, 0.3349057f, 1);
        Color dotConnectionPredictionColor = new Color(1f, 0.5424528f, 0.5424528f, .5f);


        lineGraphVisual = new LineGraphVisual(graphContainer, dotSprite, dataColor, dotConnectionColor, predictionColor, dotConnectionPredictionColor, predictionIndex);
        barChartVisual  = new BarChartVisual(graphContainer, dataColor, predictionColor, .8f, predictionIndex);

        ShowGraph(valueList, lineGraphVisual, 12, (float _f) => Mathf.RoundToInt(_f).ToString());
    }
예제 #11
0
    private void Awake()
    {
        instance = this;
        // Grab base objects references
        graphContainer    = transform.Find("graphContainer").GetComponent <RectTransform>();
        labelTemplateX    = graphContainer.Find("labelTemplateX").GetComponent <RectTransform>();
        labelTemplateY    = graphContainer.Find("labelTemplateY").GetComponent <RectTransform>();
        dashContainer     = graphContainer.Find("dashContainer").GetComponent <RectTransform>();
        dashTemplateX     = dashContainer.Find("dashTemplateX").GetComponent <RectTransform>();
        dashTemplateY     = dashContainer.Find("dashTemplateY").GetComponent <RectTransform>();
        tooltipGameObject = graphContainer.Find("tooltip").gameObject;

        gameObjectList        = new List <GameObject>();
        graphVisualObjectList = new List <IGraphVisualObject>();

        lineGraphVisual = new LineGraphVisual(graphContainer, dotSprite, Color.green, new Color(1, 1, 1, .5f));
        barChartVisual  = new BarChartVisual(graphContainer, Color.white, .8f);

        HideTooltip();

        //valueList = graphValues;
        //ShowGraph(valueList, barChartVisual, -1, (int _i) => "", (float _f) => Mathf.RoundToInt(_f) + " °C");
    }
예제 #12
0
    private void Awake()
    {
        graphContainer = transform.Find("GraphContainer").GetComponent<RectTransform>();
        labelTemplateX = graphContainer.Find("LabelTemplateX").GetComponent<RectTransform>();
        labelTemplateY = graphContainer.Find("LabelTemplateY").GetComponent<RectTransform>();
        gridTemplateY = graphContainer.Find("GridTemplateY").GetComponent<RectTransform>();
        gridTemplateX = graphContainer.Find("GridTemplateX").GetComponent<RectTransform>();
        gameObjectList = new List<GameObject>();

        valueList = new List<int>();

        valueList.Clear();

        for(int i = 0; i < 23; i++)
        {
            valueList.Add(UnityEngine.Random.Range(-300, 600));
        }

        lineGraphVisual = new LineGraphVisual(graphContainer, dotSprite);
        barChartVisual = new BarChartVisual(graphContainer, Color.blue, 0.9f);

        //Show the graph using day and $ labels for the axis (these can be anything)
        this.ShowGraph(valueList, lineGraphVisual, -1, (int _i) => "Day " + (_i + 1), (float _f) => "$" + Mathf.RoundToInt(_f));
    }
예제 #13
0
    private void Awake()
    {
        instance = this;
        // Grab base objects references
        graphContainer    = transform.Find("graphContainer").GetComponent <RectTransform>();
        labelTemplateX    = graphContainer.Find("labelTemplateX").GetComponent <RectTransform>();
        labelTemplateY    = graphContainer.Find("labelTemplateY").GetComponent <RectTransform>();
        dashContainer     = graphContainer.Find("dashContainer").GetComponent <RectTransform>();
        dashTemplateX     = dashContainer.Find("dashTemplateX").GetComponent <RectTransform>();
        dashTemplateY     = dashContainer.Find("dashTemplateY").GetComponent <RectTransform>();
        tooltipGameObject = graphContainer.Find("tooltip").gameObject;

        startYScaleAtZero     = true;
        gameObjectList        = new List <GameObject>();
        yLabelList            = new List <RectTransform>();
        graphVisualObjectList = new List <IGraphVisualObject>();

        lineGraphVisual = new LineGraphVisual(graphContainer, dotSprite, Color.green, new Color(1, 1, 1, .5f));
        barChartVisual  = new BarChartVisual(graphContainer, Color.white, .8f);
        // Set up buttons
        //transform.Find("barChartBtn").GetComponent<Button_UI>().ClickFunc = () => {
        //    SetGraphVisual(barChartVisual);
        //};
        //transform.Find("lineGraphBtn").GetComponent<Button_UI>().ClickFunc = () => {
        //    SetGraphVisual(lineGraphVisual);
        //};

        //transform.Find("decreaseVisibleAmountBtn").GetComponent<Button_UI>().ClickFunc = () => {
        //    DecreaseVisibleAmount();
        //};
        //transform.Find("increaseVisibleAmountBtn").GetComponent<Button_UI>().ClickFunc = () => {
        //    IncreaseVisibleAmount();
        //};

        //transform.Find("dollarBtn").GetComponent<Button_UI>().ClickFunc = () => {
        //    SetGetAxisLabelY((float _f) => "$" + Mathf.RoundToInt(_f));
        //};
        //transform.Find("euroBtn").GetComponent<Button_UI>().ClickFunc = () => {
        //    SetGetAxisLabelY((float _f) => "€" + Mathf.RoundToInt(_f / 1.18f));
        //};

        HideTooltip();

        // Set up base values
        //List<int> valueList = new List<int>() { 12, 17, 19, 22, 30, 12, 17, 19, 22, 30 };
        //ShowGraph(valueList, lineGraphVisual, -1, (int _i) => "" + (_i + 1), (float _f) => "" + _f);

        /*
         * // Automatically modify graph values and visual
         * bool useBarChart = true;
         * FunctionPeriodic.Create(() => {
         *  for (int i = 0; i < valueList.Count; i++) {
         *      valueList[i] = Mathf.RoundToInt(valueList[i] * UnityEngine.Random.Range(0.8f, 1.2f));
         *      if (valueList[i] < 0) valueList[i] = 0;
         *  }
         *  if (useBarChart) {
         *      ShowGraph(valueList, barChartVisual, -1, (int _i) => "Day " + (_i + 1), (float _f) => "$" + Mathf.RoundToInt(_f));
         *  } else {
         *      ShowGraph(valueList, lineGraphVisual, -1, (int _i) => "Day " + (_i + 1), (float _f) => "$" + Mathf.RoundToInt(_f));
         *  }
         *  useBarChart = !useBarChart;
         * }, .5f);
         * //*/

        //int index = 0;
        //FunctionPeriodic.Create(() => {
        //    index = (index + 1) % valueList.Count;
        //}, .1f);
        //FunctionPeriodic.Create(() => {
        //    //int index = UnityEngine.Random.Range(0, valueList.Count);
        //    UpdateValue(index, valueList[index] + UnityEngine.Random.Range(1, 3));
        //}, .02f);
    }
예제 #14
0
 private void SetGraphVisual(IGraphVisual graphVisual)
 {
     ShowGraph(valueList, graphVisual, maxVisibleValueAmount, getAxisLabelX, getAxisLabelY);
 }
예제 #15
0
    private void ShowGraph(List <int> valueList, IGraphVisual graphVisual, int maxVisibleValueAmount = -1, Func <int, string> getAxisLabelX = null, Func <float, string> getAxisLabelY = null)
    {
        this.valueList     = valueList;
        this.graphVisual   = graphVisual;
        this.getAxisLabelX = getAxisLabelX;
        this.getAxisLabelY = getAxisLabelY;

        if (maxVisibleValueAmount <= 0)
        {
            maxVisibleValueAmount = valueList.Count;
        }
        if (maxVisibleValueAmount > valueList.Count)
        {
            maxVisibleValueAmount = valueList.Count;
        }
        this.maxVisibleValueAmount = maxVisibleValueAmount;

        if (getAxisLabelX == null)
        {
            getAxisLabelX = delegate(int _i) { return(_i.ToString()); };
        }
        if (getAxisLabelY == null)
        {
            getAxisLabelY = delegate(float _f) { return(Mathf.RoundToInt(_f).ToString()); };
        }

        foreach (GameObject gameObject in gameObjectList)
        {
            Destroy(gameObject);
        }
        gameObjectList.Clear();
        yLabelList.Clear();

        foreach (IGraphVisualObject graphVisualObject in graphVisualObjectList)
        {
            graphVisualObject.CleanUp();
        }
        graphVisualObjectList.Clear();
        graphVisual.CleanUp();

        float graphHeight = graphContainer.sizeDelta.y;
        float graphWidth  = graphContainer.sizeDelta.x;

        float yMinimum, yMaximum;

        CalculateYScale(out yMinimum, out yMaximum);
        xSize = graphWidth / (maxVisibleValueAmount + 1);

        int xIndex = 0;

        for (int i = Mathf.Max(valueList.Count - maxVisibleValueAmount, 0); i < valueList.Count; ++i)
        {
            float xposition = xSize + xIndex * xSize;
            float yposition = ((valueList[i] - yMinimum) / (yMaximum - yMinimum)) * graphHeight;

            string             toolTipText       = getAxisLabelY(valueList[i]);
            IGraphVisualObject graphVisualObject = graphVisual.CreateGraphVisualObject(new Vector2(xposition, yposition), xSize, toolTipText);
            graphVisualObjectList.Add(graphVisualObject);

            RectTransform labelX = Instantiate(labelTemplateX);
            labelX.SetParent(graphContainer);
            labelX.gameObject.SetActive(true);
            labelX.anchoredPosition           = new Vector2(xposition, -20f);
            labelX.GetComponent <Text>().text = getAxisLabelX(i);
            gameObjectList.Add(labelX.gameObject);

            RectTransform dashX = Instantiate(dashTemplateX);
            dashX.SetParent(graphContainer);
            dashX.gameObject.SetActive(true);
            dashX.anchoredPosition = new Vector2(xposition, 0f);
            gameObjectList.Add(dashX.gameObject);
            xIndex++;
        }

        int sepraterCount = 10;

        for (int i = 0; i <= sepraterCount; i++)
        {
            RectTransform labelY = Instantiate(lableTemplateY);
            labelY.SetParent(graphContainer, false);
            labelY.gameObject.SetActive(true);
            float normalisedValue = i * 1f / sepraterCount;
            labelY.anchoredPosition           = new Vector2(-80f, normalisedValue * graphHeight);
            labelY.GetComponent <Text>().text = getAxisLabelY(yMinimum + (normalisedValue * (yMaximum - yMinimum)));
            yLabelList.Add(labelY);
            gameObjectList.Add(labelY.gameObject);

            RectTransform dashY = Instantiate(dashTemplateY);
            dashY.SetParent(dashContainer, false);
            dashY.gameObject.SetActive(true);
            dashY.anchoredPosition = new Vector2(0f, normalisedValue * graphHeight);
            gameObjectList.Add(dashY.gameObject);
        }
    }
예제 #16
0
    private void ShowGraph(List <int> valueList, IGraphVisual graphVisual, int maxVisibleValueAmount = -1, Func <int, string> getAxisLabelX = null, Func <float, string> getAxisLabelY = null)
    {
        this.valueList     = valueList;
        this.graphVisual   = graphVisual;
        this.getAxisLabelX = getAxisLabelX;
        this.getAxisLabelY = getAxisLabelY;

        if (valueList.Count == 0)
        {
            return;
        }

        if (maxVisibleValueAmount <= 0)
        {
            // Show all if no amount specified
            maxVisibleValueAmount = valueList.Count;
        }
        if (maxVisibleValueAmount > valueList.Count)
        {
            // Validate the amount to show the maximum
            maxVisibleValueAmount = valueList.Count;
        }

        this.maxVisibleValueAmount = maxVisibleValueAmount;

        // Test for label defaults
        if (getAxisLabelX == null)
        {
            getAxisLabelX = delegate(int _i) { return(_i.ToString()); };
        }
        if (getAxisLabelY == null)
        {
            getAxisLabelY = delegate(float _f) { return(Mathf.RoundToInt(_f).ToString()); };
        }

        // Clean up previous graph
        foreach (GameObject gameObject in gameObjectList)
        {
            Destroy(gameObject);
        }
        gameObjectList.Clear();

        foreach (IGraphVisualObject graphVisualObject in graphVisualObjectList)
        {
            graphVisualObject.CleanUp();
        }
        graphVisualObjectList.Clear();

        graphVisual.CleanUp();

        // Grab the width and height from the container
        float graphWidth  = graphContainer.sizeDelta.x;
        float graphHeight = graphContainer.sizeDelta.y;

        // Identify y Min and Max values
        float yMaximum = valueList[0];
        float yMinimum = valueList[0];

        for (int i = Mathf.Max(valueList.Count - maxVisibleValueAmount, 0); i < valueList.Count; i++)
        {
            int value = valueList[i];
            if (value > yMaximum)
            {
                yMaximum = value;
            }
            if (value < yMinimum)
            {
                yMinimum = value;
            }
        }

        float yDifference = yMaximum - yMinimum;

        if (yDifference <= 0)
        {
            yDifference = 5f;
        }
        yMaximum = yMaximum + (yDifference * 0.2f);
        yMinimum = yMinimum - (yDifference * 0.2f);

        yMinimum = 0f; // Start the graph at zero

        // Set the distance between each point on the graph
        float xSize = graphWidth / (maxVisibleValueAmount);

        // Cycle through all visible data points
        int xIndex = 0;

        for (int i = Mathf.Max(valueList.Count - maxVisibleValueAmount, 0); i < valueList.Count; i++)
        {
            float xPosition = xIndex * xSize;
            float yPosition = ((valueList[i] - yMinimum) / (yMaximum - yMinimum)) * graphHeight;

            // Add data point visual
            string             tooltipText       = getAxisLabelY(valueList[i]);
            IGraphVisualObject graphVisualObject = graphVisual.CreateGraphVisualObject(new Vector2(xPosition, yPosition), xSize, tooltipText);
            graphVisualObjectList.Add(graphVisualObject);

            // Duplicate the x label template
            RectTransform labelX = Instantiate(labelTemplateX);
            labelX.SetParent(graphContainer, false);
            labelX.gameObject.SetActive(true);
            labelX.anchoredPosition           = new Vector2(xPosition, -7f);
            labelX.GetComponent <Text>().text = getAxisLabelX(i);
            gameObjectList.Add(labelX.gameObject);

            // Duplicate the x dash template
            RectTransform dashX = Instantiate(dashTemplateX);
            dashX.SetParent(dashContainer, false);
            dashX.gameObject.SetActive(true);
            dashX.anchoredPosition = new Vector2(xPosition, -3f);
            gameObjectList.Add(dashX.gameObject);

            xIndex++;

            //graphVisualObjectList[0] = (graphVisualObject = graphVisual.CreateGraphVisualObject(new Vector2(25, 25), xSize, "F**K"));
        }
        // Set up separators on the y axis
        int separatorCount = 10;

        for (int i = 0; i <= separatorCount; i++)
        {
            // Duplicate the label template
            RectTransform labelY = Instantiate(labelTemplateY);
            labelY.SetParent(graphContainer, false);
            labelY.gameObject.SetActive(true);
            float normalizedValue = i * 1f / separatorCount;
            labelY.anchoredPosition           = new Vector2(-7f, normalizedValue * graphHeight);
            labelY.GetComponent <Text>().text = getAxisLabelY(yMinimum + (normalizedValue * (yMaximum - yMinimum)));
            gameObjectList.Add(labelY.gameObject);

            // Duplicate the dash template
            RectTransform dashY = Instantiate(dashTemplateY);
            dashY.SetParent(dashContainer, false);
            dashY.gameObject.SetActive(true);
            dashY.anchoredPosition = new Vector2(-4f, normalizedValue * graphHeight);
            gameObjectList.Add(dashY.gameObject);
        }
    }
예제 #17
0
    private void ShowGraph(List <int> valueList, IGraphVisual graphVisual, int maxVisable = -1, Func <int, string> getAxisLabelX = null, Func <float, string> getAxisLabelY = null)
    {
        this.valueList     = valueList;
        this.graphVisual   = graphVisual;
        this.getAxisLabelX = getAxisLabelX;
        this.getAxisLabelY = getAxisLabelY;

        if (getAxisLabelX == null)
        {
            getAxisLabelX = delegate(int _i) { return(_i.ToString()); };
        }
        this.maxVisable = maxVisable;

        if (getAxisLabelY == null)
        {
            getAxisLabelY = delegate(float _f) { return(Mathf.RoundToInt(_f).ToString()); };
        }
        if (maxVisable <= 0)
        {
            maxVisable = valueList.Count;
        }

        foreach (GameObject gameObject in gameObjectList)
        {
            Destroy(gameObject);
        }
        gameObjectList.Clear();

        foreach (IGraphVisualObject graphVisualObject in graphVisualObjectlist)
        {
            graphVisualObject.ClearnUp();
        }
        graphVisualObjectlist.Clear();

        float graphWidth  = graphContainer.sizeDelta.x;
        float graphHeight = graphContainer.sizeDelta.y;

        float yMax = valueList[0];
        float yMin = valueList[0];

        for (int i = Mathf.Max(valueList.Count - maxVisable, 0); i < valueList.Count; i++)
        {
            int value = valueList[i];

            if (value > yMax)
            {
                yMax = value;
            }
            if (value < yMin)
            {
                yMin = value;
            }
        }

        float yDifference = yMax - yMin;

        if (yDifference <= 0)
        {
            yDifference = 5f;
        }
        yMax = yMax + ((yDifference) * 0.2f);
        yMin = yMin - ((yDifference) * 0.2f);

        yMin = 0;         // set graph.y to zero

        float xSize  = graphWidth / (maxVisable + 1);
        int   xIndex = 0;

        transmitter_graphOffset = xSize;         // transmit the offset requred
        //Debug.Log(xSize);

        for (int i = Mathf.Max(valueList.Count - maxVisable, 0); i < valueList.Count; i++)
        {
            float xPosition = xSize + xIndex * xSize;
            float yPosition = ((valueList[i] - yMin) / (yMax - yMin)) * graphHeight;

            // Debug.Log(xPosition); // this is the index size
            transmitter_graphHeight = yPosition;             // last object height

            string tooltipText = getAxisLabelY(valueList[i]);
            graphVisualObjectlist.Add(graphVisual.CreateGraphVisualObject(new Vector2(xPosition, yPosition), xSize, tooltipText));

            RectTransform labelX = Instantiate(labelTemplateX);
            labelX.SetParent(graphContainer, false);
            labelX.gameObject.SetActive(true);
            labelX.anchoredPosition           = new Vector2(xPosition, -7f);
            labelX.GetComponent <Text>().text = getAxisLabelX(i);
            gameObjectList.Add(labelX.gameObject);

            RectTransform dashX = Instantiate(dashTemplateX);
            dashX.SetParent(graphContainer, false);
            dashX.gameObject.SetActive(true);
            dashX.anchoredPosition = new Vector2(xPosition, -3f);
            gameObjectList.Add(dashX.gameObject);

            xIndex++;
        }
        transmitter_graphSize = xIndex;         // transmits the number of values on the graph

        int separatorCount = 10;

        for (int i = 0; i <= separatorCount; i++)
        {
            RectTransform labelY = Instantiate(labelTemplateY);
            labelY.SetParent(graphContainer, false);
            labelY.gameObject.SetActive(true);
            float normalizedValue = i * 1f / separatorCount;
            labelY.anchoredPosition           = new Vector2(-7f, normalizedValue * graphHeight);
            labelY.GetComponent <Text>().text = getAxisLabelY(yMin + (normalizedValue * (yMax - yMin)));
            gameObjectList.Add(labelY.gameObject);

            RectTransform dashY = Instantiate(dashTemplateY);
            dashY.SetParent(graphContainer, false);
            dashY.gameObject.SetActive(true);
            dashY.anchoredPosition = new Vector2(-4f, normalizedValue * graphHeight);
            gameObjectList.Add(dashY.gameObject);
        }
    }
예제 #18
0
    //place points on graph in accordance to data from the valueList
    private void ShowGraph(List <int> valueList, IGraphVisual graphVisual, int maxVisibleValueAmount = -1, Func <int, string> getAxisLabelX = null, Func <float, string> getAxisLabelY = null)
    {
        this.valueList     = valueList;
        this.graphVisual   = graphVisual;
        this.getAxisLabelX = getAxisLabelX;
        this.getAxisLabelY = getAxisLabelY;


        if (maxVisibleValueAmount <= 0) //variable used to determine how many points to show on the x axis
        {
            maxVisibleValueAmount = valueList.Count;
        }
        if (maxVisibleValueAmount > valueList.Count) //variable used to determine how many points to show on the x axis
        {
            maxVisibleValueAmount = valueList.Count;
        }
        this.maxVisibleValueAmount = maxVisibleValueAmount;

        //determines what the x and y labels will be when not given an argument
        if (getAxisLabelX == null)
        {
            getAxisLabelX = delegate(int _i) { return(_i.ToString()); };
        }
        if (getAxisLabelY == null)
        {
            getAxisLabelY = delegate(float _f) { return(Mathf.RoundToInt(_f).ToString()); };
        }

        //clean up previous graph
        foreach (GameObject gameObject in gameObjectList) //destroy all previously made game objects before spawning new ones
        {
            Destroy(gameObject);
        }
        gameObjectList.Clear();
        yLabelList.Clear();

        foreach (IGraphVisualObject graphVisualObject in graphVisualObjectList)
        {
            graphVisualObject.CleanUp();
        }
        graphVisualObjectList.Clear();
        graphVisual.CleanUp();

        float graphWidth  = graphContainer.sizeDelta.x;
        float graphHeight = graphContainer.sizeDelta.y;

        float yMinimum, yMaximum;

        CalculateScale(out yMinimum, out yMaximum);


        xSize = graphWidth / (maxVisibleValueAmount + 1); //adjust spacing of data points in accordance to the graph width and the maxVisibleValueAmount, with some extra room at the end

        int xIndex = 0;                                   //variable used to determine how many points to show on the x axis

        //GameObject lastDotGameObject = null; //creates a reference to the previously created dot, to be used in connecting the dots in CreateDotConnection
        //for loop to cycle through the data in the valueList, and create the data points, x labels, etc...
        for (int i = Mathf.Max(valueList.Count - maxVisibleValueAmount, 0); i < valueList.Count; i++)
        {
            float xPosition = xSize + xIndex * xSize;                                            //determines the horizontal spacing between points
            float yPosition = ((valueList[i] - yMinimum) / (yMaximum - yMinimum)) * graphHeight; //determines the vertical spacing between points

            string tooltipText = getAxisLabelY(valueList[i]);
            graphVisualObjectList.Add(graphVisual.CreateGraphVisualObject(new Vector2(xPosition, yPosition), xSize, tooltipText));

            //create the x label
            RectTransform labelX = Instantiate(labelTemplateX);
            labelX.SetParent(graphContainer, false);
            labelX.gameObject.SetActive(true);
            labelX.anchoredPosition           = new Vector2(xPosition, -7f); //xlabel's x position is same as dot, y is under the graph
            labelX.GetComponent <Text>().text = getAxisLabelX(i);
            gameObjectList.Add(labelX.gameObject);                           //add this gameobject to the list to be destroyed after spawning

            //create the dashes for the x labels
            RectTransform dashX = Instantiate(dashTemplateX);
            dashX.SetParent(graphContainer, false);
            dashX.gameObject.SetActive(true);
            dashX.anchoredPosition = new Vector2(xPosition, -3f);
            gameObjectList.Add(dashX.gameObject); //add this gameobject to the list to be destroyed after spawning

            xIndex++;
        }
        //create the y label
        int seperatorCount = 10;

        for (int i = 0; i <= seperatorCount; i++)
        {
            RectTransform labelY = Instantiate(labelTemplateY);
            labelY.SetParent(graphContainer, false);
            labelY.gameObject.SetActive(true);
            float normalizedValue = i * 1f / seperatorCount;                                                         //determines y labels position in relation to the seperatorCount
            labelY.anchoredPosition           = new Vector2(-7f, normalizedValue * graphHeight);                     //uses the above normalizedValue and fits it in with the graphHeight
            labelY.GetComponent <Text>().text = getAxisLabelY(yMinimum + (normalizedValue * (yMaximum - yMinimum))); //determine what the y label will display
            yLabelList.Add(labelY);
            gameObjectList.Add(labelY.gameObject);                                                                   //add this gameobject to the list to be destroyed after spawning

            //create the dashes for the y labels
            RectTransform dashY = Instantiate(dashTemplateY);
            dashY.SetParent(graphContainer, false);
            dashY.gameObject.SetActive(true);
            dashY.anchoredPosition = new Vector2(-4f, normalizedValue * graphHeight);
            gameObjectList.Add(dashY.gameObject); //add this gameobject to the list to be destroyed after spawning
        }
    }
    // Draws Graph Using the GraphVisual Object.
    private void ShowGraph(List <float> valueList, IGraphVisual graphVisual, int maxVisibleValueAmount = -1, int seperatorCount = 6)
    {
        // Destroy Previous gameObjects in graphGameObjectList as we dont want them being created on top of the previous graph gameObjects.
        foreach (GameObject _gameObject in graphGameObjectList)
        {
            Destroy(_gameObject);
        }
        //clear the graphGameobject List.
        graphGameObjectList.Clear();

        // Cache value list and graph visual used.
        lastUsedValueList   = valueList;
        lastUsedGraphVisual = graphVisual;

        // Check To make sure Display Variables are in Correct Range that we want.
        if (maxVisibleValueAmount < 0)
        {
            maxVisibleValueAmount = valueList.Count;
        }

        if (maxVisibleValueAmount < 2)
        {
            maxVisibleValueAmount = 2;
        }

        if (maxVisibleValueAmount > valueList.Count)
        {
            maxVisibleValueAmount = valueList.Count;
        }

        if (seperatorCount < 2)
        {
            seperatorCount = 2;
        }

        if (seperatorCount > 15)
        {
            seperatorCount = 15;
        }

        // Cache the Checked Display Variables.
        lastUsedSeperatorCount   = seperatorCount;
        lastUsedMaxVisibleAmount = maxVisibleValueAmount;

        // Get Graph Width and Height From the Container Its Displaying In.
        float graphWidth  = graphContainer.sizeDelta.x;
        float graphHeight = graphContainer.sizeDelta.y;

        // Setup Minimum and Maximum Values to display on the Y Axis.
        float yMaximum = valueList[0];
        float yMinimum = valueList[0];

        for (int i = Mathf.Max(valueList.Count - maxVisibleValueAmount, 0); i < valueList.Count; i++)
        {
            float value = valueList[i];

            if (value > yMaximum)
            {
                yMaximum = value;
            }
            if (value < yMinimum)
            {
                yMinimum = value;
            }
        }

        // Give the graph some Padding based on the minimum and maximum Y Axis Values.
        float yDifference = yMaximum - yMinimum;

        if (yDifference <= 0)
        {
            yDifference = 5f;
        }

        yMaximum += (yDifference * 0.2f);
        yMinimum -= (yDifference * 0.2f);

        // Determine how far each step will be spaced out on the X Axis.
        float xSize = graphWidth / (maxVisibleValueAmount + 1);

        // Enable or Disable Line Renderer.
        if (useLineGraph)
        {
            graphLineRenderer.enabled       = true;
            graphLineRenderer.positionCount = maxVisibleValueAmount;
        }
        else
        {
            graphLineRenderer.enabled = false;
        }

        int xIndex = 0;

        for (int i = Mathf.Max(valueList.Count - maxVisibleValueAmount, 0); i < valueList.Count; i++)
        {
            float xPosition = xSize + xIndex * xSize;
            float yPosition = ((valueList[i] - yMinimum) / (yMaximum - yMinimum)) * graphHeight;

            string currencyAndPrice = currencyString + valueList[i].ToString();

            // Create the Graph gameObject Depending on which graph Visual we are using.
            GameObject graphVisualGameObject = graphVisual.AddGraphVisual(new Vector2(xPosition, yPosition), xSize, currencyAndPrice);
            graphGameObjectList.Add(graphVisualGameObject);

            // If we are drawing a line graph, then Set the position of each segment of the line graph.
            if (useLineGraph)
            {
                graphLineRenderer.SetPosition(i - (valueList.Count - maxVisibleValueAmount), new Vector3(xPosition, yPosition, 0));
            }

            // Create price label for the X axis.
            RectTransform labelX = Instantiate(labelTemplateX);
            labelX.SetParent(graphContainer.transform, false);
            labelX.gameObject.SetActive(true);
            labelX.anchoredPosition = new Vector2(xPosition, -5f);
            labelX.GetComponent <TextMeshProUGUI>().text = bitcoinPriceRequest.last30DatesList[i];
            graphGameObjectList.Add(labelX.gameObject);

            // Create Grid Line for the X axis.
            RectTransform lineX = Instantiate(lineTemplateX);
            lineX.SetParent(graphContainer.transform, false);
            lineX.gameObject.SetActive(true);
            lineX.anchoredPosition = new Vector2(xPosition, 0);
            graphGameObjectList.Add(lineX.gameObject);

            xIndex++;
        }

        for (int i = 0; i <= seperatorCount; i++)
        {
            // Create price label for the Y axis.
            RectTransform labelY = Instantiate(labelTemplateY);
            labelY.SetParent(graphContainer.transform, false);
            labelY.gameObject.SetActive(true);
            float normalizedValue = i * 1f / seperatorCount;
            labelY.anchoredPosition = new Vector2(-3.6f, normalizedValue * graphHeight);
            labelY.GetComponent <TextMeshProUGUI>().text = currencyString + Mathf.RoundToInt(yMinimum + (normalizedValue * (yMaximum - yMinimum))).ToString(); // I wanna set the btc prices range, no need to round as ill need a float.
            graphGameObjectList.Add(labelY.gameObject);

            // Create Grid Line for the Y axis.
            RectTransform lineY = Instantiate(lineTemplateY);
            lineY.SetParent(graphContainer.transform, false);
            lineY.gameObject.SetActive(true);
            lineY.anchoredPosition = new Vector2(0, normalizedValue * graphHeight);
            graphGameObjectList.Add(lineY.gameObject);
        }
    }
예제 #20
0
    /// <summary>
    /// Shows the graph.
    /// </summary>
    /// <param name="valueList">Value list.</param>
    /// <param name="getAxisLabelX">Get axis label x.</param>
    /// <param name="getAxisLabelY">Get axis label y.</param>
    private void ShowGraph(List<int> valueList, IGraphVisual graphVisual, int maxVisibleValueAmount = -1, Func<int, string> getAxisLabelX = null, Func<float, string> getAxisLabelY = null)
    {
        //Set the default axis label to get
        if(getAxisLabelX == null)
        {
            getAxisLabelX = delegate (int _i) { return _i.ToString(); };
        }
        if (getAxisLabelY == null)
        {
            getAxisLabelY = delegate (float _f) { return Mathf.RoundToInt(_f).ToString(); };
        }

        if(maxVisibleValueAmount <= 0)
        {
            maxVisibleValueAmount = valueList.Count;
        }

        //Cycle through the gameobject list and destroy then clear the list objects
        foreach (GameObject gameObject in gameObjectList)
        {
            Destroy(gameObject);
        }
        gameObjectList.Clear();

        //The maximum height and width of the graph container game object in scene
        float graphHeight = graphContainer.sizeDelta.y;
        float graphWidth = graphContainer.sizeDelta.x;

        //The max and min of the graph calculated against all values given
        float yMaximum = 0.0f;
        float yMinimum = 0.0f;

        for (int i = Mathf.Max(valueList.Count - maxVisibleValueAmount, 0); i < valueList.Count; i++)
        {
            int value = valueList[i];
            if(value > yMaximum)
            {
                yMaximum = value;
            }
            if(value < yMinimum)
            {
                yMinimum = value;
            }
        }

        //For error fixing if a single value is 0
        float yDifference = yMaximum - yMinimum;
        if(yDifference <= 0)
        {
            yDifference = 5.0f;
        }

        //Set the maximum and minimum to 20% difference betwen the max and min given (this is the buffer zone)
        yMaximum = yMaximum + (yDifference * 0.2f);
        yMinimum = yMinimum - (yDifference * 0.2f);

        //Distance between each point on the x-axis to scale to the number of points given to graph
        float xSize = graphWidth / (maxVisibleValueAmount + 1.0f);

        int xIndex = 0;

        //Go through each point to be graphed and create it, then connect it to the last point if possible
        for (int i = Mathf.Max(valueList.Count - maxVisibleValueAmount, 0); i < valueList.Count; i++)
        {
            float xPos = xSize + xIndex * xSize;
            float yPos = ((valueList[i] - yMinimum) / (yMaximum - yMinimum)) * graphHeight;
            Color dotColor;

            //Create a new bar object for the data at the correct position with the size (multiply to space between bars)
            //gameObjectList.AddRange(barChartVisual.AddGraphVisual(new Vector2(xPos, yPos), xSize));
            gameObjectList.AddRange(graphVisual.AddGraphVisual(new Vector2(xPos, yPos), xSize));

            //Set the dot color based on if the value is a positive or negative number
            if (valueList[i] < 0)
            {
                dotColor = Color.red;
                //gameObjectList[i].GetComponent<Image>().color = Color.red;
            }
            else
            {
                dotColor = Color.green;
                //gameObjectList[i].GetComponent<Image>().color = Color.green;

            }

            //Create a new instance of a label on the x axis,
            //Set the parent to the graph container
            //Activate it, and adjust spacing
            //Set the string name to represent the value
            RectTransform labelX = Instantiate(labelTemplateX);
            labelX.SetParent(graphContainer, false);
            labelX.gameObject.SetActive(true);
            labelX.anchoredPosition = new Vector2(xPos, -7.0f);
            labelX.GetComponent<Text>().text = getAxisLabelX(i);
            gameObjectList.Add(labelX.gameObject);

            //Set the grid on the X axis the same as setting the label
            RectTransform gridX = Instantiate(gridTemplateX);
            gridX.SetParent(graphContainer, false);
            gridX.gameObject.SetActive(true);
            gridX.anchoredPosition = new Vector2(xPos, -3.0f);
            gameObjectList.Add(gridX.gameObject);

            //Increase the index for the x values
            xIndex++;
        }

        //For each value on the Y create a new instance of the label,
        //Set the parent
        //Set it to be active
        int seperatorCount = 10;
        for(int i = 0; i <= seperatorCount; i++)
        {
            RectTransform labelY = Instantiate(labelTemplateY);
            labelY.SetParent(graphContainer, false);
            labelY.gameObject.SetActive(true);
            //For the Y values normalize the value using the current count
            //And anchor the position times the graph height otherwise all labels will stack ontop of eachother
            float normalizedYValue = i * 1.0f / seperatorCount;
            labelY.anchoredPosition = new Vector2(-7.0f, normalizedYValue * graphHeight);
            //Show the value in text as a normalized value using the minimum and maximum
            labelY.GetComponent<Text>().text = getAxisLabelY(yMinimum + (normalizedYValue * (yMaximum - yMinimum)));
            gameObjectList.Add(labelY.gameObject);

            //Set the grid on the Y axis the same as setting the label
            RectTransform gridY = Instantiate(gridTemplateY);
            gridY.SetParent(graphContainer, false);
            gridY.gameObject.SetActive(true);
            gridY.anchoredPosition = new Vector2(-4.0f, normalizedYValue * graphHeight);
            gameObjectList.Add(gridY.gameObject);
        }
    }
 private void SetGraphVisual(IGraphVisual graphVisual)
 {
     ShowGraph(this.valueList, graphVisual, this.maxVisibleValueAmount, this.getAxisLabelX, this.getAxisLabelY);
 }
예제 #22
0
    private void ShowGraph(List <int> valueList, IGraphVisual graphVisual, int maxVisibleValueAmount = -1, Func <int, string> getAxisLabelX = null, Func <float, string> getAxisLabelY = null)
    {
        this.valueList     = valueList;
        this.graphVisual   = graphVisual;
        this.getAxisLabelX = getAxisLabelX;
        this.getAxisLabelY = getAxisLabelY;

        if (maxVisibleValueAmount <= 0)
        {
            // Show all if no amount specified
            maxVisibleValueAmount = valueList.Count;
        }
        if (maxVisibleValueAmount >= 30)
        {
            // Show all if no amount specified
            maxVisibleValueAmount = 30;
        }

        this.maxVisibleValueAmount = maxVisibleValueAmount;

        // Test for label defaults
        if (getAxisLabelX == null)
        {
            getAxisLabelX = delegate(int _i) { return(_i.ToString()); };
        }
        if (getAxisLabelY == null)
        {
            getAxisLabelY = delegate(float _f) { return(Mathf.RoundToInt(_f).ToString()); };
        }

        // Clean up previous graph
        foreach (GameObject gameObject in gameObjectList)
        {
            Destroy(gameObject);
        }
        gameObjectList.Clear();
        yLabelList.Clear();

        foreach (IGraphVisualObject graphVisualObject in graphVisualObjectList)
        {
            graphVisualObject.CleanUp();
        }
        graphVisualObjectList.Clear();

        graphVisual.CleanUp();

        // Grab the width and height from the container
        float graphWidth  = graphContainer.sizeDelta.x;
        float graphHeight = graphContainer.sizeDelta.y;

        float yMinimum, yMaximum;

        CalculateYScale(out yMinimum, out yMaximum);

        // Set the distance between each point on the graph
        xSize = graphWidth / (maxVisibleValueAmount + 1);

        // Cycle through all visible data points
        int xIndex = 0;

        for (int i = Mathf.Max(valueList.Count - maxVisibleValueAmount, 0); i < valueList.Count; i++)
        {
            float xPosition = xSize + xIndex * xSize;
            float yPosition = ((valueList[i] - yMinimum) / (yMaximum - yMinimum)) * graphHeight;

            // Add data point visual

            IGraphVisualObject graphVisualObject = graphVisual.CreateGraphVisualObject(new Vector2(xPosition, yPosition), xSize);
            graphVisualObjectList.Add(graphVisualObject);


            xIndex++;
        }

        // Set up separators on the y axis
        int separatorCount = 5;

        for (int i = 0; i <= separatorCount; i++)
        {
            // Duplicate the label template
            RectTransform labelY = Instantiate(labelTemplateY);
            labelY.SetParent(graphContainer, false);
            labelY.gameObject.SetActive(true);
            float normalizedValue = i * 1f / separatorCount;
            labelY.anchoredPosition           = new Vector2(-7f, normalizedValue * graphHeight);
            labelY.GetComponent <Text>().text = getAxisLabelY(yMinimum + (normalizedValue * (yMaximum - yMinimum)));
            yLabelList.Add(labelY);
            gameObjectList.Add(labelY.gameObject);
        }
    }
예제 #23
0
    private void ShowGraph(List <int> valueList, IGraphVisual graphVisual, int maxVisibleValueAmount, Func <float, string> getAxisLabelY = null)
    {
        this.valueList     = valueList;
        this.graphVisual   = graphVisual;
        this.getAxisLabelY = getAxisLabelY;

        if (graphVisual == null || valueList == null)
        {
            return;
        }

        graphVisual.Reset();

        if (maxVisibleValueAmount <= 0)
        {
            // Show all if no amount specified
            maxVisibleValueAmount = valueList.Count;
        }
        else if (maxVisibleValueAmount > valueList.Count)
        {
            // Validate the amount to show the maximum
            maxVisibleValueAmount = valueList.Count;
        }
        this.maxVisibleValueAmount = maxVisibleValueAmount;

        //assign default function if not already assigned
        if (getAxisLabelY == null)
        {
            getAxisLabelY = delegate(float _f) { return(Mathf.RoundToInt(_f).ToString()); };
        }

        //Destorys previous graph
        foreach (GameObject o in gameObjectList)
        {
            Destroy(o);
        }
        gameObjectList.Clear();


        //defines graph position and size from container
        float graphWidth  = graphContainer.sizeDelta.x;
        float graphHeight = graphContainer.sizeDelta.y;

        //Itendify y Min and Max Values
        float yMaximum = valueList[0];
        float yMinimum = valueList[0];

        for (int i = Mathf.Max(valueList.Count - maxVisibleValueAmount, 0); i < valueList.Count; i++)
        {
            int value = valueList[i];
            if (value > yMaximum)
            {
                yMaximum = value;
            }
            if (value < yMinimum)
            {
                yMinimum = value;
            }
        }


        float yDifference = yMaximum - yMinimum;

        if (yDifference <= 0)
        {
            yDifference = 0f; //makes sure the minimum y value can't go below 0
        }

        yMaximum = yMaximum + (yDifference * 0.2f);   // makes highest value not hug top of graph
        yMaximum = yMaximum + (10 - (yMaximum % 10)); // makes the number divisible by 10
        yMinimum = yMinimum - (yDifference * 0.2f);

        //set max plotted points to max of graph
        float xSize = graphWidth / (maxVisibleValueAmount + 1);

        int xIndex = 0;

        for (int i = Mathf.Max(valueList.Count - maxVisibleValueAmount, 0); i < valueList.Count; i++)
        {
            float xPosition = xSize + xIndex * xSize;
            float yPosition = ((valueList[i] - yMinimum) / (yMaximum - yMinimum)) * graphHeight;

            string tooltipText = GetXAxisString(i) + " " + getAxisLabelY(valueList[i]);
            gameObjectList.AddRange(graphVisual.AddGraphVisual(new Vector2(xPosition, yPosition), xSize, tooltipText, i));

            //spawn X axis labels makes sure it doesn't overlap labels by making it only display 13 lables
            int modCheck = Mathf.CeilToInt(maxVisibleValueAmount / 13f);
            if (i % modCheck == 0)
            {
                RectTransform labelX = Instantiate(labelTemplateX);
                labelX.SetParent(graphContainer);
                labelX.gameObject.SetActive(true);
                labelX.anchoredPosition           = new Vector2(xPosition, -25f);
                labelX.GetComponent <Text>().text = GetXAxisString(i);
                gameObjectList.Add(labelX.gameObject);


                //spawn x axis dashes
                RectTransform dashX = Instantiate(dashTemplateX);
                dashX.SetParent(graphContainer);
                dashX.SetSiblingIndex(1);
                dashX.gameObject.SetActive(true);
                dashX.anchoredPosition = new Vector2(xPosition, 0f);
                gameObjectList.Add(dashX.gameObject);
            }



            xIndex++;
        }

        //calculate where to put y values and spawn the axis lables and dashes
        int seperatorCount = 10;

        for (int i = 0; i <= seperatorCount; i++)
        {
            //spawns y axis labels
            RectTransform labelY = Instantiate(labelTemplateY);
            labelY.SetParent(graphContainer, false);
            labelY.gameObject.SetActive(true);
            float normalizedValue = i * 1f / seperatorCount;
            labelY.anchoredPosition           = new Vector2(-30f, normalizedValue * graphHeight);
            labelY.GetComponent <Text>().text = getAxisLabelY(yMinimum + (normalizedValue * (yMaximum - yMinimum)));
            gameObjectList.Add(labelY.gameObject);


            //spawns y axis dashes
            RectTransform dashY = Instantiate(dashTemplateY);
            dashY.SetParent(graphContainer, false);
            dashY.SetSiblingIndex(1);
            dashY.gameObject.SetActive(true);
            dashY.anchoredPosition = new Vector2(0f, normalizedValue * graphHeight);
            gameObjectList.Add(dashY.gameObject);
        }
    }
예제 #24
0
    // Function that manages the graph generation procedure (receiving a list of coordinates for our points.
    public void ShowGraph(List <int> valueList, IGraphVisual graphVisual, int maxVisibleValueAmount = -1, Func <int, string> getAxisLabelX = null, Func <float, string> getAxisLabelY = null)
    {
        // Replace local reference to graphVisual by our global variable when the input is null.
        if (graphVisual == null)
        {
            graphVisual = this.GraphVisual;
        }

        // Update member variables.
        this.valueList     = valueList;
        this.GraphVisual   = graphVisual;
        this.getAxisLabelX = getAxisLabelX;
        this.getAxisLabelY = getAxisLabelY;

        // Deal with null optional inputs.
        if (getAxisLabelX == null)
        {
            getAxisLabelX = delegate(int _i) { return(_i.ToString()); };
        }
        if (getAxisLabelY == null)
        {
            getAxisLabelY = delegate(float _f) { return(Mathf.RoundToInt(_f).ToString()); };
        }

        // Check if maxVisibleValueAmount is valid.
        if (maxVisibleValueAmount <= 0)
        {
            maxVisibleValueAmount = valueList.Count;
        }
        this.maxVisibleValueAmount = maxVisibleValueAmount;

        // Destroy last objects in the plotting zone.
        foreach (GameObject gameObject in GameObjectList)
        {
            Destroy(gameObject);
        }
        GameObjectList.Clear();
        yLabelList.Clear();

        // Definition of variables.
        float graphHeight = GraphContainer.sizeDelta.y;
        float graphWidth  = GraphContainer.sizeDelta.x;

        float yMinimum, yMaximum;

        CalculateYScale(out yMinimum, out yMaximum);
        xSize = graphWidth / (maxVisibleValueAmount + 1); // Distance between consecutive points on xAxis of our graph (resolution).

        // Iterate along the list (for accessing each point coordinates).
        int xIndex = 0;

        for (int i = Mathf.Max(valueList.Count - maxVisibleValueAmount, 0); i < valueList.Count; i++)
        {
            // Conversion of sequence number to a x coordinate accordingly to our graph properties.
            float xPosition = xIndex * xSize;

            // Conversion of value inside value list to a y coordinate accordingly to our graph properties.
            float yPosition = ((valueList[i] - yMinimum) / (yMaximum - yMinimum)) * graphHeight; // Normalization to our graph maximum height.

            // Create Line Graph Visual Object.
            GraphVisualObjectList.Add(graphVisual.CreateGraphVisualObject(new Vector2(xPosition, yPosition), xSize));

            // Create x axis separators.
            RectTransform labelX = Instantiate(LabelTemplateX);
            labelX.SetParent(GraphContainer, false);
            labelX.gameObject.SetActive(true);                                 // Make template label visible.
            labelX.anchoredPosition           = new Vector2(xPosition, -0.5f); // Label position.
            labelX.GetComponent <Text>().text = getAxisLabelX(i);              // Define the text of the label.
            GameObjectList.Add(labelX.gameObject);

            // Create x axis dashes.
            RectTransform dashX = Instantiate(DashTemplateX);
            dashX.SetParent(GraphContainer, false);
            dashX.gameObject.SetActive(true);                       // Make template label visible.
            dashX.anchoredPosition = new Vector2(xPosition, -0.5f); // Label position.
            GameObjectList.Add(dashX.gameObject);

            // Update counter.
            xIndex++;
        }

        // Create y axis separators.
        int separatorCount = 10;

        for (int i = 0; i <= separatorCount; i++)
        {
            RectTransform labelY = Instantiate(LabelTemplateY);
            labelY.SetParent(GraphContainer, false);
            labelY.gameObject.SetActive(true);                                                                       // Make template label visible.
            float normalizedValue = i * 1f / separatorCount;
            labelY.anchoredPosition           = new Vector2(-0.5f, normalizedValue * graphHeight);                   // Label position.
            labelY.GetComponent <Text>().text = getAxisLabelY(yMinimum + (normalizedValue * (yMaximum - yMinimum))); // Define the text of the label.
            yLabelList.Add(labelY);
            GameObjectList.Add(labelY.gameObject);

            // Create x axis dashes.
            RectTransform dashY = Instantiate(DashTemplateY);
            dashY.SetParent(GraphContainer, false);
            dashY.gameObject.SetActive(true);                                           // Make template label visible.
            dashY.anchoredPosition = new Vector2(-0.5f, normalizedValue * graphHeight); // Label position.
            GameObjectList.Add(dashY.gameObject);
        }
    }
 // Switch between Graph Visuals With Same Data To draw graph.
 private void SetGraphVisual(IGraphVisual graphVisual)
 {
     ShowGraph(lastUsedValueList, graphVisual, lastUsedMaxVisibleAmount, lastUsedSeperatorCount);
 }
    public void ShowGraph(IList valueList, IGraphVisual graphVisual = null, int maxVisibleValueAmount = -1, Func <int, string> getAxisLabelX = null, Func <float, string> getAxisLabelY = null)
    {
        Setup();
        if (valueList == null)
        {
            valueList = new List <object> {
                0
            };
            //Debug.LogError("valueList is null!");
            //return;
        }
        this.valueList = valueList;

        if (graphVisual == null)
        {
            graphVisual = barChartVisual;
        }
        this.graphVisual = graphVisual;

        if (maxVisibleValueAmount <= 0)
        {
            // Show all if no amount specified
            maxVisibleValueAmount = valueList.Count;
        }
        if (maxVisibleValueAmount > valueList.Count)
        {
            // Validate the amount to show the maximum
            maxVisibleValueAmount = valueList.Count;
        }

        this.maxVisibleValueAmount = maxVisibleValueAmount;

        // Test for label defaults
        if (getAxisLabelX == null)
        {
            if (this.getAxisLabelX != null)
            {
                getAxisLabelX = this.getAxisLabelX;
            }
            else
            {
                getAxisLabelX = delegate(int _i) { return(_i.ToString()); };
            }
        }
        if (getAxisLabelY == null)
        {
            if (this.getAxisLabelY != null)
            {
                getAxisLabelY = this.getAxisLabelY;
            }
            else
            {
                getAxisLabelY = delegate(float _f) { return(Mathf.RoundToInt(_f).ToString()); };
            }
        }

        this.getAxisLabelX = getAxisLabelX;
        this.getAxisLabelY = getAxisLabelY;

        // Clean up previous graph
        foreach (GameObject gameObject in gameObjectList)
        {
            Destroy(gameObject);
        }
        gameObjectList.Clear();
        yLabelList.Clear();

        foreach (IGraphVisualObject graphVisualObject in graphVisualObjectList)
        {
            graphVisualObject.CleanUp();
        }
        graphVisualObjectList.Clear();

        graphVisual.CleanUp();

        // Grab the width and height from the container
        float graphWidth  = graphContainer.sizeDelta.x;
        float graphHeight = graphContainer.sizeDelta.y;

        float yMinimum, yMaximum;

        CalculateYScale(out yMinimum, out yMaximum);

        // Set the distance between each point on the graph
        xSize = graphWidth / (maxVisibleValueAmount + 1);

        // Cycle through all visible data points
        int xIndex = 0;

        for (int i = Mathf.Max(valueList.Count - maxVisibleValueAmount, 0); i < valueList.Count; i++)
        {
            float xPosition = xSize + xIndex * xSize;
            float yPosition = ((GetValue(valueList[i]) - yMinimum) / (yMaximum - yMinimum)) * graphHeight;

            // Add data point visual
            string tooltipText;
            if (getTooltipLabel != null)
            {
                tooltipText = getTooltipLabel(GetValue(valueList[i]));
            }
            else
            {
                tooltipText = getAxisLabelY(GetValue(valueList[i]));
            }
            IGraphVisualObject graphVisualObject = graphVisual.CreateGraphVisualObject(new Vector2(xPosition, yPosition), xSize, tooltipText);
            graphVisualObjectList.Add(graphVisualObject);

            // Duplicate the x label template
            RectTransform labelX = Instantiate(labelTemplateX);
            labelX.SetParent(graphContainer, false);
            labelX.gameObject.SetActive(true);
            labelX.anchoredPosition           = new Vector2(xPosition, -7f);
            labelX.GetComponent <Text>().text = getAxisLabelX(i);
            gameObjectList.Add(labelX.gameObject);

            // Duplicate the x dash template
            RectTransform dashX = Instantiate(dashTemplateX);
            dashX.SetParent(dashContainer, false);
            dashX.gameObject.SetActive(true);
            dashX.anchoredPosition = new Vector2(xPosition, -3f);
            dashX.sizeDelta        = new Vector2(graphHeight, dashX.sizeDelta.y);
            gameObjectList.Add(dashX.gameObject);

            xIndex++;
        }

        // Set up separators on the y axis
        int separatorCount = 10;

        for (int i = 0; i <= separatorCount; i++)
        {
            // Duplicate the label template
            RectTransform labelY = Instantiate(labelTemplateY);
            labelY.SetParent(graphContainer, false);
            labelY.gameObject.SetActive(true);
            float normalizedValue = i * 1f / separatorCount;
            labelY.anchoredPosition           = new Vector2(-7f, normalizedValue * graphHeight);
            labelY.GetComponent <Text>().text = getAxisLabelY(yMinimum + (normalizedValue * (yMaximum - yMinimum)));
            yLabelList.Add(labelY);
            gameObjectList.Add(labelY.gameObject);

            // Duplicate the dash template
            RectTransform dashY = Instantiate(dashTemplateY);
            dashY.SetParent(dashContainer, false);
            dashY.gameObject.SetActive(true);
            dashY.anchoredPosition = new Vector2(-4f, normalizedValue * graphHeight);
            dashY.sizeDelta        = new Vector2(graphWidth, dashY.sizeDelta.y);
            gameObjectList.Add(dashY.gameObject);
        }
    }
    //public static void ShowTooltip_Static(string tooltipText, Vector2 anchoredPosition)
    //{
    //    instance.ShowTooltip(tooltipText, anchoredPosition);
    //}
    //private void ShowTooltip(string tooltipText, Vector2 anchoredPosition)
    //{
    //    tooltipGameObject.SetActive(true);
    //    tooltipGameObject.GetComponent<RectTransform>().anchoredPosition = anchoredPosition;
    //    TextMeshProUGUI tooltipUIText = tooltipGameObject.transform.Find("Text").GetComponent<TextMeshProUGUI>();
    //    tooltipUIText.text = tooltipText;

    //    float textPaddingSize = 4f;
    //    Vector2 backgroundSize = new Vector2(
    //        tooltipUIText.preferredWidth + textPaddingSize * 2f,
    //        tooltipUIText.preferredHeight + textPaddingSize * 2f);

    //    tooltipGameObject.transform.Find("Background").GetComponent<RectTransform>().sizeDelta = backgroundSize;
    //    tooltipGameObject.transform.SetAsLastSibling();
    //}
    //public static void HideTooltip_Static()
    //{
    //    instance.HideTooltip();
    //}
    //private void HideTooltip()
    //{
    //    tooltipGameObject.SetActive(false);
    //}
    private void ShowGraph(List <float> valueList, IGraphVisual graphVisual, RectTransform performancePanel, List <IGraphVisualObject> graphVisualObjectList, List <GameObject> gameObjectList, int startX, int endX, Func <int, string> getAxisLabelX = null, Func <float, string> getAxisLabelY = null)
    {
        this.graphVisual   = graphVisual;
        this.startX        = startX;
        this.endX          = endX;
        this.getAxisLabelX = getAxisLabelX;
        this.getAxisLabelY = getAxisLabelY;

        if (getAxisLabelX == null)
        {
            getAxisLabelX = delegate(int _i) { return(_i.ToString()); };
        }
        if (getAxisLabelY == null)
        {
            getAxisLabelY = delegate(float _f) { return(Mathf.RoundToInt(_f).ToString()); };
        }
        if (endX <= 0)
        {
            endX = valueList.Count;
        }
        foreach (GameObject gameObject in gameObjectList)
        {
            Destroy(gameObject);
        }
        gameObjectList.Clear();
        yLabelList.Clear();
        foreach (IGraphVisualObject graphVisualObject in graphVisualObjectList)
        {
            graphVisualObject.CleanUp();
        }
        graphVisualObjectList.Clear();
        graphVisual.CleanUp();

        float graphWidth  = redPerformance.sizeDelta.x;
        float graphHeight = redPerformance.sizeDelta.y;
        float yMax        = valueList[0];
        float yMin        = valueList[0];

        for (int i = Mathf.Max(startX, 0); i < endX; i++)
        {
            float value = valueList[i];
            if (value > yMax)
            {
                yMax = value;
            }
            if (value < yMin)
            {
                yMin = value;
            }
        }
        float yDifference = yMax - yMin;

        if (yDifference <= 0)
        {
            yDifference = 5f;
        }
        yMax = yMax + (yDifference * 0.2f);
        yMin = yMin - (yDifference * 0.2f);

        if (startYScaleAtZero)
        {
            yMin = 0f; // Start the graph at zero
        }
        xSize = graphWidth / ((endX - startX) + 1);
        int index = 0;
        int gap   = 0;

        if (ScaleXLabel(valueList) != 0)
        {
            gap = valueList.Count / ScaleXLabel(valueList);
        }
        for (int i = Mathf.Max(startX, 0); i < endX; i++)
        {
            float xPosition = xSize + index * xSize;
            float yPosition = ((valueList[i] - yMin) / (yMax - yMin)) * graphHeight;

            string             tooltipText       = valueList[i].ToString();
            IGraphVisualObject graphVisualObject = graphVisual.CreateGraphVisualObject(new Vector2(xPosition, yPosition), xSize, tooltipText);

            graphVisualObjectList.Add(graphVisualObject);

            if (i % gap == 0)
            {
                RectTransform labelX = Instantiate(lableTemplateX);
                labelX.SetParent(performancePanel, false);
                labelX.gameObject.SetActive(true);
                labelX.anchoredPosition = new Vector2(xPosition, -20f);
                labelX.GetComponent <TextMeshProUGUI>().text = getAxisLabelX(i);
                gameObjectList.Add(labelX.gameObject);
            }
            index++;
        }
        int separatorCount = 5;

        for (int i = 0; i <= separatorCount; i++)
        {
            RectTransform labelY = Instantiate(lableTemplateY);
            labelY.SetParent(performancePanel, false);
            labelY.gameObject.SetActive(true);
            float normalizedValue = i * 1f / separatorCount;
            labelY.anchoredPosition = new Vector2(-20f, normalizedValue * graphHeight);
            labelY.GetComponent <TextMeshProUGUI>().text = getAxisLabelY(yMin + normalizedValue * (yMax - yMin));
            yLabelList.Add(labelY);
            gameObjectList.Add(labelY.gameObject);

            //    //RectTransform dashY = Instantiate(dashTemplateX);
            //    //dashY.SetParent(performance, false);
            //    //dashY.gameObject.SetActive(true);
            //    //dashY.anchoredPosition = new Vector2(-4f, normalizedValue * graphHeight);
            //    //gameObjectList.Add(dashY.gameObject);
        }
    }