-1

I am trying to add a List to a chart. This list contains a 2 and a 4.

foreach (decimal D in numbers)
{
    barChart.Series[0].Points.AddXY(1, D);
}

It needs to add D to index 1 on the X axis. However, this only outputs 4 rather than six at X1. When it gets to the 4 in the list, it overwrites the 2 that is there, rather than adding to it (making 6). How do I make it add rather than overwrite?

EDIT: I did not give enough information apparently. I am using Windows Forms. The chart I am using is in the Data section of Visual Studio 2015.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
Noah Heber
  • 82
  • 1
  • 12
  • Please read http://stackoverflow.com/help/how-to-ask before asking – Damian Dec 28 '16 at 22:00
  • What chart? Is it a plugin, a model you made, google has google charts which implement barcharts? Please specify –  Dec 28 '16 at 22:01
  • 1
    What are you targetting: Winforms, WPF, ASP..? __Always__ tag your question correctly! - AddXY means that a new DataPoint is added to the Points collection. Each will have an x-value of 1 and the y-values of the (2) points are 2 and 4. It doesn't change the y-values. To do so you would write, maybe: `barChart.Series[0].Points[0].YValues[0] += D` – TaW Dec 28 '16 at 22:02
  • Questions should not have answers in them. Either post your own answer or if TaW's post answers the question, accept that one. – LarsTech Dec 28 '16 at 22:21
  • Please tell me how I should edit my question further. If it is correct, please don't down vote it. – Noah Heber Dec 28 '16 at 22:33

1 Answers1

1

You misunderstand the meaning of the AddXY method.

It doesn't change the y- or any values.

AddXY means that a new DataPoint is added to the Points collection.

In your code each will have an x-value of 1 and the y-values of the two points are 2 and 4. To do so you would write, maybe:

barChart.Series[0].Points[0].YValues[0] += D;

If your numbers are decimals you will need to cast to double, which is the base number type for all Chart values:

barChart.Series[0].Points[0].YValues[0] += (double)D;
TaW
  • 53,122
  • 8
  • 69
  • 111
  • That worked! Thank you! My working code has been edited into my answer, if that's the right thing to do. – Noah Heber Dec 28 '16 at 22:23
  • 1
    Nevermind, LarsTech pointed out I should not have edited in my own answer into my question. Anyway, make sure you have a try statement on it, because if you don't already have any points, it will throw an index out of range exception. – Noah Heber Dec 28 '16 at 22:27
  • Yes, you are quite right. But not adding all necessary checks is rather normal for SO answers. They show how to make code work, not how to make it perfect, safe etc.. Depending on your demands a direct check on the Points available may be better than try/catch.. – TaW Dec 28 '16 at 22:30
  • Here is the code I am using: try { barChart.Series[0].Points[0].YValues[0] += (double)d; } catch { barChart.Series[0].Points.AddXY(1, d); } – Noah Heber Dec 28 '16 at 22:38
  • It is fine. The alternative might look like this: `if ( barChart.Series[0].Points.Count > 0) barChart.Series[0].Points[0].YValues[0] += (double)d; else barChart.Series[0].Points.AddXY(1, d); ` – TaW Dec 28 '16 at 22:40
  • Ok. Is there a difference? Should I use one over the other? – Noah Heber Dec 28 '16 at 22:44
  • I personally would usually prefer a check over an exception; not sure if try/catch is more expensive but I guess so; and it reads more like something unexpected which it really isn't. MSDN says: _you should stay away from things like using exceptions for control flow._. See [here](http://stackoverflow.com/questions/3480124/performance-cost-of-a-try-catch-block) but also [here](http://stackoverflow.com/questions/161942/how-slow-are-net-exceptions).. – TaW Dec 28 '16 at 22:52