ASP.. NET图表:如何使用for循环绘制序列点
本文关键字:绘制 循环 for 何使用 NET 图表 ASP | 更新日期: 2023-09-27 18:14:30
我已经用程序建立了一个图表,但是我现在要做的是格式化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);
//}
这是我的全部代码:供参考。
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();
}
}
}
如果您的数据来自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]);
}
答案如下:
// 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));
}