Skip to content

Instantly share code, notes, and snippets.

@abock abock/OxyPlot.workbook Secret
Created May 31, 2017

Embed
What would you like to do?
uti author platforms packages
com.xamarin.workbook
Aaron Bockover (github.com/abock)
Console
id version
OxyPlot.Core
1.0.0

OxyPlot SVG Rendering

All we need to do is reference the OxyPlot.Core NuGet package.

#r "OxyPlot"

Import a few namespaces that our SVG provider will need.

using System.Text;
using Xamarin.Interactive.Representations;
using OxyPlot;

We can now write the representation provider which will be called whenever a PlotModel object is the result of a cell’s execution. This is our opportunity to provide a custom representation of that object.

InteractiveAgent.RepresentationManager.AddProvider<PlotModel> (plotModel => {
    var exporter = new SvgExporter {
        Width = 300,
        Height = 250
    };

    var svgUri = "data:image/svg+xml;utf8," + exporter.ExportToString (plotModel);

    return new Image (
        Xamarin.Interactive.Representations.ImageFormat.Uri,
        Encoding.UTF8.GetBytes (svgUri),
        (int)exporter.Width,
        (int)exporter.Height);
})

Finally, we can begin using the actual OxyPlot API. First we create a model, and then add a data series to it.

using OxyPlot.Series;

var plot = new PlotModel ();
plot.Series.Add (new FunctionSeries (Math.Sin, -10, 10, 0.1, "sin(x)"));
plot.Series.Add (new FunctionSeries (Math.Cos, -10, 10, 0.1, "cos(x)"));
plot.Series.Add (new FunctionSeries (t => 5 * Math.Cos (t), t => 5 * Math.Sin (t), 0, 2 * Math.PI, 0.1, "5cos(t),5sin(t)"));

And finally render the result by ensuring the cell yields a PlotModel object as its last result.

plot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.