internal static float CalculateYPositionForStraight(ChartValueItem firstItem, ChartValueItem secondItem, float xPosition) { if (secondItem == null) { return(firstItem.Point.Y); } // Difference in value var diffVal = secondItem.Value - firstItem.Value; // Difference in Y position var diffY = secondItem.Point.Y - firstItem.Point.Y; if (diffY == 0) { return(firstItem.Point.Y); } // Difference in X position var diffX = secondItem.Point.X - firstItem.Point.X; var yValue = diffVal / diffY; var xValue = diffVal / diffX; // Current X position over first item var x = xPosition - firstItem.Point.X; var points = x * xValue; return((points / yValue) + firstItem.Point.Y); }
public ChartValueItemParam(ChartValueItem chartValueItem, ChartValueItem nextChartValueItem, ChartItem parent) { this.ChartValueItem = chartValueItem; this.NextChartValueItem = nextChartValueItem; this.BackgroundColor = parent.Color; this.TextColor = parent.TextColor; this.Parent = parent; }
public static float CalculateExactValue(ChartValueItem firstItem, ChartValueItem secondItem, float xPosition) { if (secondItem == null) { return(firstItem.Value); } // Difference in value var diffVal = secondItem.Value - firstItem.Value; // Difference in X position var diffX = secondItem.Point.X - firstItem.Point.X; var xValue = diffVal / diffX; // Current X position over first item var x = xPosition - firstItem.Point.X; var points = x * xValue; float currentVal = 0; if (secondItem != null) { try { currentVal = firstItem.Value + (int)Math.Round(decimal.Parse(points.ToString()), 0, MidpointRounding.AwayFromZero); } catch (Exception ex) { ex.Print(); } if (xPosition == firstItem.Point.X) { currentVal = firstItem.Value; } else if (xPosition == secondItem?.Point.X) { currentVal = secondItem.Value; } } else { currentVal = firstItem.Value; } return(currentVal); }
internal static SKPoint CalculateYPositionForSpline(ChartValueItem firstItem, ChartValueItem secondItem, float xPosition) { if (secondItem == null) { //return firstItem.Point.Y; return(firstItem.Point); } var y = CalculateYPositionForStraight(firstItem, secondItem, xPosition).ToRounded(); var point = firstItem.Point; var nextPoint = secondItem.Point; var offsetPoint = new SKPoint((nextPoint.X - point.X) * .5f, 0); var currentPoint = point + offsetPoint; var next = nextPoint - offsetPoint; float diffY; // Difference in Y position diffY = (firstItem.Point.Y > secondItem.Point.Y ? firstItem.Point.Y - secondItem.Point.Y : secondItem.Point.Y - firstItem.Point.Y).ToRounded(); if (diffY == 0) { return(new SKPoint(xPosition, firstItem.Point.Y)); } var insideY = y > firstItem.Point.Y ? y - firstItem.Point.Y.ToRounded() : firstItem.Point.Y - y; var t = (insideY / diffY).ToRounded(2); var beziers = HandleBezier(point, currentPoint, next, nextPoint, t); var doits = DoIt(point.X, point.Y, currentPoint.X, currentPoint.Y, next.X, next.Y, nextPoint.X, nextPoint.Y, t); return(doits); }
public static IList <ChartValueItemParam> GetChartValueItemFromX(this IList <ChartItem> chartItems, float xPosition, SKRect frame, float itemWidth) { var items = new List <ChartValueItemParam>(); if (chartItems?.Any(x => x.IsVisible) != true) { return(items); } foreach (var chartEntry in chartItems.Where(x => x.Items?.Any() == true && x.IsVisible)) { var valueItems = chartEntry.Items; ChartValueItem item = null; if (true) { // Order list and takes the first that's lower then X value item = valueItems.OrderByDescending(c => c.Point.X).FirstOrDefault(c => c.Point.X <= xPosition); } else { // Create a bound and get item inside this bound foreach (var valueItem in valueItems) { SKRect rect = new SKRect((xPosition - (itemWidth / 2)), 0, (xPosition + (itemWidth / 2)), frame.Bottom + 2); if (rect.Contains(valueItem.Point)) { item = valueItem; } } } // No item found so check if we touched left of the frame if (item == null) { item = valueItems.FirstOrDefault(); if ((xPosition <= frame.Left && item.Point.X == frame.Left)) { items.Add(new ChartValueItemParam(item, null, chartEntry)); } continue; } // Get the index of the found item int index = valueItems.ToList().IndexOf(item); // It's the last item in the list so add just the this entry if (index + 1 >= valueItems.Count()) { if ((xPosition >= frame.Right && item.Point.X == frame.Right) || item.Point.X >= (xPosition - (itemWidth / 2))) { items.Add(new ChartValueItemParam(item, null, chartEntry)); } continue; } // Takes the next item in the list var nextItem = valueItems.ToList()[index + 1]; if (item != null) { // Add current item, next item and parents line color items.Add(new ChartValueItemParam(item, nextItem, chartEntry)); } } return(items); }
protected void DrawHorizontalLabel(ChartValueItem entry, SKCanvas canvas, SKRect frame, SKRect chart) { if (!DisplayHorizontalValuesBySlider) { return; } // Draws the horizontal value the slider is on // if (string.IsNullOrEmpty(entry?.Label)) { return; } float x = chart.GetInsideXValue(TouchedPoint.X); if (ChartType == ChartType.Bar) { x = entry.Point.X; } // Draws background using (var boundPaint = new SKPaint { Color = SliderDetailColor.ToSKColor(), StrokeCap = SKStrokeCap.Round, Style = SKPaintStyle.StrokeAndFill, StrokeWidth = SliderWidth }) { var bounds = boundPaint.GetBounds( entry.Label, x, frame.Bottom + HorizontalTextSize + (HorizontalTextSize / 4), padding: InternalSliderDetailPadding); if (bounds.Right > frame.Right) { bounds.Left = frame.Right - bounds.Width + SliderWidth; bounds.Right = frame.Right + SliderWidth; } else if (bounds.Left < frame.Left) { bounds.Right = frame.Left + bounds.Width - SliderWidth; bounds.Left = frame.Left - SliderWidth; } canvas.DrawRoundRect(new SKRoundRect(bounds, SliderDetailCornerRadius), boundPaint); // Draws text using (var textPaint = new SKPaint { IsAntialias = true, TextSize = HorizontalTextSize, Color = SliderDetailTextColor.ToSKColor(), Typeface = FontTypeService.GetFontFamily(GetType().Assembly, isBold: true), TextAlign = SKTextAlign.Center, FakeBoldText = true }) { canvas.DrawText(entry.Label, bounds.MidX, bounds.MidY + (bounds.Height / 4), textPaint); } } }