如何治愈C#winforms图表的摆动?

时间:2016-03-01 21:02:20

标签: c# wpf winforms xaml charts

我在C#WPF应用程序中实现了一些实时图表,但我使用的是WinForms图表,因为它们通常很容易使用并且性能出乎意料。

无论如何,我的图表工作得很好,除了一个我无法解决的重大问题:

Wiggly Chart

当我向图表添加数据时,它有时会自行调整大小。有时它会做很多事情,给自己带来摆动,使得图表在阅读和处理时非常烦人。

我的问题如何防止这个该死的图表不断调整大小?!

其他一些信息:

图表包含在我的XAML中:

<WindowsFormsHost Grid.Row="1" Grid.ColumnSpan="2" Margin="5">
  <winformchart:Chart Dock="Fill" x:Name="Session0Chart">
    <winformchart:Chart.ChartAreas>
      <winformchart:ChartArea/>
    </winformchart:Chart.ChartAreas>
  </winformchart:Chart>
</WindowsFormsHost>

通过以下方式初始化:

// initialize it!
chart.Palette = ChartColorPalette.Bright;

// setup the labels
Font monoSpaceFont = new Font("Consolas", 10);
chart.ChartAreas[0].AxisX.LabelStyle.Font = monoSpaceFont;
chart.ChartAreas[0].AxisY.LabelStyle.Font = monoSpaceFont;

// set the axis limits appropriately
chart.ChartAreas[0].AxisY.Maximum = 600;
chart.ChartAreas[0].AxisY.Minimum = -200;

// set up grid lines and axis styles
chart.ChartAreas[0].AxisX.MinorGrid.Enabled = true;
chart.ChartAreas[0].AxisX.MinorGrid.LineDashStyle = ChartDashStyle.Dash;
chart.ChartAreas[0].AxisX.MinorGrid.LineColor = System.Drawing.Color.Gray;
chart.ChartAreas[0].AxisX.MinorGrid.Interval = 0.04;
chart.ChartAreas[0].AxisX.LabelStyle.Format = "F2";
chart.ChartAreas[0].AxisX.LabelAutoFitStyle = LabelAutoFitStyles.None;

chart.ChartAreas[0].AxisY.MajorGrid.Enabled = true;
chart.ChartAreas[0].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Solid;
chart.ChartAreas[0].AxisY.MajorGrid.Interval = 200;
chart.ChartAreas[0].AxisY.LabelAutoFitStyle = LabelAutoFitStyles.None;
chart.ChartAreas[0].AxisY.LabelStyle.Format = "F0";

chart.Series.Clear();

Series s = new Series();
s.ChartType = SeriesChartType.FastLine;
chart.Series.Add(s);
chart.Refresh();

数据点通过以下方式添加:

// if we get too many points, remove the head
if (session.Chart.Series[0].Points.Count >= Properties.Settings.Default.ECGDataPoints)
{
    session.Chart.Series[0].Points.RemoveAt(0);
}

// add the points
for (int i = data.samples.Length - 1; i >= 0; i--)
{
    session.Chart.Series[0].Points.AddXY(session.ecgT, data.samples[i]);
    session.ecgT += session.ECGPeriod / (double)data.samples.Length;
}

// only look at the last few seconds
session.Chart.ChartAreas[0].AxisX.Maximum = session.ecgT;
session.Chart.ChartAreas[0].AxisX.Minimum = session.ecgT - Properties.Settings.Default.ECGTimeWindow;

你可以提供任何帮助非常赞赏,这让我疯狂了太长时间了!

3 个答案:

答案 0 :(得分:7)

您应该设置X轴DateTime,而不是double。无论如何,这是一个心电图...... 通过使用诸如0.10000000001234之类的值递增X轴来引起摆动。

enter image description here

// No wiggle
        chartNoWiggle.Series[0].Points.AddXY(xdatetime, r.NextDouble());

        if (chartNoWiggle.Series[0].Points.Count > 10)
            chartNoWiggle.Series[0].Points.RemoveAt(0);

        chartNoWiggle.ChartAreas[0].AxisX.Minimum = chartNoWiggle.Series[0].Points[0].XValue;
        chartNoWiggle.ChartAreas[0].AxisX.Maximum = xdatetime.ToOADate();

        xdatetime = xdatetime.AddMinutes(1);

// Wiggle
        chartWiggle.Series[0].Points.AddXY(xdouble, r.NextDouble());

        if (chartWiggle.Series[0].Points.Count > 10)
            chartWiggle.Series[0].Points.RemoveAt(0);

        chartWiggle.ChartAreas[0].AxisX.Minimum = chartWiggle.Series[0].Points[0].XValue;
        chartWiggle.ChartAreas[0].AxisX.Maximum = xdouble;

        xdouble += 0.10000000001234;

答案 1 :(得分:1)

chart.ChartAreas[0].InnerPlotPosition.X = 50;
chart.ChartAreas[0].InnerPlotPosition.Y = 5;
chart.ChartAreas[0].InnerPlotPosition.Width = 80;
chart.ChartAreas[0].InnerPlotPosition.Height = 80;

Read this.

答案 2 :(得分:1)

您可以设置ChartArea PositionInnerPlotPosition以避免这些摆动。

chrt.ChartAreas[0].Position.Auto = false;
chrt.ChartAreas[0].Position.Height = 70;
chrt.ChartAreas[0].Position.Y = 5;

chrt.ChartAreas[0].InnerPlotPosition.Auto = false;
chrt.ChartAreas[0].InnerPlotPosition.Height = 50;
chrt.ChartAreas[0].InnerPlotPosition.Y = 10;