Skip to content

Commit

Permalink
Fix report chart resize (#2235)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fosol authored Aug 29, 2024
1 parent db3078e commit c6343d5
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 21 deletions.
39 changes: 28 additions & 11 deletions app/editor/src/features/admin/reports/ReportFormSections.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import * as styled from './styled';
import { createReportSection } from './utils';

export const ReportFormSections = () => {
const { values, setFieldValue } = useFormikContext<IReportModel>();
const { values, setFieldValue, setValues } = useFormikContext<IReportModel>();
const [{ filters }, { findFilters }] = useFilters();
const [{ folders }, { findFolders }] = useFolders();

Expand Down Expand Up @@ -85,16 +85,33 @@ export const ReportFormSections = () => {
};

const handleDelete = (index: number) => {
var sections = [...values.sections];
sections.splice(index, 1);
if (index === section?.sortOrder) setSection(undefined);
setFieldValue(
`sections`,
sections.map((s, i) => ({
...s,
sortOrder: i,
})),
);
if (!section) return;

// Remove the content from the removed section in the current instance.
const instance = values.instances.length ? { ...values.instances[0] } : undefined;
if (instance) {
const sectionNames = values.sections
.filter((s) => s.name !== section.name)
.map((s) => s.name);
const content = instance
? instance.content.filter((c) => sectionNames.includes(c.sectionName))
: [];
instance.content = content;
}

if (index === section.sortOrder) setSection(undefined);
setValues({
...values,
sections: values.sections
.filter((s, i) => i !== index)
.map((section, index) => ({
...section,
sortOrder: index,
})),
instances: instance
? values.instances.map((original, index) => (index === 0 ? instance : original))
: values.instances,
});
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,35 @@ export const ReportEditTemplateForm = () => {

const removeSection = React.useCallback(
(index: number) => {
setFieldValue(
'sections',
values.sections
const section = values.sections.length > index ? values.sections[index] : undefined;
if (!section) return;

// Remove the content from the removed section in the current instance.
const instance = values.instances.length ? { ...values.instances[0] } : undefined;
if (instance) {
const sectionNames = values.sections
.filter((s) => s.name !== section.name)
.map((s) => s.name);
const content = instance
? instance.content.filter((c) => sectionNames.includes(c.sectionName))
: [];
instance.content = content;
}

setValues({
...values,
sections: values.sections
.filter((s, i) => i !== index)
.map((section, index) => ({
...section,
sortOrder: index,
})),
);
instances: instance
? values.instances.map((original, index) => (index === 0 ? instance : original))
: values.instances,
});
},
[setFieldValue, values.sections],
[setValues, values],
);

/** function that runs after a user drops an item in the list */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const groupData = (
} else if (groupBy === 'source') {
result = getChartData(chart, datasets, sections, {
...options,
groupOn: (c) => c?.content?.otherSource,
groupOn: (c) => c?.content?.otherSource.toLocaleLowerCase(),
getLabel: (c) => c?.content?.otherSource,
});
} else if (groupBy === 'series') {
Expand All @@ -53,7 +53,7 @@ export const groupData = (
} else if (groupBy === 'byline') {
result = getChartData(chart, datasets, sections, {
...options,
groupOn: (c) => c?.content?.byline,
groupOn: (c) => c?.content?.byline.toLocaleLowerCase(),
labelValueWhenEmpty: 'Unknown',
});
} else if (groupBy === 'contentType') {
Expand Down
6 changes: 3 additions & 3 deletions libs/net/template/ReportEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public async Task<string> GenerateBase64ImageAsync(
// Send request to Charts API to generate base64
var body = new StringContent(dataJson, Encoding.UTF8, System.Net.Mime.MediaTypeNames.Application.Json);
var width = model.ChartTemplate.SectionSettings.Width.HasValue ? $"width={model.ChartTemplate.SectionSettings.Width.Value}" : "";
var height = model.ChartTemplate.SectionSettings.Height.HasValue ? $"&height={model.ChartTemplate.SectionSettings.Height.Value}" : "";
var height = model.ChartTemplate.SectionSettings.Height.HasValue ? $"{(String.IsNullOrWhiteSpace(width) ? "" : "&")}height={model.ChartTemplate.SectionSettings.Height.Value}" : "";
var response = await this.HttpClient.PostAsync(
this.ChartsOptions.Url.Append(
this.ChartsOptions.Base64Path,
Expand All @@ -244,7 +244,7 @@ public async Task<string> GenerateBase64ImageAsync(
/// <param name="settings"></param>
/// <param name="minAxisColumnWidth"></param>
/// <returns></returns>
private static bool ShouldResize(ChartDataModel data, API.Models.Settings.ChartSectionSettingsModel settings, int minAxisColumnWidth)
private 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 Down Expand Up @@ -410,7 +410,7 @@ await section.ChartTemplates.ForEachAsync(async chart =>
settings.UseAllContent ? null : content);
var chartRequestModel = new ChartRequestModel(chartModel);
var base64Image = await this.GenerateBase64ImageAsync(chartRequestModel);
this.Logger.LogDebug("Chart generated, sectionId:{sectionId} chartId:{chartId}", section.Id, chart.Id);
this.Logger.LogDebug("Chart generated, reportId:{reportId} instanceId:{instance} sectionId:{sectionId} chartId:{chartId}", report.Id, reportInstance?.Id, section.Id, chart.Id);
// Replace Chart Stubs with the generated image.
body = body.Replace(ReportSectionModel.GenerateChartUid(section.Id, chart.Id), base64Image);
Expand Down

0 comments on commit c6343d5

Please sign in to comment.