Exemplo n.º 1
0
    /// <summary>
    /// Configures the histogram bars from the recieved API data
    /// </summary>
    private void FillGraph(GraphBars barData, RectTransform container, float value)
    {
        RectTransform histogram = (RectTransform)container.Find("Histogram");
        RectTransform line      = (RectTransform)container.Find("Line");

        // Placing the user score indicator
        float   horizontalPercentage = value / barData.max;
        Vector3 linePos = line.position;

        linePos.x    += histogram.sizeDelta.x * horizontalPercentage;
        line.position = linePos;

        // Determining bar heights
        float highestPercentage = 0;

        foreach (Bar bar in barData.bars)
        {
            // Using highest percentage instead of max value so bars aren't tiny
            highestPercentage = Math.Max(highestPercentage, bar.percentage);
        }

        for (int i = 0; i < histogram.childCount; i++)
        {
            RectTransform bg   = (RectTransform)histogram.GetChild(i);
            RectTransform fill = (RectTransform)bg.GetChild(0);

            Bar   data = barData.bars[i];
            float yPos = bg.sizeDelta.y * 0.8f * data.percentage / highestPercentage;
            fill.sizeDelta = new Vector2(fill.sizeDelta.x, yPos);

            // Tooltip text to show when the mouse hovers over the background bar
            string tooltipText;
            if (container.name == "Time")
            {
                tooltipText = string.Format("{0} - {1}", GameTimer.FormatTime(data.range[0]), GameTimer.FormatTime(data.range[1]));
            }
            else
            {
                tooltipText = string.Format("{0} - {1}", data.range[0], data.range[1]);
            }
            bg.GetComponent <BarTooltip>().Text = tooltipText;
        }
    }
Exemplo n.º 2
0
    /// <summary>
    /// Gets graph data for the specified field
    /// Passes the base graph container and stat value to #FillGraph
    /// </summary>
    private IEnumerator GetGraphData(string field, RectTransform container, float value)
    {
        UnityWebRequest request = UnityWebRequest.Get(_graphEndPoint + "/" + field);

        request.SetRequestHeader("Accept", "application/json");

        yield return(request.SendWebRequest());

        if (request.isNetworkError || request.isHttpError)
        {
            Debug.LogError("Error getting graph: " + request.error);
            yield break;
        }

        container.Find("Disconnected").gameObject.SetActive(false);

        GraphBars recieved = JsonUtility.FromJson <GraphBars>(request.downloadHandler.text);

        FillGraph(recieved, container, value);
    }