简体   繁体   中英

X-axis value increments automatically when first point plotted is at (0,0) using C# Chart API

I'm trying to use the C# Charting API for the first time and am having some problems. I have two hardware signals that I want to plot on a graph that has an x-axis that goes from -10 to 10 and a y-axis also from -10 to 10. I'm using the SeriesChartType.Point chart type for this and in all but one case the code works the way I expect. I'm adding points to the chart by using the AddXY function. In reality the values are coming from hardware but for this example I simply hard coded the values.

The problem I'm having is that if the very first point to be plotted has an x-axis value of 0 the chart seems to ignore the 0 and simply increment the x-axis value by one for each subsequent point that is plotted.

I created the following simplified example to demonstrate my problem. I hard coded my x-axis value as 0 and my y-axis value at 2. When it plots points the first point is plotted at (1,2), the next at (2,2), then (3,2), (4,2) and finally (5,2).

I should note that this only happens if the very first point to be plotted has an x-axis value of 0, if it has ANY other value the chart will function correctly forever. I'm at a loss as to what could be causing this.

Please see the following example code:

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;
    }
}

I've already wasted a bunch of time trying to figure out why this could happen and I'm sure it is something incredibly simple but I can't seem to find the answer. Any help would be greatly appreciated.

In this case MsChart is trying to be too helpful. It detects you have inserted data points with identical X == 0 and so auto-magically switch to using the point index as X value. Note that it only happens if X is 0.

You can fix it by adding an extra empty point with a X != 0 . So adding this at the start of your data entry will give you the plot you want.

// insert an empty point with X != 0
this.chart1.Series["Series1"].Points.AddXY(10, 10);
this.chart1.Series["Series1"].Points[0].IsEmpty = true;
// add your points as normal
this.chart1.Series["Series1"].Points.AddXY(0, 2);
this.chart1.Series["Series1"].Points.AddXY(0, 2);
this.chart1.Series["Series1"].Points.AddXY(0, 2);
this.chart1.Series["Series1"].Points.AddXY(0, 2);
this.chart1.Series["Series1"].Points.AddXY(0, 2);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM