none
Legend is not appearing in the bottom of the graph using WindowsFormsHost

    Question

  • I am using WindowsFormsHost over a wpf application.

    Legends always appears on the adjacent view of the graph and not in the bottom of the graph.

    I am unable figure out the API to do that. 

    Below is the xml code

    <UserControl x:Class="project_UI.PowerMonitor"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:local="clr-namespace:project_UI"
                 xmlns:mschart="clr-namespace:System.Windows.Forms.DataVisualization.Charting;assembly=System.Windows.Forms.DataVisualization"                          
                 mc:Ignorable="d" 
                 d:DesignHeight="800" d:DesignWidth="800">
    
        <UserControl.Resources>
            <Style x:Key="VerticalSeparatorStyle" 
               TargetType="{x:Type Separator}"
               BasedOn="{StaticResource {x:Type Separator}}" >
                <Setter Property="Margin" Value="3,0,3,30"/>
                <Setter Property="LayoutTransform">
                    <Setter.Value>
                        <TransformGroup>
                            <TransformGroup.Children>
                                <TransformCollection>
                                    <RotateTransform Angle="90"/>
                                </TransformCollection>
                            </TransformGroup.Children>
                        </TransformGroup>
                    </Setter.Value>
                </Setter>
            </Style>
            <Style x:Key="bground" TargetType="TextBox">
                <Setter Property="BorderBrush" Value="White"></Setter>
                <Setter Property="BorderThickness" Value="0"></Setter>
            </Style>
            
        </UserControl.Resources>
        
        <GroupBox Margin="15,10,15,10" BorderThickness="2">
            <Grid ShowGridLines="True" Name="GridPower">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="0.75*" />
                    <ColumnDefinition Width="2.25*"/>
                </Grid.ColumnDefinitions>
    
                <Grid Grid.Column="0" Margin="10,20,10,20">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="200"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        
                        <StackPanel Grid.Row="0" HorizontalAlignment="Left" Margin="10,20,10,10">
                            <Label Height="30" Width="Auto" FontWeight="Bold" Margin="-3,0,0,0">Select Monitor Points</Label>
                            <CheckBox Name="Chk_1" Grid.Row="0"  VerticalAlignment="Center" Height="30" Checked="1_checked" Unchecked="1_unchecked">Power 1</CheckBox>
                            <CheckBox Name="Chk_2" Grid.Row="0"  VerticalAlignment="Center" Height="30" Checked="2_checked" Unchecked="2_unchecked">Power 2</CheckBox>
                            <CheckBox Name="Chk_3" Grid.Row="0"  VerticalAlignment="Center" Height="30" Checked="3_checked" Unchecked="3_unchecked">Power 3</CheckBox>
                            <CheckBox Name="Chk_4" Grid.Row="0"  VerticalAlignment="Center" Height="30" Checked="4_checked" Unchecked="4_unchecked">Power 4</CheckBox>
                            <CheckBox Name="Chk_5" Grid.Row="0"  VerticalAlignment="Center" Height="30" Checked="5_checked" Unchecked="5_unchecked">Power 5</CheckBox>
                        </StackPanel>
    
                        <StackPanel Grid.Row="1" Margin="10,30,10,20">
                            <Label Content="Polling Frequency(msec)" Height="26"  Margin="-5,0,45,0" FontWeight="Bold"/>
                            <Slider Name ="Poll_Freq" Height="25" Width="100" Margin="0,0,60,0" Minimum="100" Maximum="1000" Style="{DynamicResource SpinButton}" ValueChanged="Slider_ValueChanged" HorizontalAlignment="Left"/>
                            <Label Content="Max Polling Duration(min)" Margin="-5,4,35,0" Height="26" FontWeight="Bold"/>
                            <Slider Name="Poll_Dura" Height="25" Width="100" Margin="0,0,60,0" Minimum="1" Maximum="120" Style="{DynamicResource SpinButton}" ValueChanged="Slider_ValueChanged" HorizontalAlignment="Left"/>
                            <ToggleButton x:Name = "Btn_Poll" Content = "Start Polling" Checked = "HandleCheck" Unchecked = "HandleUnchecked" 
                                          Margin="0,30,0,0" Width = "140" HorizontalAlignment = "Left" />
                        </StackPanel>
                    </Grid>
                </Grid>
    
                <Grid Grid.Column="1" Margin="10,20,10,20">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="0.5*"/>
                        <RowDefinition Height="3*"/>
                        <RowDefinition Height="0.5*"/>
                        <RowDefinition Height="3*"/>
                        <RowDefinition Height="0.5*"/>
                        <RowDefinition Height="3*"/>
                    </Grid.RowDefinitions>
    
                    <DockPanel Grid.Row="0" Name="Dock1">
                        <Label Content="Power" HorizontalAlignment="Left" VerticalAlignment="Center" FontWeight="Bold"></Label>
                        <Button Content="Grid view" Width="60" Height="25" HorizontalAlignment="Left" VerticalAlignment="Center" Background="Aqua" Click="BtnChart1Gridview_Click"></Button>
                        <Button Content="Legends" Width="60" Height="25" HorizontalAlignment="Left" VerticalAlignment="Center" Background="Aqua" Click="BtnChart1Legend_Click"></Button>
                        <Separator Style="{DynamicResource VerticalSeparatorStyle}" VerticalAlignment="Top"/>
                        <Button Name="btn_xaxis" Content="Set X-axis Range" IsEnabled="False" Width="95" Height="25" HorizontalAlignment="Left" VerticalAlignment="Center" BorderThickness="0" Background="White"></Button>
                        <Button Name="btn_yaxis" Content="Set Y-axis Range" Width="95" Height="25" HorizontalAlignment="Left" VerticalAlignment="Center" BorderThickness="0" Background="White" Click="btn_yaxischart1"></Button>
                        <Button Name="btn_chart1color" Content="BackColor" Width="60" Height="25" HorizontalAlignment="Left" VerticalAlignment="Center" BorderThickness="0" Background="White" Click="BtnChartBackColor_Click"></Button>
                        <Label Content="Width: " HorizontalAlignment="Left" VerticalAlignment="Center"></Label>
                        <TextBox Name="txt_width" Style="{StaticResource bground}" Width="120" Height="20" HorizontalAlignment="Left" VerticalAlignment="Center" Background="AntiqueWhite" TextChanged="Txt_width_Changed"></TextBox>
                        <Label Content=" ms" HorizontalAlignment="Left" VerticalAlignment="Center"></Label>                    
                    </DockPanel>
    
                    <WindowsFormsHost x:Name="host1" Height="150" Grid.Row="1" VerticalAlignment="Top" >
                        <mschart:Chart x:Name="WinformChart1" Dock="Fill">                        
                            <mschart:Chart.ChartAreas>
                                <mschart:ChartArea></mschart:ChartArea>
                            </mschart:Chart.ChartAreas>
                            
                            <mschart:Chart.Series>                            
                            </mschart:Chart.Series>
                            
                            <mschart:Chart.Legends>
                                <mschart:Legend></mschart:Legend>                            
                            </mschart:Chart.Legends>
                        </mschart:Chart>                    
                    </WindowsFormsHost>
    
                    <DockPanel Grid.Row="2">
                        <Label Content="Voltage" HorizontalAlignment="Left" VerticalAlignment="Center" FontWeight="Bold"></Label>
                        <Button Content="Grid view" Width="60" Height="25" HorizontalAlignment="Left" VerticalAlignment="Center" Background="Aqua" Click="BtnChart2Gridview_Click"></Button>
                        <Button Content="Legends" Width="60" Height="25" HorizontalAlignment="Left" VerticalAlignment="Center" Background="Aqua" Click="BtnChart2Legend_Click"></Button>
                        <Separator Style="{DynamicResource VerticalSeparatorStyle}" />
                        <Button Name="btn_xaxis2" Content="Set X-axis Range" IsEnabled="False" Width="95" Height="25" HorizontalAlignment="Left" VerticalAlignment="Center" BorderThickness="0" Background="White"></Button>
                        <Button Name="btn_yaxis2" Content="Set Y-axis Range" Width="95" Height="25" HorizontalAlignment="Left" VerticalAlignment="Center" BorderThickness="0" Background="White"></Button>
                        <Button Name="btn_chart2color" Content="BackColor" Width="60" Height="25" HorizontalAlignment="Left" VerticalAlignment="Center" BorderThickness="0" Background="White" Click="BtnChartBackColor_Click"></Button>
                        <Label Content="Width: " HorizontalAlignment="Left" VerticalAlignment="Center"></Label>
                        <TextBox Name="txt2_width" Style="{StaticResource bground}" Width="120" Height="20" HorizontalAlignment="Left" VerticalAlignment="Center" Background="AntiqueWhite" TextChanged="Txt_width_Changed"></TextBox>
                        <Label Content=" ms" HorizontalAlignment="Left" VerticalAlignment="Center"></Label>
                    </DockPanel>
    
                    <WindowsFormsHost x:Name="host2" Height="160" Grid.Column="1" Grid.Row="3" VerticalAlignment="Top">
                        <mschart:Chart x:Name="WinformChart2" Dock="Fill">
                            <mschart:Chart.ChartAreas>
                                <mschart:ChartArea/>
                            </mschart:Chart.ChartAreas>
    
                            <mschart:Chart.Series>                            
                            </mschart:Chart.Series>
                            
                            <mschart:Chart.Legends>
                                <mschart:Legend/>
                            </mschart:Chart.Legends>
                        </mschart:Chart>
                    </WindowsFormsHost>
    
                    <DockPanel Grid.Row="4">
                        <Label Content="Current" HorizontalAlignment="Left" VerticalAlignment="Center" FontWeight="Bold"></Label>
                        <Button Content="Grid view" Width="60" Height="25" HorizontalAlignment="Left" VerticalAlignment="Center" Background="Aqua" Click="BtnChart3Gridview_Click"></Button>
                        <Button Content="Legends" Width="60" Height="25" HorizontalAlignment="Left" VerticalAlignment="Center" Background="Aqua" Click="BtnChart3Legend_Click"></Button>
                        <Separator Style="{DynamicResource VerticalSeparatorStyle}" />
                        <Button Name="btn_xaxis3" Content="Set X-axis Range" IsEnabled="False" Width="95" Height="25" HorizontalAlignment="Left" VerticalAlignment="Center" BorderThickness="0" Background="White"></Button>
                        <Button Name="btn_yaxis3" Content="Set Y-axis Range" Width="95" Height="25" HorizontalAlignment="Left" VerticalAlignment="Center" BorderThickness="0" Background="White"></Button>
                        <Button Name="btn_chart3color" Content="BackColor" Width="60" Height="25" HorizontalAlignment="Left" VerticalAlignment="Center" BorderThickness="0" Background="White" Click="BtnChartBackColor_Click"></Button>
                        <Label Content="Width: " HorizontalAlignment="Left" VerticalAlignment="Center"></Label>
                        <TextBox Name="txt3_width" Style="{StaticResource bground}" Width="120" Height="20" HorizontalAlignment="Left" VerticalAlignment="Center" Background="AntiqueWhite" TextChanged="Txt_width_Changed"></TextBox>
                        <Label Content=" msec" HorizontalAlignment="Left" VerticalAlignment="Center"></Label>
                    </DockPanel>
    
                    <WindowsFormsHost x:Name="host3" Height="160" Grid.Column="1" Grid.Row="5" VerticalAlignment="Top">
                        <mschart:Chart x:Name="WinformChart3" Dock="Fill">
                            <mschart:Chart.Series>                            
                            </mschart:Chart.Series>
                            <mschart:Chart.ChartAreas>
                                <mschart:ChartArea></mschart:ChartArea>
                            </mschart:Chart.ChartAreas>
                            <mschart:Chart.Legends>
                                <mschart:Legend />
                            </mschart:Chart.Legends>
                        </mschart:Chart>
                    </WindowsFormsHost>
                </Grid>
            </Grid>
        </GroupBox>
    </UserControl>
    

    Below is the Constructor of the xaml.cs

    Dictionary<int, double> value;
            Series s0, s1, s2, s3, vbatt;
            Series chart2_s, chart2_s1, chart2_s2, chart2_s3, chart2_vbatt;
            Series chart3_s, chart3_s1, chart3_s2, chart3_s3, chart3_vbatt;
            //DateTime timeStarted;
            //double yValue = 2;
            ////double dVariance = 0;
            //Random rand;
            //DispatcherTimer timer = new DispatcherTimer();
    
            //public GraphViewModel Xaxismax { get; set; }       
    
            public PowerMonitor()
            {
                InitializeComponent();
                            
                //Xaxismax = new GraphViewModel { xaxisrange = 50 };
                //this.DataContext = Xaxismax;
    
                s0 = WinformChart1.Series.Add("Rail1");
                s1 = WinformChart1.Series.Add("Rail2");
                s2 = WinformChart1.Series.Add("Rail3");
                s3 = WinformChart1.Series.Add("Rail4");
                vbatt = WinformChart1.Series.Add("VBATT");  
                
                chart2_s = WinformChart2.Series.Add("Rail1");
                chart2_s1 = WinformChart2.Series.Add("Rail2");
                chart2_s2 = WinformChart2.Series.Add("Rail3");
                chart2_s3 = WinformChart2.Series.Add("Rail4");
                chart2_vbatt = WinformChart2.Series.Add("VBATT");
    
                chart3_s = WinformChart3.Series.Add("Rail1");
                chart3_s1 = WinformChart3.Series.Add("Rail2");
                chart3_s2 = WinformChart3.Series.Add("Rail3");
                chart3_s3 = WinformChart3.Series.Add("Rail4");
                chart3_vbatt = WinformChart3.Series.Add("VBATT");
    
            }

    Dictionary and series are objects of the class.

    Friday, May 17, 2019 10:36 AM

All replies

  • Hi   Subbu_WPF,

    >>Legends always appears on the adjacent view of the graph and not in the bottom of the graph.

    Unfortunately, I cannot understand this issue clearly. Could you make a detailed description?

    If you want to show Legends on the bottom, you can try the following code.

        WinformChart1.Legends[0].Docking = System.Windows.Forms.DataVisualization.Charting.Docking.Bottom;
                WinformChart1.Legends[0].Alignment = System.Drawing.StringAlignment.Center;

    Or you can upload a minimal runnable demo to us(Including your test material and remove all private information). We can download it and debugging. This will help us quickly analyze your problem. 

    Thank you for your understanding.

    Best Regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, May 20, 2019 3:13 AM
    Moderator