private void SavePlot_Click(object sender, RoutedEventArgs e) { var dlg = new SaveFileDialog(); dlg.Filter = ".svg files|*.svg|.png files|*.png|.pdf files|*.pdf|.xaml files|*.xaml"; dlg.DefaultExt = ".svg"; if (dlg.ShowDialog(this).Value) { var ext = Path.GetExtension(dlg.FileName).ToLower(); switch (ext) { case ".png": plot1.SaveBitmap(dlg.FileName); break; case ".svg": var rc = new ShapesRenderContext(null); var svg = vm.Model.ToSvg(plot1.ActualWidth, plot1.ActualHeight, false, rc); File.WriteAllText(dlg.FileName, svg); break; case ".pdf": PdfExporter.Export(vm.Model, dlg.FileName, plot1.ActualWidth, plot1.ActualHeight); break; case ".xaml": plot1.SaveXaml(dlg.FileName); break; } OpenContainingFolder(dlg.FileName); } }
private void CopySvg_Click(object sender, RoutedEventArgs e) { var rc = new ShapesRenderContext(null); var svg = OxyPlot.SvgExporter.ExportToString(this.vm.Model, plot1.ActualWidth, plot1.ActualHeight, true, rc); Clipboard.SetText(svg); }
/// <summary> /// Exports the specified plot model to an xps file. /// </summary> /// <param name="model">The model.</param> /// <param name="fileName">The file name.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="background">The background color.</param> public static void Export(IPlotModel model, string fileName, double width, double height, OxyColor background) { using (var xpsPackage = Package.Open(fileName, FileMode.Create, FileAccess.ReadWrite)) { using (var doc = new XpsDocument(xpsPackage)) { var canvas = new Canvas { Width = width, Height = height, Background = background.ToBrush() }; canvas.Measure(new Size(width, height)); canvas.Arrange(new Rect(0, 0, width, height)); var rc = new ShapesRenderContext(canvas) { TextFormattingMode = TextFormattingMode.Ideal }; model.Update(true); model.Render(rc, width, height); canvas.UpdateLayout(); var xpsdw = XpsDocument.CreateXpsDocumentWriter(doc); xpsdw.Write(canvas); } } }
/// <summary> /// Exports the specified <see cref="PlotModel" /> to the specified <see cref="Stream" />. /// </summary> /// <param name="model">The model.</param> /// <param name="stream">The stream.</param> public void Export(IPlotModel model, Stream stream) { using (var xpsPackage = Package.Open(stream)) { using (var doc = new XpsDocument(xpsPackage)) { var canvas = new Canvas { Width = this.Width, Height = this.Height, Background = this.Background.ToBrush() }; canvas.Measure(new Size(this.Width, this.Height)); canvas.Arrange(new Rect(0, 0, this.Width, this.Height)); var rc = new ShapesRenderContext(canvas) { TextFormattingMode = this.TextFormattingMode }; model.Update(true); model.Render(rc, this.Width, this.Height); canvas.UpdateLayout(); var xpsdw = XpsDocument.CreateXpsDocumentWriter(doc); xpsdw.Write(canvas); } } }
/// <summary> /// Prints the specified plot model. /// </summary> /// <param name="model">The model.</param> /// <param name="width">The width (using the actual media width if not specified).</param> /// <param name="height">The height (using the actual media height if not specified).</param> public static void Print(PlotModel model, double width = double.NaN, double height = double.NaN) { PrintDocumentImageableArea area = null; var xpsDocumentWriter = PrintQueue.CreateXpsDocumentWriter(ref area); if (xpsDocumentWriter != null) { if (double.IsNaN(width)) { width = area.MediaSizeWidth; } if (double.IsNaN(height)) { height = area.MediaSizeHeight; } var canvas = new Canvas { Width = width, Height = height }; canvas.Measure(new Size(width, height)); canvas.Arrange(new Rect(0, 0, width, height)); var rc = new ShapesRenderContext(canvas); model.Update(); model.Render(rc, width, height); canvas.UpdateLayout(); xpsDocumentWriter.Write(canvas); } }
/// <summary> /// Asserts that a plot is equal to the plot stored in the "baseline" folder. /// 1. Renders the plot to file.svg /// 2. If the baseline does not exist, the current plot is copied to the baseline folder. /// 3. Checks that the svg file is equal to a baseline svg. /// </summary> /// <param name="plot">The plot.</param> /// <param name="name">The name of the baseline file.</param> public static void AreEqual(PlotModel plot, string name) { // string name = new System.Diagnostics.StackFrame(1).GetMethod().Name; string path = name + ".svg"; string baseline = @"baseline\" + path; using (var s = File.Create(path)) { var rc = new ShapesRenderContext(null); SvgExporter.Export(plot, s, 800, 500, false, rc); } if (!Directory.Exists("baseline")) { Directory.CreateDirectory("baseline"); } if (!File.Exists(baseline)) { File.Copy(path, baseline); return; } var baselineSvg = File.ReadAllText(baseline); var actualSvg = File.ReadAllText(path); Assert.IsTrue(string.Equals(baselineSvg, actualSvg), "Actual svg is not equal to baseline (" + Path.GetFullPath(baseline) + ")"); }
public void CopySvg() { var rc = new ShapesRenderContext(null); var svg = SvgExporter.ExportToString(this.Model, this.Plot.ActualWidth, this.Plot.ActualHeight, true, rc); Clipboard.SetText(svg); }
public void ExportToString_TestPlot_ValidSvgString() { var plotModel = new PlotModel("Test plot"); plotModel.Series.Add(new FunctionSeries(Math.Sin, 0, Math.PI * 8, 200, "Math.Sin")); var rc = new ShapesRenderContext(null); var svg = SvgExporter.ExportToString(plotModel, 800, 500, false, rc); SvgAssert.IsValidElement(svg); }
public void SaveSvg_TestPlot_ValidSvgFile() { var plotModel = TestModels.CreateTestModel1(); const string FileName = "PlotModelTests_Test1.svg"; var rc = new ShapesRenderContext(null); var svg = plotModel.ToSvg(800, 500, false, rc); File.WriteAllText(FileName, svg); SvgAssert.IsValidFile(FileName); }
public void ToSvg_TestPlot_ValidSvgString() { var plotModel = TestModels.CreateTestModel1(); var rc = new ShapesRenderContext(null); var svg1 = plotModel.ToSvg(800, 500, true, rc); SvgAssert.IsValidDocument(svg1); var svg2 = plotModel.ToSvg(800, 500, false, rc); SvgAssert.IsValidElement(svg2); }
public void Export_TestPlot_ValidSvgString() { var plotModel = new PlotModel("Test plot"); const string FileName = "SvgExporterTests_Plot1.svg"; plotModel.Series.Add(new FunctionSeries(Math.Sin, 0, Math.PI * 8, 200, "Math.Sin")); using (var s = File.Create(FileName)) { var rc = new ShapesRenderContext(null); SvgExporter.Export(plotModel, s, 800, 500, true, rc); } SvgAssert.IsValidFile(FileName); }
public void SaveSvg() { var path = this.GetFilename(".svg files|*.svg", ".svg"); if (path != null) { // Using a WPF render context to measure the text var textMeasurer = new ShapesRenderContext(new Canvas()); using (var s = File.Create(path)) { SvgExporter.Export(this.Model, s, this.Plot.ActualWidth, this.Plot.ActualHeight, true, textMeasurer); } OpenContainingFolder(path); } }
public void Export_AllExamplesInExampleLibrary_CheckThatAllFilesExist() { const string DestinationDirectory = "SvgExporterTests_ExampleLibrary"; if (!Directory.Exists(DestinationDirectory)) { Directory.CreateDirectory(DestinationDirectory); } foreach (var example in Examples.GetList()) { var path = Path.Combine(DestinationDirectory, StringHelper.CreateValidFileName(example.Category + " - " + example.Title, ".svg")); using (var s = File.Create(path)) { var rc = new ShapesRenderContext(null); SvgExporter.Export(example.PlotModel, s, 800, 500, true, rc); } Assert.IsTrue(File.Exists(path)); } }
private void SavePlot_Click(object sender, RoutedEventArgs e) { var dlg = new SaveFileDialog { Filter = ".svg files|*.svg|.png files|*.png|.pdf files|*.pdf|.xaml files|*.xaml", DefaultExt = ".svg" }; if (dlg.ShowDialog(this).Value) { var ext = Path.GetExtension(dlg.FileName).ToLower(); switch (ext) { case ".png": plot1.SaveBitmap(dlg.FileName, 0, 0, OxyColors.Automatic); break; case ".svg": var rc = new ShapesRenderContext(null); var svg = OxyPlot.SvgExporter.ExportToString(this.vm.Model, plot1.ActualWidth, plot1.ActualHeight, false, rc); File.WriteAllText(dlg.FileName, svg); break; case ".pdf": using (var s = File.Create(dlg.FileName)) { PdfExporter.Export(vm.Model, s, plot1.ActualWidth, plot1.ActualHeight); } break; case ".xaml": plot1.SaveXaml(dlg.FileName); break; } this.OpenContainingFolder(dlg.FileName); } }
/// <summary> /// Prints the specified plot model. /// </summary> /// <param name="model">The model.</param> public void Print(IPlotModel model) { PrintDocumentImageableArea area = null; var xpsDocumentWriter = PrintQueue.CreateXpsDocumentWriter(ref area); if (xpsDocumentWriter != null) { var width = this.Width; var height = this.Height; if (double.IsNaN(width)) { width = area.MediaSizeWidth; } if (double.IsNaN(height)) { height = area.MediaSizeHeight; } var canvas = new Canvas { Width = width, Height = height, Background = this.Background.ToBrush() }; canvas.Measure(new Size(width, height)); canvas.Arrange(new Rect(0, 0, width, height)); var rc = new ShapesRenderContext(canvas) { TextFormattingMode = this.TextFormattingMode }; model.Update(true); model.Render(rc, width, height); canvas.UpdateLayout(); xpsDocumentWriter.Write(canvas); } }
private void CopySvg_Click(object sender, RoutedEventArgs e) { var rc = new ShapesRenderContext(null); Clipboard.SetText(vm.Model.ToSvg(plot1.ActualWidth, plot1.ActualHeight, true, rc)); }
/// <summary> /// The to file. /// </summary> /// <param name="chart"> /// The chart. /// </param> public static void ToFile(PlotModel chart) { var sfd = new SaveFileDialog(); sfd.CheckFileExists = false; sfd.CheckPathExists = true; sfd.DefaultExt = ".png"; sfd.AddExtension = true; sfd.Filter = Labels.GraphicFilesFilter; sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); sfd.Title = Labels.GraphicFilesSaveFileDialogTitle; if (sfd.ShowDialog().GetValueOrDefault()) { using (var stream = new FileStream(sfd.FileName, FileMode.Create)) { BitmapEncoder encoder = null; string extension = Path.GetExtension(sfd.FileName); var enc = new ASCIIEncoding(); if (extension != null) { switch (extension.ToLower()) { case ".bmp": encoder = new BmpBitmapEncoder(); break; case ".png": encoder = new PngBitmapEncoder(); break; case ".gif": encoder = new GifBitmapEncoder(); break; case ".tif": encoder = new TiffBitmapEncoder(); break; case ".wmp": encoder = new WmpBitmapEncoder(); break; case ".svg": var rc = new ShapesRenderContext(null); var svg = OxyPlot.SvgExporter.ExportToString(chart, chart.Width, chart.Height, true, rc); stream.Write(enc.GetBytes(svg), 0, svg.Length); return; case ".xaml": var xaml = XamlExporter.ExportToString(chart, chart.Width, chart.Height, OxyColor.FromArgb(255, 255, 255, 255)); stream.Write(enc.GetBytes(xaml), 0, xaml.Length); return; case ".jpg": default: encoder = new JpegBitmapEncoder(); break; } } var bitmap = PngExporter.ExportToBitmap(chart, (int)chart.Width, (int)chart.Height, OxyColor.FromArgb(255, 255, 255, 255)); // Save to file if (encoder != null) { encoder.Frames.Add(BitmapFrame.Create(bitmap)); encoder.Save(stream); } } } }
public void CopySvg() { var rc = new ShapesRenderContext(null); Clipboard.SetText(this.Model.ToSvg(this.Plot.ActualWidth, this.Plot.ActualHeight, true, rc)); }