关于OxyPlot的文档网上真的不全,官方文档也不祥不尽,所以没有办法就只有自己写了,不多说,直接上代码,拿来就可以直接用
使用OxyPlot控件,控件名plotView1
private DateTimeAxis _dateAxis;//X轴
private LinearAxis _valueAxis;//Y轴
private PlotModel _myPlotModel;
private Random rand = new Random();//用来生成随机数
private void getStressData() {
//定义model
_myPlotModel = new PlotModel()
{
Title = "波形实时曲线",
/*LegendTitle = "Legend",
LegendOrientation = LegendOrientation.Horizontal,
LegendPlacement = LegendPlacement.Inside,
LegendPosition = LegendPosition.TopRight,
LegendBackground = OxyColor.FromAColor(200, OxyColors.Beige),
LegendBorder = OxyColors.Black*/
};
//X轴
_dateAxis = new DateTimeAxis()
{
MajorGridlineStyle = LineStyle.Solid,
MinorGridlineStyle = LineStyle.Dot,
IntervalLength = 80,
IsZoomEnabled = true,
Title= "长度(mm)",
IsPanEnabled = true
};
_myPlotModel.Axes.Add(_dateAxis);
//Y轴
_valueAxis = new LinearAxis()
{
MajorGridlineStyle = LineStyle.Solid,
MinorGridlineStyle = LineStyle.Dot,
IntervalLength = 80,
Angle = 60,
IsZoomEnabled = true,
IsPanEnabled = true,
Title = "回拨强度",
Maximum = 100,
Minimum = -1
};
_myPlotModel.Axes.Add(_valueAxis);
//添加两条曲线
var series = new LineSeries()
{
Color = OxyColors.Green,
StrokeThickness = 2,
MarkerSize = 3,
MarkerStroke = OxyColors.DarkGreen,
MarkerType = MarkerType.Diamond,
};
_myPlotModel.Series.Add(series);
plotView1.Model = _myPlotModel;
Task.Factory.StartNew(() =>
{
while (true)
{
var date = DateTime.Now;
_myPlotModel.Axes[0].Maximum = DateTimeAxis.ToDouble(date.AddSeconds(1));
var lineSer = plotView1.Model.Series[0] as LineSeries;
lineSer.Points.Add(new DataPoint(DateTimeAxis.ToDouble(date), rand.Next(100, 300) / 10.0));
if (lineSer.Points.Count > 1000)
{
lineSer.Points.RemoveAt(0);
}
_myPlotModel.InvalidatePlot(true);
Thread.Sleep(1000);
}
});
}
这里是获取折线图选择区域的点的集合
private TrackerHitResult srartSeries;//选择点区间时起始点
private TrackerHitResult endSeries;//选择点区间时结束点
double seriesStartX;//选择点区间时起始横坐标
double seriesStartY;//选择点区间时起始纵坐标
List<DataPoint> benchmark;//选择点区间包含的所有点
bool seriesFlag = false;
private void plotView1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Middle)
{
seriesStartX = e.X;
seriesStartY = e.Y;
seriesFlag = true;
}
}
private void plotView1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Middle&&seriesFlag)
{
if (seriesStartX!=e.X) {
var startCoordinates = new ScreenPoint(seriesStartX, seriesStartY);
var endCoordinates = new ScreenPoint(e.X, e.Y);
srartSeries = (_myPlotModel.Series[0] as LineSeries).GetNearestPoint(startCoordinates, false);
var endSeries = (_myPlotModel.Series[0] as LineSeries).GetNearestPoint(endCoordinates, false);
benchmark=(_myPlotModel.Series[0] as LineSeries).Points.GetRange((int)srartSeries.Index,(int)(endSeries.Index- srartSeries.Index+1));
}
}
}
至于DataPoint是什么,就是OxyPlot的点的对象,如果还不知道,就打断点看吧。