Ejemplo n.º 1
0
        protected void DrawHorizontalLabels(SKCanvas canvas, SKRect frame, SKRect chart)
        {
            if (HorizontalLabelMode == LabelMode.None)
            {
                return;
            }

            // Set y position where labels should be
            var y = frame.Bottom + HorizontalTextSize + (HorizontalTextSize / 4);

            using (var paint = new SKPaint
            {
                IsAntialias = true,
                TextSize = HorizontalTextSize,
                Color = HorizontalTextColor.ToSKColor(),
                Typeface = FontTypeService.GetFontFamily(GetType().Assembly),
                TextAlign = SKTextAlign.Center
            })
            {
                // Draw min value
                if (!string.IsNullOrEmpty(MinLabel))
                {
                    paint.TextAlign = ChartType == ChartType.Bar || HorizontalLabelMode == LabelMode.All ? SKTextAlign.Center : SKTextAlign.Left;
                    canvas.DrawText(MinLabel, chart.Left, y, paint);
                }

                // Draw max value
                if (!string.IsNullOrEmpty(MaxLabel) && MaxLabel != MinLabel)
                {
                    paint.TextAlign = ChartType == ChartType.Bar || HorizontalLabelMode == LabelMode.All ? SKTextAlign.Center : SKTextAlign.Right;
                    canvas.DrawText(MaxLabel, chart.Right, y, paint);
                }

                paint.TextAlign = SKTextAlign.Center;

                if (HorizontalLabelMode == LabelMode.All)
                {
                    // Draw all labels
                    var items = ChartValuesDistinct;

                    if (items?.Any() != true)
                    {
                        return;
                    }

                    for (int i = 0; i < items.Count(); i++)
                    {
                        var entry = items.ElementAt(i);
                        var point = entry.Point;

                        if (string.IsNullOrEmpty(entry?.Label) || entry?.Label == MinLabel || entry?.Label == MaxLabel)
                        {
                            continue;
                        }

                        canvas.DrawText(entry.Label, point.X, y, paint);
                    }
                }

                // Draw horizontal unit
                if (!string.IsNullOrEmpty(HorizontalUnit))
                {
                    paint.TextAlign = ChartType == ChartType.Bar || HorizontalLabelMode == LabelMode.All ? SKTextAlign.Center : SKTextAlign.Right;
                    canvas.DrawText(HorizontalUnit, chart.Right, DisplayHorizontalValuesBySlider ? y + HorizontalTextSize + ChartRectMargin.Bottom : y + HorizontalTextSize, paint);
                }
            }
        }
Ejemplo n.º 2
0
        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);
                }
            }
        }
Ejemplo n.º 3
0
        protected void DrawVerticalLabels(SKCanvas canvas, SKRect frame, SKRect chart)
        {
            if (VerticalLabelMode == LabelMode.None)
            {
                return;
            }

            // Calculates maximum value depending on chart height
            var maximumValue = chart.Height * (MaxValue / chart.Height);

            float left = 0;

            // Calculates where left is for all vertical labels
            switch (VerticalLabelAlignment)
            {
            case TextAlignment.Start:
                left = frame.Left - ChartRectMargin.Left;
                break;

            case TextAlignment.Center:
                left = ChartRectMargin.Left / 2;
                break;

            case TextAlignment.End:
                left = frame.Left + (ChartRectMargin.Left / 2);
                break;
            }

            using (var textPaint = new SKPaint
            {
                IsAntialias = true,
                TextSize = VerticalTextSize,
                Color = VerticalTextColor.ToSKColor(),
                Typeface = FontTypeService.GetFontFamily(GetType().Assembly),
                TextAlign = VerticalLabelAlignment == TextAlignment.Start ? SKTextAlign.Right : SKTextAlign.Left
            })
            {
                if (!string.IsNullOrEmpty(VerticalUnit))
                {
                    // Draws vertical unit and maximum value
                    canvas.DrawText(VerticalUnit, new SKPoint(left, (0f).GetVerticalAlignment(VerticalTextSize, TextAlignment.End)), textPaint);
                }

                // Draws maximum value
                canvas.DrawText(maximumValue.ToRoundedString(), new SKPoint(left, frame.Top.GetVerticalAlignment(VerticalTextSize, TextAlignment.End)), textPaint);

                // Draws bottom value
                canvas.DrawText("0", new SKPoint(left, frame.Bottom), textPaint);

                if (VerticalLabelMode == LabelMode.StartEnd)
                {
                    return;
                }

                // Draws maximum value
                canvas.DrawText(maximumValue.ToString(), new SKPoint(left, frame.Top.GetVerticalAlignment(VerticalTextSize, TextAlignment.End)), textPaint);

                // Draws bottom value
                canvas.DrawText("0", new SKPoint(left, frame.Bottom), textPaint);

                if (VerticalLabelMode == LabelMode.StartEnd)
                {
                    return;
                }

                // Draws middle value
                canvas.DrawText((maximumValue / 2).ToString(), new SKPoint(left, frame.MidY.GetVerticalAlignment(VerticalTextSize, TextAlignment.Center)), textPaint);

                if (VerticalLabelMode == LabelMode.StartCenterEnd)
                {
                    return;
                }

                // Draws rest of values
                canvas.DrawText((maximumValue * .25).ToString(), new SKPoint(left, ((frame.Height * .75f) + frame.Top).GetVerticalAlignment(VerticalTextSize, TextAlignment.Center)), textPaint);
                canvas.DrawText((maximumValue * .75).ToString(), new SKPoint(left, ((frame.Height * .25f) + frame.Top).GetVerticalAlignment(VerticalTextSize, TextAlignment.Center)), textPaint);
            }
        }