none
Vertical Bar Graph Built in Powershell RRS feed

  • Question

  • I am needing to build a Vertical bar chart to graph locations and numbers. Below is a sample of the outcome we are looking for.

    Below is the Script I already have. I think it needs some work!!

    cls
    # Load .Net Chart Control Assemblies  
    [void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")  
    [void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms.DataVisualization") 
     
    # create chart object  
    $Chart = New-object System.Windows.Forms.DataVisualization.Charting.Chart
    $Chart.Width = 800  
    $Chart.Height = 700  
    $Chart.Left = 40  
    $Chart.Top = 30
    $Chart.Anchor = 'Right,Top,Left' 
     
    # create a chartarea to draw on and add to chart  
    $ChartArea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea  
    $Chart.ChartAreas.Add($ChartArea) 
     
    # add title and axes labels 
    [void]$Chart.Titles.Add("NET DDA Dashboard - " + (Get-Date -Format D))  
    ##$ChartArea.AxisX.Title = "Branches" 
    $ChartArea.AxisX.Interval = 1 
    $ChartArea.AxisY.Interval = 5
    ##$ChartArea.AxisY.Title = "Count" 
    
    $items = Import-Csv 'C:\Users\user\Desktop\test.csv'
    
    $Branches = @(foreach($item in $items){$item.Branch}) 
    $Counts = @(foreach($item in $items){[Double]$item.Count}) 
     
    [void]$Chart.Series.Add("Data") 
    $Chart.Series["Data"].Points.DataBindXY($Branches,$Counts)
    $Chart.Series["Data"].ChartType = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::StackedBar
    $Chart.Series["Data"].color = "#62B5CC"
    $Chart.Series["Data"].color.Negative = "#9644"
     
    # save chart to file 
    $path = 'C:\Users\user\Desktop\'
    $Chart.SaveImage($path + "\chart.png", "PNG") 
     

    jeudi 25 juin 2020 16:04

Toutes les réponses

  • Hi, should we guess a place where you have a problem?

    The opinion expressed by me is not an official position of Microsoft

    jeudi 25 juin 2020 20:12
  • I'll echo Victor BCO's comment. Where's your problem?

    Is this it?

    The property 'Negative' cannot be found on this object. Verify that the property exists and can be set.

    I think you have to change the color on the datapoint, not the palette for the entire series.

    https://stackoverflow.com/questions/30368353/problems-with-changing-colors-in-chart-winform


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    vendredi 26 juin 2020 15:12
  • Change this part of your code:

    [void]$Chart.Series.Add("Data") 
    $Chart.Series["Data"].Points.DataBindXY($Branches, $Counts)
    $Chart.Series["Data"].ChartType = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::StackedBar
    $Chart.Series["Data"].Points |
        ForEach-Object{
            if ($_.YValues[0] -ge 0){
                $_.Color = "#62B5CC"
            }
            else{
                $_.Color = "#FF9644"
            }
        }
    I don't know what color you want for the negative values, but "#9644" probably isn't it. Maybe "#940044" (Jazzberry Jam)?


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    • Proposé comme réponse Ian Xue jeudi 2 juillet 2020 08:43
    vendredi 26 juin 2020 19:22
  • Hi, just want to confirm the current situations.

    If the above reply is helpful, please "mark it as answer" to help other community members find it easier.


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    jeudi 2 juillet 2020 08:43