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

PieChart method #19

Open
OneFreemanWill opened this issue May 2, 2024 · 6 comments
Open

PieChart method #19

OneFreemanWill opened this issue May 2, 2024 · 6 comments

Comments

@OneFreemanWill
Copy link

OneFreemanWill commented May 2, 2024

Hello Przemysław,

I have been struggling to use .AddPieChart and .AddChartPie to add a pie chart to Word doc output.

.AddPieChart seems to work OK in that my output doc lands up with a blank section that would be a pie, but whenever I try to use .AddChartPie I get an overload error.

Most basic example:

$PieChart = $DocumentTest.AddPieChart("Test Pie Chart", $true, 600, 300)

$PieChart.AddChartPie("Category1", 10)
$PieChart.AddChartPie("Category2", 20)
$PieChart.AddChartPie("Category3", 30)

Error:

Error: Cannot find an overload for "AddChartPie" and the argument count: "2".

I've tried making sure I'm using the expected types as defined below, but every variation seems to fail and results in the overload error.

[void] AddChartPie[T](
    [string] $name,
    [int[]] $values)

[void] AddChartPie[T](
    [string] $name,
    [List[T]] $values)

Can you offer any guidance or share an example?

@PrzemyslawKlys
Copy link
Member

PrzemyslawKlys commented May 2, 2024

You can try @(10) or $List = [System.Collections.Generic.List[int]]::new(); $List.Add(10) and see if that works. I may need to add in OfficeIMO side overload for single values.

I see examples in OfficeIMO

document.AddParagraph("This is a pie chart");
var pieChart = document.AddPieChart();
pieChart.AddCategories(categories);
pieChart.AddChartPie("Poland", new List<int> { 15, 20, 30 });

So either I made mistake, or just needs improvements.

@OneFreemanWill
Copy link
Author

Tried both ways, and both result in the same error, I suspect it is indeed due to lack of handling for single values.
I will find a workaround for now.

Thank you for your response and for the module!

@PrzemyslawKlys
Copy link
Member

Ok, i checked it and it seems the implementation differs a bit from my other projects.

The trick is - it's one name, but with 4 categories, and the values match the category...

                List<string> categories = new List<string>() { "Food", "Housing", "Mix", "Data" };
                var pieChart2 = document.AddPieChart();
                pieChart2.AddCategories(categories);
                pieChart2.AddChartPie("Poland", new List<int> { 10, 20, 30, 40 });

I need to decide what to do, as the implementation is like that in all chartts - need to think about it, but till then - using categories with one entry should work.

image

@PrzemyslawKlys
Copy link
Member

This PR should resolve your issue:

It also changes how things work when it comes to charts so you will need to change some code, and then you need to null out values as it's possible to do AddPie(5).AddPie(7).AddPie(9) but this means an object is returned on AddPie, AddLine and so on.

@OneFreemanWill
Copy link
Author

Thank you very much! Got it working now following your advice, and I understand that once PsWriteOffice is updated, this will need changing.

# Define categories and corresponding data
[string[]]$Categories = @("Compliant Resources", "Non-Compliant Resources")
$CompliantResources = $ComplianceSummary.TotalResources - $ComplianceSummary.NonCompliantResources
$NonCompliantResources = $ComplianceSummary.NonCompliantResources

# Add a Pie Chart to the document
$PieChart = $PieChartDocument.AddPieChart("Azure Policy Compliance Summary")
$PieChart.AddCategories($Categories)

# Populate the pie chart with data
$Values = [System.Collections.Generic.List[int]]::new()
$Values.Add($CompliantResources)
$Values.Add($NonCompliantResources)
$PieChart.AddChartPie("Compliance Breakdown", $Values)
$PieChart.AddLegend("Bottom")
$PieChart.RoundedCorners = $true

@PrzemyslawKlys
Copy link
Member

Yes, the whole charts have been rebuilt. I need to further improve it, but upgrading PSWriteOffice to new version will break current charts.

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