Automation

 View Only
  • 1.  Unable to add used % inside the doughnut chart

    Posted Jan 07, 2019 06:02 AM

    Hi,

    I am unable to add the used percentage inside the doughnut chart, please help

    Script

    function New-FlashArrayReportPiechart() {

        Param (

            [string]$FileName,

            [float]$snapSpaces,

            [float]$usedSpace,

            [float]$freespace

        )

           

        [void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

        [void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms.DataVisualization")

       

        $chart = New-Object System.Windows.Forms.DataVisualization.charting.chart

        $chart.Width = 700

        $chart.Height = 500

        $chart.Left = 10

        $chart.Top = 10

        $chartArea = New-Object System.Windows.Forms.DataVisualization.charting.chartArea

        $chart.chartAreas.Add($chartArea)

        [void]$chart.Series.Add("Data")

       

           $legend = New-Object system.Windows.Forms.DataVisualization.charting.Legend

           $legend.Name = "Legend"

        $legend.Font = "Proxima Nova"

           $legend.Alignment = "Center"

           $legend.Docking = "top"

           $legend.Bordercolor = "#FE5000"

           $legend.Legendstyle = "row"

        $chart.Legends.Add($legend)

            $datapoint = New-Object System.Windows.Forms.DataVisualization.charting.DataPoint(0, $snapSpaces)

        $datapoint.AxisLabel = "SnapShots " + "(" + $snapSpaces + " GB)"

        $chart.Series["Data"].Points.Add($datapoint)

           

        $datapoint = New-Object System.Windows.Forms.DataVisualization.charting.DataPoint(0, $usedSpace)

        $datapoint.AxisLabel = "Used Capacity " + "(" + $usedSpace + " GB)"

        $chart.Series["Data"].Points.Add($datapoint)

       

        $datapoint = New-Object System.Windows.Forms.DataVisualization.charting.DataPoint(0, $freespace)

        $datapoint.AxisLabel = "Free Capacity " + "(" + $freespace + " GB)"

        $chart.Series["Data"].Points.Add($datapoint)

           

            $chart.Series["Data"].chartType = [System.Windows.Forms.DataVisualization.charting.SerieschartType]::Doughnut

        $chart.Series["Data"]["DoughnutLabelStyle"] = "Outside"

        $chart.Series["Data"]["DoughnutLineColor"] = "#FE5000"

       

        $Title = New-Object System.Windows.Forms.DataVisualization.charting.Title

        $chart.Titles.Add($Title)

        $chart.SaveImage($FileName + ".png","png")

        $Script:PiechartImgSrc = ConvertTo-Base64 ($FileName + ".png")

        Remove-Item -Path ($FileName + ".png")

    }

    Output :

    I would like to get as below, used percentage in center



  • 2.  RE: Unable to add used % inside the doughnut chart

    Posted Jan 07, 2019 07:03 AM

    Although not really a PowerCLI question, you could use a TextAnnotation.

    Something like this for example.

    function New-FlashArrayReportPiechart() {

       Param (

       [string]$FileName,

       [float]$snapSpaces,

       [float]$usedSpace,

       [float]$freespace

       )


       [void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

       [void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms.DataVisualization")


       $chart = New-Object System.Windows.Forms.DataVisualization.charting.chart

       $chart.Width = 700

       $chart.Height = 500

       $chart.Left = 10

       $chart.Top = 10


       $chartArea = New-Object System.Windows.Forms.DataVisualization.charting.chartArea

       $chart.chartAreas.Add($chartArea)

       [void]$chart.Series.Add("Data")


       $legend = New-Object system.Windows.Forms.DataVisualization.charting.Legend

       $legend.Name = "Legend"

       $legend.Font = "Proxima Nova"

       $legend.Alignment = "Center"

       $legend.Docking = "top"

       $legend.Bordercolor = "#FE5000"

       $legend.Legendstyle = "row"

       $chart.Legends.Add($legend)


       $datapoint = New-Object System.Windows.Forms.DataVisualization.charting.DataPoint(0, $snapSpaces)

       $datapoint.AxisLabel = "SnapShots " + "(" + $snapSpaces + " GB)"

       $chart.Series["Data"].Points.Add($datapoint)


       $datapoint = New-Object System.Windows.Forms.DataVisualization.charting.DataPoint(0, $usedSpace)

       $datapoint.AxisLabel = "Used Capacity " + "(" + $usedSpace + " GB)"

       $chart.Series["Data"].Points.Add($datapoint)


       $datapoint = New-Object System.Windows.Forms.DataVisualization.charting.DataPoint(0, $freespace)

       $datapoint.AxisLabel = "Free Capacity " + "(" + $freespace + " GB)"

       $chart.Series["Data"].Points.Add($datapoint)


       $chart.Series["Data"].chartType = [System.Windows.Forms.DataVisualization.charting.SerieschartType]::Doughnut

       $chart.Series["Data"]["DoughnutLabelStyle"] = "Outside"

       $chart.Series["Data"]["DoughnutLineColor"] = "#FE5000"


       $annotation = New-Object System.Windows.Forms.DataVisualization.Charting.TextAnnotation

       $usedPerc = $usedSpace / ($snapSpaces + $usedSpace + $freespace)

       $annotation.Text = "$('{0:P}' -f $usedPerc)"

       $annotation.AnchorX = 50

       $annotation.AnchorY = 62

       $annotation.Font = [System.Drawing.Font]::new('Proxima Nova', 24)

       $chart.Annotations.Add($annotation)


       $Title = New-Object System.Windows.Forms.DataVisualization.charting.Title

       $chart.Titles.Add($Title)

       $chart.SaveImage($FileName + ".png", "png")

    }


    New-FlashArrayReportPiechart -FileName c:\Temp\test -snapSpaces 100 -usedSpace 80 -freespace 30



  • 3.  RE: Unable to add used % inside the doughnut chart

    Posted Jan 07, 2019 07:44 AM

    Hi LucD,

    That worked, thank you :smileyhappy:

    But one last question, how can bold the text and increase the font size ?

    $annotation.Text = "$('{0:P}' -f $usedPerc)"



  • 4.  RE: Unable to add used % inside the doughnut chart

    Posted Jan 07, 2019 08:00 AM

    That is done with the Font property on the Annotation object.


    $annotation.Font = [System.Drawing.Font]::new('Proxima Nova', 36, [System.Drawing.FontStyle]::Bold)

    To eliminate the decimals on the percentage number, you can do

    $annotation.Text = "$('{0:P0}' -f $usedPerc)"

    Then you get something like this



  • 5.  RE: Unable to add used % inside the doughnut chart

    Posted Jan 07, 2019 08:13 AM

    Hi LucD,

    I am getting this error

    Method invocation failed because [System.Drawing.Font] does not contain a method named 'new'.

    At D:\myreports\Capacity_Report.ps1:105 char:4

    +    $annotation.Font = [System.Drawing.Font]::new('Proxima Nova', 36, [System.Dra ...

    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException

        + FullyQualifiedErrorId : MethodNotFound

    and the text is not showing Bold



  • 6.  RE: Unable to add used % inside the doughnut chart

    Posted Jan 07, 2019 08:56 AM

    Could it be that you are not yet using PowerShell 5.*?
    Check with $PSVersionTable



  • 7.  RE: Unable to add used % inside the doughnut chart

    Posted Jan 07, 2019 09:23 AM

    Hi LucD,

    After changing the below lines from

    $annotation.Font = [System.Drawing.Font]::new('Proxima Nova', 30, [System.Drawing.FontStyle]::Bold)

    to

    $Font = New-Object System.Drawing.Font @('Proxima Nova','24', [System.Drawing.FontStyle]::Bold)

    $annotation.Font = $Font

    it worked.

    one last question, how can I remove the space between decimal and %

    Currently, I am seeing as 30 % but I would like to see as 30%



  • 8.  RE: Unable to add used % inside the doughnut chart
    Best Answer

    Posted Jan 07, 2019 09:30 AM

    Yes, that was the way it had to be done in pre-5.* PowerShell.

    Try changing the line to

    $annotation.Text = "$('{0:F0}%' -f ($usedPerc*100))"



  • 9.  RE: Unable to add used % inside the doughnut chart

    Posted Jan 07, 2019 09:36 AM

    perfect, that worked :smileyhappy:

    Thank you very very much.