WPF LiveChart示例

前端页面代码

<TabItem Header="直方图" >
                <wpf:CartesianChart Series="{Binding SeriesCollectionAnayColumn}" 
                                    LegendLocation="Right" >
                    <wpf:CartesianChart.AxisX>
                        <wpf:Axis Title="区间" Labels="{Binding LabelsColumn}"  Foreground="#FF8DEBF7"/>
                    </wpf:CartesianChart.AxisX>
                    <wpf:CartesianChart.AxisY>
                        <wpf:Axis Title="频率"  LabelFormatter="{Binding YFormatterColumn}" Foreground="#FF8DEBF7"/>
                    </wpf:CartesianChart.AxisY>
                    <wpf:CartesianChart.DataTooltip>
                        <!--The Selection mode property should be done automatically in future versions-->
                        <wpf:DefaultTooltip SelectionMode="SharedXValues" Background="DarkSlateGray"/>
                    </wpf:CartesianChart.DataTooltip>
                </wpf:CartesianChart>



            </TabItem>
            <TabItem Header="波形图">
                <wpf:CartesianChart Series="{Binding SeriesCollectionAnayLine}" 
                                    LegendLocation="Right" >
                    <wpf:CartesianChart.AxisX>
                        <wpf:Axis Title="时间" Labels="{Binding LabelsLine}"  Foreground="#FF8DEBF7"/>
                    </wpf:CartesianChart.AxisX>
                    <wpf:CartesianChart.AxisY>
                        <wpf:Axis Title=""  LabelFormatter="{Binding YFormatterLine}" Foreground="#FF8DEBF7"/>
                    </wpf:CartesianChart.AxisY>
                    <wpf:CartesianChart.DataTooltip>
                        <!--The Selection mode property should be done automatically in future versions-->
                        <wpf:DefaultTooltip SelectionMode="SharedXValues" Background="DarkSlateGray"/>
                    </wpf:CartesianChart.DataTooltip>
                </wpf:CartesianChart>
            </TabItem>

ViewModel代码

//定义页面上绑定的数据源
 public SeriesCollection _seriesCollectionAnayColumn { get; set; }
        public SeriesCollection SeriesCollectionAnayColumn
        {
            get => _seriesCollectionAnayColumn;
            set
            {
                _seriesCollectionAnayColumn = value;
                RaisePropertyChanged();
            }
        }
        public string[] _labelsColumn { get; set; }
        public string[] LabelsColumn
        {
            get => _labelsColumn;
            set
            {
                _labelsColumn = value;
                RaisePropertyChanged();
            }
        }
        public Func<double, string> _yFormatterColumn { get; set; }
        public Func<double, string> YFormatterColumn
        {
            get => _yFormatterColumn;
            set
            {
                _yFormatterColumn = value;
                RaisePropertyChanged();
            }
        }


        public SeriesCollection _seriesCollectionAnayLine { get; set; }
        public SeriesCollection SeriesCollectionAnayLine
        {
            get => _seriesCollectionAnayLine;
            set
            {
                _seriesCollectionAnayLine = value;
                RaisePropertyChanged();
            }
        }
        public string[] _labelsLine { get; set; }
        public string[] LabelsLine
        {
            get => _labelsLine;
            set
            {
                _labelsLine = value;
                RaisePropertyChanged();
            }
        }
        public Func<double, string> _yFormatterLine { get; set; }
        public Func<double, string> YFormatterLine
        {
            get => _yFormatterLine;
            set
            {
                _yFormatterLine = value;
                RaisePropertyChanged();
            }
        }



//为页面绑定的数据源赋值
   //折线图
                        SeriesCollectionAnayLine = new SeriesCollection();
                        LabelsLine =data.TrainTime;
                    YFormatterLine = value => value.ToString("f4");
 SeriesCollectionAnayLine.Add(new LineSeries
                        {
                            Title = data.TestData[0].DataType,
                            Values = new ChartValues<float>(YAxis),
                            LineSmoothness = 0
                        });


 //直方图
                            SeriesCollectionAnayColumn = new SeriesCollection();
                            LabelsColumn = columuResult.XAxis;
                            YFormatterColumn = value => value.ToString("f3");
                            SeriesCollectionAnayColumn.Add(new ColumnSeries
                            {
                                Title = "频率",
                                Values = new ChartValues<float>(columuResult.YAxis)
                            });
原文地址:https://www.cnblogs.com/ljy0905/p/9935966.html