private void InitChart(Size size)
 {
     _chart = new SKCartesianChart
     {
         Width = size.Width, Height = size.Height, Background = SKColors.White
     };
     _textSize = Math.Max(Math.Min(size.Width / TextScalingFactor, MaxTextSize), MinTextSize);
 }
Exemplo n.º 2
0
        private void CreateImageFromCartesianControl()
        {
            var chartControl = _cartesian;
            var skChart      = new SKCartesianChart(chartControl)
            {
                Width = 900, Height = 600,
            };

            skChart.SaveImage("CartesianImageFromControl.png");
        }
Exemplo n.º 3
0
    private void CreateImageFromCartesianControl()
    {
        // you can take any chart in the UI, and build an image from it // mark
        var chartControl = this.FindControl <CartesianChart>("cartesianChart");
        var skChart      = new SKCartesianChart(chartControl)
        {
            Width = 900, Height = 600,
        };

        skChart.SaveImage("CartesianImageFromControl.png");
    }
Exemplo n.º 4
0
    private void CreateImageFromCartesianControl()
    {
        // you can take any chart in the UI, and build an image from it // mark
        var chartControl = (CartesianChart)FindByName("cartesianChart");
        var skChart      = new SKCartesianChart(chartControl)
        {
            Width = 900, Height = 600,
        };

        skChart.SaveImage(Path.Combine(_folderPath, "CartesianImageFromControl.png"));
    }
Exemplo n.º 5
0
        private void CreateImageFromCartesianControl()
        {
            var chartControl = (CartesianChart)FindByName("cartesianChart");
            var skChart      = new SKCartesianChart(chartControl)
            {
                Width = 900, Height = 600,
            };

            skChart.SaveImage(
                Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "CartesianImageFromControl.png"));
        }
Exemplo n.º 6
0
        private void Initialize(SKCartesianChart chart)
        {
            var primaryXAxis = chart.XAxes.First();

            _textHeight = (int)primaryXAxis.NameTextSize;
            MaxWidth    = (int)(chart.Core.DrawMarginSize.Width / 2);
            MaxHeight   = (int)(chart.Height - chart.Core.DrawMarginSize.Height) / 2;

            _imageInfo = new SKImageInfo(MaxWidth, MaxHeight, SKColorType.Rgba8888, SKAlphaType.Premul);
            _surface   = SKSurface.Create(_imageInfo);
            _canvas    = _surface.Canvas;
            _canvas.Clear(SKColors.Transparent);
        }
Exemplo n.º 7
0
        public void Draw(SKCartesianChart chart)
        {
            Initialize(chart);

            var borderPaint = CreateLinePaint(SKColors.Black);
            var textPaint   = borderPaint.Clone();

            textPaint.IsStroke = false;

            var primaryXAxis    = chart.XAxes.First();
            var xAxisTitleWidth = MeasureTextWidth(primaryXAxis.Name, textPaint);

            MaxWidth -= xAxisTitleWidth / 2;
            var cursor = new LegendCursor(MaxWidth, _textHeight);

            foreach (var chartSeries in chart.Series)
            {
                if (!(chartSeries is ILineSeries <SkiaSharpDrawingContext> entry))
                {
                    continue;
                }
                cursor.CheckNextLine();
                var strokePaint = CreateStrokePaint(entry);

                _canvas.DrawLine(cursor.X, cursor.LineY, cursor.LineEndX, cursor.LineY, strokePaint);
                _canvas.DrawText(entry.Name, cursor.TextStartX, cursor.TextY, textPaint);

                // This is a bit of a shortcut, all geometries become circles. Not an issue right now
                // since we only use a circle. Still need to figure out how to use geometries more effectively.
                if (entry.GeometrySize > 0 && entry.GeometryFill is Paint geometryFill)
                {
                    DrawCircle(
                        new SKPoint(cursor.GeometryX, cursor.LineY),
                        entry.GeometrySize,
                        geometryFill.Color);
                }
                var seriesNameWidth = MeasureTextWidth(entry.Name, textPaint);
                cursor.TextWidth = seriesNameWidth;
                cursor.NextSeries();
            }

            Width  = (int)Math.Round(Math.Min(cursor.MaxX, MaxWidth));
            Height = (int)Math.Round(Math.Min(cursor.MaxY, MaxHeight));
            DrawBorder(borderPaint);
            CreateResizedImage();
        }
