protected void DrawHorizontalLabels(SKCanvas canvas, SKRect frame, SKRect chart) { if (HorizontalLabelMode != LabelMode.None) { if (!string.IsNullOrEmpty(MinLabel) && !string.IsNullOrEmpty(MaxLabel)) { canvas.DrawHorizontalText(MinLabel, chart.Left, frame.Bottom + HorizontalLabelTextSize, HorizontalLabelTextSize, HorizontalLabelColor.ToSKColor(), SKTextAlign.Center, isBold: true); canvas.DrawHorizontalText(MaxLabel, chart.Right, frame.Bottom + HorizontalLabelTextSize, HorizontalLabelTextSize, HorizontalLabelColor.ToSKColor(), SKTextAlign.Center, isBold: true); } } if (!string.IsNullOrEmpty(HorizontalUnit)) { canvas.DrawHorizontalText(HorizontalUnit, chart.Right, frame.Bottom + (HorizontalLabelTextSize * 2), HorizontalLabelTextSize, HorizontalLabelColor.ToSKColor(), SKTextAlign.Left); } if (DisplayHorizontalValuesBySlider && IsSliderVisible) { var x = ChartType == ChartType.Linear ? TouchedPoint.X : chart.GetInsideXValue(TouchedPoint.X); var valueItems = ChartEntries.GetChartValueItemFromX(x, frame, MaxItems, ChartType == ChartType.Linear); var entry = valueItems?.FirstOrDefault()?.ChartValueItem; if (string.IsNullOrEmpty(entry?.Label)) { return; } x = ChartType == ChartType.Linear ? chart.GetInsideXValue(TouchedPoint.X) : entry.Point.X; canvas.DrawHorizontalText(entry.Label, x, frame.Bottom + HorizontalLabelTextSize, HorizontalLabelTextSize, SliderColor.ToSKColor(), SKTextAlign.Center, isBold: true); } else if (HorizontalLabelMode == LabelMode.All) { var items = ChartValuesDistinct; if (items?.Any() != true) { return; } var points = ChartEntries.FirstOrDefault().Items.Select(x => x.Point).ToArray(); for (int i = 0; i < items.Count(); i++) { var entry = items.ElementAt(i); var point = points[i]; if (string.IsNullOrEmpty(entry?.Label) || entry?.Label == MinLabel || entry?.Label == MaxLabel) { continue; } canvas.DrawHorizontalText(entry.Label, point.X, frame.Bottom + HorizontalLabelTextSize, HorizontalLabelTextSize, HorizontalLabelColor.ToSKColor(), SKTextAlign.Center); } } }
private void DrawBars(SKCanvas canvas, SKRect frame, SKRect chart) { // Selected bar width var selectedValueItems = ChartEntries.GetChartValueItemFromX(chart.GetInsideXValue(TouchedPoint.X), chart, chart.GetItemWidth(MaxItems)); var selectedTags = selectedValueItems?.Select(x => x.ChartValueItem.Tag); // Invoke command with selected items SelectedValuesCommand?.Execute(new SelectedChartValueItemArgs { ChartValueItems = selectedValueItems, TouchedPoint = new SKPoint(chart.GetInsideXValue(TouchedPoint.X), TouchedPoint.Y) }); using (var paint = new SKPaint { IsAntialias = true, Style = SKPaintStyle.StrokeAndFill, StrokeCap = SKStrokeCap.Butt, }) { var groupedItems = ChartEntries.Where(x => x.IsVisible).SelectMany(x => x.Items).GroupBy(x => x.Tag.ToString()).OrderBy(x => x.Key); // Calc max items in one group var maxItemsInGroups = groupedItems.Max(x => x.Count()); // Calc how many groups there is var groupCount = groupedItems.Count(); // Calc how many bars there will be var totalBars = groupCount * maxItemsInGroups; var internalGroupMargin = maxItemsInGroups == 1 ? BarMargin : GroupMargin; var internalBarMargin = maxItemsInGroups == 1 ? 0 : BarMargin; var internalBarWidth = MinimumBarWidth; float itemWidth; if (AllowScroll) { // Calc total item width itemWidth = internalBarWidth * maxItemsInGroups + ((maxItemsInGroups - 1) * barMargin); } else { itemWidth = chart.Width / groupCount; itemWidth -= internalGroupMargin; internalBarWidth = (itemWidth / maxItemsInGroups) - ((maxItemsInGroups - 1) * barMargin); } int groupIndex = 0; float scrollLeftPadding = 0; float left = 0; float groupLeft = 0; float right = 0; groupCenters = new List <GroupChartItem>(); foreach (var groupedItem in groupedItems) { int itemIndex = 0; groupLeft = left; if (!AllowScroll && IsSliderVisible && selectedTags?.Contains(groupedItem.Key) == true) { var item = groupedItem.OrderByDescending(x => x.Value).First(); paint.Color = SliderColor.ToSKColor(); paint.PathEffect = SKPathEffect.CreateCorner(SliderCornerRadius); var bounds = new SKRect( item.Point.X - (itemWidth * .5f) - (groupMargin * .5f), item.Point.Y - (itemWidth * .5f), item.Point.X + (itemWidth * .5f) + (groupMargin * .5f), chart.Bottom + HorizontalTextSize); paint.StrokeWidth = 0; canvas.DrawRect(bounds, paint); } foreach (var item in groupedItem) { var parent = ChartEntries.FirstOrDefault(x => x.Items.Contains(item)); SKRect bounds = new SKRect(); if (AllowScroll) { if (left == 0) { left = itemWidth.FromDpiAdjusted(); groupLeft = left; scrollLeftPadding = left; } else if (itemIndex != 0) { left += internalBarMargin; } right = left + internalBarWidth; bounds = new SKRect( left, item.Point.Y, right, chart.Bottom); left = right; } else { left = (item.Point.X - (itemWidth * .5f)) + (internalBarWidth * itemIndex) + (internalBarMargin * itemIndex); right = left + internalBarWidth; bounds = new SKRect( left, item.Point.Y, right, chart.Bottom); } paint.StrokeWidth = 0; if (parent.UseDashedEffect) { paint.Color = parent.Color.ToSKColor().AsTransparency(); paint.PathEffect = SKPathEffect.CreateCorner(BarCornerRadius); canvas.DrawRect(bounds, paint); paint.Color = parent.Color.ToSKColor(); paint.PathEffect = SKPathEffect.CreateDash(new float[] { StrokeDashFirst, StrokeDashSecond }, StrokeDashPhase); paint.StrokeWidth = bounds.Width; canvas.DrawLine(bounds.MidX, bounds.Bottom, bounds.MidX, bounds.Top, paint); } else { paint.Color = parent.Color.ToSKColor(); paint.PathEffect = SKPathEffect.CreateCorner(BarCornerRadius); canvas.DrawRect(bounds, paint); } itemIndex++; } left += internalGroupMargin; groupIndex++; var groupCenterPosition = groupLeft + (itemWidth / 2); groupCenters.Add(new GroupChartItem { Label = groupedItem.FirstOrDefault().Label, Position = groupCenterPosition.FromDpiAdjusted(), Tag = groupedItem.Key }); } if (AllowScroll) { var requestedWidth = right.FromDpiAdjusted() + frame.Left.FromDpiAdjusted(); if (requestedWidth != this.WidthRequest) { this.WidthRequest = requestedWidth; } else { SetStartPosition(groupCenters); IsInitialized = true; } if (ScrollComponent != null) { var deviceWidth = Xamarin.Essentials.DeviceDisplay.MainDisplayInfo.Width; var halfDeviceWidth = ((float)(deviceWidth / 2)).FromDpiAdjusted(); var frameLeftVal = frame.Left.FromDpiAdjusted(); var rectRightMarginVal = ChartRectMargin.Right.FromDpiAdjusted(); var leftPadding = halfDeviceWidth - frameLeftVal - scrollLeftPadding.FromDpiAdjusted(); var rightPadding = halfDeviceWidth - frameLeftVal - rectRightMarginVal - itemWidth.FromDpiAdjusted(); ScrollComponent.Padding = new Thickness(leftPadding, 0, rightPadding, 0); ScrollComponent.Margin = new Thickness(frame.Left.FromDpiAdjusted(), 0, ChartRectMargin.Right.FromDpiAdjusted(), 0); if (Selector != null) { Selector.WidthRequest = itemWidth.FromDpiAdjusted(); Selector.Margin = new Thickness(halfDeviceWidth, 0, 0, this.Height - frame.Bottom.FromDpiAdjusted()); } if (!isScrollEventActive) { ScrollComponent.Scrolled += ScrollComponent_Scrolled; isScrollEventActive = true; } } if (SwipeNotificationLeft != null) { SwipeNotificationLeft.Margin = new Thickness(frame.Left.FromDpiAdjusted(), 0, 0, this.Height - frame.Bottom.FromDpiAdjusted()); SwipeNotificationLeft.WidthRequest = ScrollComponent.Padding.Left; } if (SwipeNotificationRight != null) { SwipeNotificationRight.Margin = new Thickness(0, 0, ChartRectMargin.Right.FromDpiAdjusted(), this.Height - frame.Bottom.FromDpiAdjusted()); SwipeNotificationRight.WidthRequest = ScrollComponent.Padding.Left; } } } DrawHorizontalLabel(selectedValueItems?.FirstOrDefault()?.ChartValueItem, canvas, frame, chart); }