Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LineChart.Init() resizes Rectransform after creating background #323

Closed
XanderV-2158642 opened this issue Jul 5, 2024 · 1 comment
Closed
Labels
bug Something isn't working

Comments

@XanderV-2158642
Copy link

XanderV-2158642 commented Jul 5, 2024

When creating a chart that is a child object of a canvas, the chart background fills the size of the but then resizes the to fit the chart. The Background does not get resized resulting in a chart like this:
BuggedBackground

This is the code I used when the bug occurs:

//create a simple chart
GameObject go = new GameObject("LineChart");
//Scale the chart like the canvas
go.transform.SetParent(canvas.transform);
RectTransform rtf = go.AddComponent<RectTransform>();
go.transform.localScale = new Vector3(1, 1, 1);

SetupChart(go, "LineChart");
AddData(go.GetComponent<LineChart>());

private void SetupChart(GameObject gameObject, string title )
{
    var chart = gameObject.GetComponent<LineChart>();
    if (chart == null)
    {
        chart = gameObject.AddComponent<LineChart>();
        chart.Init();
    }

    chart.EnsureChartComponent<Title>().show = true;
    chart.EnsureChartComponent<Title>().text = title;

    chart.EnsureChartComponent<Tooltip>().show = true;
    chart.EnsureChartComponent<Legend>().show = false;

    var xAxis = chart.EnsureChartComponent<XAxis>();
    var yAxis = chart.EnsureChartComponent<YAxis>();
    xAxis.show = true;
    yAxis.show = true;
    xAxis.type = Axis.AxisType.Time;
    yAxis.type = Axis.AxisType.Value;

    xAxis.splitNumber = 10;
    xAxis.boundaryGap = true;
}

private void AddData(LineChart chart)
{
    chart.RemoveData();
    chart.AddSerie<Line>();
    chart.AddSerie<Line>();
    for (int i = 0; i < 20; i++)
    {
        chart.AddXAxisData("x" + i);
        chart.AddData(0, Random.Range(10, 20));
        chart.AddData(1, Random.Range(10, 20));
    }
    chart.RefreshChart();
    chart.RefreshAllComponent();
}

This code comes from the examples folder here, but is divided in smaller functions for my purposes.

When adding rtf.sizeDelta = new Vector2(580, 300); to the first part:

//create a simple chart
GameObject go = new GameObject("LineChart");
//Scale the chart like the cnavas
go.transform.SetParent(canvas.transform);
RectTransform rtf = go.AddComponent<RectTransform>();
go.transform.localScale = new Vector3(1, 1, 1);
rtf.sizeDelta = new Vector2(580, 300);

The background is fully filled. Another workaround is disabling the background component.

Another thing I couldn't wrap my head around was that the background did fill the whole chart when changing a parameter of the chart (e.g. anchoring) at runtime in the unity scene editor. Doing this in scripts did not change the background.

Im using Unity editor v:2022.3.35f1
x-charts version: 3.11.1

@monitor1394
Copy link
Collaborator

Thank you for your feedback, this issue has been fixed in the master branch.

@monitor1394 monitor1394 added the bug Something isn't working label Jul 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants