.NET Framework Bookmark and Share   
 index > Chart Controls for .NET Framework > Changing column color incrementally
 

Changing column color incrementally

I have a column chart that has 30 data points, eachrepresentinga days worth of sales.

Each column is blue.

I need to switch it so that every 7th day is red.

That way when you look at the chart, if today is Friday, then all Friday columns are red instead of blue. Makes it easier to see sales week over week.

Is this possible?
mrb39876
The trick here is to iterate through the Series.Points object and do some comparison on the XValue. Below is a sample of some random data with the XValue as a DateTime.

DateTime[] dates = new DateTime[30];
double[] sales = new double[30];

Random rand = new Random();


for (int i = 0; i < 30; i++)
{
	dates[i] = DateTime.Now.AddDays(i);
	sales[i] = rand.NextDouble()*30;
}

Series line = new Series();
line.Points.DataBindXY(dates, sales);

chart1.Series.Clear();
chart1.Series.Add(line);

foreach (DataPoint point in line.Points)
{
	DateTime day = DateTime.FromOADate(point.XValue);
	if (day.DayOfWeek == DayOfWeek.Friday)
	{
		point.Color = Color.Red;
	}
}

The comparison above is using the explicit DayOfWeek property of the DateTime. You could substitute this with whatever condition you need to change the color.
byron wall
The trick here is to iterate through the Series.Points object and do some comparison on the XValue. Below is a sample of some random data with the XValue as a DateTime.

DateTime[] dates = new DateTime[30];
double[] sales = new double[30];

Random rand = new Random();


for (int i = 0; i < 30; i++)
{
	dates[i] = DateTime.Now.AddDays(i);
	sales[i] = rand.NextDouble()*30;
}

Series line = new Series();
line.Points.DataBindXY(dates, sales);

chart1.Series.Clear();
chart1.Series.Add(line);

foreach (DataPoint point in line.Points)
{
	DateTime day = DateTime.FromOADate(point.XValue);
	if (day.DayOfWeek == DayOfWeek.Friday)
	{
		point.Color = Color.Red;
	}
}

The comparison above is using the explicit DayOfWeek property of the DateTime. You could substitute this with whatever condition you need to change the color.
byron wall
Perfect.

'foreach (DataPoint point in line.Points)' was the life saver


mrb39876

You can use google to search for other answers

Custom Search

More Threads

• stock Intraday chart on multiple day
• Multiple cursors
• DataBindCrossTable giving a IEnumerable error
• tick marks show through markers
• How do I get AxisLabel value while setting the value for tooltip
• Printing
• problem installing chart controls
• Does the install touch the .Net framework?
• Simple Pie Chart Display
• Need .Net component for ontology graphics