Skip to content

Commit

Permalink
Fix report charts (#2233)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fosol authored Aug 29, 2024
1 parent 1904944 commit 78e0d78
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 17 deletions.
23 changes: 20 additions & 3 deletions api/node/src/routes.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
CategoryScale,
ChartTypeRegistry,
Colors,
Legend,
LinearScale,
LineElement,
Expand Down Expand Up @@ -36,6 +37,7 @@ ChartJS.register(
Tooltip,
Legend,
ChartDataLabels,
Colors,
);

let myChart: Record<string, ChartJS> = {};
Expand Down
29 changes: 18 additions & 11 deletions libs/net/template/ReportEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,6 @@ public async Task<string> 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<ChartOptionsModel>(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)) : "{}";
Expand Down Expand Up @@ -253,7 +244,7 @@ public async Task<string> GenerateBase64ImageAsync(
/// <param name="settings"></param>
/// <param name="minAxisColumnWidth"></param>
/// <returns></returns>
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;
Expand All @@ -268,7 +259,7 @@ private bool ShouldResize(ChartDataModel data, API.Models.Settings.ChartSectionS
/// <param name="data"></param>
/// <param name="settings"></param>
/// <param name="minAxisColumnWidth"></param>
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;
Expand Down Expand Up @@ -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))
Expand Down
6 changes: 3 additions & 3 deletions libs/net/template/ReportExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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("<mark>", "").Replace("</mark>", ""),
"series" => content.Series?.Name ?? "",
"sentiment" => GetSentimentIcon(content.TonePools.FirstOrDefault()?.Value ?? 0),
_ => content.OtherSource,
Expand Down Expand Up @@ -548,7 +548,7 @@ public static IEnumerable<IGrouping<string, ContentModel>> 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("<mark>", "").Replace("</mark>", "")).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),
Expand Down Expand Up @@ -726,7 +726,7 @@ public static string GetLabel(IGrouping<string, ContentModel> 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("<mark>", "").Replace("</mark>", "")) == 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),
Expand Down
2 changes: 2 additions & 0 deletions libs/npm/core/src/hooks/api/utils/mergeChartSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 78e0d78

Please sign in to comment.