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

Fuz*_*zie 12 c# wpf charts xaml winforms

我在C#WPF应用程序中实现了一些实时图表,但我使用的是WinForms图表,因为它们通常很容易使用,而且性能令人惊讶.

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

摇摇欲坠的图表

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

我的问题是:如何防止这个该死的图表不断调整自己?!

一些其他信息:

该图表包含在我的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>
Run Code Online (Sandbox Code Playgroud)

通过以下方式初始化:

// 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();
Run Code Online (Sandbox Code Playgroud)

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

// 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;
Run Code Online (Sandbox Code Playgroud)

你可以提供任何帮助,将不胜感谢,这已经快把我逼疯了的时间太长了!

jsa*_*ics 7

你应该让你的X轴DateTime,没有double.无论如何,这是一个心电图......摆动是由X轴增加0.10000000001234等值引起的.

在此输入图像描述

// 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;
Run Code Online (Sandbox Code Playgroud)