Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Canvas Deprecated? #845

Open
blogcraft opened this issue Apr 19, 2024 · 2 comments
Open

Canvas Deprecated? #845

blogcraft opened this issue Apr 19, 2024 · 2 comments

Comments

@blogcraft
Copy link

Describe the bug
I used to draw svg using Canvas. Now its deprecated and documentation still lists canvas as usable.

If there is a new way to work here, I would like to get pointed to documentation please.

To Reproduce

    public void Compose(IContainer container)
    {
        container.Column(col =>
        {
            col.Item()
                .Height(20)
                .Row(row =>
                {
                    row.ConstantItem(9)
                        .Canvas(
                            (canvas, _) =>
                            {
                                using var paint = new SKPaint
                                {
                                    Color = SKColor.Parse("#3A5EA8"),
                                    StrokeWidth = 1,
                                    IsStroke = false
                                };

                                canvas.DrawRect(0, 0, 9, 20, paint);
                            }
                        );
                    row.RelativeItem().PaddingLeft(4).AlignMiddle().Text(Text);
                });
        });
    }

Also I work with Chart library OxyPlot to generate SKSvg and then canvas.DrawPicture and also wont work.

public void Compose(IContainer container)
{
    container.Column(column =>
    {
        column
            .Item()
            .Canvas(
                (canvas, size) =>
                {
                    var model = CreatePieChart(Titulo, Data);

                    SvgExporter exporter = new() { Height = size.Height, Width = size.Width };
                    var pieChartSvg = new SKSvg();
                    MemoryStream stream = new();
                    exporter.Export(model, stream);
                    MemoryStream memoryStream = new(stream.ToArray());

                    pieChartSvg.Load(memoryStream);
                    canvas.DrawPicture(pieChartSvg.Picture);
                }
            );
    });
}

private static PlotModel CreatePieChart(string titulo, PieSlice[] data)
{
    var model = new PlotModel
    {
        Title = titulo,
        DefaultFont = "Lato",
        TitleFont = "Lato",
        SubtitleFont = "Lato",
        DefaultFontSize = 8,
        TitleFontSize = 8,
        SubtitleFontSize = 8,
        TextColor = OxyColor.Parse("#231f20"),
    };

    PieSeries? ps =
        new()
        {
            StrokeThickness = .25,
            InsideLabelPosition = .25,
            StartAngle = -90,
            Font = "Lato",
            FontSize = 8,
            TextColor = OxyColor.Parse("#231f20"),
            TickRadialLength = 0,
            TickHorizontalLength = 0,
            InsideLabelFormat = "",
            OutsideLabelFormat = "{2:#0.00} %",
            TickLabelDistance = 10,
        };

    foreach (var item in data)
    {
        ps.Slices.Add(item);
    }
    model.Series.Add(ps);
    return model;
}

Expected behavior
Draw a square or a chart.

Screenshots
image

Environment
Windows and Linux

Additional context
Need to know how the new way works :/

@MarcinZiabek
Copy link
Member

Thank you for sharing your experience 😄

As the QuestPDF 2024.3.0 removes SkiaSharp as its dependency, the integration process looks slightly different. I have just updated the documentation that:

  1. provides two methods that should be copied to the codebase to enable the integration (https://www.questpdf.com/concepts/skia-sharp-integration.html)
  2. show how to use those methods to integrate SkiaSharp-based libraries with QuestPDF (https://www.questpdf.com/examples/implementing-charts.html)

If your library can directly output SVG image, you can also use the new built-in native SVG support: https://www.questpdf.com/api-reference/image.html#svg-support

I hope it will helps. Please feel free to share your feedback

@blogcraft
Copy link
Author

blogcraft commented Apr 24, 2024

Thank a lot! It works perfectly!

I noticed 2 issues, but i created a separate Issue for them:

#863

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants