iainbrighton/PScriboCharts

Stacked Bar Chart Total Label

ipxfer opened this issue · 2 comments

I like the module you've created. Nice to have charting option thru PowerShell without diving into the .net framework directly.

This is not an issue but more of a "how to"...

I am able to create a stacked bar chart with a total on top of the bar by using combined line chart. However, line chart doesn't give you any option on the placement of the label - e.g. "top" or "bottom". Resulting in missing total label as they are superseded by the bar. My question, is there a better way with the PScriboChart? Whether using a line chart with positional option or directly from bar chart it self.

image

Hi @ipxfer,

You can set the LabelStyle custom property of each data point to force it on top. Here's a working example:

## Create random series data
$sampleData = @()
for ($i = 1; $i -le 10; $i++)
{
    $index = '{0}' -f [char] ($i + 64)
    $a = (Get-Random -Minimum 5 -Maximum 20) -as [int]
    $b = (Get-Random -Minimum 5 -Maximum 20) -as [int]
    $sampleData += [PSCustomObject] @{ Index = $index; A = $a; B = $b; Total = $a + $b }
}

$exampleChart = New-Chart -Name Issue3 -Width 600 -Height 600
$exampleChartArea = Add-ChartArea -Chart $exampleChart -Name 'exampleChartArea' -NoAxisXMajorGridLines -NoAxisYMajorGridLines -PassThru

$sampleData | Add-StackedColumnChartSeries -Name 'a' -Chart $exampleChart -ChartArea $exampleChartArea -XField 'Index' -YField 'A'
$sampleData | Add-StackedColumnChartSeries -Name 'b' -Chart $exampleChart -ChartArea $exampleChartArea -XField 'Index' -YField 'B'
$sampleData | Add-LineChartSeries -Name 'total' -Chart $exampleChart -ChartArea $exampleChartArea -XField 'Index' -YField 'Total' -Color Transparent

## Force each data point label position in the last series (the totals) to 'Top'
foreach ($dataPoint in $exampleChart.Series[-1].Points)
{
    $dataPoint['LabelStyle'] = 'Top'
}

This gets rendered like so (obviously the data is randomised!)
image

Awesome! That works perfectly. Much appreciated!!!!