EvotecIT / PSWriteOffice

Experimental PowerShell Module to create and edit Microsoft Word, Microsoft Excel, and Microsoft PowerPoint documents without having Microsoft Office installed.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PieChart method

OneFreemanWill opened this issue · comments

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?

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.

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!

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

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.

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

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