Ejemplo n.º 1
0
        protected SKPoint[] CalculatePoints(IEnumerable <ChartValueItem> valueItems, SKRect frame, SKRect chart)
        {
            var result = new List <SKPoint>();

            // Calculate how many values one y position is
            var valueY = MaxValue / frame.Height;

            // Calculate the width of one item (distance between items)
            var itemWidth = chart.GetItemWidth(MaxItems);

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

                float x;

                if (ChartValueItemsXPoints.FirstOrDefault(p => p.Item1.ToString() == entry.Tag.ToString())?.Item2 > 0)
                {
                    x = ChartValueItemsXPoints.FirstOrDefault(p => p.Item1.ToString() == entry.Tag.ToString()).Item2;
                }
                else
                {
                    x = chart.Left + (i * itemWidth);
                }

                // Calculate items y position with frame height (bottom to upper)
                var y = frame.Top + (frame.Height - (entry.Value / valueY));

                var point = new SKPoint(x, y);
                result.Add(point);

                entry.Point = point;
            }

            return(result.ToArray());
        }
Ejemplo n.º 2
0
        private void LineChart_PaintSurface(object sender, SKPaintSurfaceEventArgs e)
        {
            if (ChartEntries?.Any() != true)
            {
                return;
            }

            var info   = e.Info;
            var canvas = e.Surface.Canvas;

            canvas.Clear();

            var frame = CreateFrame(info);
            var chart = CreateChart(frame);

            if (!IsInitialized)
            {
                // Makes slider first init in middle of frame
                TouchedPoint = new SKPoint(frame.MidX, 0);
            }

            DrawVerticalLabels(canvas, frame, chart);

            if (ChartEntries.Any(x => x.IsVisible))
            {
                CalculateChartValuesXPoints(chart);

                DrawBackground(canvas, frame);
                DrawInnerFrame(canvas, frame);

                if (!string.IsNullOrEmpty(SelectedTag) && !isTouching)
                {
                    var selectedTagPosition = ChartValueItemsXPoints.FirstOrDefault(x => x.Item1.ToString() == SelectedTag)?.Item2 ?? 0f;

                    TouchedPoint = new SKPoint(selectedTagPosition, 0f);
                }

                DrawSlider(canvas, frame, chart);

                foreach (var entry in ChartEntries.Where(x => x.IsVisible).OrderByDescending(x => x.Items.Count()))
                {
                    DrawLines(entry, canvas, CalculatePoints(entry.Items, frame, chart));
                }

                DrawHorizontalLabels(canvas, frame, chart);

                // Get items on x axis
                var valueItems = ChartEntries.GetChartValueItemFromX(chart.GetInsideXValue(TouchedPoint.X), frame, frame.GetItemWidth(MaxItems));

                // Send selected items with command
                SelectedValuesCommand?.Execute(new SelectedChartValueItemArgs
                {
                    ChartValueItems = valueItems,
                    TouchedPoint    = new SKPoint(chart.GetInsideXValue(TouchedPoint.X), TouchedPoint.Y)
                });

                DrawHorizontalLabel(valueItems?.FirstOrDefault()?.ChartValueItem, canvas, frame, chart);

                DrawFrame(canvas, frame);

                DrawSliderPoints(valueItems, canvas, chart);
            }
            else
            {
                DrawInnerFrame(canvas, frame);
                DrawFrame(canvas, frame);
            }

            IsInitialized = true;
        }