Exemple #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

                NiceScale.Calculate(enumerable.Min(e => e.Value), enumerable.Max(e => e.Value), 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);
        }
Exemple #2
0
        internal static int CalculateYAxis(bool showYAxisText, bool showYAxisLines, IEnumerable <ChartEntry> entries, int yAxisMaxTicks, SKPaint yAxisTextPaint, Position yAxisPosition, int width, bool fixedRange, ref float maxValue, ref float minValue, out float yAxisXShift, out List <float> yAxisIntervalLabels)
        {
            yAxisXShift         = 0.0f;
            yAxisIntervalLabels = new List <float>();
            if (showYAxisText || showYAxisLines)
            {
                var    yAxisWidth = width;
                double range, niceMin, niceMax, tickSpacing;
                int    ticks;

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

                    NiceScale.Calculate(minValue, maxValue, yAxisMaxTicks, out range, out tickSpacing, out niceMin, out niceMax);
                    ticks = (int)(range / tickSpacing);
                }
                else
                {
                    niceMin     = minValue;
                    niceMax     = maxValue;
                    range       = niceMax - niceMin;
                    tickSpacing = range / (yAxisMaxTicks - 1);
                    ticks       = yAxisMaxTicks;
                }

                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;
                maxValue = (float)niceMax;
                minValue = (float)niceMin;
            }

            return(width);
        }