@using System.Globalization
@using Syncfusion.Blazor.Charts
<SfChart @ref="Chartobj" Width="100%" ID="chart" >
<ChartEvents LegendClick="OnLegendClick"></ChartEvents>
<ChartPrimaryXAxis ValueType="Syncfusion.Blazor.Charts.ValueType.Double" CrossesAt="new DateTime(2000, 1, 1, 0, 0, 0)" >
</ChartPrimaryXAxis>
<ChartPrimaryYAxis Title="DurationOfIncidents" ValueType="Syncfusion.Blazor.Charts.ValueType.DateTime" LabelFormat="H:mm:ss"
RangePadding="ChartRangePadding.None" Minimum="new DateTime(2000, 1, 1, 0, 0, 0)" >
</ChartPrimaryYAxis>
<ChartTooltipSettings Enable="true"></ChartTooltipSettings>
<ChartSeriesCollection>
@foreach (var entry in chartData)
{
<ChartSeries Name="@entry.First().Name" DataSource="@entry" YName="@nameof(MyDataModel.YTimeSpan)" XName="@nameof(MyDataModel.XValue)"
Type="ChartSeriesType.Column" Visible="@entry.First().Visible">
<ChartSeriesAnimation Enable="false"> </ChartSeriesAnimation>
</ChartSeries>
}
</ChartSeriesCollection>
<ChartLegendSettings Visible="true" Padding="20"></ChartLegendSettings>
</SfChart>
@code {
public bool FullRefreshChart { get; set; } = false;
public SfChart Chartobj;
public string[] colors = new string[] { "Red", "Green", "Blue", "Yellow", "Orange", "Purple", "Black", "Aqua", "Lime", "Gray", "Cyan" };
private Random rnd = new Random();
public class MyDataModel
{
public string Name { get; set; }
public string LegendTooltip { get; set; }
public string Color { get; set; }
public bool Visible { get; set; }
public int XValue { get; set; }
public int YValue { get; set; }
public DateTime YTimeSpan => new DateTime(2000, 1, 1, 0, 0, 0, new System.Globalization.CultureInfo("de").Calendar).AddSeconds(YValue);
}
public List<List<MyDataModel>> chartData = new List<List<MyDataModel>>();
protected override void OnInitialized()
{
base.OnInitialized();
CultureInfo.CurrentCulture = new CultureInfo("de");
OnSearch();
}
protected override void OnAfterRender(bool firstRender)
{
base.OnAfterRender(firstRender);
if (FullRefreshChart)
{
FullRefreshChart = false;
StateHasChanged();
}
}
public void ClearData()
{
chartData = null;
}
public void OnSearch()
{
chartData = new List<List<MyDataModel>>();
AddChartSeries(15);
}
public List<MyDataModel> GetData(string name, string color)
{
List<MyDataModel> data = new List<MyDataModel>();
for (int i = 0; i < 15; i++)
{
data.Add(new MyDataModel() { Name = name, Color = color, Visible = true, XValue = i, YValue = rnd.Next(10, 6000) });
}
return data;
}
public void OnLegendClick(ILegendClickEventArgs args)
{
var dataEntry = chartData.Select(modules => modules.Where(module => module.Name.Equals(args.Series.Name, StringComparison.OrdinalIgnoreCase)).ToList()).ToList();
dataEntry.ForEach(modules => modules.ForEach(module => module.Visible = args.Series.Visible));
//dataEntry.Visible = args.Series.Visible;
}
public void AddChartSeries(int number = 1)
{
var seriesCount = chartData.Count;
for (int index = seriesCount; index < number + seriesCount; index++)
{
var serie = GetData($"{nameof(MyDataModel.XValue)}{index}", colors[rnd.Next(colors.Length - 1)]);
chartData.Add(serie);
}
FullRefreshChart = true;
StateHasChanged();
}
public void RemoveChartSeries(int number = 1)
{
for (int index = 0; index < number; index++)
{
chartData.RemoveAt(chartData.Count - 1);
}
FullRefreshChart = true;
StateHasChanged();
}
public void ClearChartSeries()
{
chartData.Clear();
FullRefreshChart = true;
StateHasChanged();
Chartobj.ClearSeries();
}
public void ChangeVisibleSeriesHalf()
{
int index = 0;
foreach (var serie in chartData)
{
serie.First().Visible = (index % 2 == 0);
index++;
}
//FullRefreshChart = true;
//StateHasChanged();
}
public void ChangeVisibleSeriesFull()
{
int index = 0;
foreach (var serie in chartData)
{
serie.First().Visible = true;
index++;
}
//FullRefreshChart = true;
//StateHasChanged();
}
public void ChangeVisibleSeriesNone()
{
int index = 0;
foreach (var serie in chartData)
{
serie.First().Visible = false;
index++;
}
//FullRefreshChart = true;
//StateHasChanged();
}
}
The import thinks I have mark as bold. Parts of the code are not neccessary.
Kind regards
Pascal