Overlay Curve with Bar Series?

Oystein Bjorke 10 років тому 0
This discussion was imported from CodePlex

alanrorr1 wrote at 2013-10-01 00:51:

Hi,
I just started looking around in case the chart I am using now does not support my needs amply. Wondering, will the OxyPlot chart support a bar series with a smooth curve overlaid. I am looking to show where the bars fall with respect to a fitted curve (i.e. overlaid curve).

Thanks!
Alan

Gimly wrote at 2013-10-01 17:58:

You can use a PolylineAnnotation for this, simply give it the list of fitted points and use Smooth = true.

There's an example in the AnnotationExamples called PolylineAnnotations.

alanrorr1 wrote at 2013-10-02 01:19:

Gimly, Fantastic and thanks! I will give that a try. Best, Alan

alanrorr1 wrote at 2013-10-03 08:37:

Gimly,
Do you happen to have any ideas? I am stuck on this error

Error 2 Argument 1: cannot convert from 'OxyPlot.Wpf.PolylineAnnotation' to 'OxyPlot.Annotations.Annotation' D:\Development\Samples\OxyPlotSample\OxyPlotSample\MainWindow.xaml.cs 83 33 OxyPlotSample

which is caused by this line
        tmp.Annotations.Add(new PolylineAnnotation { Points = new IDataPoint[] { new DataPoint(0, 15), new DataPoint(3, 23), new DataPoint(9, 30), new DataPoint(20, 12), new DataPoint(30, 10) }, Smooth = true, Text = "Smooth Polyline" });
I tried casting the "new PolylineAnnotation" to "OxyPlot.Annotations.Annotation" with no luck.

I am not quite sure what to try next. This is a .Net/WPF application.

Thanks,
Alan

everytimer wrote at 2013-10-03 09:24:

Are you adding the PolylineAnnotation in your code-behind? If so try to refer to your model in the plot:

if plotter is the x:Name of your plot in XAML

in the code behind you need to set something like:
plotter.Model.Annotations.Add(myPoly); 
instead of simply
plotter.Annotations.Add(myPoly);
Good luck

alanrorr1 wrote at 2013-10-03 16:57:

Hi Everytimer,
Thanks for the reply! Yes, PolylineAnnotation is in the code-behind accessing the PlotModel. The XAML refers to the PlotModel through this binding with other properties (i.e. code behind accessing PlotModel) working as expected.
            <oxy:Plot x:Name="MyPlot" Model="{Binding Model1}">
            </oxy:Plot>
I tried your idea of accessing the model directing by naming the plot:
this.MyPlot.Model.Annotations.Add(new PolylineAnnotation { Points = new IDataPoint[] { new DataPoint(0, 15), new DataPoint(3, 23), new DataPoint(9, 30), new DataPoint(20, 12), new DataPoint(30, 10) }, Smooth = true, Text = "Smooth Polyline" });
Unfortunately, I am getting the same compiler error:
Argument 1: cannot convert from 'OxyPlot.Wpf.PolylineAnnotation' to 'OxyPlot.Annotations.Annotation' 
The compiler is seeing Annotations" with the "Add" method, but it looks like it is unable to accept the PolylineAnnotation object in the Add method. The Add method is expecting something it can convert to OxyPlot.Annotations.Annotation. I got this line using PolylineAnnotation from the general examples since I could not find it in the WPF samples.

Thanks!
Alan

everytimer wrote at 2013-10-03 22:18:

Ok, I think you have
using OxyPlot.Wpf;
on top, if so try to add the "normal" PolylineAnnotation:
var myPoly = new OxyPlot.Annotations.PolylineAnnotation() { /* stuff */};
MyPlot.Add(myPoly);
Good luck!

alanrorr1 wrote at 2013-10-04 08:00:

Hi Everytimer,

Beautiful, it compiles - thank you!

Best,
Alan