图例底部没有出现使用WindowsFormsHost的图例 [英] Legend is not appearing in the bottom of the graph using WindowsFormsHost

查看:65
本文介绍了图例底部没有出现使用WindowsFormsHost的图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在wpf应用程序上使用WindowsFormsHost。

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.

我无法弄清楚这样做的API。 

I am unable figure out the API to do that. 

下面是xml代码

<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>

以下是xaml.cs的构造函数

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.

推荐答案

嗨    Subbu_WPF,



>>传奇始终出现在相邻的视图中图表,而不是图表底部。

不幸的是,我无法清楚地理解这个问题。你能详细说明一下吗?

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

如果你想在底部显示Legends,你可以试试下面的代码。

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;






或者你可以上传最小的可运行演示给我们(包括您的测试材料并删除所有私人信息)。我们可以下载它并进行调试。这有助于我们快速分析您的问题。 
$


感谢您的理解。



Best问候



Yong Lu

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


这篇关于图例底部没有出现使用WindowsFormsHost的图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