我在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>
答案 0 :(得分:0)
我认为您不需要创建新的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;