wpf Oxyplot:如何设置简单的柱状图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20775716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Oxyplot: how to set up a simple column chart
提问by Monty Burns
I have to implement a simple Column Chart output in a WPF project. I have chosen the OxyPlot Library for it. Design Pattern is of course MVVM. The relevant source code parts can be seen below. What I get, when I run the project is an empty chart with categories 1 thru 5 on the x axis (which is correct) and values 0 thru 100 on the y axis (which is correct also, as i am supposed to display percentages).
我必须在 WPF 项目中实现一个简单的柱状图输出。我为它选择了 OxyPlot 库。设计模式当然是 MVVM。相关的源代码部分可以在下面看到。当我运行项目时,我得到的是一个空图表,x 轴上的类别为 1 到 5(这是正确的),y 轴上的值为 0 到 100(这也是正确的,因为我应该显示百分比) .
The data collections (named "difficulties" for the category axis and "percentages" for the values axis" are correctly filled with values, I′ve checked that one.
数据集合(类别轴命名为“难度”和值轴命名为“百分比”)正确填充了值,我已经检查过该值。
But there are no columns displayed. So I wonder what I am doing wrong. I have built my example according to this oxyplot demoand based on an example we got presented at the wpf class at university.
但是没有显示列。所以我想知道我做错了什么。我根据这个 oxyplot 演示构建了我的示例,并基于我们在大学 wpf 课程中展示的示例。
Any suggestions?
有什么建议?
Regards Roland
问候罗兰
using System;
using System.ComponentModel;
using System.Linq.Expressions;
namespace GeoCaching.Wpf.ViewModels
{
public abstract class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
protected void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
Console.WriteLine("PropertyChangedEventArgs called " + propertyName);
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
the Statistics Model itself goes here:
统计模型本身在这里:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GeoCaching.BL;
using GeoCaching.BL.Interfaces;
using GeoCaching.BL.Factories;
using GeoCaching.DAL.Common.Domain;
using GeoCaching.Wpf.ViewModels;
using OxyPlot;
using OxyPlot.Wpf;
using OxyPlot.Annotations;
using OxyPlot.Axes;
namespace GeoCaching.Wpf.ViewModels
{
public class StatisticsVM : ViewModelBase
{
private IStatisticsMgr statManager;
Dictionary<int, double> testList;
List<int> difficulties;
List<double> percentages;
public StatisticsVM()
{
PlotModel = new PlotModel();
this.difficulties = new List<int>();
this.percentages = new List<double>();
LoadData();
SetUpModel();
}
private PlotModel plotModel;
public PlotModel PlotModel
{
get { return plotModel; }
set { plotModel = value; OnPropertyChanged("PlotModel"); }
}
private void SetUpModel()
{
var temp = new PlotModel("difficulties distribution");
OxyPlot.Axes.CategoryAxis catAxis = new OxyPlot.Axes.CategoryAxis(AxisPosition.Bottom);
OxyPlot.Axes.LinearAxis valAxis = new OxyPlot.Axes.LinearAxis(AxisPosition.Left, 0, 100);
valAxis.MinimumPadding = 0;
valAxis.AbsoluteMinimum = 0;
OxyPlot.Series.ColumnSeries cs = new OxyPlot.Series.ColumnSeries();
cs.ItemsSource = percentages;
temp.Axes.Add(catAxis);
temp.Axes.Add(valAxis);
temp.Series.Add(cs);
PlotModel = temp;
PlotModel.RefreshPlot(true);
}
//fetch statistics data from
//database
private void LoadData()
{
statManager = StatisticsMgrFactory.GetStatisticsManager();
testList = new Dictionary<int, double>();
testList = statManager.GroupByDifficulty();
//extract keys and values
//for statistical display on axes
foreach (KeyValuePair<int,double> item in testList)
{
difficulties.Add(item.Key);
percentages.Add(item.Value);
}
}
}
}
the code behind the xaml window:
xaml 窗口背后的代码:
using GeoCaching.Wpf.ViewModels;
namespace GeoCaching.Wpf
{
/// <summary>
/// Interaction logic for ChartTest.xaml
/// </summary>
public partial class ChartTest : Window
{
public ChartTest()
{
InitializeComponent();
this.DataContext = new StatisticsVM();
}
}
}
and the xaml itself:
和 xaml 本身:
<Window x:Class="GeoCaching.Wpf.ChartTest"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:oxy="http://oxyplot.codeplex.com"
Title="ChartTest" Height="300" Width="300">
<Grid>
<oxy:Plot Title="Bar series" LegendPlacement="Outside" LegendPosition="RightTop" LegendOrientation="Vertical" Model="{Binding PlotModel}">
</oxy:Plot>
</Grid>
</Window>
回答by KidCode
I believe your issue is coming from within the SetupModelmethod, specifically, this line:
我相信您的问题来自SetupModel方法内部,特别是这一行:
cs.ItemsSource = percentages;
I've been working with oxyPlot column charts and I've never been able to get a chart to work if I set the ItemSourceproperty. Instead, I have to add a new ColumnItem()for every item within my item source.
我一直在使用 oxyPlot 柱状图,但如果我设置了该ItemSource属性,我就永远无法使图表工作。相反,我必须为new ColumnItem()我的项目源中的每个项目添加一个。
Example:
例子:
foreach (double pc in percentages)
{
catAxis.ActualLabels.Add (/*Add your difficulty labels here for each column*/);
cs.Items.Add (new ColumnItem () { Value = pc });
}
I know this question is quite old, but I thought id share this for anyone else who's having trouble. (Feel free to update my answer if you know why using the actual ItemSourceproperty doesn't work!)
我知道这个问题已经很老了,但我认为我可以将这个问题分享给遇到问题的其他人。(如果您知道为什么使用实际ItemSource属性不起作用,请随时更新我的答案!)

