Chartjs-Converting ChartJSCore to Highcharts.net – issue with dynamic series data in a loop

0πŸ‘

I resolved the problem thusly, because the HighChart data expects its own formatted ColumnSeriesData – for whatever strange reason the devs decided to do this, you can’t pass in a generic list:

IDictionary<string, ColumnSeries> columnSeries = new Dictionary<string, ColumnSeries>();

            foreach (var seriesId in seriesIds)
            {
                var seriesName = _seriesRepository.GetSeriesName(seriesId);
                var seriesTotalsByLast6Months = _seriesRepository.GetSeriesTotalsByLast6Months(seriesId);
                List<ColumnSeriesData> columnSeriesData = new List<ColumnSeriesData>();
                seriesTotalsByLast6Months.ForEach(p => columnSeriesData.Add(new ColumnSeriesData { Y = p.Amount }));

                columnSeries[seriesId.ToString()] = new ColumnSeries
                {
                    Name = seriesName.First(),
                    Data = columnSeriesData
                };
            }

And for the loop to add the column series data to the chart (where chartOptions is the name of your HighChart):

foreach (var x in columnSeries.Values)
            {
                //add column series to the chart
                chartOptions.Series.Add(x);
            }

Leave a comment