简体   繁体   中英

How to display data on WPF Toolkit AreaSeries Chart?

I am using WPF Toolkit to display data on AreaSeries chart but it's not appearing. Don't know what I am doing wrong. Please help/suggest.

Below is the xaml code:

<Window xmlns:chart="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"

x:Class="GRAPHPrototype.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:GRAPHPrototype"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>       
    <chart:Chart Name="chart1">            
    </chart:Chart>
</Grid>

And the C# code :

public partial class MainWindow : Window
{
    static List<Ready4LOS> Ready4LOS = new List<Data.Ready4LOS>();
    public MainWindow()
    {
        InitializeComponent();

        InitChart();
        SampleData();
        chart1.DataContext = Ready4LOS;
    }

    private void SampleData()
    {

        Ready4LOS.Add(new Data.Ready4LOS() { Case = 5, Time = DateTime.Now.AddMinutes(-75) });
        Ready4LOS.Add(new Data.Ready4LOS() { Case = 15, Time = DateTime.Now.AddMinutes(-65) });
        Ready4LOS.Add(new Data.Ready4LOS() { Case = 5, Time = DateTime.Now.AddMinutes(-55) });
        Ready4LOS.Add(new Data.Ready4LOS() { Case = 8, Time = DateTime.Now.AddMinutes(-45) });
        Ready4LOS.Add(new Data.Ready4LOS() { Case = 22, Time = DateTime.Now.AddMinutes(-35) });
        Ready4LOS.Add(new Data.Ready4LOS() { Case = 35, Time = DateTime.Now.AddMinutes(-25) });
        Ready4LOS.Add(new Data.Ready4LOS() { Case = 4, Time = DateTime.Now.AddMinutes(-05) });
    }

    private void InitChart()
    {
        System.Windows.Data.Binding indi = new System.Windows.Data.Binding("Case");
        System.Windows.Data.Binding dep = new System.Windows.Data.Binding("Time");
        AreaSeries ares = new AreaSeries();
        ares.IndependentValueBinding = indi;
        ares.DependentValueBinding = dep;

        DateTimeAxis dta = new DateTimeAxis();
        dta.Interval = 10;
        dta.IntervalType = DateTimeIntervalType.Minutes;
        dta.Title = "Time";
        dta.Orientation = AxisOrientation.X;
        dta.Minimum = DateTime.Now.AddMinutes(-80);
        dta.Maximum = DateTime.Now;

        LinearAxis yaxis = new LinearAxis();
        yaxis.Minimum = 0;
        yaxis.Interval = 2;
        yaxis.Title = "Case";
        yaxis.Orientation = AxisOrientation.Y;
        chart1.Axes.Add(yaxis);
        chart1.Axes.Add(dta);
        chart1.Series.Add(ares);
    }
}

I have the input data in this format

class Ready4LOS : INotifyPropertyChanged
{
    int _case;
    DateTime _time;

    public int Case
    {
        get
        {
            return _case;
        }
        set
        {
            _case = value;
            NotifyPropertyChanged("Case");
        }
    }

    public DateTime Time
    {
        get
        {
            return _time;
        }
        set
        {
            _time = value;
            NotifyPropertyChanged("Time");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

There are 2 problems with your code:

  1. You didn't bind your List to the AreaSeries ItemSource
  2. You switched the indi and dep Binding

Try

private void InitChart()
{
    System.Windows.Data.Binding indi = new System.Windows.Data.Binding("Case");
    System.Windows.Data.Binding dep = new System.Windows.Data.Binding("Time");
    AreaSeries ares = new AreaSeries();
    ares.ItemsSource = Ready4LOS;
    ares.IndependentValueBinding = dep;
    ares.DependentValueBinding = indi;

    DateTimeAxis dta = new DateTimeAxis();
    dta.Interval = 10;
    dta.IntervalType = DateTimeIntervalType.Minutes;
    dta.Title = "Time";
    dta.Orientation = AxisOrientation.X;
    dta.Minimum = DateTime.Now.AddMinutes(-80);
    dta.Maximum = DateTime.Now;

    LinearAxis yaxis = new LinearAxis();
    yaxis.Minimum = 0;
    yaxis.Interval = 2;
    yaxis.Title = "Case";
    yaxis.Orientation = AxisOrientation.Y;

    chart1.Axes.Add(dta);
    chart1.Axes.Add(yaxis);
    chart1.Series.Add(ares);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM