简体   繁体   English

在Windows Form Visual Studio 2010中将图形的X轴除以数周

[英]Divide X-axis of a graph in weeks in windows form visual studio 2010

我想在图表上显示一年(2013-14)的数据,并希望在几周(约52列)的windows窗体Visual Studio 2010中破坏我的x轴。请告诉我如何设置它。

The code below 下面的代码

  • fills a Chart Series with one point per day for a given year 在给定年份中每天用一个点填充一个图表系列
  • sets the Y-values to random test value 将Y值设置为随机测试值
  • will either:
    • show one date every 7 days 每7天显示一次日期
    • or show the week number for the frist day of each week 显示每周第一天的星期数

Please note: The week number can be calculated in various ways, depending on the culture info on your machine. 请注意:周数可以通过多种方式计算,具体取决于您计算机上的区域性信息。 If you need, you can always set the values of CalendarWeekRule and FirstDayOfWeek to the ones you actually want. 如果需要,可以始终将CalendarWeekRuleFirstDayOfWeek的值设置为实际所需的值。


using System.Windows.Forms.DataVisualization.Charting;
using System.Globalization;

private void setChart(int Year)
{

    bool showAsDate = true;
    var s = new Series(year.ToString() );
    s.ChartType = SeriesChartType.Column;
    var d = new DateTime(year, 01, 01);
    int weekday = (int)d.DayOfWeek;
    int maxDays = new DateTime(year, 12, 31).DayOfYear;

    Random R = new Random();

    for (int i = 0; i < maxDays ; i++)
    {
        s.Points.AddXY(d.AddDays(i), R.Next(100) - 50);
    }

    chart1.Series[0].Points.Clear();
    chart1.Series.Clear();
    chart1.Series.Add(s);

    if (showAsDate )
    {
        chart1.Series[0].XValueType = ChartValueType.DateTime;
        chart1.ChartAreas[0].AxisX.LabelStyle.Format = "MM-dd";
        chart1.ChartAreas[0].AxisX.Interval = 7;
        chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Days;
        chart1.ChartAreas[0].AxisX.IntervalOffset = 0;
    }
    else
    {
        DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
        Calendar cal = dfi.Calendar;
        for (int i = 0; i < maxDays ; i++)
        {
           chart1.Series[0].Points[i].AxisLabel = 
                  cal.GetWeekOfYear(d.AddDays(i), 
                  dfi.CalendarWeekRule, dfi.FirstDayOfWeek).ToString();
           chart1.ChartAreas[0].AxisX.IntervalOffset = 2 - weekday;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM