当前位置:   article > 正文

在WPF中使用Livechart写一个简单的柱状图_livecharts库绘制树状图

livecharts库绘制树状图

首先,你需要在你的WPF项目中安装LiveCharts NuGet包。你可以通过NuGet包管理器搜索“LiveCharts.Wpf”来安装它。

然后,在XAML文件中添加一个CartesianChart控件:

  1. <Window x:Class="WpfApp12.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
  7. xmlns:local="clr-namespace:WpfApp12"
  8. mc:Ignorable="d"
  9. Title="MainWindow" Height="450" Width="800">
  10. <Grid>
  11. <lvc:CartesianChart Series="{Binding SeriesCollection}" LegendLocation="Right"/>
  12. </Grid>
  13. </Window>

接下来,在你的ViewModel类中创建一个SeriesCollection属性,并在构造函数中初始化它:

  1. using LiveCharts;
  2. using LiveCharts.Wpf;
  3. public class MainViewModel
  4. {
  5. public SeriesCollection SeriesCollection { get; set; }
  6. public MainViewModel()
  7. {
  8. SeriesCollection = new SeriesCollection
  9. {
  10. new ColumnSeries
  11. {
  12. Title = "Series 1",
  13. Values = new ChartValues<double> { 3, 5, 2, 7 }
  14. }
  15. ,
  16. new ColumnSeries
  17. {
  18. Title = "Series 2",
  19. Values = new ChartValues<double> { 1, 6, 4, 9 }
  20. }
  21. };
  22. }
  23. }

这里我们创建了一个包含一个ColumnSeries的SeriesCollection。ColumnSeries表示柱状图,Title属性是这个系列的标题,Values属性是这个系列的数据。

最后,在MainWindow的构造函数中将ViewModel设置为DataContext:

  1. public MainWindow()
  2. {
  3. InitializeComponent();
  4. DataContext = new MainViewModel();
  5. }

现在你应该可以运行这个程序并看到一个简单的柱状图:

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号