Пример #1
0
        /// <summary>
        ///     Gets the value at a given point on the X-axis.
        /// </summary>
        /// <param name="x">Position on x-axis to get value.</param>
        /// <param name="result">Reference to variable to store value in.</param>
        /// <returns>
        ///     True if a value was retrieved. False if a value could not be retrieved because no data points are available
        ///     for the given position on the x-axis.
        /// </returns>
        public bool GetValueAtPoint(float x, out float result)
        {
            result = 0.0f;

            for (int i = 0; i < Data.Count; i++)
            {
                GraphDataPoint point = Data[i];

                // Point is exactly x.
                if (point.X == x)
                {
                    result = point.Y;
                    return(true);
                }

                // Point is somewhere between last value and this value.
                if (point.X > x)
                {
                    if (i > 0)
                    {
                        GraphDataPoint prevPoint = Data[i - 1];

                        // Linear interpolate to get value at x-value.
                        float delta = (x - prevPoint.X) / (point.X - prevPoint.X);
                        result = prevPoint.Y + (point.Y - prevPoint.Y) * delta;

                        return(true);
                    }

                    // No last value = no data for given point.
                    return(false);
                }
            }

            return(false);
        }
Пример #2
0
        /// <summary>
        ///     Adds a new data point to the graph.
        /// </summary>
        /// <param name="x">Data points value on the x-axis.</param>
        /// <param name="y">Data points value on the y-axis.</param>
        public void AddDataPoint(float x, float y)
        {
            if (MinimumInterval != 0 && Data.Count > 0)
            {
                float Elapsed = x - Data[Data.Count - 1].X;
                if (Elapsed < MinimumInterval)
                {
                    return;
                }
            }

            GraphDataPoint newPoint = new GraphDataPoint {
                X = x, Y = y
            };

            Data.Add(newPoint);

            // Adjust Y axis if value added is over current max.
            if (YAxis.AutoAdjustMax)
            {
                float OriginalMax = YAxis.Max;

                YAxis.Max          = Math.Max(y, YAxis.Max);
                YAxis.GridInterval = YAxis.Max / 5.0f;

                if (YAxis.Max != OriginalMax)
                {
                    YAxis.MaxLabel = FormatValue(YAxis.Max);
                }
            }

            // If this is a sliding window we need to remove any points outside the window.
            if (SlidingWindow)
            {
                bool  bWasRemoved = false;
                float startValue  = 0.0f;

                for (int i = 0; i < Data.Count; i++)
                {
                    GraphDataPoint point = Data[i];
                    if (x - point.X > XAxis.Max)
                    {
                        Data.RemoveAt(i);
                        i--;

                        bWasRemoved = true;
                        startValue  = point.Y;
                    }
                }

                // If we have removed an entry, add a dummy value back to the start to ensure our graph
                // doesn't suddenly clip at the end. Happens if the value being removed is over window and the value being
                // added is less than the window.
                if (bWasRemoved)
                {
                    AddDataPoint(x - XAxis.Max, startValue);
                }
            }

            // Always sort the resulting data along the X-axis, it makes drawing simpler.
            // If this becomes an issue, do sorting at the insertion point rather than sorting
            // entire list.
            //            this.Data.Sort((c1, c2) => Math.Sign(c1.X - c2.X));
        }