Recent research in WPF, Chart need to use similar controls to draw a line graph, the abscissa custom time information. Has studied DynamicDataDisplay.ChartPlotter, Toolkit, as WPF with too few people, this information can be found too little. Although DynamicDataDisplay.ChartPlotter to achieve horizontal axis displays the time, but I think history shows time information, non-current time, then discarded. How do I change the next big if there is to know God, be reminded.
All these led me to want to use under Chart familiar Winform to draw.
1. First, add a reference
System.Windows.Forms.dll
WindowsFormsIntegration.dll
System.Windows.Forms.DataVisualization.dll
.. 2 XAML add a namespace
xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration" xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" xmlns:Chr="clr-namespace:System.Windows.Forms.DataVisualization.Charting;assembly=System.Windows.Forms.DataVisualization"
3.WPF is not directly quoted WinForm Chart, but can be hosted in WindowsFormsHost
use, so we have to add a host container.
<WindowsFormsHost x:Name="WFHost_HistoryFlow" Margin="1"> <Chr:Chart x:Name="Chart_HistoryFlow" GetToolTipText="Chart_HistoryFlow_GetToolTipText" BackColor="WhiteSmoke" MouseWheel="Chart_HistoryFlow_MouseWheel"/> </WindowsFormsHost>
4. Add the namespace code behind .cs
using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting;
5. Background Code
5.1 Private variables
DataTable dataTable = new DataTable();
5.2 Initialization
/// <summary> /// 設定Chart Control /// </summary> private void SetChart() { ChartArea ca = new ChartArea("ChartArea1"); this.Chart_HistoryFlow.ChartAreas.Add(ca); ChartArea ca_Pres = new ChartArea("ChartArea1"); this.Chart_HistoryPres.ChartAreas.Add(ca_Pres); ChartArea ca_Ratio = new ChartArea("ChartArea1"); this.Chart_HistoryRatio.ChartAreas.Add(ca_Ratio); //Processor System.Windows.Forms.DataVisualization.Charting.Legend lgFlow = new System.Windows.Forms.DataVisualization.Charting.Legend("Legend1"); lgFlow.IsTextAutoFit = true; lgFlow.Docking = Docking.Top; this.Chart_HistoryFlow.Legends.Add(lgFlow); System.Windows.Forms.DataVisualization.Charting.Legend lgPres = new System.Windows.Forms.DataVisualization.Charting.Legend("Legend1"); lgPres.IsTextAutoFit = true; lgPres.Docking = Docking.Top; this.Chart_HistoryPres.Legends.Add(lgPres); System.Windows.Forms.DataVisualization.Charting.Legend lgRatio = new System.Windows.Forms.DataVisualization.Charting.Legend("Legend1"); lgRatio.IsTextAutoFit = true; lgRatio.Docking = Docking.Top; this.Chart_HistoryRatio.Legends.Add(lgRatio); SetChartAutoBar(Chart_HistoryFlow); SetChartAutoBar (Chart_HistoryPres); SetChartAutoBar (Chart_HistoryRatio); } /// <Summary> /// set the cursor line graph /// </ Summary> Private void SetChartAutoBar (the Chart Chart) { // set the cursor chart.ChartAreas [ 0 ]. = CursorX.IsUserEnabled to true ; chart.ChartAreas [ 0 ] = .CursorX.AutoScroll to true ; chart.ChartAreas [ 0 ] = .CursorX.IsUserSelectionEnabled to true ; // whether to set the X-axis may be scaled chart.ChartAreas [ 0 ] = .AxisX.ScaleView.Zoomable to true ; // rolling built into coordinate axes chart.ChartAreas [ 0 ] = .AxisX.ScrollBar.IsPositionedInside to true ; // set the size of the scroll bar chart.ChartAreas [ 0 ] .AxisX.ScrollBar.Size = 10 ; // set the scroll bar button styles, the following code is all the buttons on the scroll bar are displayed chart.ChartAreas [ 0 ] = .AxisX.ScrollBar.ButtonStyle ScrollBarButtonStyles.All ; // minimum amount is set automatically enlarged and reduced chart.ChartAreas [ 0 ] = .AxisX.ScaleView.SmallScrollSize Double .NaN; chart.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSize = 1; }
5.3 Event
/// <Summary> /// historical traffic line chart mouse scroll bar corresponding to the minimum and maximum data slide stop position /// </ Summary> /// <param name = "SENDER"> </ param> /// <param name = "E"> </ param> Private void Chart_HistoryFlow_MouseWheel ( Object SENDER, System.Windows.Forms.MouseEventArgs E) { // holding down the Ctrl, scaled IF ((System.Windows.Forms.Control.ModifierKeys & Keys .Control) == Keys.Control) { IF (e.Delta < 0 ) Chart_HistoryFlow.ChartAreas [ 0 ] + = .AxisX.ScaleView.Size . 4 ; the else Chart_HistoryFlow.ChartAreas [ 0 = -] .AxisX.ScaleView.Size . 4 ; } // do not press Ctrl, scroll the else { IF (e.Delta < 0 ) { // stop the current view position longer than the maximum data + IF ( Chart_HistoryFlow.ChartAreas [ 0 ] + .AxisX.ScaleView.Position Chart_HistoryFlow.ChartAreas [ 0 ] .AxisX.ScaleView.Size <Chart_HistoryFlow.ChartAreas [ 0 ] .AxisX.ScaleView.ViewMaximum) Chart_HistoryFlow.ChartAreas [ 0 ] .AxisX.ScaleView. + = the Position . 4 ; } The else { // the current stop position is less than the minimum data IF (Chart_HistoryFlow.ChartAreas [ 0 ] .AxisX.ScaleView.Position> Chart_HistoryFlow.ChartAreas [ 0 ] .AxisX.ScaleView.ViewMinimum) Chart_HistoryFlow.ChartAreas [ 0 ] .AxisX.ScaleView .POSITION - = . 4 ; } } } /// <Summary> /// flow pulse line graph cursor display detailed data /// </ Summary> /// <param name = "SENDER"> </ param> // / <param name = "E"> </ param> Private void Chart_HistoryFlow_GetToolTipText(object sender, System.Windows.Forms.DataVisualization.Charting.ToolTipEventArgs e) { if (e.HitTestResult.ChartElementType == ChartElementType.DataPoint) { this.Cursor = System.Windows.Input.Cursors.Cross; int i = e.HitTestResult.PointIndex; string time = this.dataTable.Rows[i]["时间"].ToString(); string aFlow = this.dataTable.Rows[i]["A脉冲"].ToString(); string bFlow = this.dataTable.Rows[i]["B脉冲"].ToString(); string aPressure = this.dataTable.Rows[i]["A压力"].ToString(); string bPressure = this.dataTable.Rows[i]["B压力"].ToString(); string abRatio = this.dataTable.Rows[i]["AB比率"].ToString(); e.Text = $"Time: {time} \ r \ nA pulses: {aFlow} \ r \ nB pulse: {bFlow} \ r \ nA Pressure: {aPressure} \ r \ nB Pressure: {bPressure} \ r \ nAB ratio: {abRatio} " ; } the else { the this .cursor = System.Windows.Input.Cursors.Arrow; } }
5.4 write data to the Chart of
/// <Summary> /// the DataTable write data in Chart /// </ Summary> /// <param name = "the dataTable"> the DataTable contains data </ param> /// <param name = "Chart "> the chart data to be written </ param> public void DataTableToChart (the DataTable the dataTable, the chart chart, String title, String of Series1, String Series2) { chart.Series.Clear (); // Clear in the chart chart.Titles .clear (); chart.Titles.Add (title); // add a title chart.DataSource = dataTable; ACodeSeries Series = chart.Series.Add (of Series1); // add the first table aCodeSeries.ChartType = SeriesChartType.Line; // set line chart aCodeSeries.YValueMembers of Series1 =; // Y axis data aCodeSeries.XValueMember = " time " ; Series bCodeSeries = chart.Series.Add (Series2); // add a second table bCodeSeries.ChartType = SeriesChartType.Line; // set line chart bCodeSeries.YValueMembers = Series2; }
To write data to the DataTable, my column called "Time", "A pulse", "B Pulse", "A pressure", "B pressure", "AB ratio."
5.5 display
OVER ~ Sahua