Skip to content

Instantly share code, notes, and snippets.

@Evshved
Last active May 10, 2018 00:29
Show Gist options
  • Save Evshved/6b1624355c2a1cd73bb44c8f716267b3 to your computer and use it in GitHub Desktop.
Save Evshved/6b1624355c2a1cd73bb44c8f716267b3 to your computer and use it in GitHub Desktop.
using System;
using System.Linq;
using OxyPlot;
using OxyPlot.Annotations;
using OxyPlot.Axes;
using OxyPlot.Series;
namespace SpectrumAnalyzer.Helpers
{
public class Plotter
{
public bool Initialized
{
get
{
if (PlotFrame != null)
{
return true;
}
else
{
return false;
}
}
}
public PlotModel PlotFrame { get; private set; }
public enum PlotMethod
{
Replace = 1,
Combine = 2
}
public Plotter()
{
Initialize();
ShowTestPlot();
}
private void ShowTestPlot()
{
PlotFrame.Title = "Test Plot with markers";
const int NumberOfAngles = 6;
var customMarkerOutline = new ScreenPoint[NumberOfAngles];
for (int i = 0; i < NumberOfAngles; i++)
{
double th = Math.PI * (2.0 * i / (NumberOfAngles - 1) - 0.5);
const double R = 1;
customMarkerOutline[i] = new ScreenPoint(Math.Cos(th) * R, Math.Sin(th) * R);
}
var MainCurve = new LineSeries
{
StrokeThickness = 2,
};
MainCurve.Points.Add(new DataPoint(0, 6));
MainCurve.Points.Add(new DataPoint(1, 2));
MainCurve.Points.Add(new DataPoint(2, 4));
MainCurve.Points.Add(new DataPoint(3, 2));
MainCurve.Points.Add(new DataPoint(4, 7));
MainCurve.Points.Add(new DataPoint(6, 6));
MainCurve.Points.Add(new DataPoint(8, 8));
PlotFrame.Series.Add(MainCurve);
var scatterSeries = new ScatterSeries {
MarkerType = MarkerType.Custom,
MarkerOutline = customMarkerOutline,
MarkerFill = OxyColors.DarkRed,
MarkerSize = 10
};
scatterSeries.Points.Add(new ScatterPoint(4, 7));
scatterSeries.Points.Add(new ScatterPoint(0, 6));
PlotFrame.Series.Add(scatterSeries);
}
public void Clear()
{
if (this.Initialized)
{
PlotFrame.Series.Clear();
}
}
private void Initialize()
{
PlotFrame = new PlotModel();
PlotFrame.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom });
PlotFrame.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Maximum = 10, Minimum = 0 });
}
public void Plot(Spectrum spectrum, PlotMethod plotMethod)
{
if (plotMethod == PlotMethod.Replace)
{
this.Clear();
}
// TODO: Spectrum checkouts
PlotFrame.Title = spectrum.SpectrumName;
var series1 = new OxyPlot.Series.LineSeries
{
StrokeThickness = 1,
Color = OxyColors.Red
};
if (plotMethod == PlotMethod.Combine)
{
series1.Color = OxyColors.Blue;
}
for (int i = 0; i < spectrum.Bins.Count; i++)
{
series1.Points.Add(new DataPoint(spectrum.Bins[i].X, spectrum.Bins[i].Y));
}
RecountPlotAxes(spectrum);
PlotFrame.Series.Add(series1);
PlotFrame.InvalidatePlot(true);
}
private void RecountPlotAxes(Spectrum spectrum)
{
var minX = spectrum.Bins.Min(x => x.X);
var maxX = spectrum.Bins.Max(x => x.X);
var minY = spectrum.Bins.Min(x => x.Y);
var maxY = spectrum.Bins.Max(x => x.Y);
PlotFrame.Axes.Clear();
PlotFrame.Axes.Add(new LinearAxis
{
Position = AxisPosition.Bottom,
AbsoluteMinimum = minX,
AbsoluteMaximum = maxX,
Minimum = minX,
Maximum = maxX
});
PlotFrame.Axes.Add(new LinearAxis
{
Position = AxisPosition.Left,
AbsoluteMinimum = minY,
AbsoluteMaximum = maxY,
Minimum = minY,
Maximum = maxY
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment