【问题标题】:remove white spaces below winforms graph删除winforms图形下方的空格
【发布时间】:2015-07-11 06:24:52
【问题描述】:

我在 winforms 中创建了一个图表,并使用 winformhost 在 WPF 中托管它。当我在 wpf 中实现它时,我的图表下方有一个空格。如果我减小图表的大小,图表会变小。如何删除图表下方的空间?

这是我的代码。

XAML.cs

var chartArea = new ChartArea("EQGraph");

Chart chart1 = this.FindName("EQGraph") as Chart;
chart1.ChartAreas.Add("EQGraph");

chart1.Series.Add("Front Left");

chart1.ChartAreas[0].AxisX.Maximum = 20000;
chart1.ChartAreas[0].AxisX.Minimum = 10;
chart1.ChartAreas[0].AxisX.IsLogarithmic = true;

chart1.ChartAreas[0].AxisX.MinorGrid.Interval = 1;
chart1.ChartAreas[0].AxisX.MinorGrid.Enabled = true;

chart1.ChartAreas[0].AxisY.Maximum = 20;
chart1.ChartAreas[0].AxisY.Minimum = -50;
chart1.ChartAreas[0].AxisY.Interval = 5;

chart1.ChartAreas[0].AxisX.MinorGrid.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.DashDotDot;

chart1.ChartAreas[0].AxisY.MajorGrid.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.DashDotDot;

chart1.ChartAreas[0].AxisX.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 10F, System.Drawing.FontStyle.Bold);
chart1.ChartAreas[0].AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 10F, System.Drawing.FontStyle.Bold);

chart1.ChartAreas[0].AxisX.Title = "Frequency(Hz)";
chart1.ChartAreas[0].AxisY.Title = "Gain";

int[] xValuesFrontLeft = { 10, 100, 1000, 5000, 4200, 8499 };
int[] yValuesFrontLeft = { 16, 10, -5, -10, 35, -40 };
chart1.Series["Front Left"].Points.DataBindXY(xValuesFrontLeft, yValuesFrontLeft);

chart1.Series["Front Left"].ChartType = SeriesChartType.Spline;

XAML

<DockPanel Grid.Column="3" Grid.Row="1" Grid.RowSpan="9" Background="#FFFBF9F9">

  <WindowsFormsHost x:Name="host" Height="500">
    <winformchart:Chart x:Name="EQGraph" Dock="Fill">
      <winformchart:Chart.Series >
        <winformchart:Series Name="series" ChartType="Line"/>
      </winformchart:Chart.Series>
      <winformchart:Chart.ChartAreas>
        <winformchart:ChartArea/>
      </winformchart:Chart.ChartAreas>
    </winformchart:Chart>
  </WindowsFormsHost>

</DockPanel>

【问题讨论】:

    标签: c# wpf winforms


    【解决方案1】:

    我认为您不需要创建新的ChartArea 来放置图表,因为您的图表已经在 XAML 中放置在 WindowsFormsHost 中,并且它已停靠,因此它的大小与其主机相同。像这样使用该图表:

    // Remove these lines
    // var chartArea = new ChartArea("EQGraph");
    // Chart chart1 = this.FindName("EQGraph") as Chart;
    // chart1.ChartAreas.Add("EQGraph");
    
    // Now you will be adding values, series and properties to your graph inside WinFormsHost
    Chart chart1 = EQGraph;
    

    您也可以在代码中的所有位置直接将chart1 替换为EQGraph

    EQGraph.Series.Add("Front Left");
    
    EQGraph.ChartAreas[0].AxisX.Maximum = 20000;
    EQGraph.ChartAreas[0].AxisX.Minimum = 10;
    EQGraph.ChartAreas[0].AxisX.IsLogarithmic = true;
    
    EQGraph.ChartAreas[0].AxisX.MinorGrid.Interval = 1;
    EQGraph.ChartAreas[0].AxisX.MinorGrid.Enabled = true;
    
    EQGraph.ChartAreas[0].AxisY.Maximum = 20;
    EQGraph.ChartAreas[0].AxisY.Minimum = -50;
    EQGraph.ChartAreas[0].AxisY.Interval = 5;
    
    EQGraph.ChartAreas[0].AxisX.MinorGrid.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.DashDotDot;
    
    EQGraph.ChartAreas[0].AxisY.MajorGrid.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.DashDotDot;
    
    EQGraph.ChartAreas[0].AxisX.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 10F, System.Drawing.FontStyle.Bold);
    EQGraph.ChartAreas[0].AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 10F, System.Drawing.FontStyle.Bold);
    
    EQGraph.ChartAreas[0].AxisX.Title = "Frequency(Hz)";
    EQGraph.ChartAreas[0].AxisY.Title = "Gain";
    
    int[] xValuesFrontLeft = { 10, 100, 1000, 5000, 4200, 8499 };
    int[] yValuesFrontLeft = { 16, 10, -5, -10, 35, -40 };
    EQGraph.Series["Front Left"].Points.DataBindXY(xValuesFrontLeft, yValuesFrontLeft);
    
    EQGraph.Series["Front Left"].ChartType =SeriesChartType.Spline;
    

    【讨论】:

    • 非常感谢老兄,你让我开心:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    • 2018-02-20
    • 2011-12-08
    • 2016-02-14
    • 1970-01-01
    相关资源
    最近更新 更多