如何使用c#设置日期时间图表的最小值和最大值?(How to set minimum and maximum values for date time chart using c#?)

编程入门 行业动态 更新时间:2024-10-25 05:11:37
如何使用c#设置日期时间图表的最小值和最大值?(How to set minimum and maximum values for date time chart using c#?)

我试图在我的Windows应用程序中每隔一秒绘制一个带有实时数据的折线图。 为此,我需要为图表设置最小值(0秒)和最大值(10分钟)。 10分钟后,最小值为10分钟,最大值为20分钟。 所以我每次都要显示10分钟的数据。 我需要从一开始就使用滚动条显示以前的数据。 我尝试了以下代码,但我无法设置图表的最小值和最大值。请解决我的问题。

series1.XValueType = ChartValueType.DateTime; series1.IsXValueIndexed = true; series1.YAxisType = AxisType.Primary; series1.ChartType = SeriesChartType.Line; this.chart1.Series.Add(series1); series2.XValueType = ChartValueType.DateTime; series2.IsXValueIndexed = true; series2.YAxisType = AxisType.Secondary; series2.ChartType = SeriesChartType.Line; this.chart1.Series.Add(series2); chart1.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm:ss"; chart1.ChartAreas[0].CursorX.IntervalType = DateTimeIntervalType.Seconds; chart1.ChartAreas[0].CursorX.AutoScroll = true; chart1.ChartAreas[0].CursorY.AutoScroll = true; chart1.ChartAreas[0].AxisX.ScrollBar.Size = 15; chart1.ChartAreas[0].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.All; chart1.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = false; chart1.ChartAreas[0].AxisX.ScrollBar.Enabled = true; chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true; chart1.ChartAreas[0].AxisY.ScaleView.Zoomable = true; chart1.ChartAreas[0].AxisY2.ScaleView.Zoomable = true; chart1.ChartAreas[0].CursorX.IsUserEnabled = true; chart1.ChartAreas[0].CursorX.IsUserSelectionEnabled = true; DateTime minValue, maxValue; minValue = DateTime.Now; maxValue = minValue.AddSeconds(600); chart1.ChartAreas[0].AxisX.Minimum = minValue.ToOADate(); chart1.ChartAreas[0].AxisX.Maximum = maxValue.ToOADate(); chart1.ChartAreas[0].AxisX.ScaleView.Zoom(chart1.ChartAreas[0].AxisX.Minimum, chart1.ChartAreas[0].AxisX.Maximum);

I am trying to plot a line chart with real time data for every one second in my windows application. For that I need to set minimum (0 seconds) and maximum (10 minutes) values for the chart . After 10 minutes, minimum value is 10 minutes and maximum value is 20 minutes. So I have to display,10 minutes data every time. I need to display previous data with scrollbar from the very beginning. I tried the following code but I am unable to set the min and max value of chart.Please solve my problem.

series1.XValueType = ChartValueType.DateTime; series1.IsXValueIndexed = true; series1.YAxisType = AxisType.Primary; series1.ChartType = SeriesChartType.Line; this.chart1.Series.Add(series1); series2.XValueType = ChartValueType.DateTime; series2.IsXValueIndexed = true; series2.YAxisType = AxisType.Secondary; series2.ChartType = SeriesChartType.Line; this.chart1.Series.Add(series2); chart1.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm:ss"; chart1.ChartAreas[0].CursorX.IntervalType = DateTimeIntervalType.Seconds; chart1.ChartAreas[0].CursorX.AutoScroll = true; chart1.ChartAreas[0].CursorY.AutoScroll = true; chart1.ChartAreas[0].AxisX.ScrollBar.Size = 15; chart1.ChartAreas[0].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.All; chart1.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = false; chart1.ChartAreas[0].AxisX.ScrollBar.Enabled = true; chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true; chart1.ChartAreas[0].AxisY.ScaleView.Zoomable = true; chart1.ChartAreas[0].AxisY2.ScaleView.Zoomable = true; chart1.ChartAreas[0].CursorX.IsUserEnabled = true; chart1.ChartAreas[0].CursorX.IsUserSelectionEnabled = true; DateTime minValue, maxValue; minValue = DateTime.Now; maxValue = minValue.AddSeconds(600); chart1.ChartAreas[0].AxisX.Minimum = minValue.ToOADate(); chart1.ChartAreas[0].AxisX.Maximum = maxValue.ToOADate(); chart1.ChartAreas[0].AxisX.ScaleView.Zoom(chart1.ChartAreas[0].AxisX.Minimum, chart1.ChartAreas[0].AxisX.Maximum);

最满意答案

微软有一个他们为这里找到的mscharts做的示例项目。 在Working with Data目录中,有一个实时数据部分。 他们有一个互动的例子做你要求的。 为方便起见,我复制了以下代码。 希望这会有所帮助。

