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

标签 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;

如果您能提供任何帮助,我们将非常感谢,这让我发疯太久了!

最佳答案

您应该将 X 轴设置为 DateTime,而不是 double。无论如何,这是一个心电图...... 摆动是由 X 轴增加 0.10000000001234 等值引起的。

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;

关于c# - 如何治愈C# winforms图表的摆动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35733426/

相关文章:

c# - Getter 无体,Setter 有

c# - WPF 绑定(bind)到 contentcontrol.child 调用了两次

c# - 数据绑定(bind) : One vs two way

javascript - 网站有脚本错误,显示为空白

c# - 在 C# 中的外部浏览器中打开嵌入式 html

c# - 为什么 string 类型没有转换为 int 类型?

c# - Android 的 Mono 无法构建 - 找不到 Novell.MonoDroid.CSharp.targets

c# - 使用 FluentFTP 将文件从 FTP 复制到 blob 存储的 Azure 函数

c# - WPF: MemoryStream 占用大量内存

c# - 如何杀死一个窗口?