Reputation: 24679
I like the way that the chart control seems to automatically determine the X axis range for me based on the data, but in this case, the data can only be whole numbers.
What is the easiest way to specify whole numbers for this axis?
Upvotes: 6
Views: 7087
Reputation: 669
<ChartAreas>
<asp:ChartArea Name="Default">
<AxisY Interval="1" IntervalType="Number"></AxisY>
</asp:ChartArea>
</ChartAreas>
Upvotes: 0
Reputation: 66
Looks like you can do this by setting the YValueType
property on any relevant series to an integer type - for example:
Chart.Series[0].YValueType = ChartValueType.Int32;
Upvotes: 4
Reputation: 133
It isn't working for very small amounts (1-5 clicks). I have solved that like this:
Dictionary<string, int> clicks = new Dictionary<string, int>();
for (int i = 0; i < 24; i++)
{
clicks.Add(string.Format("{0:00}:00", i), 0);
}
foreach (DateTime dateTime in rawClicks)
{
clicks[string.Format("{0:00}:00", dateTime.Hour)]++;
}
Chart chart = new Chart();
chart.ChartAreas.Add("Default");
Axis x = chart.ChartAreas["Default"].AxisX;
Axis y = chart.ChartAreas["Default"].AxisY;
x.Interval = 1;
x.IntervalType = DateTimeIntervalType.Auto;
chart.Series.Add("Default");
chart.Series["Default"]["PixelPointWidth"] = "15";
int maxNumberOfClicks = 0;
for (int i = 0; i < 24; i++)
{
string key = string.Format("{0:00}:00", i);
chart.Series["Default"].Points.AddXY(key, clicks[key]);
if (maxNumberOfClicks < clicks[key])
{
maxNumberOfClicks = clicks[key];
}
}
y.Interval = Math.Ceiling((double)maxNumberOfClicks / (double)7);
if (y.Interval == 0)
{
y.Interval = 1;
}
Upvotes: 2
Reputation: 24679
chartClicks.ChartAreas(0).AxisY.IntervalOffsetType = DateTimeIntervalType.Number
Upvotes: 2