using System.Windows.Forms.DataVisualization.Charting; ... private Thread addDataRunner; private Random rand = new Random(); private System.Windows.Forms.DataVisualization.Charting.Chart chart1; public delegate void AddDataDelegate(); public AddDataDelegate addDataDel; ... private void RealTimeSample_Load(object sender, System.EventArgs e) { // create the Adding Data Thread but do not start until start button clicked ThreadStart addDataThreadStart = new ThreadStart(AddDataThreadLoop); addDataRunner = new Thread(addDataThreadStart); // create a delegate for adding data addDataDel += new AddDataDelegate(AddData); } private void startTrending_Click(object sender, System.EventArgs e) { // Disable all controls on the form startTrending.Enabled = false; // and only Enable the Stop button stopTrending.Enabled = true; // Predefine the viewing area of the chart minValue = DateTime.Now; maxValue = minValue.AddSeconds(120); chart1.ChartAreas[0].AxisX.Minimum = minValue.ToOADate(); chart1.ChartAreas[0].AxisX.Maximum = maxValue.ToOADate(); // Reset number of series in the chart. chart1.Series.Clear(); // create a line chart series Series newSeries = new Series( "Series1" ); newSeries.ChartType = SeriesChartType.Line; newSeries.BorderWidth = 2; newSeries.Color = Color.OrangeRed; newSeries.XValueType = ChartValueType.DateTime; chart1.Series.Add( newSeries ); // start worker threads. if ( addDataRunner.IsAlive == true ) { addDataRunner.Resume(); } else { addDataRunner.Start(); } } private void stopTrending_Click(object sender, System.EventArgs e) { if ( addDataRunner.IsAlive == true ) { addDataRunner.Suspend(); } // Enable all controls on the form startTrending.Enabled = true; // and only Disable the Stop button stopTrending.Enabled = false; } /// Main loop for the thread that adds data to the chart. /// The main purpose of this function is to Invoke AddData /// function every 1000ms (1 second). private void AddDataThreadLoop() { while (true) { chart1.Invoke(addDataDel); Thread.Sleep(1000); } } public void AddData() { DateTime timeStamp = DateTime.Now; foreach ( Series ptSeries in chart1.Series ) { AddNewPoint( timeStamp, ptSeries ); } } /// The AddNewPoint function is called for each series in the chart when /// new points need to be added. The new point will be placed at specified /// X axis (Date/Time) position with a Y value in a range +/- 1 from the previous /// data point's Y value, and not smaller than zero. public void AddNewPoint( DateTime timeStamp, System.Windows.Forms.DataVisualization.Charting.Series ptSeries ) { double newVal = 0; if ( ptSeries.Points.Count > 0 ) { newVal = ptSeries.Points[ptSeries.Points.Count -1 ].YValues[0] + (( rand.NextDouble() * 2 ) - 1 ); } if ( newVal < 0 ) newVal = 0; // Add new data point to its series. ptSeries.Points.AddXY( timeStamp.ToOADate(), rand.Next(10, 20)); // remove all points from the source series older than 1.5 minutes. double removeBefore = timeStamp.AddSeconds( (double)(90) * ( -1 )).ToOADate(); //remove oldest values to maintain a constant number of data points while ( ptSeries.Points[0].XValue < removeBefore ) { ptSeries.Points.RemoveAt(0); } chart1.ChartAreas[0].AxisX.Minimum = ptSeries.Points[0].XValue; chart1.ChartAreas[0].AxisX.Maximum = DateTime.FromOADate(ptSeries.Points[0].XValue).AddMinutes(2).ToOADate(); chart1.Invalidate(); } /// Clean up any resources being used. protected override void Dispose( bool disposing ) { if ( (addDataRunner.ThreadState & ThreadState.Suspended) == ThreadState.Suspended) { addDataRunner.Resume(); } addDataRunner.Abort(); if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } ...

Finally I found solution for my problem with little bit modifications for the code.Thanks for your support.I implemented a line chart with two series and the chart has to display 1 minute data in every frame.

int viewcount=0,count=0,mviewcount=60; public void AddData() // executing using thread { while (true) { if (flag) // making flag as true by calling timer for every 1sec { flag = false; DateTime timeStamp = DateTime.Now; double y1 = 0.0; double y2= 0.0; y1= gety1(count); y2= gety2(count + 1); AddNewPoint(timeStamp, chart1.Series[0], chart1.Series[1], oilvalue, tempvalue); count++; } Thread.Sleep(1); } } public void AddNewPoint(DateTime timeStamp, System.Windows.Forms.DataVisualization.Charting.Series ptSeries1, System.Windows.Forms.DataVisualization.Charting.Series ptSeries2, double y1, double y2) { if (this.chart1.InvokeRequired) { BeginInvoke((Action)(() => { this.chart1.Series[0].Points.AddXY(timeStamp.ToOADate(), y1); this.chart1.Series[1].Points.AddXY(timeStamp.ToOADate(), y2); if ((count % 60) == 0) { mviewcount += 60; viewcount += 60; chart1.ChartAreas[0].AxisX.Minimum = DateTime.FromOADate(ptSeries1.Points[count - 1].XValue).ToOADate(); chart1.ChartAreas[0].AxisX.Maximum = DateTime.FromOADate(ptSeries1.Points[count - 1].XValue).AddMinutes(1).ToOADate(); min = chart1.ChartAreas[0].AxisX.Minimum; max = chart1.ChartAreas[0].AxisX.Maximum; } if (count >= 60) { if ((count >= viewcount) || (count <= mviewcount)) { chart1.Series[0].Points[0].AxisLabel = System.DateTime.FromOADate(chart1.Series[0].Points[count - 1].XValue).ToString(); chart1.ChartAreas[0].AxisX.ScaleView.Position = max; } } chart1.Update(); chart1.ChartAreas[0].RecalculateAxesScale(); })); } }

更多推荐

本文发布于:2023-08-01 22:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1365758.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最大值   图表   如何使用   最小值   日期

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!