Skip to content

Commit 48594d3

Browse files
committed
Replace listing by a new pie chart
1 parent f9f6762 commit 48594d3

2 files changed

Lines changed: 53 additions & 37 deletions

File tree

src/Toolbox/Charts/ChartDataFileGenerator.cs

Lines changed: 50 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -485,9 +485,11 @@ public void GeneratePoemMetricBarAndPieChartDataFile(Root data, int? seasonId, b
485485
var isGeneral = seasonId is null && forSonnet != true;
486486
var rootDir = Path.Combine(Directory.GetCurrentDirectory(),
487487
configuration[Constants.CHART_DATA_FILES_ROOT_DIR]!);
488-
var fileName = seasonId is not null ? "poems-verse-length-bar.js" : forSonnet == true ? "sonnet-verse-length-pie.js" : "poems-verse-length-pie.js";
488+
var fileName = seasonId is not null ? "poems-verse-length-bar.js" :
489+
forSonnet == true ? "sonnet-verse-length-pie.js" : "poems-verse-length-pie.js";
489490
var subDir = seasonId is not null ? $"season-{seasonId}" : forSonnet == true ? "taxonomy" : "general";
490-
var chartId = seasonId is not null ? $"season{seasonId}VerseLengthBar" : forSonnet == true ? "sonnetVerseLengthPie" : "poemVerseLengthPie";
491+
var chartId = seasonId is not null ? $"season{seasonId}VerseLengthBar" :
492+
forSonnet == true ? "sonnetVerseLengthPie" : "poemVerseLengthPie";
491493
using var streamWriter = new StreamWriter(Path.Combine(rootDir, subDir, fileName));
492494
var chartDataFileHelper = new ChartDataFileHelper(streamWriter,
493495
seasonId is not null ? ChartType.Bar : ChartType.Pie, 1);
@@ -496,8 +498,9 @@ public void GeneratePoemMetricBarAndPieChartDataFile(Root data, int? seasonId, b
496498
var variableMetricData = new Dictionary<string, int>();
497499
var poems = seasonId is not null
498500
? data.Seasons.First(x => x.Id == seasonId).Poems
499-
: forSonnet == true ? data.Seasons.SelectMany(x => x.Poems).Where(x => x.PoemType == "sonnet")
500-
: data.Seasons.SelectMany(x => x.Poems);
501+
: forSonnet == true
502+
? data.Seasons.SelectMany(x => x.Poems).Where(x => x.PoemType == "sonnet")
503+
: data.Seasons.SelectMany(x => x.Poems);
501504

502505
foreach (var poem in poems)
503506
{
@@ -585,7 +588,7 @@ public void GeneratePoemMetricBarAndPieChartDataFile(Root data, int? seasonId, b
585588
{
586589
dataLines.AddRange(regularMetricChartData);
587590
dataLines.AddRange(variableMetricChartData);
588-
591+
589592
chartDataFileHelper.WriteData(dataLines, true);
590593

591594
chartDataFileHelper.WriteAfterData(chartId, ["Poèmes"],
@@ -619,8 +622,7 @@ public void GeneratePoemMetricBarAndPieChartDataFile(Root data, int? seasonId, b
619622
/// <summary>
620623
/// Generates a pie chart data file representing the intensity of poem creation by counting the number of poems created on each day.
621624
/// The method aggregates poem data from two `Root` objects, processes the intensity of poem creation,
622-
/// and generates output files: a pie chart data file "poem-intensity-pie.js"
623-
/// and a markdown file "most_intense_days.md" listing the most intense creation days.
625+
/// and generates output file: a pie chart data file "poem-intensity-pie.js"
624626
/// </summary>
625627
/// <param name="data">The primary source of French poems data.</param>
626628
/// <param name="dataEn">The secondary source of English poems data.</param>
@@ -676,34 +678,6 @@ public void GeneratePoemIntensityPieChartDataFile(Root data, Root dataEn)
676678
chartDataFileHelper.WriteData(dataLines, true);
677679
chartDataFileHelper.WriteAfterData("poemIntensityPie", ["Les jours de création sont-ils intenses ?"]);
678680
streamWriter.Close();
679-
680-
// Most intense days content file
681-
var intensityKeys = intensityDict.Keys.OrderDescending().Where(x => x > 2);
682-
var filePath = Path.Combine(Directory.GetCurrentDirectory(), configuration[Constants.CONTENT_ROOT_DIR]!,
683-
"../includes/most_intense_days.md");
684-
var streamWriter2 = new StreamWriter(filePath);
685-
686-
streamWriter2.WriteLine("+++");
687-
streamWriter2.WriteLine("title = \"Les jours les plus intenses\"");
688-
streamWriter2.WriteLine("+++");
689-
690-
foreach (var key in intensityKeys)
691-
{
692-
streamWriter2.WriteLine($"- {key} poèmes en un jour :");
693-
var matchingIntensities = dataDict.Where(x => x.Value == key).Select(x => x.Key);
694-
// ReSharper disable once PossibleMultipleEnumeration
695-
var years = matchingIntensities.Select(x => x.Substring(6)).Distinct();
696-
697-
foreach (var year in years)
698-
{
699-
// ReSharper disable once PossibleMultipleEnumeration
700-
var dates = matchingIntensities.Where(x => x.Substring(6) == year).Select(x => x.ToDateTime()).Order();
701-
streamWriter2.Write($" - {year} : ");
702-
streamWriter2.WriteLine(string.Join(", ", dates.Select(x => x.ToString("ddd dd MMM"))));
703-
}
704-
}
705-
706-
streamWriter2.Close();
707681
}
708682

709683
/// <summary>
@@ -734,6 +708,11 @@ public void GeneratePoemByDayOfWeekPieChartDataFile(Root data, Root dataEn)
734708
}
735709
}
736710

711+
WriteByDayOfWeekChartFile(dataDict, "poem-dayofweek-pie.js", "poemDayOfWeekPie");
712+
}
713+
714+
private void WriteByDayOfWeekChartFile(Dictionary<int, int> dataDict, string fileName, string chartId)
715+
{
737716
var dataLines = new List<DataLine>();
738717
var baseColor = "rgba(72, 149, 239, {0})";
739718
var baseAlpha = 0.2;
@@ -753,17 +732,51 @@ public void GeneratePoemByDayOfWeekPieChartDataFile(Root data, Root dataEn)
753732
{ NumberDecimalSeparator = ".", NumberDecimalDigits = 1 }))));
754733
}
755734

756-
var fileName = "poem-dayofweek-pie.js";
757735
var rootDir = Path.Combine(Directory.GetCurrentDirectory(),
758736
configuration[Constants.CHART_DATA_FILES_ROOT_DIR]!);
759737
using var streamWriter = new StreamWriter(Path.Combine(rootDir, "general", fileName));
760738
var chartDataFileHelper = new ChartDataFileHelper(streamWriter, ChartType.Pie);
761739
chartDataFileHelper.WriteBeforeData();
762740
chartDataFileHelper.WriteData(dataLines, true);
763-
chartDataFileHelper.WriteAfterData("poemDayOfWeekPie", ["Par jour de la semaine"]);
741+
chartDataFileHelper.WriteAfterData(chartId, ["Par jour de la semaine"]);
764742
streamWriter.Close();
765743
}
766744

745+
/// <summary>
746+
/// Processes and generates a pie chart data file representing the distribution of poems
747+
/// categorized by the day of the week on which they were written, for days when more than two poems were written.
748+
/// This method consolidates
749+
/// data from two `Root` objects (primary and secondary sources) and calculates poem counts
750+
/// for each day of the week, identified as Monday through Sunday.
751+
/// The generated "intenseDays-dayofweek-pie.js" file will include visual data for each day of the week with corresponding
752+
/// values and colors.
753+
/// </summary>
754+
/// <param name="data">The primary source of French poems data.</param>
755+
/// <param name="dataEn">The secondary source of English poems data.</param>
756+
public void GenerateIntenseByDayOfWeekPieChartDataFile(Root data, Root dataEn)
757+
{
758+
// Get poems grouped by TextDate when there are more than 2 poems for a given date
759+
var poems = data.Seasons.SelectMany(x => x.Poems)
760+
.Where(x => x.TextDate != "01.01.1994")
761+
.ToList();
762+
poems.AddRange(dataEn.Seasons.SelectMany(x => x.Poems));
763+
var groups = poems.GroupBy(x => x.TextDate)
764+
.Where(x => x.Count() > 2)
765+
.ToDictionary(x => x.Key, x => x.Count());
766+
767+
var dataDict = new Dictionary<int, int>();
768+
foreach (var group in groups)
769+
{
770+
var dayOfWeek = group.Key.ToDateTime().DayOfWeek;
771+
if (!dataDict.TryAdd((int)dayOfWeek, group.Value))
772+
{
773+
dataDict[(int)dayOfWeek] += group.Value;
774+
}
775+
}
776+
777+
WriteByDayOfWeekChartFile(dataDict, "intenseDays-dayofweek-pie.js", "intenseDaysDayOfWeekPie");
778+
}
779+
767780
/// <summary>
768781
/// Generates a pie chart data file for English poems based on the day of the week they are dated.
769782
/// The method processes poems from the given `Root` object and categorizes them by the day of the week.

src/Toolbox/Program.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ private static void ImportEnPoemsContentFiles()
345345
Console.WriteLine("Charts for day radar OK");
346346

347347
_chartDataFileGenerator.GeneratePoemIntensityPieChartDataFile(_data, _dataEn);
348+
_chartDataFileGenerator.GenerateIntenseByDayOfWeekPieChartDataFile(_data, _dataEn);
348349
Console.WriteLine("Poem intensity chart OK");
349350

350351
_chartDataFileGenerator.GenerateEnPoemByDayOfWeekPieChartDataFile(_dataEn);
@@ -533,6 +534,7 @@ private static void GenerateDependantChartDataFilesAndCheckQuality(int seasonId,
533534
_chartDataFileGenerator.GeneratePoemsByDayRadarChartDataFile(_data, _dataEn, null, null,
534535
forLaMortExtraTag: true);
535536
_chartDataFileGenerator.GeneratePoemIntensityPieChartDataFile(_data, _dataEn);
537+
_chartDataFileGenerator.GenerateIntenseByDayOfWeekPieChartDataFile(_data, _dataEn);
536538
_chartDataFileGenerator.GeneratePoemByDayOfWeekPieChartDataFile(_data, _dataEn);
537539
Console.WriteLine(
538540
"Poems by day general and specific, poem intensity, poem by day of week, chart data files OK");
@@ -604,6 +606,7 @@ private static void GeneratePoemsRadarChartDataFile(MenuItem menuChoice)
604606
_chartDataFileGenerator.GeneratePoemsByDayRadarChartDataFile(_data, _dataEn, null, null,
605607
forLaMortExtraTag: true);
606608
_chartDataFileGenerator.GeneratePoemIntensityPieChartDataFile(_data, _dataEn);
609+
_chartDataFileGenerator.GenerateIntenseByDayOfWeekPieChartDataFile(_data, _dataEn);
607610
_chartDataFileGenerator.GeneratePoemByDayOfWeekPieChartDataFile(_data, _dataEn);
608611
Console.WriteLine(
609612
"Poems by day general and specific, poem intensity, poem by day of week, chart data files OK");

0 commit comments

Comments
 (0)