ASP.NET微软图表控件MsChart实例之Cpu信息和内存使用监控图表
实例使用到了Ajax的方法,Windows Api获取系统内存的方法
新建cpuchart.aspx页面,代码如下
<asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:脚本管理器> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <内容模板> <asp:Chart ID="ChartMemory" runat="server" BackColor="LightSteelBlue" BackGradientStyle="TopBottom" BackSecondaryColor="White" EnableTheming="False" EnableViewState="True" Height="363px" Width="415px"> <传说> <asp:Legend Alignment="Center" Docking="Bottom" Name="Legend1" Title="图例"> </asp:图例> </传说> <标题> <asp:Title Font="微软雅黑, 16pt" Name="Title1" Text="系统内存监控图表"> </asp:标题> </标题> <系列> <asp:Series BorderColor="White" BorderWidth="3" ChartArea="ChartArea1" ChartType="Spline" Legend="Legend1" Name="已使用物理内存" XValueType="Double" YValueType="双"> </asp:系列> <asp:Series BorderWidth="3" ChartArea="ChartArea1" ChartType="Spline" Legend="Legend1" Name="全部占用内存"> </asp:系列> <asp:Series ChartArea="ChartArea2" ChartType="StackedArea" Legend="Legend1" 名称="CPU"> </asp:系列> </系列> <图表区域> <asp:ChartArea BackColor="224, 224, 224" BackGradientStyle="LeftRight" 名称="ChartArea1"> </asp:图表区域> <asp:ChartArea Name="ChartArea2"> </asp:图表区域> </图表区域> </asp:图表> <asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick"> </asp:定时器> </内容模板> </asp:更新面板>
添加了图片控件和一个Ajax的发生以及Ajax的ScriptManager,UpdatePanel,把时间和图表都拖进UpdatePanel里面。注意,这里需要添加<ContentTemplate>标签,否则会报错。将事件属性的间隔时间为一同时(1000)
实现功能cpuchart.aspx.cs代码
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
使用 System.Runtime.InteropServices;
使用 System.Diagnostics;
使用 System.Web.UI.DataVisualization.Charting;
使用 System.Text;
公共部分类 demo_CpuChart : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
static PerformanceCounter pc = new PerformanceCounter("Processor", "% Processor Time", "_Total");
protected void Timer1_Tick(object sender, EventArgs e)
{
MEMORY_INFO MemInfo = new MEMORY_INFO();
ComputerInfo.GlobalMemoryStatus(ref MemInfo);
//使用内存
系列系列 = ChartMemory.Series[0];
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 = (double)(MemInfo.dwTotalPhys - MemInfo.dwAvailPhys) / 1024 / 1024;
series.Points.AddXY(lastXValue, lastYValue);
//总内存
系列 = ChartMemory.Series[1];
lastYValue = (double)(MemInfo.dwTotalVirtual + MemInfo.dwTotalPhys - MemInfo.dwAvailPhys - MemInfo.dwAvailVirtual) / 1024 / 1024;
series.Points.AddXY(lastXValue, lastYValue);
//中央处理器
系列 = ChartMemory.Series[2];
lastYValue = (double)pc.NextValue();
series.Points.AddXY(lastXValue, lastYValue);
// 如果点数超过 100,则从图表左侧删除点。
while (this.ChartMemory.Series[0].Points.Count > 80)
{
// 删除系列点
foreach(this.ChartMemory.Series 中的系列)
{
s.Points.RemoveAt(0);
}
}
// 调整分类尺度
double axisMinimum = this.ChartMemory.Series[0].Points[0].XValue;
this.ChartMemory.ChartAreas[0].AxisX.Minimum = axisMinimum;
this.ChartMemory.ChartAreas[0].AxisX.Maximum = axisMinimum + 99;
}
/// <摘要>
///取得计算机的系统信息
/// </总结>
公共类 ComputerInfo
{
/// <摘要>
/// 取得Windows的目录
/// </总结>
/// <param name="WinDir"></param>
/// <param name="count"></param>
[DllImport("kernel32")]
public static extern void GetWindowsDirectory(StringBuilder WinDir, int count);
/// <摘要>
/// 获取系统路径
/// </总结>
/// <param name="SysDir"></param>
/// <param name="count"></param>
[DllImport("kernel32")]
public static extern void GetSystemDirectory(StringBuilder SysDir, int count);
/// <摘要>
/// 取得CPU信息
/// </总结>
/// <param name="cpuinfo"></param>
[DllImport("kernel32")]
public static extern void GetSystemInfo(ref CPU_INFO cpuinfo);
/// <摘要>
/// 取得内存状态
/// </总结>
/// <param name="meminfo"></param>
[DllImport("kernel32")]
public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);
/// <摘要>
/// 取得系统时间
/// </总结>
/// <param name="stinfo"></param>
[DllImport("kernel32")]
public static extern void GetSystemTime(ref SYSTEMTIME_INFO stinfo);
公共计算机信息()
{
}
}
//定义CPU的信息结构
[结构布局(LayoutKind.Sequential)]
公共结构 CPU_INFO
{
公共单位 dwOemId;
公共单位 dwPageSize;
公共 uint lpMinimumApplicationAddress;
公共 uint lpMaximumApplicationAddress;
公共 uint dwActiveProcessorMask;
公共 uint dwNumberOfProcessors;
公共 uint dwProcessorType;
公共 uint dwAllocationGranularity;
公共 uint dwProcessorLevel;
公共 uint dwProcessorRevision;
}
//定义内存的信息结构
[结构布局(LayoutKind.Sequential)]
公共结构 MEMORY_INFO
{
公共单位 dwLength;
公共 uint dwMemoryLoad;
公共单位 dwTotalPhys;
公共单位 dwAvailPhys;
公共 uint dwTotalPageFile;
公共 uint dwAvailPageFile;
公共 uint dwTotalVirtual;
公共 uint dwAvailVirtual;
}
//定义系统时间的信息结构
[结构布局(LayoutKind.Sequential)]
公共结构 SYSTEMTIME_INFO
{
公共 ushort wYear;
公共 ushort wMonth;
公共 ushort wDayOfWeek;
公共 ushort wDay;
公共 ushort wHour;
公共 ushort wMinute;
公共 ushort wSecond;
公共 ushort wMilliseconds;
}
}
一次,MEMORY_INFO,ComputerInfo是一个定义的结构体和调用Win32 API接口的一个访问类。
使用 System.Runtime.InteropServices; 使用 System.Diagnostics; 使用 System.Web.UI.DataVisualization.Charting; 使用 System.Text;
以上几个为新引用的空间
为保证程序正常运行,还需要在C盘根目录下创建一个临时目录临时图片文件,用于临时搭建实时监控图片