Quantcast
Channel: OxyPlot (moved to GitHub)
Viewing all articles
Browse latest Browse all 2061

Commented Unassigned: Oxyplot not rendering using c# wpf [10202]

$
0
0
Hi-

I've added a Oxy Plot to a WPF Grid from code-behind using C#. The issue is, plot is not rendered in the UI, but there is a blank space instead. Please look at the code for the issues. It will be great if you post for a solution before the weekend, as we have to demo the chart on Monday. TIA!!

Here is the code-snippet-
```cs
using OxyPlot;
using OxyPlot.Series;
using OxyPlot.Axes;
using OxyPlot.Wpf;
using OxyPlot.Xps;
Private void CreateControlPlot()
{
Grid chartContentGrid = null;
Plot plot = new Plot { Height = 500, Width = 500 };

PlotModel plotModel = CreateControlLevelChart(1);

Binding plotBinding = new Binding();
plotBinding.Source = plotModel;
plot.SetBinding(Plot.ModelProperty, plotBinding);

plot.SetValue(Grid.RowProperty, 0);

chartContentGrid.Children.Add(new ContentPresenter { Content = plot });
}

private PlotModel CreateControlLevelChart(int level)
{
var controlPlotModel = new PlotModel();
controlPlotModel.LegendSymbolLength = 24;
controlPlotModel.Title = "Levey Jennings: Control Report - Level " + level;

// x-axis (Result Date)
var linearXaxis = new OxyPlot.Axes.DateTimeAxis();
linearXaxis.Position = AxisPosition.Bottom;
linearXaxis.Angle = 90;
controlPlotModel.Axes.Add(linearXaxis);

// y-axis (Concentration)
var linearYaxis = new OxyPlot.Axes.LinearAxis();
linearYaxis.Position = AxisPosition.Left;
controlPlotModel.Axes.Add(linearYaxis);

// add Result date vs concentration series
var concentrationSeries = new OxyPlot.Series.ScatterSeries();
concentrationSeries.MarkerFill = OxyColors.SkyBlue;
concentrationSeries.MarkerSize = 6;
concentrationSeries.MarkerStroke = OxyColors.Green;
concentrationSeries.MarkerStrokeThickness = 5;
concentrationSeries.MarkerType = MarkerType.Circle;
concentrationSeries.Title = "Result Date vs Concentration";

// add mean series
var meanSeries = new OxyPlot.Series.LineSeries();
meanSeries.Color = OxyColors.SkyBlue;
meanSeries.MarkerFill = OxyColors.Red;
meanSeries.MarkerSize = 6;
meanSeries.MarkerStroke = OxyColors.Yellow;
meanSeries.BrokenLineThickness = 3;
meanSeries.MarkerStrokeThickness = 5;
meanSeries.MarkerType = MarkerType.Circle;
meanSeries.Title = "Mean";

//add sd series
var sdSeries = new OxyPlot.Series.LineSeries();
sdSeries.Color = OxyColors.SkyBlue;
sdSeries.MarkerFill = OxyColors.Black;
sdSeries.MarkerSize = 6;
sdSeries.MarkerStroke = OxyColors.Green;
sdSeries.BrokenLineThickness = 3;
sdSeries.MarkerStrokeThickness = 5;
sdSeries.MarkerType = MarkerType.Circle;
sdSeries.Title = "Mean";

// add the points to the series
foreach (var data in _data)
{
concentrationSeries.Points.Add(CreateChartScatterPoint(data.ResultDate, data.Concentration));
meanSeries.Points.Add(CreateChartDataPoint(data.ResultDate, data.Mean));
sdSeries.Points.Add(CreateChartDataPoint(data.ResultDate, data.StandardDevation));
}

// add the series to the plot model
controlPlotModel.Series.Add(concentrationSeries);
controlPlotModel.Series.Add(meanSeries);
controlPlotModel.Series.Add(sdSeries);

return controlPlotModel;
}
```
Comments: There is a `NullReferenceException` in your code. I fixed this, then the plot shows up as expected. Note that `Plot` is obsolete, replace with `PlotView`.

Viewing all articles
Browse latest Browse all 2061

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>