예제 #1
0
        /// <summary>
        /// Get a sequence of datapoints corresponding to a specific category, ensuring there is a value for each of the categories.
        /// </summary>
        /// <param name="dataset"></param>
        /// <param name="categoryNames"></param>
        /// <returns></returns>
        private List <decimal> GetDataPointsForAllCategories(ChartJsCategoryValuesDataset dataset, List <string> categoryNames)
        {
            var dataValues = new List <decimal>();

            foreach (var categoryName in categoryNames)
            {
                var datapoint = dataset.DataPoints.FirstOrDefault(x => x.Category == categoryName);

                if (datapoint == null)
                {
                    dataValues.Add(0);
                }
                else
                {
                    dataValues.Add(datapoint.Value);
                }
            }

            return(dataValues);
        }
예제 #2
0
        /// <summary>
        /// Convert a collection of time series datasets to category-value datasets, where the categories represent discrete periods in the specified time scale.
        /// </summary>
        /// <remarks>
        /// Quantizing the data points in this way will substantially improve the performance of Chart.js for large data sets.
        /// </remarks>
        private List <ChartJsCategoryValuesDataset> GetCategoryDatasets(ChartJsTimeSeriesTimeScaleSpecifier timeScale)
        {
            var quantizedDatasets = new List <ChartJsCategoryValuesDataset>();

            foreach (var dataset in this.Datasets)
            {
                var datapoints = dataset.DataPoints;

                var datasetQuantized = new ChartJsCategoryValuesDataset();

                datasetQuantized.Name        = dataset.Name;
                datasetQuantized.BorderColor = dataset.BorderColor;
                datasetQuantized.FillColor   = dataset.FillColor;

                if (timeScale == ChartJsTimeSeriesTimeScaleSpecifier.Day)
                {
                    var quantizedDataPoints = datapoints
                                              .GroupBy(x => new { Day = x.DateTime })
                                              .Select(x => new ChartJsCategoryValuesDataPoint
                    {
                        Category = x.Key.Day.ToString(DateFormatStringDayMonthYear),
                        Value    = x.Sum(y => y.Value),
                        SortKey  = x.Key.Day.ToString("yyyyMMdd"),
                    })
                                              .OrderBy(x => x.SortKey)
                                              .ToList();

                    datasetQuantized.DataPoints = quantizedDataPoints.Cast <IChartJsCategoryValuesDataPoint>().ToList();
                }
                else if (timeScale == ChartJsTimeSeriesTimeScaleSpecifier.Month)
                {
                    var quantizedDataPoints = datapoints
                                              .GroupBy(x => new { Month = new DateTime(x.DateTime.Year, x.DateTime.Month, 1) })
                                              .Select(x => new ChartJsCategoryValuesDataPoint
                    {
                        Category = x.Key.Month.ToString(DateFormatStringMonthYear),
                        Value    = x.Sum(y => y.Value),
                        SortKey  = x.Key.Month.ToString("yyyyMM"),
                    })
                                              .OrderBy(x => x.SortKey)
                                              .ToList();

                    datasetQuantized.DataPoints = quantizedDataPoints.Cast <IChartJsCategoryValuesDataPoint>().ToList();
                }
                else if (timeScale == ChartJsTimeSeriesTimeScaleSpecifier.Year)
                {
                    var quantizedDataPoints = datapoints
                                              .GroupBy(x => new { Year = new DateTime(x.DateTime.Year, 1, 1) })
                                              .Select(x => new ChartJsCategoryValuesDataPoint
                    {
                        Category = x.Key.Year.ToString("yyyy"),
                        Value    = x.Sum(y => y.Value),
                        SortKey  = x.Key.Year.ToString("yyyy"),
                    })
                                              .OrderBy(x => x.SortKey)
                                              .ToList();

                    datasetQuantized.DataPoints = quantizedDataPoints.Cast <IChartJsCategoryValuesDataPoint>().ToList();
                }
                else
                {
                    throw new NotImplementedException("Timescale is not implemented");
                }

                quantizedDatasets.Add(datasetQuantized);
            }

            return(quantizedDatasets);
        }