GabrielVa
GabrielVa

Reputation: 2388

Drawing chart using C# in Visual Studio

I'm working on a web app with a .NET chart. I want to be able to place a tick that has today's date on the X axis.

Is there an easy way to put this on there along with the other dates in my chart? The data is derived from a SQL view.

Upvotes: 0

Views: 1161

Answers (1)

scrat.squirrel
scrat.squirrel

Reputation: 3836

You could copy the data series that you have - to a new series by enumerating/iterating through the Points collection, then insert your (new) data point where you want it to be in the new series. Then assign/replace the new series into your chart. See namespace System.Windows.Forms.DataVisualization.Charting, see classes Series and DataPointCollection.

This is how I'd write it (may need some improvement):

Series oldSeries = myChart.Series[0];
Series newSeries = new Series();
DataPointCollection newPoints = new DataPointCollection();
double newXValue, newYValue;
for (int i = 0; i < oldSeries.Points.Count; i++)
{
  //... add old points here
  newPoints.AddXY(oldSeries.Points[i].XValue, oldSeries.Points[i].YValues[0]);
  if (oldSeries.Points[i] ...) //your condition here
  {
    //your logic for the new point
    newXValue = 100;
    newYValue = 100;
    newPoints.AddXY(newXValue, newYValue);
  }
}
newSeries.Points = newPoints;
myChart.Series.Clear();
myChart.Series.Add(newSeries);

Hope this helps :)

Upvotes: 2

Related Questions