Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Trying to implement a simple line chart in C# (2013) that applies a series of negative to positive values on both the X and Y axis. However the chart does not accept the negative Series points for the X axis. The -100 to +100 values are properly displayed on the X axis, but the -50 "Y" value originates at the "0" X axis value position. What am I doing wrong?

The C# code:

C#
public void DrawSampleChart()
{
    Graph.Chart chart = new Graph.Chart();
    chart.Dock = DockStyle.Fill;
    Main.ChartPanel.Controls.Clear();
    Main.ChartPanel.Controls.Add(chart);
    chart.ChartAreas.Add(new Graph.ChartArea());

    //  Define the horizontal ("X") axis attributes
    chart.ChartAreas[0].AxisX.Minimum = -100.0;
    chart.ChartAreas[0].AxisX.Maximum = 100.0;
    chart.ChartAreas[0].AxisX.Interval = 10;

    //  Define the vertical ("Y") axis attributes
    chart.ChartAreas[0].AxisY.Minimum = -100;
    chart.ChartAreas[0].AxisY.Maximum = 100;
    chart.ChartAreas[0].AxisY.Interval = 10;

    //  Add sample series
    Graph.Series series = new Graph.Series("Series1");
    series.ChartType = Graph.SeriesChartType.Line;
    for (int n = -50; n <= 50; n++)
        series.Points.Add(n, n);
    chart.Series.Add(series);
}


What I have tried:

The above code compiles and executes on Visual Studio 2013.
Posted
Updated 6-Jun-16 14:39pm

1 solution

Found the problem. Changed the code as follows

C#
for (int n = -50; n <= 50; n++)
    //series.Points.Add(n, n);    // this was wrong
    series.Points.Add(new Graph.DataPoint(n,n));
chart.Series.Add(series);
 
Share this answer
 
Comments
Patrice T 7-Jun-16 16:51pm    
Use Accept answer to close the question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900