1

I have a .net program that lets the user select a chart from a dropdown and a data range then queries sql for the data to display the chart, everything is fine unless they pick a date range that would only return 1 value for that particular chart.

When this happens since there is only 1 point on the graph it does not display anything for line or area charts (it does work on bar and column).

i have thought about forcing the graph type to column if there is only 1 data point but was wondering if there was a setting i am missing that would allow a straight line on a line graph if there was only 1 datapoint.

well based on the idea of faking a 2nd point i came up with this that technically does what i want it to do.

this is called when there is only 1 point

 foreach (Series ser in mainChart.Series)
 {
     ser.Points.AddY(ser.Points[0].YValues[0]);
 }

I thought i might should add in case anyone else needs this that the code above needs to be after the chart has been DataBound (had .DataBind() called).

RustyH
  • 473
  • 7
  • 22
  • the definition of a line is two non-congruent points. By definition you cannot have a line with only one point. – Marc B Aug 22 '14 at 14:39
  • Yes i am aware of the definition of a line, and yes i realize it would be pointless as a graph, that doesn't help the end user understand that the chart is not technically empty but only have 1 point. – RustyH Aug 22 '14 at 14:41
  • Then add a second point. e.g. if your single value is `(8,9)`, then add `(0,9)` or `(8,0)` to produce a horizontal/vertical line through your point. – Marc B Aug 22 '14 at 14:41
  • that's basically what i was asking Marc B is if there was a setting that did that automatically, I am converting an old program that used a dated version of infragestics and on their graphs it seemed to do this automatically. In other words it showed a line regardless of the number of points – RustyH Aug 22 '14 at 14:44
  • Does the example have to be real data? Had you thought about faking it with a second point if the example doesn't have the right data? – Joe Aug 22 '14 at 14:46
  • You would rather lie to the user with fake data? The line chart is meaningless in this case – Joe Phillips Aug 22 '14 at 14:47
  • No i don't want fake data but if they unknowingly selected a data range that would only return 1 value i would rather show a straight line across the graph than nothing at all. – RustyH Aug 22 '14 at 14:48
  • i straight line at that value i should add – RustyH Aug 22 '14 at 14:48

3 Answers3

0

The definition of a line is basically the space between two points. You can't make a line out of one point without it being completely arbitrary.

Once you decide which angle the line should be oriented, just make a 2nd data point that fakes the line however you want it to look. But it actually sounds like you'd prefer a scatter plot, not a line chart. I would suggest changing the chart you use. You will most likely be able to draw lines on the scatter plot as well if needed.

Joe Phillips
  • 49,743
  • 32
  • 103
  • 159
0

I put this in my question but since it is the answer thought i would put it here also

this is called when there is only 1 point

foreach (Series ser in mainChart.Series)
{
   ser.Points.AddY(ser.Points[0].YValues[0]);
}
RustyH
  • 473
  • 7
  • 22
0

I had the same problem and I solved it by placing another datapoint with the same values, so it is only to duplicate the same value, when there is only one, and you will place the point. So it would be something similar to this:

this.g.Series[serieID].Points.AddXY(x, y); // <-- Duplicate this line if you only have one value
eberlast
  • 160
  • 2
  • 7