/// <summary> /// Works out the actual X/Y points for the each value within the /// data and draws the line graph. /// </summary> private void UpdateLineGraph() { // Only proceed if there are some actual values and there are enough values if (this.DataValues == null || this.DataValues.Count < 2 || container.ActualHeight == 0 || this.MaxValue <= this.MinValue) { GraphLine.Visibility = Visibility.Hidden; LastPointMarkerEllipse.Visibility = Visibility.Hidden; ScaleCurrentValue.Content = string.Empty; return; } int count = this.DataValues.Count; double max = Math.Max(this.Threshold, this.DataValues.Max()); this.MaxValue = Math.Max(DEFAULT_MAX, max); double scale = this.MaxValue - this.MinValue; double valuePerPoint = container.ActualHeight / scale; double constantOffset = container.ActualWidth / Math.Min(MAX_POINTS, count); double xOffSet = 0; // For each item work out what the actual X/Y should be _graphPoints.Clear(); int start = count > MAX_POINTS ? count - MAX_POINTS : 0; for (int i = start; i < count; i++) { double trueDiff = this.DataValues[i] - this.MinValue; double heightPx = trueDiff * valuePerPoint; double yValue = container.ActualHeight - heightPx; _graphPoints.Add(new Point(xOffSet, yValue)); xOffSet += constantOffset; } // Add Polygon Points GraphLine.Points = _graphPoints; // set LastPointMarkerEllipse Point lastPoint = _graphPoints.Last(); LastPointMarkerEllipse.SetValue(Canvas.LeftProperty, lastPoint.X - (LastPointMarkerEllipse.Width / 2.0)); LastPointMarkerEllipse.SetValue(Canvas.TopProperty, lastPoint.Y - (LastPointMarkerEllipse.Height / 2.0)); LastPointMarkerEllipse.Visibility = Visibility.Visible; // Set label ScaleCurrentValue.Content = this.DataValues.Last().ToString("N0"); ScaleCurrentValue.SetValue(Canvas.LeftProperty, lastPoint.X - (LastPointMarkerEllipse.Width * 2.0)); if (lastPoint.Y < (GraphLine.ActualHeight / 2.0)) { ScaleCurrentValue.SetValue(Canvas.TopProperty, lastPoint.Y + LastPointMarkerEllipse.Height); } else { ScaleCurrentValue.SetValue(Canvas.TopProperty, lastPoint.Y - (LastPointMarkerEllipse.Height * 3.0)); } // Got points now so show graph GraphLine.Visibility = Visibility.Visible; }
/// <summary> /// Works out the actual X/Y points for the each value within the /// ObservableCollection<GraphDataItem> property. /// </summary> private void ObtainPointsForValues() { //Only proceed if there are some actual values and there are enough values if (DataValues == null || container.ActualHeight == 0 || DataValues.Count < MIN_ITEMS_TO_PLOT) { GraphLine.Visibility = Visibility.Hidden; LastPointMarkerEllipse.Visibility = Visibility.Hidden; ScaleCurrentValue.Content = string.Empty; return; } else { #region Workout Points double scale = MaxValue - MinValue; double valuePerPoint = container.ActualHeight / scale; double constantOffset = container.ActualWidth / DataValues.Count; double xOffSet = 0; ///for each item seen work out what the actual X/Y should be ///based on a bit of Maths _graphPoints.Clear(); for (int i = 0; i < DataValues.Count; i++) { double trueDiff = DataValues[i] - MinValue; double heightPx = trueDiff * valuePerPoint; double yValue = container.ActualHeight - heightPx; _graphPoints.Add(new Point(xOffSet, yValue)); xOffSet += constantOffset; } Point lastPoint = _graphPoints.Last(); //set LastPointMarkerEllipse LastPointMarkerEllipse.SetValue(Canvas.LeftProperty, lastPoint.X - (LastPointMarkerEllipse.Width / 2)); LastPointMarkerEllipse.SetValue(Canvas.TopProperty, lastPoint.Y - (LastPointMarkerEllipse.Height / 2)); LastPointMarkerEllipse.Visibility = Visibility.Visible; #endregion #region Label Current Data Point ScaleCurrentValue.Content = this.DataValues.Last().ToString("N0"); ScaleCurrentValue.SetValue(Canvas.LeftProperty, lastPoint.X - (LastPointMarkerEllipse.Width * 2)); ScaleCurrentValue.SetValue(Canvas.TopProperty, lastPoint.Y - (LastPointMarkerEllipse.Height * 2)); #endregion //Add Polygon Points GraphLine.Points = _graphPoints; //Got points now so show graph GraphLine.Visibility = Visibility.Visible; } }