Esempio n. 1
0
        internal static int CalculateYAxis(bool showYAxisText, bool showYAxisLines, IEnumerable <ChartEntry> entries, int yAxisMaxTicks, SKPaint yAxisTextPaint, Position yAxisPosition, int width, out float yAxisXShift, out List <float> yAxisIntervalLabels)
        {
            yAxisXShift         = 0.0f;
            yAxisIntervalLabels = new List <float>();
            if (showYAxisText || showYAxisLines)
            {
                var yAxisWidth = width;

                var enumerable = entries.ToList(); // to avoid double enumeration
                var minValue   = enumerable.Min(e => e.Value);
                var maxValue   = enumerable.Max(e => e.Value);
                if (minValue == maxValue)
                {
                    if (minValue >= 0)
                    {
                        maxValue += 100;
                    }
                    else
                    {
                        maxValue = 0;
                    }
                }

                NiceScale.Calculate(minValue, maxValue, yAxisMaxTicks, out var range, out var tickSpacing, out var niceMin, out var niceMax);

                var ticks = (int)(range / tickSpacing);

                yAxisIntervalLabels = Enumerable.Range(0, ticks)
                                      .Select(i => (float)(niceMax - (i * tickSpacing)))
                                      .ToList();

                var longestYAxisLabel      = yAxisIntervalLabels.Aggregate(string.Empty, (max, cur) => max.Length > cur.ToString().Length ? max : cur.ToString());
                var longestYAxisLabelWidth = MeasureHelper.MeasureTexts(new string[] { longestYAxisLabel }, yAxisTextPaint).Select(b => b.Width).FirstOrDefault();
                yAxisWidth = (int)(width - longestYAxisLabelWidth);
                if (yAxisPosition == Position.Left)
                {
                    yAxisXShift = longestYAxisLabelWidth;
                }

                // to reduce chart width
                width = yAxisWidth;
            }

            return(width);
        }
Esempio n. 2
0
        internal static void DrawYAxis(bool showYAxisText, bool showYAxisLines, Position yAxisPosition, SKPaint yAxisTextPaint, SKPaint yAxisLinesPaint, float margin, float animationProgress, float maxValue, float valueRange, SKCanvas canvas, int width, float yAxisXShift, List <float> yAxisIntervalLabels, float headerHeight, SKSize itemSize, float origin)
        {
            if (showYAxisText || showYAxisLines)
            {
                int cnt       = 0;
                var intervals = yAxisIntervalLabels
                                .Select(t => new ValueTuple <string, SKPoint>
                                        (
                                            t.ToString(),
                                            new SKPoint(yAxisPosition == Position.Left ? yAxisXShift : width, MeasureHelper.CalculatePoint(margin, animationProgress, maxValue, valueRange, t, cnt++, itemSize, origin, headerHeight).Y)
                                        ))
                                .ToList();

                if (showYAxisText)
                {
                    DrawYAxisText(yAxisTextPaint, yAxisPosition, canvas, intervals);
                }

                if (showYAxisLines)
                {
                    var lines = intervals.Select(tup =>
                    {
                        (_, SKPoint pt) = tup;

                        return(yAxisPosition == Position.Right ?
                               SKRect.Create(0, pt.Y, width, 0) :
                               SKRect.Create(yAxisXShift, pt.Y, width, 0));
                    });

                    DrawYAxisLines(margin, yAxisLinesPaint, canvas, lines);
                }
            }
        }