Exemplo n.º 8
0
        public View()
        {
            InitializeComponent();
            Size = new System.Drawing.Size(90, 90);

            var viewModel = new ViewModel();


            // Adding a cartesian chart to the UI...
            _cartesian = new CartesianChart
            {
                Series = viewModel.CatesianSeries,

                // out of livecharts properties...
                Location = new System.Drawing.Point(0, 0),
                Size     = new System.Drawing.Size(400, 200),
                //Anchor = AnchorStyles.Left | AnchorStyles.Right
            };
            Controls.Add(_cartesian);

            // Adding a pie chart to the UI...
            _pie = new PieChart
            {
                Series = viewModel.PieSeries,

                // out of livecharts properties...
                Location = new System.Drawing.Point(0, 200),
                Size     = new System.Drawing.Size(400, 200),
                //Anchor = AnchorStyles.Left | AnchorStyles.Right
            };
            Controls.Add(_pie);

            // Adding a map chart to the UI...
            _map = new GeoMap
            {
                Shapes = viewModel.MapShapes,

                // out of livecharts properties...
                Location = new System.Drawing.Point(0, 400),
                Size     = new System.Drawing.Size(400, 200),
                //Anchor = AnchorStyles.Left | AnchorStyles.Right
            };
            Controls.Add(_map);

            // CARTESIAN CHART IMAGE

            // you can create an image of a chart from memory using the
            // SKCartesianChart, SKPieChart or SKGeoMap classes.

            // in the case of this sample
            // the image was generated at the root folder
            var cartesianChart = new SKCartesianChart
            {
                Width  = 900,
                Height = 600,
                Series = viewModel.CatesianSeries
            };

            // notice classes that implement ISkiaSharpChart (SKCartesianChart, SKPieChart and SKGeoMap classes)
            // do not require a UI you can use this objects installing only the
            // LiveChartsCore.SkiaSharpView package.

            // you can save the image to png (by default), or use the second argument to specify another format.
            cartesianChart.SaveImage("CartesianImageFromMemory.png"); // <- path where the image will be generated

            // alternatively you can get the image and do different operations:
            using var image = cartesianChart.GetImage();
            using var data  = image.Encode();
            var base64 = Convert.ToBase64String(data.AsSpan());

            // or you could also use a chart in the user interface to create an image
            CreateImageFromCartesianControl();


            // PIE CHART IMAGE
            new SKPieChart
            {
                Width  = 900,
                Height = 600,
                Series = viewModel.PieSeries
            }.SaveImage("PieImageFromMemory.png");

            // or create it from a control in the UI
            CreateImageFromPieControl();


            // GEO MAP CHART IMAGE
            new SKGeoMap
            {
                Width  = 900,
                Height = 600,
                Shapes = viewModel.MapShapes
            }.SaveImage("MapImageFromMemory.png");

            // or create it from a control in the UI
            CreateImageFromGeoControl();
        }
Exemplo n.º 9
0
        public View()
        {
            InitializeComponent();

            var vm = new ViewModel();

            DataContext = vm;

            // CARTESIAN CHART

            // you can create an image of a chart from memory using the
            // SKCartesianChart, SKPieChart or SKGeoMap classes.

            // in the case of this sample
            // the image was generated at the root folder ( samples/AvaloniaSample/bin/Debug/{targetFramework}/ )

            var cartesianChart = new SKCartesianChart
            {
                Width  = 900,
                Height = 600,
                Series = vm.CatesianSeries
            };

            // notice classes that implement ISkiaSharpChart (SKCartesianChart, SKPieChart and SKGeoMap classes)
            // do not require a UI you can use this objects installing only the
            // LiveChartsCore.SkiaSharpView package.

            // you can save the image to png (by default), or use the second argument to specify another format.
            cartesianChart.SaveImage("CartesianImageFromMemory.png"); // <- path where the image will be generated

            // alternatively you can get the image and do different operations:
            using var image = cartesianChart.GetImage();
            using var data  = image.Encode();
            var base64 = Convert.ToBase64String(data.AsSpan());

            // or you could also use a chart in the user interface to create an image
            CreateImageFromCartesianControl();


            // PIE CHART
            new SKPieChart
            {
                Width  = 900,
                Height = 600,
                Series = vm.PieSeries
            }.SaveImage("PieImageFromMemory.png");

            // or create it from a control in the UI
            CreateImageFromPieControl();


            // GEO MAP CHART
            new SKGeoMap
            {
                Width  = 900,
                Height = 600,
                Values = vm.MapValues
            }.SaveImage("MapImageFromMemory.png");

            // or create it from a control in the UI
            CreateImageFromGeoControl();
        }