How to plot a line graph in WPF MVVM atrchitecture

One07k-4914 101 Reputation points
2021-03-31T08:08:05.877+00:00

Hi ,

I wanted to plot one 2D graph,X axis time,Y axis some number.

I tried using Polyline.But in mvvm architecture i am not able to bind the data.Can anyone help/suggest me a way to fix the problem.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,667 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
760 questions
0 comments No comments
{count} votes

Accepted answer
  1. DaisyTian-1203 11,616 Reputation points
    2021-04-01T09:23:01.92+00:00

    I did a sample in C#, below is the code:
    XAML code is:

     <Window.DataContext>  
            <local:MyViewModel></local:MyViewModel>  
        </Window.DataContext>  
        <StackPanel>  
            <Polyline Height="300"  Name="_myPolyline" Stroke="{Binding myModel.ColorName}" Tag="123213" StrokeThickness="4" Points="{Binding myModel.points}" ></Polyline>  
        </StackPanel>  
    

    The cs code is:

    public partial class MainWindow : Window  
        {  
            public MainWindow()  
            {  
                InitializeComponent();  
                MyViewModel vm = new MyViewModel();  
                _myPolyline.Points = vm.myModel.points;  
            }  
        }  
      
        public class MyViewModel  
        {  
      
            public int currentSecond = 0;  
            Random rd = new Random();  
            public PointCollection LtPoint = new PointCollection();  
      
            public MyModel myModel { get; set; } = new MyModel();  
            public MyViewModel()  
            {  
                DispatcherTimer timer = new DispatcherTimer();  
                timer.Tick += Timer_Tick;  
                timer.Interval = TimeSpan.FromMilliseconds(500);  
                timer.Start();  
      
                myModel = new MyModel()  
                {  
                    points = LtPoint,  
                    ColorName = "Blue"  
                };  
            }  
      
            private void Timer_Tick(object sender, EventArgs e)  
            {  
                currentSecond++;  
                double x = currentSecond * 10;  
                double y = rd.Next(1, 200);  
                LtPoint.Add(new Point(x, y));  
            }  
        }  
      
        public class MyModel  
        {  
            public PointCollection points { get; set; } = new PointCollection();  
      
            public string ColorName { get; set; }  
        }  
    

    83576-2.gif


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,231 Reputation points
    2021-03-31T13:26:05.737+00:00

    Hi,
    you can display PLine and use Interactivity from NuGet. Try following demo:

    XAML:

    <Window x:Class="Window052"  
            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:WpfApp1.WpfApp052"  
            xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"  
            mc:Ignorable="d"  
            Title="Window052" Height="450" Width="800">  
      <Window.DataContext>  
        <local:ViewModel/>  
      </Window.DataContext>  
      <Canvas Background="White">  
        <i:Interaction.Behaviors>  
          <local:CanvasBehavior/>  
        </i:Interaction.Behaviors>  
      </Canvas>  
    </Window>  
    

    And classes in VB.NET:

    It's impossible to post code, please see attached txt.

    83334-x.txt

    1 person found this answer helpful.
    0 comments No comments