MsChart 实时曲线

bychgh bychgh 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
用 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>

...全文

相关推荐

MSChart实时曲线

微软推出的免费控件,在工业监控软件开发中能大大降低开发成本,该控件用来做各种参数实时监控、数据图形显示很是方便,本例采用Chart控件实现实时曲线显示,示例很简单,权当抛砖引玉吧。

阿卡

shixian.rar_mschart_mschart曲线_坐标实时_控件坐标_曲线

该程序可以实时显示外部接受的数据,相比mschart控件制作的实时曲线效果较佳,没有闪烁问题。显示的背景,坐标值,以及曲线的颜色等都可以更改,非常棒

小贝德罗

mschart显示实时曲线.ra

这个用的人应较多 源码啊 以下为类的字段 private int width; 要生成曲线图的宽度 private int height; 要生成曲线图的高度 private int N 31; 取31个关键点 private int M 25; 每M个像素点取一个关键点 private Point[] pointList; 用来绘制曲线的关键点 依次将这些点连接起来即得到曲线 private Random random new Random ; 生成随机数 private Bitmap currentImage; 当前要绘制的图片 private Color backColor; 图片背景色 private Color foreColor; 图片前景色 &lt;summary&gt;">这个用的人应较多 源码啊 以下为类的字段 private int width; 要生成曲线图的宽度 private int height; 要生成曲线图的高度 private int N 31; 取31个关键点 private int M 25; 每M个像素点取一个关键点 [更多]

w200w200

MsChart动态曲线 VC6

根据http://www.cnblogs.com/wy-wangyan/archive/2011/05/13/2045851.html 利用MsChart控件做的动态曲线实例,供大家参考

zw2111983

demo_mschart.rar_MFC MsChart_MFC 曲线_MFC 画曲线_MFC曲线图_曲线

MSChart Demo Application, MFC平台上可以画曲线,柱状图等

weixin_42653672
评论 7
收藏 已收藏
分享
返回顶部
bychgh
.NET社区 62,271 成员 668,968 内容