本来想多写点 感觉有可能是废话直接看代码吧!
CHART支持缩放。,X轴是时间单位 注意浮点时间转换的方法。原来是数据可加载的数据 后来改成直接添加数据了要用数据库的自己去掉//
XML文件
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Charting="clr-namespace:System.Windows.Forms.DataVisualization.Charting;assembly=System.Windows.Forms.DataVisualization" x:Class="TESTCHART.MainWindow"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
.s文件
public partial class MainWindow : Window
{
//WFHCHART
DataTable dt = new DataTable();
Chart mainChart = null;
public MainWindow()
{
InitializeComponent();
mainChart = WFHCHART.Child as Chart;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
dt.Columns.Add("Processor");
dt.Columns.Add("Time", Type.GetType("System.DateTime"));
for (int i = 0; i < 30; i++)
{
DataRow dr = dt.NewRow();
dr["Processor"] = i * DateTime.Now.Millisecond;
dr["Time"] = DateTime.Now + new TimeSpan(0, i, 0);
dt.Rows.Add(dr);
}
ChartArea ca = new ChartArea("ChartArea1");
ca.Area3DStyle.Enable3D = false;
ca.BackColor = System.Drawing.Color.FromArgb(0, 255, 255);
// ca.AxisY.Maximum = 90000;
mainChart.ChartAreas.Add(ca);
Legend lgCPU = new Legend("Legend1");
lgCPU.IsTextAutoFit = true;
lgCPU.Docking = Docking.Bottom;
mainChart.Legends.Add(lgCPU);
Series seCPU = new Series("SeriesCPU");
seCPU.ChartArea = "ChartArea1";
seCPU.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
seCPU.IsVisibleInLegend = true;
seCPU.Legend = "Legend1";
seCPU.LegendText = "CPU";
// seCPU.XValueMember = "Time";
// seCPU.YValueMembers = "Processor";
seCPU.Color = System.Drawing.Color.FromArgb(255, 0, 0);
seCPU.XValueType = ChartValueType.DateTime;
// seCPU.IsValueShownAsLabel = true;//显示数据点的值
// seCPU.MarkerStyle = MarkerStyle.Circle;
mainChart.Series.Add(seCPU);
// mainChart.DataSource = dt;
// mainChart.DataBind();
seCPU.ToolTip = "X值:#VALX\nY值:#VALY";
mainChart.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm:ss";
mainChart.ChartAreas[0].AxisX.LabelStyle.Angle =0;
mainChart.ChartAreas[0].AxisY.LabelStyle.Format = "0.00";
// seCPU.MarkerSize = 5;
// seCPU.MarkerStyle = MarkerStyle.Circle;
mainChart.MouseMove += mainChart_MouseMove;
mainChart.MouseDown += mainChart_MouseDown;
mainChart.MouseUp += mainChart_MouseUp;
}
private bool MouseDown = false;
private Point ptDown;
private Point ptUp;
void mainChart_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
try
{
ptUp = new Point(e.X, e.Y);
if (ptUp.X > ptDown.X)
{
mainChart.ChartAreas[0].AxisY.Minimum = mainChart.ChartAreas[0].AxisY.PixelPositionToValue(ptUp.Y);
mainChart.ChartAreas[0].AxisY.Maximum = mainChart.ChartAreas[0].AxisY.PixelPositionToValue(ptDown.Y);
mainChart.ChartAreas[0].AxisX.Maximum = mainChart.ChartAreas[0].AxisX.PixelPositionToValue(ptUp.X);
mainChart.ChartAreas[0].AxisX.Minimum = mainChart.ChartAreas[0].AxisX.PixelPositionToValue(ptDown.X);
}
else
{
mainChart.ChartAreas[0].AxisY.Minimum = System.Double.NaN;
mainChart.ChartAreas[0].AxisY.Maximum = System.Double.NaN;
mainChart.ChartAreas[0].AxisX.Maximum = System.Double.NaN;
mainChart.ChartAreas[0].AxisX.Minimum = System.Double.NaN;
}
MouseDown = false;
}
catch(Exception ex)
{
mainChart.ChartAreas[0].AxisY.Minimum = System.Double.NaN;
mainChart.ChartAreas[0].AxisY.Maximum = System.Double.NaN;
mainChart.ChartAreas[0].AxisX.Maximum = System.Double.NaN;
mainChart.ChartAreas[0].AxisX.Minimum = System.Double.NaN;
MouseDown = false;
}
}
void mainChart_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
//throw new NotImplementedException();
ptDown = new Point(e.X,e.Y);
MouseDown = true;
}
void mainChart_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
try
{
var area = mainChart.ChartAreas[0];
double xValue = 0;
xValue = area.AxisX.PixelPositionToValue(e.X);
double yValue = area.AxisY.PixelPositionToValue(e.Y);
tbTEXT.Text = string.Format("{0:F0},{1:g}", yValue, DateTime.FromOADate(xValue));
}
catch(Exception r)
{
}
// throw new NotImplementedException();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// mainChart.Scale(new System.Drawing.SizeF(600, 800));
mainChart.Series[0].Points.Clear();
for(int i = 0;i<100;i++)
{ //
mainChart.Series[0].Points.AddXY(DateTime.Now+new TimeSpan(0,i,0), i);
}
}
}