当前位置:   article > 正文

WPF图表Live Charts(三)柱状图_livecharts柱状图

livecharts柱状图

简介

前面介绍了Live Charts的基础和设置,接下介绍一下如何实现一个柱状图。该柱状图主要实现打开多个文件,读取文件的多个数据并显示的功能。

效果预览

在这里插入图片描述
打开多个数据文件,同时读取文件的文件的大小,文件的的温度数据,分两个Y轴显示,同时有各自的单位。

前台代码
<Grid >
        <lvc:CartesianChart Series="{Binding SeriesCollection}" LegendLocation="Top">
            <lvc:CartesianChart.AxisX>
                <lvc:Axis Title="fielname" Labels="{Binding Labels}" FontSize="14"></lvc:Axis>
            </lvc:CartesianChart.AxisX>
            <lvc:CartesianChart.AxisY>
                <lvc:Axis x:Name="axisT"  LabelFormatter="{Binding FormatterT}" Foreground="DodgerBlue" FontSize="14" />
                <lvc:Axis x:Name="axisS"  LabelFormatter="{Binding FormatterS}" Foreground="IndianRed" Position="RightTop" FontSize="14" />
            </lvc:CartesianChart.AxisY>
        </lvc:CartesianChart>
        <Button x:Name="button_open" Content="open" HorizontalAlignment="Right" VerticalAlignment="Top" Width="40" Click="button_openClick" 
                Background="{x:Null}" BorderBrush="{DynamicResource {x:Static SystemColors.HotTrackBrushKey}}" 
                Foreground="{DynamicResource {x:Static SystemColors.HotTrackBrushKey}}" Opacity="0.5" Height="20"/>
    </Grid>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
后台代码
   public partial class MainWindow : Window
    {
        public Func<double, string> FormatterT { get; set; }
        public Func<double, string> FormatterS { get; set; }
        public SeriesCollection SeriesCollection { get; set; }
        public string[] Labels { get; set; }

        List<double> filesize = new List<double>();
        List<string> filenames = new List<string>();
        List<double> temperature = new List<double>();

        //新建一条文件大小的柱状图
        ColumnSeries sizeseries = new ColumnSeries();
        //新建一条温度的柱状图
        ColumnSeries templeseries = new ColumnSeries();
        public MainWindow()
        {			
            InitializeComponent();

            //添加两个纵坐标的单位
            FormatterT = value => value + "C°";
            FormatterS = value => value + "KB";

            templeseries.Title = "temp";
            //温度的柱状图依赖于第一条纵坐标
            templeseries.ScalesYAt = 0;
            axisT.Title = "temp/C°";

            sizeseries.Title = "size";
            //文件大小的柱状图依赖于第二条纵坐标
            sizeseries.ScalesYAt = 1;
            axisS.Title = "size/KB";

            SeriesCollection = new SeriesCollection { };
            SeriesCollection.Add(templeseries);
            SeriesCollection.Add(sizeseries);
            
            //绑定数据
            DataContext = this;
        }
        public  void button_openClick(object sender, RoutedEventArgs e)
        {
            filenames.Clear();
            filesize.Clear();
            temperature.Clear();
            OpenFileDialog open_files = new OpenFileDialog();
            //打开多个文件
            open_files.Multiselect = true;
            if (open_files.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                foreach (string file in open_files.FileNames)
                {
                    FileInfo MyFileInfo = new FileInfo(file);
                    //获取文件的大小
                    filesize.Add((int )MyFileInfo.Length/1024  );
                    //获取文件的名称
                    filenames.Add (System.IO.Path.GetFileNameWithoutExtension(file));
                    //通过正则表达式获取文件中的数据信息
                    StreamReader sr = new StreamReader(file);
                    string pattern = @"DHT11 temp:([0-9]+(.[0-9]{1,3})?),";
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        Regex r = new Regex(pattern , RegexOptions.None);
                        Match mc = r.Match(line );
                        string tempStr = mc.Groups[1].Value;
                        temperature.Add (Convert.ToDouble (tempStr));
                        break;
                    }
                    sr.Close();
                }         
            }
            Labels = filenames.ToArray();
            templeseries.Values = new ChartValues<double>(temperature);
            sizeseries.Values = new ChartValues<double>(filesize);
        }     
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/270794
推荐阅读
相关标签
  

闽ICP备14008679号