MsChart 实时曲线

bychgh 2011-04-08 10:00:32
用 ScriptManager ,timer, 来做一个实时动态曲线图,每3秒从数据库中提取最新一条数据给Y轴赋值,设定X轴数值大于20的时候,X轴数值自动加1,


while (this.Chart1.Series[0].Points.Count > 20)
{
foreach (Series s in this.Chart1.Series)
{
s.Points.RemoveAt(0);
}
}

double axisMinimum = this.Chart1.Series[0].Points[0].XValue;
this.Chart1.ChartAreas[0].AxisX.Minimum = axisMinimum;
this.Chart1.ChartAreas[0].AxisX.Maximum = axisMinimum + 20;

曲线走到21,就报错:索引超出范围


代码片段



protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
string strWhere = "";
DataTable myDataTable0 = tools.GetPageInfoTable("wxdysjjc", "bh,cgqmc,sbbh", "id", "0", strWhere, 0);
if (myDataTable0.Rows.Count > 0)
{
this.Chart1.Series[0].Points.AddXY(0, 0);
foreach (DataRow row in myDataTable0.Rows)
{
string seriesName = row["cgqmc"].ToString();
Chart1.Series.Add(seriesName);
Chart1.Series[seriesName].ChartType = SeriesChartType.Line;
Chart1.Series[seriesName].BorderWidth = 1;
this.Chart1.Series[seriesName].Points.AddXY(0, 0);

sbbhlist += row["sbbh"].ToString() + ",";
}
this.shebei_bh.Value = sbbhlist;
}

}
// Chart1.Titles.Add("数据监测");
Chart1.ChartAreas[0].AxisY.Title = "数值";

this.Chart1.Series[0].IsVisibleInLegend = true;
this.Chart1.ChartAreas[0].AxisX.Interval = 2;//数据间隔
this.Chart1.ChartAreas[0].AxisX.MinorTickMark.Interval = 2;//刻度
this.Chart1.ChartAreas[0].AxisX.MinorTickMark.Enabled = true;//显示刻度

}
protected void timer1_Tick(object sender, EventArgs e)
{
string allbh = this.shebei_bh.Value;
string strTmp = allbh.Substring(0, allbh.Length - 1);
if (strTmp != "")
{
string[] sbbhlist = strTmp.Split(',');

for (int i = 0; i < sbbhlist.Length; i++)
{
Series series = this.Chart1.Series[i];

int xCount = series.Points.Count == 0 ? 0 : series.Points.Count - 1;
// double lastXValue = series.Points.Count == 0 ? 1 : series.Points[xCount].XValue + 1;

double lastYValue = series.Points[series.Points.Count - 1].YValues[0];
double lastXValue = series.Points[series.Points.Count - 1].XValue + 1;
string strwhere = tools.GetWherePrameter("sbbh", "sbbh", sbbhlist[i].ToString(), DbOperator.Equal);
DataTable DataTablejilu = tools.GetPageInfoTable("wxdylsjl", "*", "cj_sj", "1", strwhere, 1);
if (DataTablejilu.Rows[0]["zhi"].ToString() != "")
{
lastYValue = Convert.ToInt32(DataTablejilu.Rows[0]["zhi"].ToString());
}
else
{
lastYValue = 0;
}
lastYValue = Convert.ToInt32(DataTablejilu.Rows[0]["zhi"].ToString());
series.Points.AddXY(lastXValue++, lastYValue);

while (this.Chart1.Series[0].Points.Count > 20)
{
foreach (Series s in this.Chart1.Series)
{
s.Points.RemoveAt(0);
}
}

double axisMinimum = this.Chart1.Series[0].Points[0].XValue;
this.Chart1.ChartAreas[0].AxisX.Minimum = axisMinimum;
this.Chart1.ChartAreas[0].AxisX.Maximum = axisMinimum + 20;

}


}


}




前台


<form id="form1" runat="server">
<asp:HiddenField ID="shebei_bh" runat="server" />
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="updatepanel1" runat="server">
<ContentTemplate>
<asp:Chart ID="Chart1" runat="server" BackColor="LightSteelBlue" BackGradientStyle="TopBottom"
BackSecondaryColor="White" EnableTheming="False" EnableViewState="True" Height="317px"
Width="415px">
<%-- <Legends>
<asp:Legend Alignment="Center" Docking="Bottom" Name="Legend1" Title="图例">
</asp:Legend>
</Legends>--%>

