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

Page crashes after some time #180

Open
MarvinKlein1508 opened this issue Mar 16, 2021 · 1 comment · May be fixed by #192
Open

Page crashes after some time #180

MarvinKlein1508 opened this issue Mar 16, 2021 · 1 comment · May be fixed by #192
Labels
bug Something isn't working

Comments

@MarvinKlein1508
Copy link

Describe the bug

We have implemented ChartJs within a blazor page. When we go to the page everything works fine. Now if we leave the tab open for x minutes/hours, the website crashes and we get an error message.

I don't got this behaviour in Firefox yet, only in Chrome.

Which Blazor project type is your bug related to?

  • Server-Side

Which charts does this bug apply to?

<Chart Config="_chartConfig" @ref="_chart" />

To Reproduce

Steps to reproduce the behavior:
We are using ChartJs v 2.0.2.
We implemented the chart in this way:
<Chart Config="_chartConfig" @ref="_chart" />

InitChartData() is called in OnParametersSetAsync()

Now keep the page open. It can take up to a few few minutes or hours until the page crashes.

Additional context / logging

Add any additional context about the problem here.

Category: Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost
EventId: 111
SpanId: 155a6ee05355f14b
TraceId: 7ba32bda0dc0df4e8fc029139ced18f9
ParentId: 0000000000000000
RequestId: 800001cb-000a-f800-b63f-84710c7967bb
RequestPath: /_blazor
TransportConnectionId: MIPBcXk0NMVBF9fEKt-Z5w

Unhandled exception in circuit 'WPZDgZ09abqTNY91AXwwP7xZwcfpPAlEnyN229qjEuw'.

Exception: 
Microsoft.JSInterop.JSException: Could not find a chart with the given id. 568bdcd0-6079-42a7-86bf-c5e66f502a2a
   at Microsoft.JSInterop.JSRuntime.InvokeAsync[TValue](Int64 targetInstanceId, String identifier, Object[] args)
   at ChartJs.Blazor.Chart.OnAfterRenderAsync(Boolean firstRender)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)

Code example

Please provide full code examples below where possible to make it easier for the developers to check your issues.
Please also add the working JavaScript equivalent so we can see what you were trying to achieve.

protected override async Task OnParametersSetAsync()
{
	if (CustomerId > 0)
	{
		_customer = await Kunde.GetKundeAsync(Kundennummer.ToString());
		if (_customer is not null)
		{
			/* More Stuff is happening here */

			Umsatzdaten = new Dictionary<int, Jahresumsatz>();
			for (int i = 0; i < UmsatzJahre; i++)
			{
				if(!Umsatzdaten.ContainsKey(i))
				{
					Umsatzdaten.Add(i, await customerService.GetCustomerDataAsync(_customer.KUND_A_NR, DateTime.Now.Year - i));
				}
			}

			InitChartData();
		}
		else
		{
			/* More Stuff is happening here */
		}
	}
}

private void InitChartData()
{

	_chartConfig = new LineConfig
	{
		Options = new LineOptions
		{
			Responsive = true,
			Title = new OptionsTitle
			{
				Display = true,
				Text = $"Umsatz der letzten {UmsatzJahre} Jahre"


			}
		}
	};

	_chartConfig.Data.Labels.Clear();
	_chartConfig.Data.Datasets.Clear();

	for (int i = 1; i <= 12; i++)
	{
		_chartConfig.Data.Labels.Add(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(i));
	}

	for (int i = 0; i < UmsatzJahre; i++)
	{
		string lbl = (DateTime.Now.Year - i).ToString();
		if (Umsatzdaten.ContainsKey(i))
		{
			IDataset<decimal> tmpDs = new LineDataset<decimal>(Umsatzdaten[i].ToIEnumerable())
			{
				Label = lbl,
				BackgroundColor = ColorUtil.FromDrawingColor(ChartHelper.GetColor(i)),
				BorderColor = ColorUtil.FromDrawingColor(ChartHelper.GetColor(i)),
				Fill = FillingMode.Disabled,
			};
			_chartConfig.Data.Datasets.Add(tmpDs);
		}
	}
}
@MarvinKlein1508 MarvinKlein1508 added the bug Something isn't working label Mar 16, 2021
@pgrimstrup
Copy link

pgrimstrup commented May 27, 2021

I have the same issue on a page that refreshes the displayed data every 10 seconds.

I just had a quick look at the base chart class and it appears to use an ID from the Config for the chart component. Since the Config is being recreated, a new ID is being generated. This means that Blazor cannot locate the original chart component to check for changes in the DOM.

Workaround: Do not recreate the Config instance within the InitChartData() method. Create an instance in the Page Initialize() method, and then update the properties, such as Options. You will need to keep the original Config instance so the ID doesn't change.

Possible Fix: Allow the ID to be specified in the Config constructor, or make the ID property read/write.

Note that this means that all sample code where this technique is being used will need to be changed as well.

Edit: Alternatively, just move the CanvasId into the Chart component. I have submitted a PR with this.

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
2 participants