我第一次尝试使用 C# Charting API 并且遇到了一些问题。我有两个硬件信号,我想将它们绘制在一个 x 轴从 -10 到 10 和一个 y 轴也从 -10 到 10 的图表上。我为此使用 SeriesChartType.Point 图表类型除了一种情况外,代码都按我期望的方式工作。我正在使用 AddXY 函数向图表添加点。实际上,这些值来自硬件,但对于这个示例,我只是对这些值进行了硬编码。
我遇到的问题是,如果要绘制的第一个点的 x 轴值为 0,则图表似乎会忽略 0,并且只需为绘制的每个后续点将 x 轴值增加一个。
我创建了以下简化示例来演示我的问题。我将我的 x 轴值硬编码为 0,将我的 y 轴值硬编码为 2。当它绘制点时,第一个点绘制在 (1,2),下一个点绘制在 (2,2),然后是 (3,2) , (4,2) 最后是 (5,2)。
我应该注意,这只发生在要绘制的第一个点的 x 轴值为 0 时,如果它有任何其他值,图表将永远正确运行。我不知道是什么原因造成的。
请参阅以下示例代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ConfigureChartSettings();
AddFakeData();
}
private void AddFakeData()
{
this.chart1.Series["Series1"].Points.AddXY(0, 2); //Gets plotted at (1,2)
this.chart1.Series["Series1"].Points.AddXY(0, 2); //Gets plotted at (2,2)
this.chart1.Series["Series1"].Points.AddXY(0, 2); //Gets plotted at (3,2)
this.chart1.Series["Series1"].Points.AddXY(0, 2); //Gets plotted at (4,2)
this.chart1.Series["Series1"].Points.AddXY(0, 2); //Gets plotted at (5,2)
}
private void ConfigureChartSettings()
{
//Set point chart type
this.chart1.Series["Series1"].ChartType = SeriesChartType.Point;
//Set the market size
this.chart1.Series["Series1"].MarkerSize = 5;
//Set the marker shape to a circle
this.chart1.Series["Series1"].MarkerStyle = MarkerStyle.Circle;
//X and Y values are both between -10 and 10 so set the x and y axes accordingly
this.chart1.ChartAreas["ChartArea1"].AxisX.Minimum = -10.0;
this.chart1.ChartAreas["ChartArea1"].AxisX.Maximum = 10.0;
this.chart1.ChartAreas["ChartArea1"].AxisY.Minimum = -10.0;
this.chart1.ChartAreas["ChartArea1"].AxisY.Maximum = 10.0;
//Set the titles of the X and Y axes
this.chart1.ChartAreas["ChartArea1"].AxisX.Title = "XSignalName";
this.chart1.ChartAreas["ChartArea1"].AxisY.Title = "YSignalName";
//Set the Intervals of the X and Y axes,
this.chart1.ChartAreas["ChartArea1"].AxisX.Interval = 5;
this.chart1.ChartAreas["ChartArea1"].AxisY.Interval = 5;
//Set the MajorGrid interval to 5.
this.chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.Interval = 5;
this.chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.Interval = 5;
//Set the MinorGrid interval to 1.
this.chart1.ChartAreas["ChartArea1"].AxisX.MinorGrid.Interval = 1;
this.chart1.ChartAreas["ChartArea1"].AxisY.MinorGrid.Interval = 1;
//Set the MinorGrid style to Dot so that it is less obstructive.
this.chart1.ChartAreas["ChartArea1"].AxisX.MinorGrid.LineDashStyle = ChartDashStyle.Dot;
this.chart1.ChartAreas["ChartArea1"].AxisY.MinorGrid.LineDashStyle = ChartDashStyle.Dot;
//Enable the minor grids.
this.chart1.ChartAreas["ChartArea1"].AxisX.MinorGrid.Enabled = true;
this.chart1.ChartAreas["ChartArea1"].AxisY.MinorGrid.Enabled = true;
}
}
我已经浪费了很多时间试图弄清楚为什么会发生这种情况,我确信这是非常简单的事情,但我似乎找不到答案。任何帮助将不胜感激。