gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:31:15 25 4
gpt4 key购买 nike

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

数据集合(分类轴命名为“难度”,值(value)轴命名为“百分比”)正确填充了值,我检查了那个。

但是没有显示任何列。所以我想知道我做错了什么。我根据 this oxyplot demo 构建了示例并基于我们在大学 wpf 类(class)中展示的示例。

有什么建议吗?

问候罗兰

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<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 窗口背后的代码:

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 本身:

<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>

最佳答案

我相信您的问题出在 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 });
}

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

关于c# - Oxyplot:如何设置简单的柱形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20775716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com