-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXamlExporter.cs
93 lines (82 loc) · 3.36 KB
/
XamlExporter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="XamlExporter.cs" company="OxyPlot">
// Copyright (c) 2014 OxyPlot contributors
// </copyright>
// <summary>
// Provides functionality to export plots to XAML.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace OxyPlot.Dark.Wpf
{
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Media;
using System.Xml;
/// <summary>
/// Provides functionality to export plots to XAML.
/// </summary>
public static class XamlExporter
{
/// <summary>
/// Export the specified plot model to an xaml string.
/// </summary>
/// <param name="model">The model.</param>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="background">The background.</param>
/// <returns>A xaml string.</returns>
public static string ExportToString(IPlotModel model, double width, double height, OxyColor background)
{
var sb = new StringBuilder();
using (var sw = new StringWriter(sb))
{
var xw = XmlWriter.Create(sw, new XmlWriterSettings { Indent = true });
Export(model, xw, width, height, background);
}
return sb.ToString();
}
/// <summary>
/// Exports the specified plot model to a xaml file.
/// </summary>
/// <param name="model">The model.</param>
/// <param name="fileName">Name of the file.</param>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="background">The background.</param>
public static void Export(PlotModel model, string fileName, double width, double height, OxyColor background)
{
using (var sw = new StreamWriter(fileName))
{
var xw = XmlWriter.Create(sw, new XmlWriterSettings { Indent = true });
Export(model, xw, width, height, background);
}
}
/// <summary>
/// Exports the specified plot model to a xml writer.
/// </summary>
/// <param name="model">The model.</param>
/// <param name="writer">The xml writer.</param>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="background">The background.</param>
private static void Export(IPlotModel model, XmlWriter writer, double width, double height, OxyColor background)
{
var c = new Canvas();
if (background.IsVisible())
{
c.Background = background.ToBrush();
}
c.Measure(new Size(width, height));
c.Arrange(new Rect(0, 0, width, height));
var rc = new CanvasRenderContext(c) { UseStreamGeometry = false };
rc.TextFormattingMode = TextFormattingMode.Ideal;
model.Update(true);
model.Render(rc, width, height);
c.UpdateLayout();
XamlWriter.Save(c, writer);
}
}
}