/// <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); #if !NET35 rc.TextFormattingMode = TextFormattingMode.Ideal; #endif model.Update(true); model.Render(rc, width, height); canvas.UpdateLayout(); var xpsdw = XpsDocument.CreateXpsDocumentWriter(doc); xpsdw.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) + ")"); }
/// <summary> /// Exports the specified plot model to a bitmap. /// </summary> /// <param name="model">The plot model.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="background">The background.</param> /// <param name="dpi">The resolution.</param> /// <returns>A bitmap.</returns> public static BitmapSource ExportToBitmap(PlotModel model, int width, int height, OxyColor background = null, int dpi = 96) { 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) { RendersToScreen = false }; model.Update(); model.Render(rc, width, height); canvas.UpdateLayout(); var bmp = new RenderTargetBitmap(width, height, dpi, dpi, PixelFormats.Pbgra32); bmp.Render(canvas); return(bmp); // alternative implementation: // http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.rendertargetbitmap.aspx // var dv = new DrawingVisual(); // using (var ctx = dv.RenderOpen()) // { // var vb = new VisualBrush(canvas); // ctx.DrawRectangle(vb, null, new Rect(new Point(), new Size(width, height))); // } // bmp.Render(dv); }
/// <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(PlotModel model, double width, double height, OxyColor background = null) { var g = new Grid(); if (background != null) { g.Background = background.ToBrush(); } var c = new Canvas(); g.Children.Add(c); var size = new Size(width, height); g.Measure(size); g.Arrange(new Rect(0, 0, width, height)); g.UpdateLayout(); var rc = new ShapesRenderContext(c) { UseStreamGeometry = false }; model.Update(); model.Render(rc, width, height); var sb = new StringBuilder(); using (var sw = new StringWriter(sb)) { var xw = XmlWriter.Create(sw, new XmlWriterSettings { Indent = true }); XamlWriter.Save(c, xw); } return sb.ToString(); }
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> /// Exports the specified plot model to a bitmap. /// </summary> /// <param name="model">The model to export.</param> /// <returns>A bitmap.</returns> public BitmapSource ExportToBitmap(IPlotModel model) { var scale = 96d / this.Resolution; var canvas = new Canvas { Width = this.Width * scale, Height = this.Height * scale, Background = this.Background.ToBrush() }; canvas.Measure(new Size(canvas.Width, canvas.Height)); canvas.Arrange(new Rect(0, 0, canvas.Width, canvas.Height)); var rc = new ShapesRenderContext(canvas) { RendersToScreen = false, TextFormattingMode = TextFormattingMode.Ideal }; model.Update(true); model.Render(rc, canvas.Width, canvas.Height); canvas.UpdateLayout(); var bmp = new RenderTargetBitmap(this.Width, this.Height, this.Resolution, this.Resolution, PixelFormats.Pbgra32); bmp.Render(canvas); return(bmp); // alternative implementation: // http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.rendertargetbitmap.aspx // var dv = new DrawingVisual(); // using (var ctx = dv.RenderOpen()) // { // var vb = new VisualBrush(canvas); // ctx.DrawRectangle(vb, null, new Rect(new Point(), new Size(width, height))); // } // bmp.Render(dv); }
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 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 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 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); }
/// <summary> /// When overridden in a derived class, is invoked whenever application code or internal processes call <see cref="M:System.Windows.FrameworkElement.ApplyTemplate" /> . /// </summary> public override void OnApplyTemplate() { base.OnApplyTemplate(); this.grid = this.GetTemplateChild(PartGrid) as Grid; if (this.grid == null) { return; } this.canvas = new Canvas(); this.grid.Children.Add(this.canvas); this.canvas.UpdateLayout(); this.renderContext = new ShapesRenderContext(this.canvas); this.overlays = new Canvas(); this.grid.Children.Add(this.overlays); this.zoomControl = new ContentControl(); this.overlays.Children.Add(this.zoomControl); this.CommandBindings.Add(new CommandBinding(PlotCommands.ResetAxes, (s, e) => this.ResetAllAxes())); }
/// <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 g = new Grid(); if (background.IsVisible()) { g.Background = background.ToBrush(); } var c = new Canvas(); g.Children.Add(c); var size = new Size(width, height); g.Measure(size); g.Arrange(new Rect(0, 0, width, height)); g.UpdateLayout(); var rc = new ShapesRenderContext(c) { UseStreamGeometry = false }; model.Update(true); model.Render(rc, width, height); var sb = new StringBuilder(); using (var sw = new StringWriter(sb)) { var xw = XmlWriter.Create(sw, new XmlWriterSettings { Indent = true }); XamlWriter.Save(c, xw); } return(sb.ToString()); }
private void UpdateVisuals() { if (canvas != null) { canvas.Children.Clear(); } if (internalModel == null) { return; } if (Title != null) { internalModel.Title = Title; } if (Subtitle != null) { internalModel.Subtitle = Subtitle; } if (canvas != null) { var wrc = new ShapesRenderContext(canvas); internalModel.Render(wrc); } if (plotFrame != null) { plotFrame.Model = internalModel; plotAliasedFrame.Model = internalModel; plotFrame.InvalidateVisual(); plotAliasedFrame.InvalidateVisual(); } }
private void UpdateVisuals() { if (canvas != null) canvas.Children.Clear(); if (internalModel == null) return; if (Title != null) internalModel.Title = Title; if (Subtitle != null) internalModel.Subtitle = Subtitle; if (canvas != null) { var wrc = new ShapesRenderContext(canvas); internalModel.Render(wrc); } if (plotFrame != null) { plotFrame.Model = internalModel; plotAliasedFrame.Model = internalModel; plotFrame.InvalidateVisual(); plotAliasedFrame.InvalidateVisual(); } }
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)); } }
/// <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); #if !NET35 rc.TextFormattingMode = this.TextFormattingMode; #endif model.Update(true); model.Render(rc, width, height); canvas.UpdateLayout(); xpsDocumentWriter.Write(canvas); } }
/// <summary> /// Exports the specified plot model to a bitmap. /// </summary> /// <param name="model">The model to export.</param> /// <returns>A bitmap.</returns> public BitmapSource ExportToBitmap(IPlotModel model) { var scale = 96d / this.Resolution; var canvas = new Canvas { Width = this.Width * scale, Height = this.Height * scale, Background = this.Background.ToBrush() }; canvas.Measure(new Size(canvas.Width, canvas.Height)); canvas.Arrange(new Rect(0, 0, canvas.Width, canvas.Height)); var rc = new ShapesRenderContext(canvas) { RendersToScreen = false }; #if !NET35 rc.TextFormattingMode = TextFormattingMode.Ideal; #endif model.Update(true); model.Render(rc, canvas.Width, canvas.Height); canvas.UpdateLayout(); var bmp = new RenderTargetBitmap(this.Width, this.Height, this.Resolution, this.Resolution, PixelFormats.Pbgra32); bmp.Render(canvas); return bmp; // alternative implementation: // http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.rendertargetbitmap.aspx // var dv = new DrawingVisual(); // using (var ctx = dv.RenderOpen()) // { // var vb = new VisualBrush(canvas); // ctx.DrawRectangle(vb, null, new Rect(new Point(), new Size(width, height))); // } // bmp.Render(dv); }
/// <summary> /// Exports the specified plot model to a bitmap. /// </summary> /// <param name="model">The plot model.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="background">The background.</param> /// <param name="dpi">The resolution.</param> /// <returns>A bitmap.</returns> public static BitmapSource ExportToBitmap(PlotModel model, int width, int height, OxyColor background = null, int dpi = 96) { 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) { RendersToScreen = false }; model.Update(); model.Render(rc, width, height); canvas.UpdateLayout(); var bmp = new RenderTargetBitmap(width, height, dpi, dpi, PixelFormats.Pbgra32); bmp.Render(canvas); return bmp; // alternative implementation: // http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.rendertargetbitmap.aspx // var dv = new DrawingVisual(); // using (var ctx = dv.RenderOpen()) // { // var vb = new VisualBrush(canvas); // ctx.DrawRectangle(vb, null, new Rect(new Point(), new Size(width, height))); // } // bmp.Render(dv); }
/// <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); #if !NET35 rc.TextFormattingMode = this.TextFormattingMode; #endif model.Update(true); model.Render(rc, this.Width, this.Height); canvas.UpdateLayout(); var xpsdw = XpsDocument.CreateXpsDocumentWriter(doc); xpsdw.Write(canvas); } } }
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); } }
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 CopySvg() { var rc = new ShapesRenderContext(null); Clipboard.SetText(this.Model.ToSvg(this.Plot.ActualWidth, this.Plot.ActualHeight, true, rc)); }
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); } }
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> /// When overridden in a derived class, is invoked whenever application code or internal processes call <see cref="M:System.Windows.FrameworkElement.ApplyTemplate"/> . /// </summary> public override void OnApplyTemplate() { base.OnApplyTemplate(); this.grid = this.GetTemplateChild(PartGrid) as Grid; if (this.grid == null) { return; } this.canvas = new Canvas(); this.grid.Children.Add(this.canvas); this.canvas.UpdateLayout(); this.renderContext = new ShapesRenderContext(this.canvas); this.overlays = new Canvas(); this.grid.Children.Add(this.overlays); this.zoomControl = new ContentControl(); this.overlays.Children.Add(this.zoomControl); this.CommandBindings.Add(new CommandBinding(ResetAxesCommand, this.ResetAxesHandler)); var resetAxesInputBinding = new InputBindingX { Command = ResetAxesCommand }; BindingOperations.SetBinding( resetAxesInputBinding, InputBindingX.GeztureProperty, new Binding("ResetAxesGesture") { Source = this }); this.InputBindings.Add(resetAxesInputBinding); }
/// <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); } }
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); }