Visual Studio 2010 Chart control: Make Y Axis an Integer value, not decimal
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 who开发者_如何转开发le numbers for this axis?
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;
chartClicks.ChartAreas(0).AxisY.IntervalOffsetType = DateTimeIntervalType.Number
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;
}
<ChartAreas>
<asp:ChartArea Name="Default">
<AxisY Interval="1" IntervalType="Number"></AxisY>
</asp:ChartArea>
</ChartAreas>
精彩评论