Search code examples
wpfoxyplot

Plot line graph on button Click


I am developing wpf application.In that I need to plot a real time graph on clicking a button.

I tried writing this.MyModel.InvalidatePlot(true); in click().

Here is the code which I tried.

MainWindow.xaml

<Window x:Class="OxyPlotSample.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:oxy="http://oxyplot.org/wpf"

        mc:Ignorable="d"
        Title="FX2 Controller Communication" Height="768" Width="1366" WindowStartupLocation="CenterScreen" WindowState="Maximized">
    <Window.Resources>

        <Style x:Key="menuParentStyle" TargetType="MenuItem">
            <Setter Property="Background" Value="Silver"/>
            <Setter Property="Foreground" Value="Black"/>
        </Style>

        <SolidColorBrush x:Key="Menu.Static.Foreground" Color="Black"/>
        <SolidColorBrush x:Key="Menu.Static.Background" Color="Silver"/>
        <SolidColorBrush x:Key="Menu.Static.Border" Color="Transparent"/>
        <Style x:Key="MenuItemStyle1" TargetType="{x:Type MenuItem}">
            <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
            <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
            <Setter Property="Background" Value="Silver"/>
            <Setter Property="BorderBrush" Value="Transparent"/>
            <Setter Property="Foreground" Value="Black"/>
            <Setter Property="BorderThickness" Value="2"/>
            <Setter Property="ScrollViewer.PanningMode" Value="Both"/>
            <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
            <Setter Property="Template" Value="{DynamicResource MenuItemControlTemplate1}"/>
            <Style.Triggers>
                <Trigger Property="Role" Value="TopLevelHeader">
                    <Setter Property="Background" Value="Transparent"/>
                    <Setter Property="BorderBrush" Value="Transparent"/>
                    <Setter Property="Foreground" Value="{StaticResource Menu.Static.Foreground}"/>
                    <Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=TopLevelHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}"/>
                    <Setter Property="Padding" Value="6,0"/>
                </Trigger>
                <Trigger Property="Role" Value="TopLevelItem">
                    <Setter Property="Background" Value="{StaticResource Menu.Static.Background}"/>
                    <Setter Property="BorderBrush" Value="{StaticResource Menu.Static.Border}"/>
                    <Setter Property="Foreground" Value="{StaticResource Menu.Static.Foreground}"/>
                    <Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=TopLevelItemTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}"/>
                    <Setter Property="Padding" Value="6,0"/>
                </Trigger>
                <Trigger Property="Role" Value="SubmenuHeader">
                    <Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=SubmenuHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}"/>
                </Trigger>
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter Property="Background" Value="Pink" />
                </Trigger>
                <Trigger Property="IsKeyboardFocusWithin" Value="true">
                    <Setter Property="Background" Value="#0a99f3" />
                </Trigger>
            </Style.Triggers>
        </Style>
        <ControlTemplate x:Key="MenuItemControlTemplate1" TargetType="{x:Type MenuItem}">
            <Border x:Name="templateRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                <Grid Margin="-1">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition MinWidth="22" SharedSizeGroup="MenuItemIconColumnGroup" Width="Auto"/>
                        <ColumnDefinition Width="13"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="30"/>
                        <ColumnDefinition SharedSizeGroup="MenuItemIGTColumnGroup" Width="Auto"/>
                        <ColumnDefinition Width="20"/>
                    </Grid.ColumnDefinitions>
                    <ContentPresenter x:Name="Icon" Content="{TemplateBinding Icon}" ContentSource="Icon" HorizontalAlignment="Center" Height="16" Margin="3" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" Width="16"/>
                    <Border x:Name="GlyphPanel" BorderBrush="#FF26A0DA" BorderThickness="1" Background="#3D26A0DA" ClipToBounds="False" HorizontalAlignment="Center" Height="22" Margin="-1,0,0,0" Visibility="Hidden" VerticalAlignment="Center" Width="22">
                        <Path x:Name="Glyph" Data="F1M10,1.2L4.7,9.1 4.5,9.1 0,5.2 1.3,3.5 4.3,6.1 8.3,0 10,1.2z" Fill="#FF212121" FlowDirection="LeftToRight" Height="11" Width="10"/>
                    </Border>
                    <ContentPresenter x:Name="menuHeaderContainer" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" Grid.Column="2" ContentStringFormat="{TemplateBinding HeaderStringFormat}" ContentSource="Header" HorizontalAlignment="Left" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center"/>
                    <TextBlock x:Name="menuGestureText" Grid.Column="4" Margin="{TemplateBinding Padding}" Opacity="0.7" Text="{TemplateBinding InputGestureText}" VerticalAlignment="Center"/>
                </Grid>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="Icon" Value="{x:Null}">
                    <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
                </Trigger>
                <Trigger Property="IsChecked" Value="True">
                    <Setter Property="Visibility" TargetName="GlyphPanel" Value="Visible"/>
                    <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
                </Trigger>
                <Trigger Property="IsHighlighted" Value="True">
                    <Setter Property="Background" TargetName="templateRoot" Value="Blue"/>
                    <!--<Setter Property="BorderBrush" TargetName="templateRoot" Value="Yellow"/>-->
                </Trigger>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="#FF707070"/>
                    <Setter Property="Fill" TargetName="Glyph" Value="#FF707070"/>
                </Trigger>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="IsHighlighted" Value="True"/>
                        <Condition Property="IsEnabled" Value="False"/>
                    </MultiTrigger.Conditions>
                    <Setter Property="Background" TargetName="templateRoot" Value="#0A000000"/>
                    <Setter Property="BorderBrush" TargetName="templateRoot" Value="#21000000"/>
                </MultiTrigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
        <ControlTemplate x:Key="MenuItemControlTemplate2" TargetType="{x:Type MenuItem}">
            <Border x:Name="templateRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                <Grid VerticalAlignment="Center">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <ContentPresenter x:Name="Icon" Content="{TemplateBinding Icon}" ContentSource="Icon" HorizontalAlignment="Center" Height="16" Margin="3" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" Width="16"/>
                    <Path x:Name="GlyphPanel" Data="F1M10,1.2L4.7,9.1 4.5,9.1 0,5.2 1.3,3.5 4.3,6.1 8.3,0 10,1.2z" Fill="{TemplateBinding Foreground}" FlowDirection="LeftToRight" Margin="3" Visibility="Collapsed" VerticalAlignment="Center"/>
                    <ContentPresenter ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" Grid.Column="1" ContentStringFormat="{TemplateBinding HeaderStringFormat}" ContentSource="Header" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    <Popup x:Name="PART_Popup" AllowsTransparency="True" Focusable="False" IsOpen="{Binding IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}" Placement="Bottom">
                        <Border x:Name="SubMenuBorder" BorderBrush="#FF999999" BorderThickness="1" Background="#FFF0F0F0" Padding="2">
                            <ScrollViewer x:Name="SubMenuScrollViewer" Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}">
                                <Grid RenderOptions.ClearTypeHint="Enabled">
                                    <Canvas HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0">
                                        <Rectangle x:Name="OpaqueRect" Fill="{Binding Background, ElementName=SubMenuBorder}" Height="{Binding ActualHeight, ElementName=SubMenuBorder}" Width="{Binding ActualWidth, ElementName=SubMenuBorder}"/>
                                    </Canvas>
                                    <Rectangle Fill="#FFD7D7D7" HorizontalAlignment="Left" Margin="29,2,0,2" Width="1"/>
                                    <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Cycle" Grid.IsSharedSizeScope="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" KeyboardNavigation.TabNavigation="Cycle"/>
                                </Grid>
                            </ScrollViewer>
                        </Border>
                    </Popup>
                </Grid>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsSuspendingPopupAnimation" Value="True">
                    <Setter Property="PopupAnimation" TargetName="PART_Popup" Value="None"/>
                </Trigger>
                <Trigger Property="Icon" Value="{x:Null}">
                    <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
                </Trigger>
                <Trigger Property="IsChecked" Value="True">
                    <Setter Property="Visibility" TargetName="GlyphPanel" Value="Visible"/>
                    <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
                </Trigger>
                <Trigger Property="IsHighlighted" Value="True">
                    <Setter Property="Background" TargetName="templateRoot" Value="Blue"/>
                    <!--<Setter Property="BorderBrush" TargetName="templateRoot" Value="#FF26A0DA"/>-->
                </Trigger>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="#FF707070"/>
                    <Setter Property="Fill" TargetName="GlyphPanel" Value="#FF707070"/>
                </Trigger>
                <Trigger Property="CanContentScroll" SourceName="SubMenuScrollViewer" Value="False">
                    <Setter Property="Canvas.Top" TargetName="OpaqueRect" Value="{Binding VerticalOffset, ElementName=SubMenuScrollViewer}"/>
                    <Setter Property="Canvas.Left" TargetName="OpaqueRect" Value="{Binding HorizontalOffset, ElementName=SubMenuScrollViewer}"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>


    </Window.Resources>
    <Grid>

        <DockPanel>
            <Menu DockPanel.Dock="Top">
                <MenuItem Header="_File" Style="{StaticResource menuParentStyle}" ItemContainerStyle="{DynamicResource MenuItemStyle1}" Template="{DynamicResource MenuItemControlTemplate2}" Height="30" FontSize="14">
                    <MenuItem Command="Save" Height="30" FontSize="14"/>
                    <Separator />
                    <MenuItem Header="_Exit" Height="30" FontSize="14"/>
                </MenuItem>
                <MenuItem Header="_Aquire Mode" Style="{StaticResource menuParentStyle}" ItemContainerStyle="{DynamicResource MenuItemStyle1}" Template="{DynamicResource MenuItemControlTemplate2}" Height="30" FontSize="14">
                    <MenuItem Header="Sum" Height="30" FontSize="14"/>
                    <MenuItem Header="Average" Height="30" FontSize="14"/>

                </MenuItem>
            </Menu>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="200" />
                    <ColumnDefinition Width="5" />

                </Grid.ColumnDefinitions>
                <Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="685" Margin="9,11,0,0" Stroke="Black" VerticalAlignment="Top" Width="181"/>

                <StatusBar HorizontalAlignment="Left" Height="70" Margin="5,622,-1143,0" VerticalAlignment="Top" Width="1144" Grid.Column="1">
                    <ScrollBar Height="46" Width="12"/>
                </StatusBar>

                <Button Content="Single" HorizontalAlignment="Left" Margin="44,352,0,0" VerticalAlignment="Top" Width="132" Height="59" FontSize="20" Click="Single_Click"/>
                <Button Content="Continuous" HorizontalAlignment="Left" Margin="41,468,0,0" VerticalAlignment="Top" Width="130" Height="68" FontSize="20"/>
                <Button Content="Stop" HorizontalAlignment="Left" Margin="43,573,0,0" VerticalAlignment="Top" Width="126" Height="64" FontSize="20"/>
                <TextBox HorizontalAlignment="Left" Height="36" Margin="45,133,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"/>
                <TextBox HorizontalAlignment="Left" Height="36" Margin="47,257,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"/>
                <Label Content="Accumulation Number :" HorizontalAlignment="Left" Margin="36,65,0,0" VerticalAlignment="Top" Width="146" Height="34" FontSize="13"/>
                <Label Content="Integration Time :" HorizontalAlignment="Left" Margin="48,197,0,0" VerticalAlignment="Top" Width="123" Height="34" FontSize="14"/>
                <GridSplitter Grid.Column="1" Width="5" HorizontalAlignment="Stretch" />


            </Grid>
            <Grid>
                <oxy:PlotView x:Name="Plot1" Model="{Binding MyModel}"  Margin="10,10,10,98" DataContext="MyModel.Series" />
                <oxy:LineSeries DataContext="MyModel.Series"/>


            </Grid>

        </DockPanel>
    </Grid>

