首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Oxyplot:如何设置简单的柱状图

Oxyplot:如何设置简单的柱状图
EN

Stack Overflow用户
提问于 2013-12-26 02:39:51
回答 1查看 8.2K关注 0票数 3

我必须在WPF项目中实现一个简单的柱状图输出。我为它选择了OxyPlot库。设计模式当然是MVVM。相关的源代码部分如下所示。当我运行这个项目时,我得到的是一个空图表,在x轴上有类别1到5(这是正确的),在y轴上有0到100的值(这也是正确的,因为我应该显示百分比)。

数据集合(类别轴的名称为“困难”,值轴的名称为“百分比”)“正确填充了值,我已经检查过了。

但是没有显示任何列。所以我想知道我做错了什么。我根据this oxyplot demo构建了我的示例,并基于我们在大学的wpf课堂上展示的一个示例。

有什么建议吗?

问候罗兰

代码语言:javascript
复制
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));
            }
        }
    }
}

统计模型本身如下所示:

代码语言:javascript
复制
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);
            }
        }
    }
}

xaml窗口背后的代码:

代码语言:javascript
复制
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();
        }
    }
}

和xaml本身:

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

发布于 2015-11-01 16:28:56

我相信你的问题来自于SetupModel方法,特别是这一行:

代码语言:javascript
复制
cs.ItemsSource = percentages; 

我一直在处理oxyPlot柱状图,如果我设置了ItemSource属性,我就永远不能让图表正常工作。相反,我必须为我的项源中的每个项添加一个new ColumnItem()

示例:

代码语言:javascript
复制
foreach (double pc in percentages)
{
    catAxis.ActualLabels.Add (/*Add your difficulty labels here for each column*/);
    cs.Items.Add (new ColumnItem () { Value = pc });
}

我知道这个问题很老了,但我想我会把这个分享给其他有困难的人。(如果您知道为什么使用实际的ItemSource属性不起作用,请随时更新我的答案!)

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20775716

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档