OxyPlot导出到PNG

时间:2018-04-06 08:36:16

标签: wpf charts save oxyplot

我试图将图表导出到PNG。那是我的代码:

Chart.xaml

<Window x:Class="P1.Chart"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:oxy="http://oxyplot.org/wpf"
    xmlns:local="clr-namespace:P1"
    Title="Example 1 (WPF)" Height="800" Width="800"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">

<Grid>
    <oxy:Plot>
        <oxy:Plot.Axes>
            <oxy:LinearAxis Position="Bottom" Minimum="-20" Maximum="20" />
            <oxy:LinearAxis Position="Left" Minimum="-20" Maximum="20" />
        </oxy:Plot.Axes>

        <oxy:Plot.Series>
            <oxy:LineSeries ItemsSource="{Binding Points1}" LineStyle="None"  MarkerType="Circle" MarkerSize="3" MarkerFill="Red" Title="Point 1"/>
            <oxy:LineSeries ItemsSource="{Binding Points2}" LineStyle="None"  MarkerType="Circle" MarkerSize="3" MarkerFill="Green" Title="Point 2"/>
            <oxy:LineSeries ItemsSource="{Binding Points3}" LineStyle="None"  MarkerType="Circle" MarkerSize="3" MarkerFill="Blue" Title="Point 3"/>   
        </oxy:Plot.Series>
    </oxy:Plot>
</Grid>

Chart.xaml.cs

    namespace P1
{
    public partial class Chart: Window
    {
        public IList<DataPoint> Points1 { get;  set; }
        public IList<DataPoint> Points2 { get; set; }
        public IList<DataPoint> Points3 { get; set; }

    public Chart(Learn x1)
    {      

        this.Points1 = new List<DataPoint>
        {
            new DataPoint(Convert.ToDouble(a1),a2),
        };

        this.Points2 = new List<DataPoint>
        {
            new DataPoint(b1,b2),
        };

        this.Points3 = new List<DataPoint>
        {
            new DataPoint(c1,c2),
        };

        InitializeComponent();
    }

是否可以使用以下代码在PNG中保存图表窗口?

    var pngExporter = new PngExporter { Width = 600, Height = 400, Background = OxyColors.White };
    pngExporter.ExportToFile(plotModel, fileName);

我没有plotmodel因为我在xaml中绑定所有内容,你有任何建议吗?

1 个答案:

答案 0 :(得分:4)

如果您想从代码中导出,可以使用:

<oxy:Plot x:Name="Plot">
PngExporter.Export(this.Plot.ActualModel, fileName, 600, 400, OxyColors.White)