<Series>
<asp:Series Name="Series1" ChartType="Line">
</asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="Default" BorderColor="64, 64, 64, 64" BorderDashStyle="Solid"
BackSecondaryColor="White" BackColor="64, 165, 191, 228" ShadowColor="Transparent"
BackGradientStyle="TopBottom">
<Position Y="2" Height="94" Width="94" X="2"></Position>
<AxisY LineColor="64, 64, 64, 64" Maximum="100">
<LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold"></LabelStyle>
<MajorGrid LineColor="64, 64, 64, 64"></MajorGrid>
</AxisY>
<AxisX LineColor="64, 64, 64, 64" IsMarginVisible="False" Maximum="20" Minimum="0"
IsStartedFromZero="False">
<LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold"></LabelStyle>
<MajorGrid LineColor="64, 64, 64, 64"></MajorGrid>
</AxisX>
</asp:ChartArea>
</ChartAreas>
</asp:Chart>
<div>
<asp:Timer ID="timer1" Interval="2000" runat="server" OnTick="timer1_Tick">
</asp:Timer>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>

...全文
506 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
bychgh 2011-04-09
  • 打赏
  • 举报
回复
关键在这里,问题解决,换成这种

// double lastXValue = series.Points.Count == 0 ? 1 : series.Points[xCount].XValue + 1;




int xCount = series.Points.Count == 0 ? 0 : series.Points.Count - 1;
// double lastXValue = series.Points.Count == 0 ? 1 : series.Points[xCount].XValue + 1;

double lastYValue = series.Points[series.Points.Count - 1].YValues[0];
double lastXValue = series.Points[series.Points.Count - 1].XValue + 1;

bychgh 2011-04-08
  • 打赏
  • 举报
回复
贴一个 y轴 数值是随机数产生的,这个可以正常

protected void Page_Load(object sender, EventArgs e)
{

string myconn = ConfigurationManager.AppSettings["conn_access"];
OleDbConnection conn = new OleDbConnection(myconn);
string mysql = "select * from jcsj";
OleDbCommand cmd = new OleDbCommand(mysql, conn);
conn.Open();
OleDbDataAdapter oda = new OleDbDataAdapter();
oda.SelectCommand = cmd;
DataSet ds = new DataSet();
oda.Fill(ds);
sbCount = ds.Tables[0].Rows.Count;

if (!IsPostBack)
{
// Chart1.Titles.Add("数据监测");
this.Chart1.Series[0].Points.AddXY(0, 0);
foreach (DataRow row in ds.Tables[0].Rows)
{
string seriesName = row["cgqmc"].ToString();
Chart1.Series.Add(seriesName);
Chart1.Series[seriesName].ChartType = SeriesChartType.Line;
Chart1.Series[seriesName].BorderWidth = 1;
this.Chart1.Series[seriesName].Points.AddXY(0, 0);

}
}

Chart1.ChartAreas[0].AxisY.Title = "数值";


this.Chart1.ChartAreas[0].AxisX.Interval = 3;//数据间隔为5
this.Chart1.ChartAreas[0].AxisX.MinorTickMark.Interval = 3;//刻度为1
this.Chart1.ChartAreas[0].AxisX.MinorTickMark.Enabled = true;//显示刻度
}
protected void timer1_Tick(object sender, EventArgs e)
{
Random rand = new Random();
//for (int col = 1; col < ds.Tables[0].Columns.Count; col++)
//{
// string colName = ds.Tables[0].Columns[col].ColumnName;
// int yVal = Convert.ToInt32(row[colName].ToString());
// Chart1.Series[seriesName].Points.AddXY(colName, yVal);
//}
foreach (Series series in this.Chart1.Series)
{
double lastYValue = series.Points[series.Points.Count - 1].YValues[0];
double lastXValue = series.Points[series.Points.Count - 1].XValue + 1;
for (int pointIndex = 0; pointIndex < 5; pointIndex++)
{
lastYValue += rand.Next(-3, 4);
if (lastYValue >= 100.0)
{
lastYValue -= 25.0;
}
else if (lastYValue <= 10.0)
{
lastYValue += 25.0;
}
series.Points.AddXY(lastXValue++, lastYValue);
series.IsVisibleInLegend = true;


}
while (this.Chart1.Series[0].Points.Count > 60)
{

foreach (Series s in this.Chart1.Series)
{
s.Points.RemoveAt(0);
}
}
double axisMinimum = this.Chart1.Series[0].Points[0].XValue;
this.Chart1.ChartAreas[0].AxisX.Minimum = axisMinimum;
this.Chart1.ChartAreas[0].AxisX.Maximum = axisMinimum + 60;

}

}


bychgh 2011-04-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 kingdom_0 的回复:]
Maximum="20"
是不是这里的问题。
[/Quote]

不是
kingdom_0 2011-04-08
  • 打赏
  • 举报
回复
Maximum="20"
是不是这里的问题。
bychgh 2011-04-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 seanding1989 的回复:]
把页面上的xy 最大和最后 都取消
[/Quote]

不是
SeanDing1989 2011-04-08
  • 打赏
  • 举报
回复
把页面上的xy 最大和最后 都取消

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