I have created a chart in winforms and host it in WPF using winformhost. When I am implementing this in wpf, there is a space below my graph. If I reduce the size of chart, chart becomes small. How to remove that space below the graph?
Here is my code.
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>
I don't think you need to create new ChartArea
to place your graph, because your graph is placed inside WindowsFormsHost
in XAML already, and it's docked, so it will be the same size as it's host. Use that chart like this:
// 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;
You could also replace chart1
with EQGraph
inside your code directly, at all places:
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.