Secondary axis labels not working?

Okay so I have a datetime x-axis on an MSChart. I want to plot months below the first of each month and years below the change of a year. Here's what I have so far:

for (int i = 0; i < rdate.Length -1 ; i++)
{
    if (rdate[i].Day == 01 && set == 0)

    chart1.ChartAreas[0].AxisX.CustomLabels.Add(
       rdate[i].AddDays(-20).ToOADate(), rdate[i].AddDays(20).ToOADate(), 
       Convert.ToString(rdate[i].ToString("MMMM")), 1, LabelMarkStyle.None);
    set = 1;

    if (rdate[i].Day > 01) 
    set = 0;

    i++;

    if (rdate[i].Year > rdate[i-1].Year)

    chart1.ChartAreas[0].AxisX.CustomLabels.Add(
       rdate[i].AddDays(-20).ToOADate(), rdate[i].AddDays(20).ToOADate(), 
       Convert.ToString(rdate[i].ToString("yyyy")), 2, LabelMarkStyle.None);     
}

However for some reason this skips some months... The years do not show up at all.

rdate is a datetime array used to populate the x axis.

Here is an example of what my code does: 在这里输入图像描述

As you can see, the labels are behaving unexpectedly. I would also like to show a larger tick mark for these dates, and reduce the number of day labels based upon the date range, but I'm at a loss. Anyone done this sort of thing before?


I recently had a similar issue with MSChart when adding too many labels to the x-axis. The solution was reduce the number of ticks without losing data.

This approach worked for me but you will have to adapt it to your specific needs.

dataSeries.XValueType = ChartValueType.Auto;

dataSeries.Points.AddXY(record.DateTime, value);

I then determined the min and max dates for the given data to determine the preferred interval, your implementation will vary:

var totalDays = (maxDate.Value - minDate.Value).TotalDays;

if (totalDays < 60)
    chartArea.AxisX.IntervalType = DateTimeIntervalType.Days;
else if (totalDays < 120)
    chartArea.AxisX.IntervalType = DateTimeIntervalType.Weeks;
else
    chartArea.AxisX.IntervalType = DateTimeIntervalType.Months;

Specify the AxisX label format: In your case you might have to change the Format together with the interval.

chartArea.AxisX.LabelStyle.Format =  Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern;

Hopefully there are some key parts that will provide value for you but you still have to modify it for your particular needs.

链接地址: http://www.djcxy.com/p/10480.html

上一篇: 在Mac OS X 10.5.8中安装Nokogiri gem的问题

下一篇: 辅助轴标签不起作用?