From 78e0d78f0018a954804852781791d062861a9a21 Mon Sep 17 00:00:00 2001 From: Jeremy Foster Date: Thu, 29 Aug 2024 13:08:37 -0700 Subject: [PATCH] Fix report charts (#2233) --- api/node/src/routes.ts | 23 +++++++++++++-- .../settings/template/charts/ChartViewer.tsx | 2 ++ libs/net/template/ReportEngine.cs | 29 ++++++++++++------- libs/net/template/ReportExtensions.cs | 6 ++-- .../src/hooks/api/utils/mergeChartSettings.ts | 2 ++ 5 files changed, 45 insertions(+), 17 deletions(-) diff --git a/api/node/src/routes.ts b/api/node/src/routes.ts index ee1c9614ae..5680b712ae 100644 --- a/api/node/src/routes.ts +++ b/api/node/src/routes.ts @@ -1,5 +1,14 @@ import { Router } from 'express'; -import { CategoryScale, Colors } from 'chart.js'; +import { + CategoryScale, + Colors, + Legend, + LinearScale, + LineElement, + PointElement, + Title, + Tooltip, +} from 'chart.js'; import Chart from 'chart.js/auto'; import { getString, convertChartJsConfigToBase64String, generateBase64, sendImage } from './utils'; import { ChartTypes } from './charts'; @@ -9,8 +18,16 @@ Chart.register(CategoryScale); Chart.defaults.animation = false; Chart.defaults.responsive = false; Chart.defaults.maintainAspectRatio = true; -Chart.register(Colors); -Chart.register(ChartDataLabels); +Chart.register( + LinearScale, + PointElement, + LineElement, + Title, + Tooltip, + Legend, + ChartDataLabels, + Colors, +); const routes = Router(); diff --git a/app/subscriber/src/features/my-reports/edit/settings/template/charts/ChartViewer.tsx b/app/subscriber/src/features/my-reports/edit/settings/template/charts/ChartViewer.tsx index 5f3fa5951b..24e1bcf04c 100644 --- a/app/subscriber/src/features/my-reports/edit/settings/template/charts/ChartViewer.tsx +++ b/app/subscriber/src/features/my-reports/edit/settings/template/charts/ChartViewer.tsx @@ -1,6 +1,7 @@ import { CategoryScale, ChartTypeRegistry, + Colors, Legend, LinearScale, LineElement, @@ -36,6 +37,7 @@ ChartJS.register( Tooltip, Legend, ChartDataLabels, + Colors, ); let myChart: Record = {}; diff --git a/libs/net/template/ReportEngine.cs b/libs/net/template/ReportEngine.cs index e49a53ec3b..6ae0485534 100644 --- a/libs/net/template/ReportEngine.cs +++ b/libs/net/template/ReportEngine.cs @@ -217,15 +217,6 @@ public async Task GenerateBase64ImageAsync( { // If the chart should be resized, calculate the new size and update the options. UpdateChartSize(dataModel, model.ChartTemplate.SectionSettings, 30); - var sectionJsonText = model.ChartTemplate.SectionSettings.Options.ToJson(); - if (sectionJsonText != "{}") - { - var chartOptions = JsonSerializer.Deserialize(model.ChartTemplate.SectionSettings.Options); - if (chartOptions != null) - { - model.ChartTemplate.SectionSettings.Options = JsonDocument.Parse(JsonSerializer.Serialize(chartOptions, this.SerializerOptions)); - } - } } var optionsJson = model.ChartTemplate.SectionSettings.Options != null ? JsonSerializer.Serialize(MergeChartOptions(model.ChartTemplate.Settings, model.ChartTemplate.SectionSettings)) : "{}"; @@ -253,7 +244,7 @@ public async Task GenerateBase64ImageAsync( /// /// /// - private bool ShouldResize(ChartDataModel data, API.Models.Settings.ChartSectionSettingsModel settings, int minAxisColumnWidth) + private static bool ShouldResize(ChartDataModel data, API.Models.Settings.ChartSectionSettingsModel settings, int minAxisColumnWidth) { var axisLabelCount = data.Labels.Length; var size = settings.IsHorizontal == false ? settings.Height : settings.Width; @@ -268,7 +259,7 @@ private bool ShouldResize(ChartDataModel data, API.Models.Settings.ChartSectionS /// /// /// - private void UpdateChartSize(ChartDataModel data, API.Models.Settings.ChartSectionSettingsModel settings, int minAxisColumnWidth) + private static void UpdateChartSize(ChartDataModel data, API.Models.Settings.ChartSectionSettingsModel settings, int minAxisColumnWidth) { var axisLabelCount = data.Labels.Length; var size = settings.IsHorizontal == false ? settings.Height : settings.Width; @@ -457,6 +448,22 @@ private JsonDocument MergeChartOptions(API.Models.Settings.ChartTemplateSettings defaultJson.Add("indexAxis", sectionIndexAxis.CopyNode()); } + // There appears to be no way to modify a value... + if (sectionJson.TryGetPropertyValue("maintainAspectRatio", out JsonNode? sectionMaintainAspectRatio)) + { + if (defaultJson.ContainsKey("maintainAspectRatio")) + defaultJson.Remove("maintainAspectRatio"); + defaultJson.Add("maintainAspectRatio", sectionMaintainAspectRatio.CopyNode()); + } + + // There appears to be no way to modify a value... + if (sectionJson.TryGetPropertyValue("aspectRatio", out JsonNode? sectionAspectRatio)) + { + if (defaultJson.ContainsKey("aspectRatio")) + defaultJson.Remove("aspectRatio"); + defaultJson.Add("aspectRatio", sectionAspectRatio.CopyNode()); + } + if (sectionJson.TryGetPropertyValue("scales", out JsonNode? sectionScales)) { if (defaultJson.TryGetPropertyValue("scales", out JsonNode? scales)) diff --git a/libs/net/template/ReportExtensions.cs b/libs/net/template/ReportExtensions.cs index 9329e53374..5e35995d04 100644 --- a/libs/net/template/ReportExtensions.cs +++ b/libs/net/template/ReportExtensions.cs @@ -245,7 +245,7 @@ public static string GetContentGroupByPropertyValue(this ContentModel content, s "contentType" => content.ContentType.ToString(), "topicType" => content.Topics.FirstOrDefault()?.TopicType.ToString() ?? "", "topicName" => content.Topics.FirstOrDefault()?.Name ?? "", - "byline" => content.Byline, + "byline" => content.Byline.Replace("", "").Replace("", ""), "series" => content.Series?.Name ?? "", "sentiment" => GetSentimentIcon(content.TonePools.FirstOrDefault()?.Value ?? 0), _ => content.OtherSource, @@ -548,7 +548,7 @@ public static IEnumerable> GroupContent( { "mediaType" => content.GroupBy(c => c.MediaType?.Name ?? "Other").OrderBy(group => group.Key), "contentType" => content.GroupBy(c => c.ContentType.ToString()).OrderBy(group => group.Key), - "byline" => content.GroupBy(c => string.IsNullOrWhiteSpace(c.Byline) ? "Unknown" : c.Byline).Where((g) => !excludeEmptyValues || g.Key != "Unknown").OrderBy(group => group.Key), + "byline" => content.GroupBy(c => string.IsNullOrWhiteSpace(c.Byline) ? "Unknown" : c.Byline.Replace("", "").Replace("", "")).Where((g) => !excludeEmptyValues || g.Key != "Unknown").OrderBy(group => group.Key), "series" => content.GroupBy(c => c.Series?.Name ?? c.OtherSeries ?? "None").Where((g) => !excludeEmptyValues || g.Key != "None").OrderBy(group => group.Key), "sentiment" => content.GroupBy(c => GetSentimentValue(c)?.ToString() ?? "None").Where((v) => !excludeEmptyValues || v.Key != "None").OrderByDescending(group => group.Key), "sentimentSimple" => content.GroupBy(c => GetSentimentRating(c) ?? "None").Where((v) => !excludeEmptyValues || v.Key != "None").OrderBy(group => group.Key), @@ -726,7 +726,7 @@ public static string GetLabel(IGrouping dataset, API.Model { "mediaType" => content.Where(c => (c.MediaType?.Name ?? "Other") == label), "contentType" => content.Where(c => c.ContentType.ToString() == label), - "byline" => content.Where(c => (string.IsNullOrWhiteSpace(c.Byline) ? "Unknown" : c.Byline) == label), + "byline" => content.Where(c => (string.IsNullOrWhiteSpace(c.Byline) ? "Unknown" : c.Byline.Replace("", "").Replace("", "")) == label), "series" => content.Where(c => (c.Series?.Name ?? "None") == label), "sentiment" => content.Where(c => (GetSentimentValue(c)?.ToString() ?? "None") == label), "sentimentSimple" => content.Where(c => (GetSentimentRating(c) ?? "None") == label), diff --git a/libs/npm/core/src/hooks/api/utils/mergeChartSettings.ts b/libs/npm/core/src/hooks/api/utils/mergeChartSettings.ts index b78cd8acd9..e5d6d01daf 100644 --- a/libs/npm/core/src/hooks/api/utils/mergeChartSettings.ts +++ b/libs/npm/core/src/hooks/api/utils/mergeChartSettings.ts @@ -63,6 +63,8 @@ export const mergeChartSettings = ( minBarLength: override.minBarLength, options: { ...initSettings.options, + maintainAspectRatio: override.maintainAspectRatio, + aspectRatio: override.aspectRatio, indexAxis: override.isHorizontal || override.isHorizontal === undefined ? 'x' : 'y', plugins: { ...initSettings.options.plugins,