diff --git a/PortfolioManager/ViewModels/PlotterWorkspaceViewModel.cs b/PortfolioManager/ViewModels/PlotterWorkspaceViewModel.cs new file mode 100644 index 0000000..0c677b8 --- /dev/null +++ b/PortfolioManager/ViewModels/PlotterWorkspaceViewModel.cs @@ -0,0 +1,21 @@ +using System; +using ScottPlot.Avalonia; + +namespace PortfolioManager.ViewModels +{ + public class PlotterLoadedEventArgs : EventArgs + { + public AvaPlot AvaPlot { get; set; } = default; + } + + public abstract class PlotterWorkspaceViewModel : WorkspaceViewModel + { + public EventHandler OnPlotterLoadedEventHandler; + + public void OnPlotterLoaded(AvaPlot avaPlot) + { + EventHandler handler = this.OnPlotterLoadedEventHandler; + if (null != handler) handler(this, new PlotterLoadedEventArgs() { AvaPlot = avaPlot }); + } + } +} \ No newline at end of file diff --git a/PortfolioManager/ViewModels/ScottPlotViewModel.cs b/PortfolioManager/ViewModels/ScottPlotViewModel.cs index dad88b1..facab7a 100644 --- a/PortfolioManager/ViewModels/ScottPlotViewModel.cs +++ b/PortfolioManager/ViewModels/ScottPlotViewModel.cs @@ -1,26 +1,47 @@ +using System; +using MarketData.DataAccess; +using MarketData.MarketDataModel; using ScottPlot.Avalonia; +using ScottPlot.Plottables; +using ShimSkiaSharp; +using SkiaSharp; namespace PortfolioManager.ViewModels { - public partial class ScottPlotViewModel : WorkspaceViewModel + + + + public partial class ScottPlotViewModel : PlotterWorkspaceViewModel { + private AvaPlot plotter = default; public ScottPlotViewModel() { - double[] dataX = { 1, 2, 3, 4, 5 }; - double[] dataY = { 1, 4, 9, 16, 25 }; - - // AvaPlot avaPlot = this.Find("AvaPlot"); - // ((ScottPlotViewModel)DataContext).Plotter = avaPlot; - - - if (Plotter != null) - { - Plotter.Plot.Add.Scatter(dataX, dataY); - Plotter.Refresh(); - } + OnPlotterLoadedEventHandler += PlotterLoadedEvent; } + public void PlotterLoadedEvent(object sender, PlotterLoadedEventArgs e) + { + Prices prices = PricingDA.GetPrices("SPY", 180); + DateTime[] dates = new DateTime[prices.Count]; + double[] closes = new double[prices.Count]; + for (int index = 0; index < prices.Count; index++) + { + dates[index] = prices[index].Date; + closes[index] = prices[index].Close; + } + + + plotter = e.AvaPlot; + // double[] dataX = { 1, 2, 3, 4, 5 }; + // double[] dataY = { 1, 4, 9, 16, 25 }; + // plotter.Plot.Add.Scatter(dataX, dataY); + Scatter closeScatter = plotter.Plot.Add.ScatterLine(dates, closes, ScottPlot.Color.FromSKColor(SKColors.Red)); + closeScatter.LegendText = "Close"; + closeScatter.LineWidth = 3; + plotter.Plot.Axes.AutoScale(); + plotter.Refresh(); + } // ********************************************** P E R S I S T E N C E ************************* public override bool CanPersist() { @@ -35,10 +56,5 @@ namespace PortfolioManager.ViewModels public override void SetSaveParameters(SaveParameters saveParameters) { } - - // ********************************************************************************************** - - public AvaPlot Plotter { get; set; } = default; - } } \ No newline at end of file diff --git a/PortfolioManager/Views/ScottPlotView.axaml b/PortfolioManager/Views/ScottPlotView.axaml index f7044fc..4396bdb 100644 --- a/PortfolioManager/Views/ScottPlotView.axaml +++ b/PortfolioManager/Views/ScottPlotView.axaml @@ -11,5 +11,5 @@ mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:DataType="vm:ScottPlotViewModel" x:Class="PortfolioManager.Views.ScottPlotView"> - + diff --git a/PortfolioManager/Views/ScottPlotView.axaml.cs b/PortfolioManager/Views/ScottPlotView.axaml.cs index df3ae9a..1ca6712 100644 --- a/PortfolioManager/Views/ScottPlotView.axaml.cs +++ b/PortfolioManager/Views/ScottPlotView.axaml.cs @@ -10,16 +10,22 @@ namespace PortfolioManager.Views; public partial class ScottPlotView : UserControl { + private AvaPlot avaPlot = default; public ScottPlotView() { InitializeComponent(); - AvaPlot avaPlot = this.Find("AvaPlot"); - avaPlot.Loaded += OnLoadedEvent(this, null); } - protected void OnLoadedEvent(object sender, EventArgs args) + private void AvaPlot_Loaded(object sender, RoutedEventArgs e) { - // if (null == tabControl) return; - // tabControl.SelectedIndex = args.Index; + // This code will execute when the AvaPlot is loaded + if (sender is ScottPlot.Avalonia.AvaPlot) + { + if (default == avaPlot) + { + PlotterWorkspaceViewModel viewModel = (DataContext as PlotterWorkspaceViewModel); + viewModel.OnPlotterLoaded(this.Find("AvaPlot")); + } + } } } \ No newline at end of file