2021-10-26 乐帮网
c# wpf
图表控制oxyplot在WPF中使得有以下几点需要注意。
(1)2.0和2.1版本使得方法不同。一个段简单的代码。
在2.0中我们可能这样写:
<oxy:Plot x:Name="LinePlot" Grid.Row="2" Grid.Column="1" Height="290" Width="700">
<oxy:Plot.Series>
<oxy:LineSeries Color="Aqua" ItemsSource="{Binding GpsLineList}"/>
<oxy:LineSeries Color="Yellow" ItemsSource="{Binding GpsLineList2}"/>
</oxy:Plot.Series>
</oxy:Plot>
但是在2.1中这么写就会遇到错误:命名空间“clr-namespace:OxyPlot.Series;assembly=OxyPlot”中不存在“Plot”名称。
新版本中你得这么用:
<oxy:PlotView Grid.Row="2" Grid.Column="1" Height="290" Width="700" Model="{Binding PieSeries}"/>
(2)以前使用oxyplot主要是画各种线型图,现在才发现新版本里有饼图有柱状图等。可以从官方找示例代码看一下。功能是越来越强大了。还远远不止这些。还支持动态数据源。
主要示例代码可以查看
https://oxyplot.readthedocs.io/en/latest/getting-started/hello-wpf.html#create-project
https://github.com/oxyplot/oxyplot/tree/develop/Source/Examples/ExampleLibrary/Series
这里我也使用2.1写了简单的一段示例代码:
public MainViewModel()
{
this.MyModel = new PlotModel { Title = "Example 1" };
this.MyModel.Series.Add(new FunctionSeries(Math.Cos, 0, 10, 0.1, "cos(x)"));
this.TwoColorLineSeries = GetTwoColorLineSeries();
this.RectangleBarSeries = GetRectangleBarSeries();
this.PieSeries = GetPieSeries();
}
public PlotModel MyModel { get; private set; }
public PlotModel TwoColorLineSeries { get; private set; }
public PlotModel RectangleBarSeries { get; private set; }
public PlotModel PieSeries { get; private set; }
public static PlotModel GetTwoColorLineSeries()
{
var model = new PlotModel { Title = "TwoColorLineSeries" };
var l = new Legend
{
LegendSymbolLength = 24
};
model.Legends.Add(l);
var s1 = new TwoColorLineSeries
{
Title = "Temperature at Eidesmoen, December 1986.",
TrackerFormatString = "December {2:0}: {4:0.0} °C",
Color = OxyColors.Red,
Color2 = OxyColors.LightBlue,
StrokeThickness = 3,
Limit = 0,
InterpolationAlgorithm = InterpolationAlgorithms.CanonicalSpline,
MarkerType = MarkerType.Circle,
MarkerSize = 4,
MarkerStroke = OxyColors.Black,
MarkerStrokeThickness = 1.5,
};
var temperatures = new[] { 5, 0, 7, 7, 4, 3, 5, 5, 11, 4, 2, 3, 2, 1, 0, 2, -1, 0, 0, -3, -6, -13, -10, -10, 0, -4, -5, -4, 3, 0, -5 };
for (int i = 0; i < temperatures.Length; i++)
{
s1.Points.Add(new DataPoint(i + 1, temperatures[i]));
}
model.Series.Add(s1);
model.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Title = "Temperature", Unit = "°C", ExtraGridlines = new[] { 0.0 } });
model.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, Title = "Date" });
return model;
}
public static PlotModel GetRectangleBarSeries()
{
var model = new PlotModel { Title = "RectangleBarSeries" };
var l = new Legend
{
LegendPlacement = LegendPlacement.Outside
};
model.Legends.Add(l);
var s1 = new RectangleBarSeries { Title = "RectangleBarSeries 1" };
s1.Items.Add(new RectangleBarItem { X0 = 2, X1 = 8, Y0 = 1, Y1 = 4 });
s1.Items.Add(new RectangleBarItem { X0 = 6, X1 = 12, Y0 = 6, Y1 = 7 });
model.Series.Add(s1);
var s2 = new RectangleBarSeries { Title = "RectangleBarSeries 2" };
s2.Items.Add(new RectangleBarItem { X0 = 2, X1 = 8, Y0 = -4, Y1 = -1 });
s2.Items.Add(new RectangleBarItem { X0 = 6, X1 = 12, Y0 = -7, Y1 = -6 });
model.Series.Add(s2);
return model;
}
private static PlotModel GetPieSeries()
{
var model = new PlotModel { Title = "World population by continent" };
var ps = new PieSeries
{
StrokeThickness = 2.0,
InsideLabelPosition = 0.8,
AngleSpan = 360,
StartAngle = 0
};
// http://www.nationsonline.org/oneworld/world_population.htm
// http://en.wikipedia.org/wiki/Continent
ps.Slices.Add(new PieSlice("Africa", 1030) { IsExploded = true });
ps.Slices.Add(new PieSlice("Americas", 929) { IsExploded = true });
ps.Slices.Add(new PieSlice("Asia", 4157));
ps.Slices.Add(new PieSlice("Europe", 739) { IsExploded = true });
ps.Slices.Add(new PieSlice("Oceania", 35) { IsExploded = true });
model.Series.Add(ps);
return model;
}
效果图如下:
源码下载地址:
链接:https://pan.baidu.com/s/1Q4r7xdHm_YtOrJgDMXBX4g
关注我的微信公众号
在公众号里留言交流
投稿邮箱:1052839972@qq.com
庭院深深深几许?杨柳堆烟,帘幕无重数。
玉勒雕鞍游冶处,楼高不见章台路。
雨横风狂三月暮。门掩黄昏,无计留春住。
泪眼问花花不语,乱红飞过秋千去。
如果感觉对您有帮助
欢迎向作者提供捐赠
这将是创作的最大动力