ASP.NET图表:如何使用for循环绘制系列点

时间:2014-07-23 16:23:25

标签: c# asp.net for-loop charts

我已经以编程方式构建了一个图表,但我现在要做的是格式化X轴,以便每个点代表24小时格式的时间。

我想从" 04:00:00"开始时间并一直走到" 16:00:00"间隔15分钟 - 我需要在X轴上绘制40个点。不要担心Y轴值。我知道我想用数据库填充这些值。

这是我到目前为止所开始的(如下所示),但我还无法想象如何使用for循环来实现这一目标。

有人可以了解我如何实现这一目标吗?

//for (int i = 0; i < 41; i++)
    //{
    Chart1.Series[0].Points.AddXY("04:00:00", 5);
    Chart1.Series[0].Points.AddXY("04:15:00", 10);
    Chart1.Series[0].Points.AddXY("04:30:00", 15);
    Chart1.Series[0].Points.AddXY("04:45:00", 25);
    ...
    ...
    ...
    Chart1.Series[0].Points.AddXY("16:00:00", 25);
//}

以下是我的全部代码:FYI。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.DataVisualization.Charting;

namespace DishMES
{
   public partial class WebForm1 : System.Web.UI.Page
   {
    protected void Page_Load(object sender, EventArgs e)
    {
        buildRepairedChart();
    }

    public void buildRepairedChart()
    {
        //CREATE CHART
        Chart1.Height = Unit.Pixel(200);
        Chart1.Width = Unit.Pixel(900);

        //DEFINE CHART AREA
        ChartArea chartArea = new ChartArea();
        Chart1.ChartAreas.Add(chartArea);

        ChartArea3DStyle areaStyle = new ChartArea3DStyle(chartArea);
        areaStyle.Rotation = 0;
        Axis yAxis = new Axis(chartArea, AxisName.Y);
        Axis xAxis = new Axis(chartArea, AxisName.X);
        var ca = Chart1.ChartAreas[0];
        ca.AxisX.LabelStyle.Format = "hh:mm:ss";
        ca.AxisX.Minimum = 0;
        ca.AxisX.Interval = 1;


        // BUILD CHART SERIES
        Series series = new Series("Default");
        Chart1.Series.Add(series);
        var s = Chart1.Series[0];
        s.ToolTip = "#VALX{t}";
        s.XValueType = ChartValueType.Time;

        //TIME STUFF


        // BIND DATA TO CHART
        //for (int i = 0; i < 41; i++)
        //{
        Chart1.Series[0].Points.AddXY("04:00:00", 5);
        Chart1.Series[0].Points.AddXY("04:15:00", 10);
        Chart1.Series[0].Points.AddXY("04:30:00", 15);
        Chart1.Series[0].Points.AddXY("04:45:00", 25);
        //}
        Chart1.DataBind();
    }
}

}

2 个答案:

答案 0 :(得分:0)

假设您的数据来自source,例如列表或数据集,

Series s = new Series();
int i=0;
foreach(var d in source)
{
    s.Points.AddXY(d.Date, d.Point); //or d.Key,d.Value, or d["Date"],d["Point"]
}

Chart1.Series.Add(s);

我真的希望你不必手动绘制日期。

var list = new Dictionary<DateTime,object>();
DateTime start = DateTime.ParseExact(
        "3:45:00 AM", 
        "h:mm:ss tt", 
        CultureInfo.InvariantCulture);
for(int i=0;i<48;i++)
{
    start=start.AddMinutes(15);
    list.add(start,source[i]);
}

然后如上所述。

答案 1 :(得分:0)

以下是答案:

        // BIND DATA TO CHART
        for (int i = 0; i < 43; i++)
        {
            //TIME STUFF FOR SQL
            int startBin = 14400;
            int endBin = 15300;
            TimeSpan stime = TimeSpan.FromSeconds(startBin + (i * 900));
            TimeSpan etime = TimeSpan.FromSeconds(endBin + (i * 900));
            string xAxisStart = string.Format("{0:D2}:{1:D2}:{2:D2}", stime.Hours, stime.Minutes, stime.Seconds);
            string xAxisStop = string.Format("{0:D2}:{1:D2}:{2:D2}", etime.Hours, etime.Minutes, etime.Seconds);

            //TARGET SERIES 
            RepairedChart.Series["REPAIRED"].Points.AddXY(xAxisStart, getRepaired2(xAxisStart, xAxisStop));
            RepairedChart.Series["TARGET"].Points.AddXY(xAxisStart, getScheduleRepairTarget(xAxisStart, xAxisStop));
        }