WPF 使用MSCHART 控制元件程式碼
本來想多寫點 感覺有可能是廢話直接看程式碼吧!
CHART支援縮放。,X軸是時間單位 注意浮點時間轉換的方法。原來是資料可載入的資料 後來改成直接新增資料了要用資料庫的自己去掉//
XML檔案
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 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"> <Grid> <WindowsFormsHost x:Name="WFHCHART" HorizontalAlignment="Left" Height="248" Margin="10,33,0,0" VerticalAlignment="Top" Width="497"> <Charting:Chart Name="mainChart" Text="測試CHART" > <Charting:Chart.Titles> <Charting:Title Text="WTF"/> </Charting:Chart.Titles> </Charting:Chart> </WindowsFormsHost> <Button Content="Button" HorizontalAlignment="Left" Margin="90,297,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/> <TextBox x:Name="tbTEXT" HorizontalAlignment="Left" Height="23" Margin="75,5,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="407"/>
</Grid> </Window>
.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);
} } }