我正在用C#制作一个与捐赠数据链接的图表。每周,总捐款记录在表中。图表会提取这些数字,并以折线图的形式对其进行绘制。

我遇到的问题是每个星期都被记录为一周中的某天,因此应基于星期工作的控件无法正常工作。我在下面附加了我的代码和结果图片。

我想知道为什么这里的每个数据点都显示为一周中的某一天。这是我加载数据的方式吗?

感谢您提供的任何帮助。

这是我的代码:

        #region Chart4
        string DonationSelect = this.DonationSelect.SelectedValue;
        Time = Convert.ToInt32(DonationSelect);

        if (Time > 3)
            Chart4.Series[0].IsValueShownAsLabel = false;
        else
            Chart4.Series[0].IsValueShownAsLabel = true;

        Chart4.Series[0].ChartType = SeriesChartType.Line;
        Chart4.ChartAreas["ChartArea1"].AxisX.IsMarginVisible = true;

        StartDate = GetNextDay(DateTime.Today, DayOfWeek.Sunday);
        TargetDate = GetNextDay(StartDate.AddMonths(-1 * Time), DayOfWeek.Sunday);

        for (DateTime i = TargetDate; i <= DateTime.Now; i = i.AddDays(7))
        {
            int Donations = 0;
            var oDonation = oDatabase.ExecuteScalar(@"SELECT Offering FROM
            tblStats WHERE Date=@Date",
                "Date", i);

            if (oDonation != null && oDonation != DBNull.Value)
            {
                Donations = Convert.ToInt32(oDonation);
            }
            //int Attendance = Convert.ToInt32();

            string Date = i.ToShortDateString();

            Chart4.Series[0].Points.AddXY(Date, Donations);
        }

        StripLine StripLine = new StripLine();

        StripLine.BackColor = Color.LightGray;
        StripLine.Interval = 1;
        StripLine.IntervalType = DateTimeIntervalType.Weeks;
        StripLine.StripWidth = .5;
        StripLine.StripWidthType = DateTimeIntervalType.Weeks;
        StripLine.IntervalOffset = 0;
        StripLine.IntervalOffsetType = DateTimeIntervalType.Days;

        Chart4.Legends.Add(new Legend("Legend3"));
        Chart4.Series[0].Legend = "Legend3";
        Chart4.Series[0].IsVisibleInLegend = true;
        Chart4.Series[0].LegendText = "Donations";
        Chart4.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
        Chart4.BackColor = Color.PowderBlue;
        Chart4.Series[0].BorderWidth = 3;
        Chart4.Series[0].BorderColor = Color.Orange;
        Chart4.ChartAreas["ChartArea1"].AxisX.Title = "Date";
        Chart4.ChartAreas["ChartArea1"].BackColor = Color.LightGray;
        Chart4.ChartAreas["ChartArea1"].BackSecondaryColor = Color.White;
        Chart4.ChartAreas["ChartArea1"].BackGradientStyle = GradientStyle.TopBottom;
        Chart4.ChartAreas["ChartArea1"].AxisX.StripLines.Add(StripLine);
        #endregion

最佳答案

根据http://msdn.microsoft.com/en-us/library/system.globalization.calendar.getweekofyear.aspx,您可以尝试替换以下行:

string Date = i.ToShortDateString();
Chart4.Series[0].Points.AddXY(Date, Donations);


和那些 :

  DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
  Calendar cal = dfi.Calendar;
  Chart4.Series[0].Points.AddXY(cal.GetWeekOfYear(i, dfi.CalendarWeekRule,
                                      dfi.FirstDayOfWeek), Donations);

关于c# - .Net图表将日期显示为一周中的几天,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13614209/

10-10 23:23