Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring MakeChart with ScottPlot #4

Merged
merged 4 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 51 additions & 8 deletions DataAnalysis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Text.RegularExpressions;
using Plotly.NET.ImageExport;
using Plotly.NET;
using ScottPlot;

class DataAnalysis
{
Expand Down Expand Up @@ -35,7 +36,7 @@ public static async Task Invoke()


await MakeJSON(baseSeq);
MakePieChart(baseSeq);
MakeChart(baseSeq);
}

static async Task MakeJSON(IOrderedEnumerable<IGrouping<string, string>> baseSeq)
Expand All @@ -58,7 +59,7 @@ await File.WriteAllTextAsync(
);
}

static void MakePieChart(IOrderedEnumerable<IGrouping<string, string>> baseSeq)
static void MakeChart(IOrderedEnumerable<IGrouping<string, string>> baseSeq)
{
var plotSeq = baseSeq.Where(item => item.Key != "unknown")
.Select(item => new { key = item.Key, count = item.Count() })
Expand Down Expand Up @@ -94,13 +95,55 @@ static void MakePieChart(IOrderedEnumerable<IGrouping<string, string>> baseSeq)
)
.ToDictionary(item => item.Item1, item => (double)item.Item2);


var doughnut = Plotly.NET.CSharp.Chart.Doughnut<double, string, string>(
values: plotSeq.Select(item => item.Value).ToList(),
Labels: plotSeq.Select(item => item.Key).ToList()
var random = new Random();
var myplot = new ScottPlot.Plot();
Fonts.AddFontFile(
name: "NotoSerifTC",
path: Path.Combine("NotoSerifTC-Black.ttf")
);

doughnut.WithTitle("statistics of contributors")
.SavePNG("contributorStat", Width: 700, Height: 450);
double total = plotSeq.Select(x => x.Value).Sum();
var slices = plotSeq.Select(item =>
{
var slice = new PieSlice(
item.Value,
new ScottPlot.Color(string.Format("#{0:X6}", Guid.NewGuid().ToString().Substring(0, 6))),
$"{item.Value / total * 100:0.0}%"
);

slice.LabelFontSize = 20;
slice.LabelBold = true;
slice.LabelFontColor = new ScottPlot.Color("#ffffff");

slice.LegendText = item.Key;

return slice;
})
.ToList();
var pie = myplot.Add.Pie(slices);

pie.DonutFraction = .5;
pie.SliceLabelDistance = 0.8;


myplot.Axes.Frameless();
myplot.HideAxesAndGrid();
// 目前沒辦法假名+漢字
myplot.Font.Automatic();
myplot.ShowLegend(Edge.Right);

myplot.SavePng("contributorStat.png", 1200, 800);




// Plotly.NET
// var doughnut = Plotly.NET.CSharp.Chart.Doughnut<double, string, string>(
// values: plotSeq.Select(item => item.Value).ToList(),
// Labels: plotSeq.Select(item => item.Key).ToList()
// );

// doughnut.WithTitle("statistics of contributors")
// .SavePNG("contributorStat", Width: 700, Height: 450);
}
}
1 change: 1 addition & 0 deletions YTPlayListDownloader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<PackageReference Include="FFMpegCore" Version="5.1.0" />
<PackageReference Include="Plotly.NET.CSharp" Version="0.11.1" />
<PackageReference Include="Plotly.NET.ImageExport" Version="5.0.1" />
<PackageReference Include="ScottPlot" Version="5.0.40" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
<PackageReference Include="TagLibSharp" Version="2.3.0" />
Expand Down
Loading
Loading