Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用c#设置日期时间图表的最小值和最大值?_C#_Winforms_Mschart - Fatal编程技术网

如何使用c#设置日期时间图表的最小值和最大值?

如何使用c#设置日期时间图表的最小值和最大值?,c#,winforms,mschart,C#,Winforms,Mschart,我试图在我的windows应用程序中绘制一个每秒钟有实时数据的折线图。为此,我需要为图表设置最小值(0秒)和最大值(10分钟)。10分钟后,最小值为10分钟,最大值为20分钟。所以我必须每次显示10分钟的数据。我需要从一开始就用滚动条显示以前的数据。我尝试了以下代码,但无法设置图表的最小值和最大值。请解决我的问题 series1.XValueType = ChartValueType.DateTime; series1.IsXValueIndexed = true; serie

我试图在我的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);  

微软有一个他们为mscharts开发的示例项目。在使用数据目录中,有一个实时数据部分。他们有一个互动的例子,按照你的要求做。为了方便起见,我复制了下面的代码。希望这能有所帮助

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 );
}        
... 
使用System.Windows.Forms.DataVisualization.Charting;
...
私有线程addDataRunner;
private Random rand=new Random();
private System.Windows.Forms.DataVisualization.Charting.chart1;
公共委托void AddDataDelegate();
公共AddDataDelegate addDataDel;
...
私有void RealTimeSample_加载(对象发送方,System.EventArgs e)
{
//创建添加数据线程,但在单击开始按钮之前不要启动
ThreadStart addDataThreadStart=新的ThreadStart(AddDataThreadLoop);
addDataRunner=新线程(addDataThreadStart);
//创建用于添加数据的委托
addDataDel+=新的AddDataDelegate(AddData);
}
private void startTrending_单击(对象发送者,System.EventArgs e)
{
//禁用窗体上的所有控件
startTrending.Enabled=false;
//并且只启用停止按钮
stopTrending.Enabled=true;
//预定义图表的查看区域
minValue=DateTime.Now;
maxValue=minValue.AddSeconds(120);
chart1.ChartAreas[0].axix.Minimum=minValue.ToOADate();
chart1.ChartAreas[0].AxisX.Maximum=maxValue.ToOADate();
//重置图表中的系列数。
图1.Series.Clear();
//创建折线图系列
系列新闻系列=新系列(“系列1”);
newSeries.ChartType=SeriesChartType.Line;
newSeries.BorderWidth=2;
newSeries.Color=Color.oranged;
newSeries.XValueType=ChartValueType.DateTime;
图表1.Series.Add(新闻系列);
//启动辅助线程。
if(addDataRunner.IsAlive==true)
{
addDataRunner.Resume();
}
其他的
{
addDataRunner.Start();
}
}
私有void stopTrending_单击(对象发送方,System.EventArgs e)
{
if(addDataRunner.IsAlive==true)
{
addDataRunner.Suspend();
}
//启用窗体上的所有控件
startTrending.Enabled=true;
//并且只禁用停止按钮
stopTrending.Enabled=false;
}
///向图表中添加数据的线程的主循环。
///此函数的主要目的是调用AddData
///每1000毫秒(1秒)运行一次。
私有void AddDataThreadLoop()
{
while(true)
{
图表1.调用(addDataDel);
睡眠(1000);
}
}
public void AddData()
{
DateTime timeStamp=DateTime.Now;
foreach(系列图1.系列中的系列)
{
AddNewPoint(时间戳,ptSeries);
}
}
///在以下情况下,将为图表中的每个系列调用AddNewPoint函数:
///需要添加新的点。新点将放置在指定的位置
///X轴(日期/时间)位置,Y值在上一个值的+/-1范围内
///数据点的Y值,且不小于零。
public void AddNewPoint(DateTime时间戳,System.Windows.Forms.DataVisualization.Charting.Series ptSeries)
{
双newVal=0;
如果(ptSeries.Points.Count>0)
{
newVal=ptSeries.Points[ptSeries.Points.Count-1].yValue[0]+((rand.NextDouble()*2)-1);
}
if(newVal<0)
newVal=0;
//将新数据点添加到其系列中。
ptSeries.Points.AddXY(timeStamp.ToOADate(),rand.Next(10,20));
//删除源系列中超过1.5分钟的所有点。
double removeBefore=timeStamp.AddSeconds((double)(90)*(-1)).ToOADate();
//删除最旧的值以保持恒定的数据点数量
while(ptSeries.Points[0].XValue
微软为mscharts开发了一个示例项目。在使用数据目录中,有一个实时数据部分。他们有一个互动的例子,按照你的要求做。为了方便起见,我复制了下面的代码。希望这能有所帮助

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 );
}        
... 
使用System.Windows.Forms.DataVisualization.Charting;
...
私有线程addDataRunner;
private Random rand=new Random();
private System.Windows.Forms.DataVisualization.Charting.chart1;
公共委托void AddDataDelegate();
公共AddDataDelegate addDataDel;
...
私有void RealTimeSample_加载(对象发送方,System.EventArgs e)
{
//创建添加数据线程,但在单击开始按钮之前不要启动
ThreadStart addDataThreadStart=新的ThreadStart(AddDataThreadLoop);
addDataRunner=新线程(addDataThreadStart);
//创建用于添加数据的委托
addDataDel+=新的AddDataDelegate(AddData);
}
private void startTrending_单击(对象发送者,System.EventArgs e)
{
//禁用窗体上的所有控件
startTrending.Enabled=false;
//
        if (this.chart1.InvokeRequired)
        {
            BeginInvoke((Action)(() =>
            {
                if (!chartState) return;

                DateTime now = DateTime.Now;
                chart1.ResetAutoValues();

                for (int i = 0; i < chart1.Series.Count; i++)
                {
                    if (chart1.Series[i].Points.Count > 0)
                    {
                        while (chart1.Series[i].Points[0].XValue < now.AddMinutes(-60).ToOADate())
                        {
                            chart1.Series[i].Points.RemoveAt(0);

                            chart1.ChartAreas[0].AxisX.Minimum = chart1.Series[i].Points[0].XValue;
                            chart1.ChartAreas[0].AxisX.Maximum = now.AddMinutes(1).ToOADate();
                        }
                    }
                }

                chart1.Series["Sebeke"].Points.AddXY(now.ToOADate(), AnlikSebeke);
                chart1.Series["Turbin"].Points.AddXY(now.ToOADate(), AnlikTurbin);
                chart1.Series["Tuketim"].Points.AddXY(now.ToOADate(), AnlikTuketim);
                chart1.Invalidate();
            }));
        }