Beispiel #1
0
        //
        // Update the chart and the viewport periodically
        //
        private void chartUpdateTimer_Tick(object sender, EventArgs e)
        {
            WinChartViewer viewer = winChartViewer1;

            // Enables auto scroll if the viewport is showing the latest data before the update
            bool autoScroll = (currentIndex > 0) && (0.01 + viewer.getValueAtViewPort("x",
                                                                                      viewer.ViewPortLeft + viewer.ViewPortWidth) >= timeStamps[currentIndex - 1]);

            // Get new data from the queue and append them to the data arrays
            var packets = buffer.get();

            if (packets.Count <= 0)
            {
                return;
            }

            // if data arrays have insufficient space, we need to remove some old data.
            if (currentIndex + packets.Count >= sampleSize)
            {
                // For safety, we check if the queue contains too much data than the entire data arrays. If
                // this is the case, we only use the latest data to completely fill the data arrays.
                if (packets.Count > sampleSize)
                {
                    packets = new ArraySegment <DataPacket>(packets.Array, packets.Count - sampleSize, sampleSize);
                }

                // Remove oldest data to leave space for new data. To avoid frequent removal, we ensure at
                // least 5% empty space available after removal.
                int originalIndex = currentIndex;
                currentIndex = sampleSize * 95 / 100 - 1;
                if (currentIndex > sampleSize - packets.Count)
                {
                    currentIndex = sampleSize - packets.Count;
                }

                for (int i = 0; i < currentIndex; ++i)
                {
                    int srcIndex = i + originalIndex - currentIndex;
                    timeStamps[i]  = timeStamps[srcIndex];
                    dataSeriesA[i] = dataSeriesA[srcIndex];
                    dataSeriesB[i] = dataSeriesB[srcIndex];
                }
            }

            // Append the data from the queue to the data arrays
            for (int n = packets.Offset; n < packets.Offset + packets.Count; ++n)
            {
                DataPacket p = packets.Array[n];
                timeStamps[currentIndex]  = p.elapsedTime;
                dataSeriesA[currentIndex] = p.series0;
                dataSeriesB[currentIndex] = p.series1;
                ++currentIndex;
            }

            //
            // As we added more data, we may need to update the full range.
            //

            double startDate = timeStamps[0];
            double endDate   = timeStamps[currentIndex - 1];

            // Use the initialFullRange (which is 60 seconds in this demo) if this is sufficient.
            double duration = endDate - startDate;

            if (duration < initialFullRange)
            {
                endDate = startDate + initialFullRange;
            }

            // Update the new full data range to include the latest data
            bool axisScaleHasChanged = viewer.updateFullRangeH("x", startDate, endDate,
                                                               Chart.KeepVisibleRange);

            if (autoScroll)
            {
                // Scroll the viewport if necessary to display the latest data
                double viewPortEndPos = viewer.getViewPortAtValue("x", timeStamps[currentIndex - 1]);
                if (viewPortEndPos > viewer.ViewPortLeft + viewer.ViewPortWidth)
                {
                    viewer.ViewPortLeft = viewPortEndPos - viewer.ViewPortWidth;
                    axisScaleHasChanged = true;
                }
            }

            // Set the zoom in limit as a ratio to the full range
            viewer.ZoomInWidthLimit = zoomInLimit / (viewer.getValueAtViewPort("x", 1) -
                                                     viewer.getValueAtViewPort("x", 0));

            // Trigger the viewPortChanged event. Updates the chart if the axis scale has changed
            // (scrolling or zooming) or if new data are added to the existing axis scale.
            viewer.updateViewPort(axisScaleHasChanged || (duration < initialFullRange), false);
        }