From f1621d626da72bf497438e67892f3a9622c8e93d Mon Sep 17 00:00:00 2001 From: arkein Date: Fri, 20 May 2016 00:50:49 +0300 Subject: [PATCH] new charting (balance). + new charting library (OxyPlot). + new balance chart (lability). --- .../Controls/BalanceReportControl.xaml | 13 ++++ .../Controls/BalanceReportControl.xaml.cs | 28 ++++++++ Hearthstone Treasury/Controls/MainWindow.xaml | 5 ++ .../Hearthstone Treasury.csproj | 16 +++++ .../ViewModels/BalanceReportViewModel.cs | 65 +++++++++++++++++++ .../ViewModels/MainWindowViewModel.cs | 4 ++ Hearthstone Treasury/packages.config | 2 + 7 files changed, 133 insertions(+) create mode 100644 Hearthstone Treasury/Controls/BalanceReportControl.xaml create mode 100644 Hearthstone Treasury/Controls/BalanceReportControl.xaml.cs create mode 100644 Hearthstone Treasury/ViewModels/BalanceReportViewModel.cs diff --git a/Hearthstone Treasury/Controls/BalanceReportControl.xaml b/Hearthstone Treasury/Controls/BalanceReportControl.xaml new file mode 100644 index 0000000..60760eb --- /dev/null +++ b/Hearthstone Treasury/Controls/BalanceReportControl.xaml @@ -0,0 +1,13 @@ + + + + + diff --git a/Hearthstone Treasury/Controls/BalanceReportControl.xaml.cs b/Hearthstone Treasury/Controls/BalanceReportControl.xaml.cs new file mode 100644 index 0000000..16f04e0 --- /dev/null +++ b/Hearthstone Treasury/Controls/BalanceReportControl.xaml.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace Hearthstone_Treasury.Controls +{ + /// + /// Interaction logic for BalanceReportControl.xaml + /// + public partial class BalanceReportControl : UserControl + { + public BalanceReportControl() + { + InitializeComponent(); + } + } +} diff --git a/Hearthstone Treasury/Controls/MainWindow.xaml b/Hearthstone Treasury/Controls/MainWindow.xaml index 8420ec5..81b14e5 100644 --- a/Hearthstone Treasury/Controls/MainWindow.xaml +++ b/Hearthstone Treasury/Controls/MainWindow.xaml @@ -48,6 +48,11 @@ + + + + + diff --git a/Hearthstone Treasury/Hearthstone Treasury.csproj b/Hearthstone Treasury/Hearthstone Treasury.csproj index d6542bc..c84f313 100644 --- a/Hearthstone Treasury/Hearthstone Treasury.csproj +++ b/Hearthstone Treasury/Hearthstone Treasury.csproj @@ -43,6 +43,14 @@ ..\packages\MahApps.Metro.1.1.2.0\lib\net45\MahApps.Metro.dll True + + ..\packages\OxyPlot.Wpf.2011.3.4094\lib\OxyPlot.dll + True + + + ..\packages\OxyPlot.Wpf.2011.3.4094\lib\OxyPlot.Wpf.dll + True + @@ -97,6 +105,9 @@ + + BalanceReportControl.xaml + DistributionChart.xaml @@ -130,6 +141,7 @@ + @@ -143,6 +155,10 @@ + + Designer + MSBuild:Compile + Designer MSBuild:Compile diff --git a/Hearthstone Treasury/ViewModels/BalanceReportViewModel.cs b/Hearthstone Treasury/ViewModels/BalanceReportViewModel.cs new file mode 100644 index 0000000..54c6e8c --- /dev/null +++ b/Hearthstone Treasury/ViewModels/BalanceReportViewModel.cs @@ -0,0 +1,65 @@ +using OxyPlot; +using ReactiveUI; +using ReactiveUI.Fody.Helpers; +using System; +using System.Linq; +using System.Reactive; +using System.Reactive.Linq; + +namespace Hearthstone_Treasury.ViewModels +{ + public class BalanceReportViewModel : ReactiveObject + { + private PluginSettingsViewModel _settings; + + private TransactionListViewModel _list; + + public BalanceReportViewModel(PluginSettingsViewModel settings, TransactionListViewModel list) + { + _settings = settings; + _list = list; + + BalanceLineModel = new PlotModel() + { + Axes = + { + new DateTimeAxis { IntervalType = DateTimeIntervalType.Days, StringFormat = "dd-MMM", Position = AxisPosition.Bottom }, + new LinearAxis { Position = AxisPosition.Left } + }, + }; + + Observable.Merge(_settings.WhenAnyValue(s => s.InitialBalance).Select(_ => Unit.Default), _list.Changed.Select(_ => Unit.Default)) + .Subscribe(_ => RefreshModel(BuildSeries())); + } + + private void RefreshModel(ISeries series) + { + BalanceLineModel.Series.Clear(); + BalanceLineModel.Series.Add(series); + BalanceLineModel.UpdateData(); + } + + private ISeries BuildSeries() + { + var series = new LineSeries(); + + int rollingBalance = _settings.InitialBalance; + var groupedTransactions = _list.Transactions.GroupBy(t => t.Moment.Date).OrderBy(g => g.Key); + + foreach (var transactionsGroup in groupedTransactions) + { + if (series.Points == null || series.Points.Count == 0) + { + series.Points.Add(DateTimeAxis.CreateDataPoint(transactionsGroup.Key.Subtract(TimeSpan.FromDays(1)), rollingBalance)); + } + rollingBalance += transactionsGroup.Sum(t => t.Difference); + series.Points.Add(DateTimeAxis.CreateDataPoint(transactionsGroup.Key, rollingBalance)); + } + + return series; + } + + [Reactive] + public PlotModel BalanceLineModel { get; set; } + } +} diff --git a/Hearthstone Treasury/ViewModels/MainWindowViewModel.cs b/Hearthstone Treasury/ViewModels/MainWindowViewModel.cs index 6d73a4e..884f1d0 100644 --- a/Hearthstone Treasury/ViewModels/MainWindowViewModel.cs +++ b/Hearthstone Treasury/ViewModels/MainWindowViewModel.cs @@ -12,6 +12,7 @@ public MainWindowViewModel(PluginSettingsViewModel settings, TransactionListView Settings = settings; TransactionList = transactionsList; Reports = new ReportControlViewModel(TransactionList); + BalanceReport = new BalanceReportViewModel(settings, TransactionList); Statistics = new StatisticsViewModel(settings, transactionsList); OptionsFlyoutOpen = ReactiveCommand.Create(this.WhenAnyValue(x => x.OptionsFlyoutState, state => !state)); @@ -29,6 +30,9 @@ public MainWindowViewModel(PluginSettingsViewModel settings, TransactionListView [Reactive] public ReportControlViewModel Reports { get; private set; } + [Reactive] + public BalanceReportViewModel BalanceReport { get; private set; } + [Reactive] public StatisticsViewModel Statistics { get; private set; } diff --git a/Hearthstone Treasury/packages.config b/Hearthstone Treasury/packages.config index 61af96a..107a89e 100644 --- a/Hearthstone Treasury/packages.config +++ b/Hearthstone Treasury/packages.config @@ -2,6 +2,8 @@ + +