Exemplo n.º 1
0
        // << path-geometry-set-line-geometry

        // >> path-gettingstarted-custom-geometry
        internal RadPathGeometry CreateArc()
        {
            var geometry = new RadPathGeometry();

            RadPathFigure figure = new RadPathFigure();

            figure.StartPoint = new Point(0.5, 0.5);
            // >> path-geometry-arc-segment
            RadArcSegment segmentA = new RadArcSegment();

            segmentA.Center     = new Point(0.5, 0.5);
            segmentA.Size       = new Size(1, 1);
            segmentA.StartAngle = 315;
            segmentA.SweepAngle = 270;
            // << path-geometry-arc-segment
            figure.Segments.Add(segmentA);

            RadArcSegment segmentB = new RadArcSegment();

            segmentB.Center     = new Point(0.5, 0.5);
            segmentB.Size       = new Size(1 - 0.1, 1 - 0.1);
            segmentB.StartAngle = 315 + 270;
            segmentB.SweepAngle = -270;
            figure.Segments.Add(segmentB);

            geometry.Figures.Add(figure);

            return(geometry);
        }
        private static Grid GeneratePieChart(IList <ChartDataPoint> dataPoints, List <Color> colors, bool showLegend = true)
        {
            // Root container to hold the chart and any legend
            var container = new Grid();

            container.RowDefinitions.Add(new RowDefinition {
                Height = new GridLength(3, GridUnitType.Star)
            });
            container.RowDefinitions.Add(new RowDefinition {
                Height = new GridLength(1, GridUnitType.Star)
            });

            // ***** Part 1 - PIE SERIES GENERATION ***** //

            // Sum up all the values to be displayed
            var totalValue = dataPoints.Sum(d => d.Value);

            // Variable to keep track of where each slice ended.
            double currentPosition = 0;

            // Iterate over the data points to create slices.
            for (int i = 0; i < dataPoints.Count; i++)
            {
                // Determine the percentage of the whole the slice uses.
                double slicePercentage = dataPoints[i].Value / totalValue;

                // Calculate the sweep using that percentage
                double sweep = slicePercentage * 360;

                // Create the ArcSegment using the calculated values.
                var segment = new RadArcSegment
                {
                    Center     = new Point(0.5, 0.5),
                    Size       = new Size(1, 1),
                    StartAngle = currentPosition,
                    SweepAngle = sweep,
                };

                // Important - Calculate the last segment's ending angle in order to have a valid start angle for the next loop.
                currentPosition = currentPosition + sweep - 360;

                // Prepare the required PathFigure and add the ArcSegment
                var figure = new RadPathFigure {
                    StartPoint = new Point(0.5, 0.5)
                };
                figure.Segments.Add(segment);

                // Create the PathGeometry and add the PathFigure
                var geometry = new RadPathGeometry();
                geometry.Figures.Add(figure);

                // Construct the RadPath
                // - Select a Fill color from the brushes parameter (important: use a modulus to wrap to the beginning)
                // - Use the Geometry created from the value
                var slice = new RadPath
                {
                    Fill              = new RadSolidColorBrush(colors[i % colors.Count]),
                    Geometry          = geometry,
                    HorizontalOptions = LayoutOptions.Center,
                    VerticalOptions   = LayoutOptions.Center,
                    WidthRequest      = 100,
                    HeightRequest     = 100,
                    Margin            = new Thickness(0, 20, 0, 0)
                };

                // This isn't necessary, but added for completion.
                Grid.SetRow(slice, 0);

                // Finally, add it to the container.
                container.Children.Add(slice);
            }

            // ***** Part 2 - LEGEND GENERATION ***** //

            if (showLegend)
            {
                var legendPanel = new StackLayout();
                legendPanel.Orientation       = StackOrientation.Horizontal;
                legendPanel.HorizontalOptions = LayoutOptions.Center;
                legendPanel.VerticalOptions   = LayoutOptions.Center;
                legendPanel.Margin            = new Thickness(0, 16, 0, 0);
                legendPanel.Spacing           = 5;

                for (int i = 0; i < dataPoints.Count; i++)
                {
                    // Use a RadBorder with only a bottom thickness and match the color to the slice
                    var legendItem = new RadBorder();
                    legendItem.BorderColor     = colors[i % colors.Count];
                    legendItem.BorderThickness = new Thickness(0, 0, 0, 2);

                    // Create a Label for each data point and use the Title property
                    var label = new Label
                    {
                        Text      = dataPoints[i].Title,
                        FontSize  = 12,
                        Margin    = new Thickness(0, 0, 0, 2),
                        TextColor = (Color)Application.Current.Resources["LightGrayTextColor"]
                    };

                    legendItem.Content = label;

                    legendPanel.Children.Add(legendItem);
                }

                Grid.SetRow(legendPanel, 1);

                container.Children.Add(legendPanel);
            }

            return(container);
        }