</Window>

MainWindow.xaml.cs

      using System.Collections.Generic;
      using System.Windows;
      using OxyPlot;

      namespace OxyPlotSample
     {
       public partial class MainWindow : Window
       {
          public MainWindow()
         {
             this.MyModel = plot;
             this.DataContext = this;
             InitializeComponent();
          }
          public PlotModel MyModel { get; set; }
          public PlotModel plot = new PlotModel { };

        private void Single_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Single Clicked!!!!");
            OxyPlot.Series.LineSeries series = new OxyPlot.Series.LineSeries();

            List<double> XValues = new List<double> { 0, 5, 10, 22, 30 };
            List<double> YValues = new List<double> { 2, 11, 4, 15, 20 };
            this.MyModel = new PlotModel { Title = "Example 1" };
            System.Diagnostics.Debug.WriteLine(XValues.Count);
            for (int i = 0; i < XValues.Count; i++)
            {
                System.Diagnostics.Debug.WriteLine(XValues[i] + "," + YValues[i]);
                series.Points.Add(new DataPoint(XValues[i], YValues[i]));
            }

            plot.Series.Add(series);
            this.MyModel = plot;
            this.DataContext = this;
            this.MyModel.InvalidatePlot(true);
            System.Diagnostics.Debug.WriteLine(MyModel.Series.Count);
        }

       }
    }

I am expecting to update the datacontext after adding series. Please Suggest the solution for this.


Solution

  • You are mixing too many ideas here. If you are using PlotView and also have the Model defined with the required series, all you would need to do is to Bind the model.

    <Grid>
         <oxy:PlotView x:Name="Plot1" Model="{Binding MyModel}"  Margin="8,11,12,97" />
    </Grid>
    

    Since your Model already has the details for the plotting the Line Series, It would plot the Line Series by itself.