Wrzucanie punktów na wykres - WPF

0

Cześć,
Przenoszę swój projekt z Windows Form na WPF w Visual Studio i mam problem z wrzucaniem punktów na wykres po naciśnięciu buttona. Niestety z WPF'em nigdy nie miałem do czynienia, dlatego prosiłbym o wskazanie błędu w moim kodzie.

<Window x:Class="WPF_Chart.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
        xmlns:datavis="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"        
        Title="MainWindow" Height="350" Width="525"
        xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit">


    <Grid Margin="79,0,18,0">

        <chartingToolkit:Chart Name="chart1"
                               BorderBrush="{x:Null}"
                               Foreground="Black">
            <chartingToolkit:Chart.LegendStyle>
                <Style TargetType="datavis:Legend">
                    <Setter Property="Width"
                            Value="0"/>
                </Style>
            </chartingToolkit:Chart.LegendStyle>
            <chartingToolkit:LineSeries
                        Title="Rates"
                        ItemsSource="{Binding PointsList}"
                        IndependentValueBinding="{Binding X}"
                        DependentValueBinding="{Binding Y}">
                <chartingToolkit:LineSeries.DependentRangeAxis>
                    <chartingToolkit:LinearAxis
                                Orientation="Y"
                                Title="Y Value"
                                AxisLabelStyle="{StaticResource NumericAxisLabelStyle}"
                                ShowGridLines="True" />
                </chartingToolkit:LineSeries.DependentRangeAxis>
            </chartingToolkit:LineSeries>

            <chartingToolkit:Chart.Axes>
                <chartingToolkit:LinearAxis Orientation="X" 
                                        Title="X Value"
                                        ShowGridLines="True" />
            </chartingToolkit:Chart.Axes>
        </chartingToolkit:Chart>
        
        <Button x:Name="button"
                Content="Button"
                HorizontalAlignment="Left"
                Margin="-70,45,0,0"
                VerticalAlignment="Top"
                Width="50"
                Height="50"
                Click="button_Click"/>

    </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.DataVisualization.Charting;


namespace WPF_Chart
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public class NewPoints 
        {
            public NewPoints (double x, double y)
            {
                this.X = x;
                this.Y = y;
            }
            public double X { get; set; }
            public double Y { get; set; }
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            List<NewPoints> PointsList = new List<NewPoints>();

            PointsList.Add(new NewPoints(0, 1.1));
            PointsList.Add(new NewPoints(1.5, 2.25));

            chart1.DataContext = PointsList;
        }
    }
}
 
1

Mniej więcej takie cuś powinno zadziałać, piszę z pamieci więc niczego sobie uciąć nie dam jak nie zadziała :P

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;



namespace WPF_Chart
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public ObservableCollection<NewPoints> PointsList { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            PointsList = new ObservableCollection<NewPoints>();
            chart1.DataContext = this;
        }

        public class NewPoints
        {
            public NewPoints(double x, double y)
            {
                this.X = x;
                this.Y = y;
            }
            public double X { get; set; }
            public double Y { get; set; }
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            PointsList.Clear();
            PointsList.Add(new NewPoints(0, 1.1));
            PointsList.Add(new NewPoints(1.5, 2.25));            
        }
    }
}
 
0

Chwała Ci, to działa! :)
Wielkie dzięki

0

Jeszcze tylko jedno pytanie: dlaczego ładowanie punktów na wykres tyle trwa, czy da się ten proces jakoś skrócić?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;


namespace WPF_Chart
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public ObservableCollection<Point> PointsList { get; set; }

        public MainWindow()
        {
            InitializeComponent();

            PointsList = new ObservableCollection<Point>();
            chart1.DataContext = this;
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            DateTime time_now = DateTime.Now, time_then; 

            for (int i = 0; i < 500; i++)
            {
                PointsList.Add(new Point(i, i));
            }

            time_then = DateTime.Now;

            MessageBox.Show((time_then - time_now).ToString());
        }
    }
}
 
