Пример #1
0
        public static void DrawGrid(SKCanvas canvas, GridSize gridSize, Padding padding, List <string> xAxis, List <string> yAxis, string title, List <string> secondYAxis = null)
        {
            // TODO consider what to do with the last entry -> add empty label?
            int columns = xAxis.Count - 1;
            int rows    = yAxis.Count - 1;


            var labelPaint = MediumTextPaint;

            // draw columns
            for (int i = 0; i <= columns; i++)
            {
                canvas.DrawText($"{xAxis[i]}", new SKPoint(gridSize.XMin + (i * (gridSize.XSize / columns)) - 25, gridSize.YMin + labelPaint.TextSize), labelPaint);

                if (i < columns)
                {
                    canvas.DrawLine(new SKPoint(gridSize.XMin + (i * (gridSize.XSize / columns)), gridSize.YMin), new SKPoint(gridSize.XMin + (i * (gridSize.XSize / columns)), gridSize.YMax), DefaultDrawing);
                }
            }
            canvas.DrawLine(new SKPoint(gridSize.XMax, gridSize.YMin), new SKPoint(gridSize.XMax, gridSize.YMax), DefaultDrawing);

            // draw rows
            for (int i = 0; i <= rows; i++)
            {
                var leftText = labelPaint;
                leftText.TextAlign = SKTextAlign.Right;

                canvas.DrawText($"{yAxis[i]}", new SKPoint(padding.Left - 5, padding.Top + gridSize.YSize - (gridSize.YSize / rows) * i + labelPaint.TextSize / 2), labelPaint);
                if (secondYAxis != null)
                {
                    canvas.DrawText($"{secondYAxis[i]}", new SKPoint(gridSize.XMax + 15, padding.Top + gridSize.YSize - (gridSize.YSize / rows) * i + labelPaint.TextSize / 2), labelPaint);
                }

                if (i < rows)
                {
                    canvas.DrawLine(new SKPoint(gridSize.XMin, padding.Top + gridSize.YSize - (gridSize.YSize / rows) * i), new SKPoint(gridSize.XMax, padding.Top + gridSize.YSize - (gridSize.YSize / rows) * i), DefaultDrawing);
                }
            }
            canvas.DrawLine(new SKPoint(gridSize.XMin, gridSize.YMax), new SKPoint(gridSize.XMax, gridSize.YMax), DefaultDrawing);

            // Draw title
            canvas.DrawText(title, new SKPoint(padding.Left, padding.Top / 2), TitleTextPaint);
        }
Пример #2
0
        public static List <SKPoint> GetPoints(Dictionary <DateTime, int> data, GridSize gridSize, bool yZeroIndexed = true, DateTime?minDate = null, DateTime?maxDate = null, bool overlapDays = false, int maxValue = -1)
        {
            // TODO implement overlap days

            List <SKPoint> dataPoints = new List <SKPoint>();



            // assume the dictionary is ordered
            // todo ensure that just in case

            DateTime firstDateTime = minDate ?? data.First().Key;
            DateTime lastDateTime  = maxDate ?? data.Last().Key;

            long totalSec = (long)(lastDateTime - firstDateTime).TotalSeconds; // sec granularity

            if (totalSec == 0)
            {
                return(dataPoints);
            }

            int minVal = int.MaxValue;
            int maxVal = int.MinValue;

            foreach (var item in data)
            {
                if (item.Value < minVal)
                {
                    minVal = item.Value;
                }
                if (item.Value > maxVal)
                {
                    maxVal = item.Value;
                }
            }

            if (yZeroIndexed)
            {
                minVal = 0;
            }

            // set max value incase its specified
            if (maxValue > 0)
            {
                maxVal = maxValue;
            }

            foreach (var item in data)
            {
                if (maxVal == 0)
                {
                    break;
                }

                long currentTotalSec = (long)(item.Key - firstDateTime).TotalSeconds; // sec granularity

                decimal xPercentage = currentTotalSec / (decimal)totalSec;
                decimal yPercentage = (item.Value - minVal) / (decimal)(maxVal - minVal);

                int xpos = (int)(gridSize.XSize * xPercentage) + gridSize.XMin;
                int ypos = gridSize.YSize - (int)(gridSize.YSize * yPercentage) + gridSize.YMax;

                dataPoints.Add(new SKPoint(xpos, ypos));
            }

            return(dataPoints);
        }