Oxyplot:如何设置简单的柱形图

 PearlLisa_Shanghai_901 发布于 2023-02-08 10:47

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

数据集合(类别轴的"难点"和值轴的"百分比")正确填充了值,我检查了一个.

但是没有显示任何列.所以我想知道我做错了什么.我根据这个oxyplot演示构建了我的示例,并基于我们在大学的wpf课程中提供的示例.

有什么建议?

关心罗兰

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));
            }
        }
    }
}

统计模型本身就在这里:

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 testList;
        List difficulties;
        List percentages;

        public StatisticsVM()
        {
            PlotModel = new PlotModel();
            this.difficulties = new List();
            this.percentages = new List();
            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();

            testList = statManager.GroupByDifficulty();

            //extract keys and values
            //for statistical display on axes
            foreach (KeyValuePair item in testList)
            {
                difficulties.Add(item.Key);
                percentages.Add(item.Value);
            }
        }
    }
}

xaml窗口背后的代码:

using GeoCaching.Wpf.ViewModels;

namespace GeoCaching.Wpf
{
    /// 
    /// Interaction logic for ChartTest.xaml
    /// 
    public partial class ChartTest : Window
    {
        public ChartTest()
        {
            InitializeComponent();
            this.DataContext = new StatisticsVM();
        }
    }
}

和xaml本身:


    


        

        
    

KidCode.. 6

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

cs.ItemsSource = percentages; 

我一直在使用oxyPlot柱形图,如果我设置了ItemSource属性,我就无法获得图表.相反,我必须为new ColumnItem()我的项目源中的每个项目添加一个.

例:

foreach (double pc in percentages)
{
    catAxis.ActualLabels.Add (/*Add your difficulty labels here for each column*/);
    cs.Items.Add (new ColumnItem () { Value = pc });
}

我知道这个问题已经很老了,但我认为id会为遇到麻烦的其他人分享这个问题.(如果您知道为什么使用实际ItemSource属性不起作用,请随时更新我的​​答案!)

1 个回答
  • 我相信你的问题来自SetupModel方法,特别是这一行:

    cs.ItemsSource = percentages; 
    

    我一直在使用oxyPlot柱形图,如果我设置了ItemSource属性,我就无法获得图表.相反,我必须为new ColumnItem()我的项目源中的每个项目添加一个.

    例:

    foreach (double pc in percentages)
    {
        catAxis.ActualLabels.Add (/*Add your difficulty labels here for each column*/);
        cs.Items.Add (new ColumnItem () { Value = pc });
    }
    

    我知道这个问题已经很老了,但我认为id会为遇到麻烦的其他人分享这个问题.(如果您知道为什么使用实际ItemSource属性不起作用,请随时更新我的​​答案!)

    2023-02-08 10:50 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有