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