当前项目需要一个图表控件,尝试用过mschart和livechart,都不是很满意。这里试一下WPFToolkit.DataVisualization。
- 引用dll,通过nuget包管理器下载WPFToolkit.DataVisualization和WPFToolkit(前者依赖于后者,两个都要安装)
- xaml代码:
<Window x:Class="ChartTestInWPF.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:ChartTestInWPF" xmlns:charting="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <charting:Chart Height="271" Name="chart1" Width="379"> <charting:Chart.Series> <charting:PieSeries ItemsSource="{Binding}" DependentValuePath="Value" IndependentValuePath="Key" Title="Pet Preference" IsSelectionEnabled="True" /> </charting:Chart.Series> </charting:Chart> </Grid> </Window>
cs文件代码:
- chart1.DataContext = new KeyValuePair<string, int>[] {
- new KeyValuePair<string, int>("Dog", 30),
- new KeyValuePair<string, int>("Cat", 25),
- new KeyValuePair<string, int>("Rat", 5),
- new KeyValuePair<string, int>("Hampster", 8),
- new KeyValuePair<string, int>("Rabbit", 12) };
运行效果:
- 上面说明调用控件成功了,但是我是要绘制一个线性图,并且是动态显示数据的,这里采用了MVVM模式。效果如下
- 代码上传到微云了:下载地址:https://share.weiyun.com/5TVMsJt
- 参考了以下网页:https://www.cnblogs.com/HQFZ/p/4446727.html,http://www.it1352.com/437797.html