added a component that renders the parsed JSON chart into an MudBlazor Chart

This commit is contained in:
Nils Kruthoff 2026-08-04 14:52:22 +02:00
parent 6f79c3802b
commit d975b31b75
No known key found for this signature in database
GPG Key ID: A5C0151B4DDB172C
2 changed files with 70 additions and 0 deletions

View File

@ -0,0 +1,44 @@
@namespace AIStudio.Chat
@using MudBlazor
@inherits AIStudio.Components.MSGComponentBase
@if (this.Result.Chart is { } chart)
{
<MudPaper Class="pa-4 my-3" Outlined="false" Elevation="4">
<MudText Typo="Typo.h6" Class="mb-3">@chart.Title</MudText>
@if (chart.Type is ChartDefinitionType.PIE or ChartDefinitionType.DONUT)
{
<div class="d-flex justify-center">
<MudChart ChartType="@this.ChartType"
InputData="@chart.Series[0].Values.ToArray()"
InputLabels="@chart.Categories.ToArray()"
Width="50%"
Height="200px" />
</div>
}
else
{
<div class="d-flex justify-center">
<MudChart ChartType="@this.ChartType"
ChartSeries="@this.ChartSeries"
XAxisLabels="@chart.Categories.ToArray()"
AxisChartOptions="@this.AxisChartOptions"
Width="75%"
Height="350px" />
</div>
}
@if (chart.Caption is not null)
{
<MudText Typo="Typo.caption" Align="Align.Center" Class="d-block">
<em>@chart.Caption</em>
</MudText>
}
</MudPaper>
}
else
{
<MudAlert Severity="Severity.Warning" Variant="Variant.Outlined" Class="my-3">
@string.Format(T("This chart cannot be displayed: {0}"), this.Result.Error)
</MudAlert>
<pre class="overflow-auto"><code>@this.Result.RawJson</code></pre>
}

View File

@ -0,0 +1,26 @@
using AIStudio.Components;
using Microsoft.AspNetCore.Components;
namespace AIStudio.Chat;
public partial class ChartBlock : MSGComponentBase
{
private AxisChartOptions AxisChartOptions { get; } = new() { MatchBoundsToSize = true };
[Parameter]
public ChartBlockParseResult Result { get; set; } = ChartBlockParseResult.Invalid(string.Empty, string.Empty);
private ChartType ChartType => this.Result.Chart?.Type switch
{
ChartDefinitionType.BAR => ChartType.Bar,
ChartDefinitionType.STACKED_BAR => ChartType.StackedBar,
ChartDefinitionType.LINE => ChartType.Line,
ChartDefinitionType.PIE => ChartType.Pie,
ChartDefinitionType.DONUT => ChartType.Donut,
_ => ChartType.Bar,
};
private List<ChartSeries> ChartSeries => this.Result.Chart?.Series
.Select(series => new ChartSeries { Name = series.Name, Data = series.Values.ToArray() })
.ToList() ?? [];
}