Chart.js Blazor服务器端Chartjs

Chart.js Blazor服务器端Chartjs,chart.js,blazor-server-side,Chart.js,Blazor Server Side,我正在使用blazor服务器端并尝试使用blazor.Chartjs显示图表。我的问题是,我有一个表单,用户输入日期,应用程序将从数据库中获取所选日期的数据。第一次将显示图表,但当用户更改日期时,我希望使用新数据更改图表,但出现以下错误: Unhandled exception rendering component: Could not find a chart with the given id. c4e7005b-e203-46f1-9719-68c6d14d848f Microsoft.

我正在使用blazor服务器端并尝试使用blazor.Chartjs显示图表。我的问题是,我有一个表单,用户输入日期,应用程序将从数据库中获取所选日期的数据。第一次将显示图表,但当用户更改日期时,我希望使用新数据更改图表,但出现以下错误:

Unhandled exception rendering component: Could not find a chart with the given id. c4e7005b-e203-46f1-9719-68c6d14d848f
Microsoft.JSInterop.JSException: Could not find a chart with the given id. c4e7005b-e203-46f1-9719-68c6d14d848f
   at Microsoft.JSInterop.JSRuntime.InvokeWithDefaultCancellation[T](String identifier, Object[] args)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)
fail: Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost[111]
      Unhandled exception in circuit '268yoqwqVBLFanukxnq3R4LjrnKrEAgIdVkQSPnIgfY'.
Microsoft.JSInterop.JSException: Could not find a chart with the given id. c4e7005b-e203-46f1-9719-68c6d14d848f
   at Microsoft.JSInterop.JSRuntime.InvokeWithDefaultCancellation[T](String identifier, Object[] args)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)

很长一段时间我都有同样的问题。我最终发现我需要清除数据集并将新数据添加回新数据集,然后更新图表

我正在用_lineConfig.data.Datasets.Clear()清除数据集

然后创建一个新版本,如下所示

TempProductionLineTotal = SqlProdData.GetProductionTotals(searchDate[0], searchDate[1]);

_productionDataSet = new LineDataset<TimeTuple<int>>   
{
BackgroundColor = ColorUtil.FromDrawingColor(System.Drawing.Color.Orange),
BorderColor = ColorUtil.FromDrawingColor(System.Drawing.Color.Orange),
Label = "Daily Total",
Fill = false,
BorderWidth = 2,
PointRadius = 1,
PointBorderWidth = 2,
SteppedLine = SteppedLine.False,
Hidden = false,
LineTension = 0.0   };

_productionDataSet.AddRange(TempProductionLineTotal

.Select(p => new TimeTuple<int>(new Moment(p.DateNTime),Convert.ToInt32(p.Daily_Lineal))));

_lineConfig.Data.Datasets.Add(_productionDataSet);
TempProductionLineTotal=SqlProdData.GetProductionTotals(searchDate[0],searchDate[1]);
_productionDataSet=新建LineDataset
{
BackgroundColor=ColorUtil.FromDrawingColor(System.Drawing.Color.Orange),
BorderColor=ColorUtil.FromDrawingColor(System.Drawing.Color.Orange),
Label=“每日总计”,
填充=假,
边框宽度=2,
点半径=1,
PointBorderWidth=2,
SteppedLine=SteppedLine.False,
隐藏=错误,
线张力=0.0};
_productionDataSet.AddRange(TempProductionLineTotal
.Select(p=>newtimetuple(新时刻(p.DateNTime),Convert.ToInt32(p.Daily\u Lineal));
_lineConfig.Data.Datasets.Add(_productionDataSet);

您如何尝试更新图表的数据集?我最近在更新条形图时也遇到了一些困难。我尝试在BarConfig中更新数据集,但它是只读的,因此无法更新。然后,我尝试使用新的数据集为图表重新创建BarConfig对象。这导致我出现与您相同的错误,因为标识图表的Id似乎与BarConfig对象绑定。最后,我必须清除BarConfig中的数据集,创建一个新的BarDataset对象,添加更新的值,然后将其添加回原始BarConfig对象