It may be a little late, but lately I have been asking the same question: I need to build several rows dynamically (several yield curves based on user-selected currencies), but I do not want to directly link Plot using PlotModel , as others properties (e.g. Title ) should be set in my view model as code instead of XAML markup.
So, I defined the PlotModel resource as a resource, PlotModel it to the plot. And look at PlotModel when the view is loaded. Thanks to this approach, I can define visual materials (for example, Title, Axes, Legend, etc.) using the XAML markup, and also put the logic for generating the series in my view model code.
Not sure if this is a good way, but it solves my problem.
1) XAML - define a resource
<UserControl.Resources> <oxyPlot:PlotModel x:Key="TestPlotModel" Title="XXX Curves (Preview)" Subtitle="Scroll mousewheel to zoom; Right-drag to pan" LegendPlacement="Outside" LegendBorder="{x:Static Member=oxyPlot:OxyColors.Black}" > <oxyPlot:PlotModel.Axes> <axes:LinearAxis Title="Rate (%)" Position="Left" StartPosition="0" StringFormat="#.00000" MajorGridlineStyle="Solid" MajorGridlineColor="{x:Static Member=oxyPlot:OxyColors.LightGray}" MinorGridlineStyle="Dot" MinorGridlineColor="{x:Static Member=oxyPlot:OxyColors.LightGray}" > </axes:LinearAxis> <axes:LinearAxis Title="Maturity (Days)" Position="Bottom" StartPosition="0" MajorGridlineStyle="Solid" MajorGridlineColor="{x:Static Member=oxyPlot:OxyColors.LightGray}" > </axes:LinearAxis> </oxyPlot:PlotModel.Axes> </oxyPlot:PlotModel> </UserControl.Resources>
2) XAML - Plot
<oxy:Plot Grid.Row="1" Model="{Binding Source={StaticResource TestPlotModel}}"> </oxy:Plot>
3) View the model - get the model from view, but do not snap
protected override void OnViewLoaded(object view) { base.OnViewLoaded(view); this._model = (PlotModel)((XXXView)view).FindResource("TestPlotModel"); }
4) View Model - Create Multiple Series
_model.Series.Clear(); foreach (var currency in distinctCurrencies) { IEnumerable<DataPoint> dataPoints = ...; LineSeries lineSeries = new LineSeries() { Title = String.Format("*{0}", currency), ItemsSource = dataPoints }; _model.Series.Add(lineSeries); }
hope this helps!
Karl Cheng
source share