using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; namespace IntelligentControlForsx.MyControls { public partial class HistoryLine : UserControl { public HistoryLine() { InitializeComponent(); this.chart1.Series[0].Points.AddXY(1, 3.5); this.chart1.Series[0].Points.AddXY(2, 4.2); this.chart1.Series[0].Points.AddXY(3, 6); this.chart1.Series[0].Points.AddXY(4, 5.5); this.chart1.Series[0].Points.AddXY(5, 5.2); this.chart1.Series[0].Points.AddXY(6, 5.8); this.chart1.Series[0].Points.AddXY(7, 5); this.chart1.Series[1].Points.AddXY(1, 30); this.chart1.Series[1].Points.AddXY(2, 80); this.chart1.Series[1].Points.AddXY(3, 60); this.chart1.Series[1].Points.AddXY(4, 700); this.chart1.Series[1].Points.AddXY(5, 60); this.chart1.Series[1].Points.AddXY(6, 850); this.chart1.Series[1].Points.AddXY(7, 444); } //Dictionary SpeedDic; public void BuildChart(DateTime[] dts,Double[] speeds,Double[] powers) { chart1.Series[0].Points.Clear(); chart1.Series[1].Points.Clear(); if (dts.Length == speeds.Length && dts.Length == powers.Length) { for (int i = 0; i < dts.Length; i++) { double thePower=0.0; bool powerIsNum = Double.TryParse(powers[i].ToString(), out thePower); double theSpeed = 0.0; bool speedIsNum = Double.TryParse(speeds[i].ToString(), out theSpeed); if (powerIsNum && speedIsNum) { try { int p = Convert.ToInt32(speeds[i]); int s = Convert.ToInt32(powers[i]); } catch (Exception) { continue; } this.chart1.Series[0].Points.AddXY(dts[i], speeds[i]); this.chart1.Series[1].Points.AddXY(dts[i], powers[i]); } } } chart1.Invalidate(); } private void chart1_GetToolTipText(object sender, System.Windows.Forms.DataVisualization.Charting.ToolTipEventArgs e) { if (e.HitTestResult.ChartElementType == ChartElementType.DataPoint) { int i = e.HitTestResult.PointIndex; //DataPoint dp = e.HitTestResult.Series.Points[i]; DataPoint dp1 = this.chart1.Series[0].Points[i]; DataPoint dp2 = this.chart1.Series[1].Points[i]; //分别显示x轴和y轴的数值,其中{1:F3},表示显示的是float类型,精确到小数点后3位。 e.Text = string.Format("时间:{0};\n风速:{1:F2};\n功率:{2:F2};", dp1.XValue, dp1.YValues[0], dp2.YValues[0]); } } } }