mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-08-11 19:52:10 +00:00
added a component that renders the parsed JSON chart into an MudBlazor Chart
This commit is contained in:
parent
6f79c3802b
commit
d975b31b75
44
app/MindWork AI Studio/Chat/ChartBlock.razor
Normal file
44
app/MindWork AI Studio/Chat/ChartBlock.razor
Normal 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>
|
||||||
|
}
|
||||||
26
app/MindWork AI Studio/Chat/ChartBlock.razor.cs
Normal file
26
app/MindWork AI Studio/Chat/ChartBlock.razor.cs
Normal 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() ?? [];
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user