Last active
December 9, 2019 07:21
-
-
Save 8/ce0d4de5dee74f6f43c528620a0c17d5 to your computer and use it in GitHub Desktop.
Script to help setup Oxyplot for use in C# Scripts (.csx) using dotnet-script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "nuget: OxyPlot.Core.Drawing, 2.0.0" | |
using OxyPlot; | |
using OxyPlot.Axes; | |
using OxyPlot.Core.Drawing; | |
using OxyPlot.Series; | |
/* export functions */ | |
static PlotModel ExportToPng(this PlotModel plot, string file, int width = 1024, int height = 768) | |
{ | |
PngExporter.Export(plot, file, width, height, OxyColors.White); | |
return plot; | |
} | |
static PlotModel ExportToSvg(this PlotModel plot, string file, double width = 1024, double height = 768) | |
{ | |
using (var fs = File.Create(file)) | |
SvgExporter.Export(plot, fs, width, height, true); | |
return plot; | |
} | |
enum ImageType { Png, Svg }; | |
static PlotModel Export(this PlotModel plot, string file, ImageType imageType, int width = 1024, int height = 768) | |
=> imageType switch | |
{ | |
ImageType.Png => plot.ExportToPng(file, width, height), | |
ImageType.Svg => plot.ExportToSvg(file, width, height), | |
_ => throw new ArgumentOutOfRangeException(nameof(imageType)), | |
}; | |
/* PlotModel extensions */ | |
static PlotModel AddSeries(this PlotModel plotModel, Series series) | |
{ | |
plotModel.Series.Add(series); | |
return plotModel; | |
} | |
static PlotModel GetPlot() | |
=> new PlotModel { }; | |
/* helper for FunctionSeries */ | |
static FunctionSeries GetSeries(Func<double, double> f, double start, double end, double step, string title = null) | |
=> new FunctionSeries(f, x0: start, x1: end, dx: step, title: title); | |
static Func<Func<double, double>, string, FunctionSeries> GetSeries(double start, double end, double step) | |
=> (Func<double, double> f, string title) => GetSeries(f, start, end, step, title); | |
/* helper for HistogramSeries */ | |
private static HistogramSeries GetHistogramSeries() | |
=> new HistogramSeries { StrokeThickness = 1 }; | |
static HistogramSeries AddItem(this HistogramSeries series, HistogramItem item) | |
{ | |
series.Items.Add(item); | |
return series; | |
} | |
static HistogramSeries AddItems(this HistogramSeries series, IEnumerable<HistogramItem> items) | |
{ | |
foreach (var item in items) | |
series.AddItem(item); | |
return series; | |
} | |
static HistogramSeries GetSeries(this IEnumerable<HistogramItem> items, Func<HistogramSeries> getHistogram = null) | |
=> (getHistogram switch | |
{ | |
null => GetHistogramSeries(), | |
Func<HistogramSeries> f => f(), | |
}).AddItems(items); | |
static HistogramItem GetHistogramItem(double start, double end, int count) | |
=> new HistogramItem(start, end, Math.Abs(end-start) * count, count); | |
static HistogramItem GetHistogramItem(IGrouping<double, int> group, double groupRange, int groupCount) | |
=> GetHistogramItem(group.Key * groupRange, (group.Key+1) * groupRange, group.Count()); | |
static IEnumerable<HistogramItem> GetHistogramItems(this IEnumerable<IGrouping<double, int>> groups, double groupRange) | |
=> groups.Select(g => GetHistogramItem(g, groupRange, groups.Count())); | |
static HistogramSeries GetSeries(this IEnumerable<IGrouping<double, int>> groupings, double groupRange) | |
=> groupings.GetHistogramItems(groupRange).GetSeries(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment