IChartDataSource
A chart datasource is used to supply data to the charting components such as the ones in the Lisa.Modules.ChartJS module.
Interface declaration
public interface IChartDataSource { Task<IEnumerable<ChartDataItem>> GetChartData(IColorPalette colorPalette, Guid websiteId); }
Example
Here is an example of a IChartDataSource implementation, the "Static chart datasource". It allows users to manually specify data items to show on charts.
[Description("Allows you to manually specify static chart data to display")] [DisplayName("Static chart datasource")] public class StaticChartDataSource : IChartDataSource { [EditorBrowsable] [Parameter] [DisplayName("Series")] [Description("The data shown on the chart")] [Category("Chart data")] [EditorArgument("recordName", "data")] public List<StaticChartDataItem> ChartData { get; set; } public async Task<IEnumerable<ChartDataItem>> GetChartData(IColorPalette colorPalette, Guid websiteId) { if (ChartData != null) { List<ChartDataItem> result = new List<ChartDataItem>(); var idx = 0; foreach (var item in ChartData) { result.Add(new ChartDataItem() { Value = item.Value, Label = item.Legend, Color = (item.Color != null) ? item.Color?.ToHtmlColor() : GetColor(colorPalette, idx, websiteId) }); idx++; } return result; } return null; } public string GetColor(IColorPalette colorPalette, int idx, Guid websiteId) { if (colorPalette != null) { var colors = colorPalette.Colors(websiteId)?.ToList(); if (colors != null && colors.Any()) { return colors[Math.Min(idx, colors.Count() - 1)]; } } return ""; } }
Registration
This is how the implementation is registered into the DI container in Veva, this should be done in your module config file (The class which implements IModule).
public void ConfigureServices(IServiceCollection services, IConfiguration configuration) { services.AddNamed<IChartDataSource, StaticChartDataSource>("StaticChartDataSource"); }