<Window x:Class="WPF_Chart.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
        xmlns:datavis="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"        
        Title="MainWindow" Height="350" Width="525"
        xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit">

    <Grid Margin="79,0,18,0">

        
        <chartingToolkit:Chart Name="chart1"
                               BorderBrush="{x:Null}"
                               Foreground="Black">
            <chartingToolkit:Chart.LegendStyle>
                <Style TargetType="datavis:Legend">
                    <Setter Property="Width"
                            Value="0"/>
                </Style>
            </chartingToolkit:Chart.LegendStyle>
            <chartingToolkit:LineSeries                
                        Title="Rates"
                        ItemsSource="{Binding PointsList}"
                        IndependentValueBinding="{Binding X}"
                        DependentValueBinding="{Binding Y}">
                <chartingToolkit:LineSeries.DataPointStyle>
                    <Style TargetType="chartingToolkit:LineDataPoint">
                        <Setter Property="Visibility" Value="Collapsed"/>
                        <Setter Property="Width" Value="0"/>
                        <Setter Property="Background" Value="Red"/>
                    </Style>
                </chartingToolkit:LineSeries.DataPointStyle>
                <chartingToolkit:LineSeries.DependentRangeAxis>
                    <chartingToolkit:LinearAxis
                                Orientation="Y"
                                Title="Y Value"
                                AxisLabelStyle="{StaticResource NumericAxisLabelStyle}"
                                ShowGridLines="True" />
                </chartingToolkit:LineSeries.DependentRangeAxis>
            </chartingToolkit:LineSeries>
            <chartingToolkit:Chart.Axes>
                <chartingToolkit:LinearAxis Orientation="X" 
                                        Title="X Value"
                                        ShowGridLines="True" />
            </chartingToolkit:Chart.Axes>
        </chartingToolkit:Chart>
        
        
        <Button x:Name="button"
                Content="Button"
                HorizontalAlignment="Left"
                Margin="-70,45,0,0"
                VerticalAlignment="Top"
                Width="50"
                Height="50"
                Click="button_Click"/>

        
    </Grid>
</Window>
1

bo ObservableCollection przy każdym dodanym punkcie wysyła zdarzenie o tym i powoduje odpalenie całej logiki z przerysowaniem widoku z nowym punktem
Jeżeli chcesz stworzyć nową kolekcję to po prostu stwórz nową ObservableCollection w kontruktorze podając listę punktów, a potem przypisz do widoku (potrebujesz o tym poinformować zdarzeniem PropertyChanged). Lub jeśli kontrolka która obserwuje kolekcję to wspiera to wywołaj coś w stylu BeginUpdate a na końcu EndUpdate

1

Dokładnie tak jak napisał Mały Młot, widocznie wykres nie radzi sobie z notyfikowaniem po każdorazowym dodaniu punktów, więc trzeba je dodać na raz:

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;


namespace WPF_Chart
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        protected ObservableCollection<Point> pointsList;
        public ObservableCollection<Point> PointsList 
        { 
            get
            {
                return pointsList;
            }
            set
            {
                pointsList = value;
                OnPropertyChanged("PointsList");
            }
            
        }

        public MainWindow()
        {
            InitializeComponent();           
            chart1.DataContext = this;
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            DateTime time_now = DateTime.Now, time_then;

            var newPointList = new List<Point>();

            for (int i = 0; i < 500; i++)
            {
                newPointList.Add(new Point(i, i));
            }

            PointsList = new ObservableCollection<Point>(newPointList);

            time_then = DateTime.Now;

            MessageBox.Show((time_then - time_now).ToString());
        }



        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

0

Niestety nie dało to zamierzonego efektu, z sekundę muszę każdorazowo odczekać zanim wykres się wypełni, aczkolwiek bardzo dziękuję za pomoc i poświęcony czas :)

Potrzebuję wrzucania punktów na wykres w czasie rzeczywistym ale WPF toolkit chyba się do tego nie nadaje, albo po prostu jestem teraz jeszcze zbyt niedoświadczony by za niego się brać :(

1 użytkowników online, w tym zalogowanych: 0, gości: 1