在WPF中使用ScottPlot

huangapple go评论62阅读模式
英文:

Using ScottPlot in WPF

问题

我对WPF和数据绑定相当新。我正在使用WPF用户控件创建使用ScottPlot的条形图。在研究了ScottPlot网站之后,我仍然无法将我的图表与后台代码绑定起来。
我将ScottPlot图表导入了我的UC并命名为chartWPF。现在我有一些后台代码来自定义图表数据。
如何将这两者连接起来?

这是我的后台代码:

public UReport()
{
    InitializeComponent();

    var plt = new ScottPlot.Plot(600, 400);
                    
    double[] values = { 26, 20, 23, 7, 16 };
    double[] positions = { 0, 1, 2, 3, 4 };
    string[] labels = { "PHP", "JS", "C++", "GO", "VB" };
    plt.AddBar(values, positions);
    plt.XTicks(positions, labels);
    plt.SetAxisLimits(yMin: 0);
    plt.SaveFig("bar_labels.png");
}

这是我的UC XAML:

<ScottPlot:WpfPlot x:Name="chartWpf"/>
英文:

I'm quite new with WPF and data binding. I am creating a bar chart using ScottPlot in WPF User Control. After studying the ScottPlot website I am still unable to bind my chart to the code behind.
I imported the ScottPlot chart into my UC and named it chartWPF. Now I have some code behind customizing the chart data.
How can I connect these two together?

This is what i have as the code behind:

public UReport()
    {
        InitializeComponent();

        var plt = new ScottPlot.Plot(600, 400);
                    
        double[] values = { 26, 20, 23, 7, 16 };
        double[] positions = { 0, 1, 2, 3, 4 };
        string[] labels = { &quot;PHP&quot;, &quot;JS&quot;, &quot;C++&quot;, &quot;GO&quot;, &quot;VB&quot; };
        plt.AddBar(values, positions);
        plt.XTicks(positions, labels);
        plt.SetAxisLimits(yMin: 0);
        plt.SaveFig(&quot;bar_labels.png&quot;);

     
    }

and this is in my UC XAML:

&lt;ScottPlot:WpfPlot x:Name=&quot;chartWpf&quot;/&gt;

答案1

得分: 0

Your code doesn't add a bar chart to the WpfPlot; it creates an uninteractive plot and adds the bar chart to that. You can access the plot object of the WpfPlot with chartWpf.Plot, so your code becomes something like this:

public UReport() { 
    InitializeComponent();                    
    double[] values = { 26, 20, 23, 7, 16 };
    double[] positions = { 0, 1, 2, 3, 4 };
    string[] labels = { "PHP", "JS", "C++", "GO", "VB" };

    chartWpf.Plot.AddBar(values, positions);
    chartWpf.Plot.XTicks(positions, labels);
    chartWpf.Plot.SetAxisLimits(yMin: 0);
    chartWpf.Plot.SaveFig("bar_labels.png");     
}
英文:

Your code doesn't add a bar chart to the WpfPlot, it creates an uninteractive plot and adds the bar chart to that. You can access the plot object of the WpfPlot with chartWpf.Plot, so your code becomes something like this:

public UReport() { 
    InitializeComponent();                    
    double[] values = { 26, 20, 23, 7, 16 };
    double[] positions = { 0, 1, 2, 3, 4 };
    string[] labels = { &quot;PHP&quot;, &quot;JS&quot;, &quot;C++&quot;, &quot;GO&quot;, &quot;VB&quot; };

    chartWpf.Plot.AddBar(values, positions);
    chartWpf.Plot.XTicks(positions, labels);
    chartWpf.Plot.SetAxisLimits(yMin: 0);
    chartWpf.Plot.SaveFig(&quot;bar_labels.png&quot;);     
}

huangapple
  • 本文由 发表于 2023年5月22日 23:14:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76307611.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定