英文:
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 = { "PHP", "JS", "C++", "GO", "VB" };
plt.AddBar(values, positions);
plt.XTicks(positions, labels);
plt.SetAxisLimits(yMin: 0);
plt.SaveFig("bar_labels.png");
}
and this is in my UC XAML:
<ScottPlot:WpfPlot x:Name="chartWpf"/>
答案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 = { "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");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论