如何使用PowerShell从SQL Server数据创建图表

介绍 (Introduction)

Intended audience

目标听众

This document is intended for application developers and database administrators who are willing to learn how to generate charts from data stored in Microsoft SQL Server using PowerShell.

本文档适用于愿意学习如何使用PowerShell从Microsoft SQL Server中存储的数据生成图表的应用程序开发人员和数据库管理员。

Context

语境

In previous article entitled Overview of PowerShell Charting capabilities we’ve seen some components of an assembly called System.Windows.Forms.DataVisualization assembly that is part of the .NET Framework and how to load it inside PowerShell.

在上一篇名为《 PowerShell Charting功能概述》的文章中我们已经看到了一个名为System.Windows.Forms.DataVisualization程序集的组件的某些组件,该组件是.NET Framework的一部分,以及如何将其加载到PowerShell中。

Now, it’s time to use what we’ve learned so far.

现在,该使用我们到目前为止所学的知识了。

We will first build a general purpose methodology for building charts. In short, we will build a template script that we will then apply to practical examples and check we can build different kinds of charts using this template.

我们将首先构建用于构建图表的通用方法。 简而言之,我们将构建一个模板脚本,然后将其应用于实际示例,并检查我们是否可以使用此模板构建各种图表。

创建图表,一般情况 (Creating a chart, general case)

In next section, we will have a close look at some practical examples with sample data. While each example will show a different kind of chart, the same principle will be used all the time and it’s the subject of this section: presenting a generic script that can be specialized for each kind of chart.

在下一节中,我们将仔细研究一些带有示例数据的实际示例。 尽管每个示例都将显示不同类型的图表,但始终会使用相同的原理,这是本节的主题:提供一个可以针对每种图表专用的通用脚本。

Creating a chart object and setting some properties

创建图表对象并设置一些属性

Here is a generic function that will be used to build an empty chart object. We will limit to set following properties:

这是一个通用函数,将用于构建空图表对象。 我们将限制设置以下属性:

  • Width

    宽度
  • Height

    高度
  • Title

    标题
  • Background color

    背景颜色
  • Title (if any)

    标题(如果有)

It will also optionally create a default chart area.

它还可以选择创建默认图表区域。

Function New-Chart() {
    param (
        [cmdletbinding()]
        [parameter(ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $true, Mandatory = $True)]
        [int]$width,
        [parameter(ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $true, Mandatory = $True)]
        [int]$height,
        [parameter(ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $true, Mandatory = $False)]
        [string]$ChartTitle,
        [parameter(ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $true, Mandatory = $False)]
        [string]$ChartTitleFont = $null,
        [parameter(ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $true, Mandatory = $False)]
        [System.Drawing.ContentAlignment]$ChartTitleAlign = [System.Drawing.ContentAlignment]::TopCenter,
        [parameter(ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $true, Mandatory = $False)]
        [System.Drawing.Color]$ChartColor = [System.Drawing.Color]::White,
        [parameter(ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $true, Mandatory = $False)]
        [boolean]$WithChartArea = $true,
        [parameter(ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $true, Mandatory = $False)]
        [boolean]$WithChartLegend = $true
    )
    # Example:  $Chart = New-Chart -width 1024 -height 800 -ChartTitle "test"
    
    
    $CurrentChart = New-object System.Windows.Forms.DataVisualization.Charting.Chart
    
    if($CurrentChart -eq $null) {
        throw "Unable to create Chart Object"
    }
    
    $CurrentChart.Width         = $width 
    $CurrentChart.Height        = $height
    #TODO:$CurrentChart.Left    = $LeftPadding
    #TODO:$CurrentChart.Top     = $TopPadding
    
    $CurrentChart.BackColor     = $ChartColor
    
    if($WithChartArea) {
        $CurrentChartArea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea
        
        if($CurrentChartArea -eq $null) {
            throw "Unable to create ChartArea object"
        }
        
        $CurrentChart.ChartAreas.Add($CurrentChartArea)
    }
    
    if([String]::isNullOrEmpty($ChartTitleFont)) {
        $ChartTitleFont = "Arial,13pt"
    }
    
    if(-Not [String]::isNullOrEmpty($ChartTitle)) {
        [void]$CurrentChart.Titles.Add($ChartTitle)
        $CurrentChart.Titles[0].Font        = $ChartTitleFont
        $CurrentChart.Titles[0].Alignment   = $ChartTitleAlign
    }
    
    if($WithChartLegend) {
        $ChartLegend = New-Object system.Windows.Forms.DataVisualization.Charting.Legend
        $ChartLegend.name = "Chart Legend"
        $Chart.Legends.Add($ChartLegend)
    }
    
    $CurrentChart
}

Here is a sample call:

这是一个示例调用:

$Chart = New-Chart -width 1024 -height 800 -ChartTitle "test"

Common ChartArea object creation and settings instructions

常见的ChartArea对象创建和设置说明

In this example, a default chart area is created. We will access it using following code:

在此示例中,将创建一个默认图表区域。 我们将使用以下代码访问它:

$Chart.ChartAreas[0]

We can create one using the code from above:

我们可以使用上面的代码创建一个:

$CurrentChartArea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea
 
if($CurrentChartArea -eq $null) {
    throw "Unable to create CharArea object"
}
$CurrentChart.ChartAreas.Add($CurrentChartArea)

In that case, it’s preferable to give a name to the areas:

在这种情况下,最好给这些区域起个名字:

$Chart.ChartAreas[0].Name              = "DefaultArea"

We could by the way set titles for X and Y axes of this area as follows:

顺便说一下,我们可以为该区域的X和Y轴设置标题,如下所示:

$Chart.ChartAreas[0].AxisY.Title       = "Y axis Title"
$Chart.ChartAreas[0].AxisX.Title       = "X axis TItle"

We could also set visual attributes like the interval step to be used for one or both axes:

我们还可以设置视觉属性,例如用于一个或两个轴的间隔步长:

$Chart.ChartAreas[0].AxisX.Interval    = 1

Or also precise the kind of data that is stored for that axis:

或者也可以精确地为该轴存储数据的类型:

$Chart.ChartAreas[0].AxisX.IntervalType  = <A type of interval>

The type of interval is a value of the enumeration called:

时间间隔的类型是枚举的值,称为:

[System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType]

Please, refer to documentation page for further details about axis settings.

请参考文档页面,以获取有关轴设置的更多详细信息。

Defining a legend for our chart

为图表定义图例

We can create a Legend object and add it to the $Chart object we created.

我们可以创建一个Legend对象,并将其添加到我们创建的$ Chart对象中。

$ChartLegend = New-Object system.Windows.Forms.DataVisualization.Charting.Legend
$ChartLegend.name = "Chart Legend"
$Chart.Legends.Add($ChartLegend)

The name of the Legend object instance is pretty important. We will use it when adding data series.

Legend对象实例的名称非常重要。 添加数据系列时,我们将使用它。

Adding data series to an existing chart object

将数据系列添加到现有图表对象

In order to create data it’s pretty simple and straight forwards:

为了创建数据,它非常简单明了:

$Chart.Series.Add("MyData")

As we set a name to the series, here is a mean to get back to the object quite simply:

当我们为系列设置名称时,这是一种非常简单地返回对象的方法:

$Chart.Series["MyData"]

Alternately, you could use following PowerShell function:

或者,您可以使用以下PowerShell功能:

Function New-ChartSeries() {
    param (
        [cmdletbinding()]
        [parameter(ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $true, Mandatory = $True)]
        [String]$SeriesName,
        [parameter(ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $true, Mandatory = $False)]
        [int]$BorderWidth = 3,
        [parameter(ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $true, Mandatory = $False)]
        [boolean]$IsVisibleInLegend = $true,
        [parameter(ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $true, Mandatory = $False)]
        [string]$ChartAreaName = $null,
        [parameter(ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $true, Mandatory = $False)]
        [string]$LegendName    = $null,
        [parameter(ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $true, Mandatory = $False)]
        [string]$HTMLColor     = $null,
        [parameter(ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $true, Mandatory = $False)]
        [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]$ChartType = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::Column
    )
    
    $CurrentChartSeries = New-Object  System.Windows.Forms.DataVisualization.Charting.Series
    
    if($CurrentChartSeries -eq $null) {
        throw "Unable to create Chart Series"
    }
    
    $CurrentChartSeries.Name                = $SeriesName
    $CurrentChartSeries.ChartType           = $ChartType 
    $CurrentChartSeries.BorderWidth         = $BorderWidth 
    $CurrentChartSeries.IsVisibleInLegend   = $IsVisibleInLegend 
    
    if(-Not([string]::isNullOrEmpty($ChartAreaName))) {
        $CurrentChartSeries.ChartArea = $ChartAreaName
    }
    
    if(-Not([string]::isNullOrEmpty($LegendName))) {
        $CurrentChartSeries.Legend = $LegendName
    }
    
    if(-Not([string]::isNullOrEmpty($HTMLColor))) {
        $CurrentChartSeries.Color = $HTMLColor
    }
    
    $CurrentChartSeries
}

Example usage

用法示例

Following code will create a chart series that will be displayed as a histogram (bar columns) and add it to the $Chart object we defined previously:

以下代码将创建一个图表系列,该系列将显示为直方图(条形列)并将其添加到我们先前定义的$ Chart对象中:

$ChartSeries = New-ChartSeries -SeriesName "Series"
$Chart.Series.Add($ChartSeries)

Adding data points to a data series

将数据点添加到数据系列

If we look at the Series object documentation, we will see that it has a collection object called Points that stores all the data points of the chart. This object has multiple methods to add data to it and we will review two of them:

如果查看Series对象文档 ,我们将看到它具有一个称为Points的集合对象,该对象存储图表的所有数据点。 该对象有多种向其添加数据的方法,我们将回顾其中的两个:

  • DataBindXY(<XaxisValues>,<YaxisValues>)

    DataBindXY(<XaxisValues>,<YaxisValues>)
  • AddXY(<XaxisValue>,<YaxisValue>)

    AddXY(<XaxisValue>,<YaxisValue>)

The former will allow you to bind data sets if you got all the values that go to the X axis separately from all the values that go to the Y axis. A good case usage for this is when your data is stored in a HashTable from which it’s easy to get back only keys and only values:

如果您获得了前往X轴的所有值与前往Y轴的所有值分开的所有值,则前者将允许您绑定数据集。 一种很好的用法是将数据存储在HashTable中,从中很容易只取回键和值:

$Chart.Series["Series"].Points.DataBindXY(
    $MyHashMap.Keys(),
    $MyHashMap.Values()
)

The latter will be used to add one point at a time. It’s pretty useful when you run a query that returns the (key,value) pair against SQL Server and you want to create a chart with the results of that query.

后者将用于一次添加一个点。 当您运行针对SQL Server返回(键,值)对的查询,并希望使用该查询的结果创建图表时,此功能非常有用。

In that case, we will use following algorithm:

在这种情况下,我们将使用以下算法:

$SqlQuery = "<MyQuery>" # SELECT XAxisValue, YAxisValue ...
 
$QueryDataSet = Invoke-SqlCmd -ServerInstance "<MyInstance>" `
                              -Database "MyDb"`
                              -Query $SqlQuery
Foreach ($SqlRec in $QueryDataSet) {
    [void]$Chart.Series["Series"].Points.AddXY (
        $SqlRec.XAxisValue,
        $SqlRec.YAxisValue
    )
}

Displaying a chart

显示图表

Now we saw how to build a chart object instance and add data to it. That a good point, but we need to do something with that object and why not displaying it on screen?

现在,我们了解了如何构建图表对象实例并向其中添加数据。 很好,但是我们需要对该对象做些什么,为什么不在屏幕上显示呢?

To do so, we will define a PowerShell function that will create a window form and add the chart to it and display.

为此,我们将定义一个PowerShell函数,该函数将创建一个窗口窗体并将图表添加到其中并显示。

function Display-Chart() {
    param (
        [cmdletbinding()]
        [parameter(ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $true, Mandatory = $True)]
        [System.Windows.Forms.DataVisualization.Charting.Chart]$Chart2Display,
        [parameter(ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $true, Mandatory = $False)]
        [string]$Title = "New Chart",
        [parameter(ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $true, Mandatory = $False)]
        [int]$width,
        [parameter(ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $true, Mandatory = $False)]
        [int]$height
    )
    
    if($Chart2Display -eq $null) {
        throw "Null value provided for Chart2Display parameter"
    }
    
    $Chart2Display.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right -bor
                    [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Left
    
    $WindowsFormObj         = New-Object Windows.Forms.Form
    $WindowsFormObj.Text    = $Title
    
    if($width -eq $null -or $width -lt $Chart2Display.Width) {
        $width = $Chart2Display.Width  * 1.2
    }
    
    if($height -eq $null -or $height -lt $Chart2Display.Height) {
        $height = $Chart2Display.Height * 1.2
    }
    
    $WindowsFormObj.Width   = $width
    $WindowsFormObj.Height  = $height
    
    $WindowsFormObj.Controls.Add($Chart2Display)
    $WindowsFormObj.Add_Shown({$WindowsFormObj.Activate()})
    #$WindowsFormObj.CenterToScreen()
    $WindowsFormObj.ShowDialog() | Out-Null
}

Exporting a chart to an image file

将图表导出到图像文件

Now, let’s say we just want to store the chart on disk. It’s pretty simple to accomplish using the SaveImage method from the Chart object class:

现在,假设我们只想将图表存储在磁盘上。 使用Chart对象类中的SaveImage方法非常简单:

$Chart.SaveImage("<TargetPath>","<FileFormat>")

The available file formats are defined in the System.Drawing.Imaging.ImageFormat class.

可用的文件格式在System.Drawing.Imaging.ImageFormat类中定义。

Amongst the 12 formats defined in this class, we will find:

在此类中定义的12种格式中,我们将发现:

  • BMP

    骨形态发生蛋白
  • GIF

    GIF
  • JPEG

    JPEG格式
  • PNG

    PNG

Putting everything together

放在一起

You will find below as a summary a generic template that can be used to build any kind of chart.

您将在下面找到作为概述的通用模板,该模板可用于构建任何种类的图表。

# ------------------
# Script Settings
$ChartTitle      = "test"
$ChartSeriesType = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::Column
$ChartSeriesHTMLColor = $null
# ------------------
 
#Chart object creation
$Chart = New-Chart -width 1024 -height 800 -ChartTitle $ChartTitle -WithChartArea $true -WithChartLegend $false
# Chart area settings
$Chart.ChartAreas[0].Name              = "DefaultArea"
$Chart.ChartAreas[0].AxisY.Title       = "Y axis Title"
$Chart.ChartAreas[0].AxisX.Title       = "X axis TItle"
    #optional settings for axes
 
# Chart Legend
$ChartLegend = New-Object system.Windows.Forms.DataVisualization.Charting.Legend
$ChartLegend.name = "Chart Legend"
$Chart.Legends.Add($ChartLegend)
 
# Chart Series creation
$ChartSeries = New-ChartSeries -SeriesName "Series" -LegendName "Chart Legend" –ChartAreaName "DefaultArea" -ChartType $ChartSeriesType -HTMLColor $ChartSeriesHTMLColor
$Chart.Series.Add($ChartSeries)
 
# Get back data to set in series
 
<#
    Choice #1: Data Binding commands
#>
 
<#
    Choice #2: Points settings
#>
# Do something with the chart object (display or save to disk)

用法示例 (Example usages)

In following, we will use AdventureWorks database and a different T-SQL query per example.

在下面的示例中,我们将使用AdventureWorks数据库和不同的T-SQL查询。

As we don’t want to give a good reading experience to our readers, we will use different queries and different data sets and generate charts.

由于我们不想给读者带来良好的阅读体验,因此我们将使用不同的查询和不同的数据集并生成图表。

Creating a pie chart

创建饼图

We will take data from the HumanResources.Employee table and create a pie chart that will show the number of employees by gender and marital status. This is translated to following T-SQL query:

我们将从HumanResources.Employee表中获取数据,并创建一个饼图,该饼图将按性别和婚姻状况显示员工人数。 这被转换为以下T-SQL查询:

SELECT 
	CASE Gender 
		WHEN 'M' THEN 'Man' 
		WHEN 'F' THEN 'Woman' 
		ELSE Gender
	END 
	+  ' ' +
	CASE MaritalStatus
		WHEN 'M' THEN 'Married' 
		WHEN 'S' THEN 'Single' 
		ELSE MaritalStatus 
	END as SexAndMaritalStatus, COUNT(*)as OccurCount
FROM HumanResources.Employee
GROUP BY Gender,MaritalStatus

This query gives back following results:

该查询返回以下结果:

Now, let’s build a chart object using following PowerShell script. We will use the general template and add two parameters for target server instance and database. We will choose the option of displaying the chart on screen.

现在,让我们使用以下PowerShell脚本构建一个图表对象。 我们将使用常规模板,并为目标服务器实例和数据库添加两个参数。 我们将选择在屏幕上显示图表的选项。

Here is the PowerShell code:

这是PowerShell代码:

# ------------------
# Script Settings
 
$ChartTitle      = "Pie Chart Example"
$ChartSeriesType = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::Pie
$ChartSeriesHTMLColor = $null
 
$TargetServer   = "MyServer"
$TargetDatabase = "AdventureWorks"
 
# ------------------
 
$PieChart = New-Chart -width 1024 -height 800 -ChartTitle $ChartTitle -WithChartArea $true -WithChartLegend $false
# Chart area settings
$PieChart.ChartAreas[0].Name              = "DefaultArea"
$PieChart.ChartAreas[0].AxisY.Title       = "Gender and marital status"
$PieChart.ChartAreas[0].AxisX.Title       = "Count in enterprise"
    #optional settings for axes
 
# Chart Legend
$ChartLegend = New-Object system.Windows.Forms.DataVisualization.Charting.Legend
$ChartLegend.name = "Chart Legend"
$PieChart.Legends.Add($ChartLegend)
 
# Chart Series creation
$ChartSeries = New-ChartSeries -SeriesName "Series" -LegendName "Chart Legend" –ChartAreaName "DefaultArea" -ChartType $ChartSeriesType -HTMLColor $ChartSeriesHTMLColor
$PieChart.Series.Add($ChartSeries)
 
# Get back data to set in series
$SqlQuery = "SELECT `r`n" +
            "   CASE Gender `r`n" +
            "       WHEN 'M' THEN 'Man' `r`n" +
            "       WHEN 'F' THEN 'Woman' `r`n" +
            "       ELSE Gender`r`n" +
            "   END `r`n" +
            "   +  ' ' +`r`n" +
            "   CASE MaritalStatus`r`n" +
            "       WHEN 'M' THEN 'Married' `r`n" +
            "       WHEN 'S' THEN 'Single' `r`n" +
            "       ELSE MaritalStatus `r`n" +
            "   END as SexAndMaritalStatus, COUNT(*) as OccurCount`r`n" +
            "FROM HumanResources.Employee`r`n" +
            "GROUP BY Gender,MaritalStatus`r`n" +
            ";"
            
$QueryDataSet = Invoke-SqlCmd -ServerInstance $TargetServer -Database $TargetDatabase -Query $SqlQuery -ErrorAction Stop 
<#
    Choice #2: Points settings
#>
 
Foreach ($SqlRec in $QueryDataSet) {
    [void]$PieChart.Series["Series"].Points.AddXY(
        $SqlRec.SexAndMaritalStatus,
        $SqlRec.OccurCount
    )
}
 
 
# Do something with the chart object (display or save to disk)
Display-Chart -Chart2Display $PieChart -Title "Pie Chart Example"

We can see that most employees are single men (what we could have noticed based on the results set).

我们可以看到大多数员工都是单身男人(根据结果集我们可能会注意到)。

Creating a bar chart

创建条形图

We will use following query and same algorithm except that we will define a Bar chart and not a Pie chart.

我们将使用以下查询和相同的算法,只是我们将定义条形图而不是饼图。

SELECT 
	CASE PersonType
		WHEN 'SC' THEN 'Store Contact'
		WHEN 'IN' THEN 'Individual (retail) customer'
		WHEN 'SP' THEN 'Sales person'
		WHEN 'EM' THEN 'Employee (non-sales)'
		WHEN 'VC' THEN 'Vendor Contact'
		ELSE 'General Contact'
	END as PersonType, 
	COUNT(*) PersonCount 
FROM Person.Person
GROUP BY PersonType
;

This query gives back following results set:

该查询返回以下结果集:

Here is the corresponding PowerShell Code:

这是相应的PowerShell代码:

# ------------------
# Script Settings
 
$ChartTitle      = "Bar Chart Example"
$ChartSeriesType = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::Bar
$ChartSeriesHTMLColor = $null
 
$TargetServer   = "MyServer"
$TargetDatabase = "AdventureWorks"
 
# ------------------
 
$BarChart = New-Chart -width 1024 -height 800 -ChartTitle $ChartTitle -WithChartArea $true -WithChartLegend $false
# Chart area settings
$BarChart.ChartAreas[0].Name              = "DefaultArea"
$BarChart.ChartAreas[0].AxisY.Title       = "Count in enterprise"
$BarChart.ChartAreas[0].AxisX.Title       = "Kind of person"
    #optional settings for axes
 
# Chart Legend
$ChartLegend = New-Object system.Windows.Forms.DataVisualization.Charting.Legend
$ChartLegend.name = "Chart Legend"
$BarChart.Legends.Add($ChartLegend)
 
# Chart Series creation
$ChartSeries = New-ChartSeries -SeriesName "Series" -LegendName "Chart Legend" –ChartAreaName "DefaultArea" -ChartType $ChartSeriesType -HTMLColor $ChartSeriesHTMLColor
$BarChart.Series.Add($ChartSeries)
 
# Get back data to set in series
$SqlQuery = "SELECT `r`n" +
            "    CASE PersonType`r`n" +
            "        WHEN 'SC' THEN 'Store Contact'`r`n" +
            "        WHEN 'IN' THEN 'Individual (retail) customer'`r`n" +
            "        WHEN 'SP' THEN 'Sales person'`r`n" +
            "        WHEN 'EM' THEN 'Employee (non-sales)'`r`n" +
            "        WHEN 'VC' THEN 'Vendor Contact'`r`n" +
            "        ELSE 'General Contact'`r`n" +
            "    END as PersonType, `r`n" +
            "    COUNT(*) PersonCount `r`n" +
            "FROM Person.Person`r`n" +
            "GROUP BY PersonType`r`n" +
            ";"
            
$QueryDataSet = Invoke-SqlCmd -ServerInstance $TargetServer -Database $TargetDatabase -Query $SqlQuery -ErrorAction Stop 
<#
    Choice #2: Points settings
#>
 
Foreach ($SqlRec in $QueryDataSet) {
    [void]$BarChart.Series["Series"].Points.AddXY(
        $SqlRec.PersonType,
        $SqlRec.PersonCount
    )
}
 
 
# Do something with the chart object (display or save to disk)
Display-Chart -Chart2Display $BarChart -Title "Bar Chart Example"

And here is the corresponding chart:

这是对应的图表:

If you take a closer look to the code, you see that, except the query that is different, the only line that has changed is the assignment to the $ChartSeriesType variable.

如果仔细看一下代码,您会发现,除了查询不同以外,唯一更改的行是对$ ChartSeriesType变量的赋值。

Going further

更进一步

These two examples will give you, I hope so, the knowledge necessary to be able to create advanced charts with multiple chart areas and/or multiple series into the same chart.

希望如此,这两个示例将为您提供必要的知识,以便能够在同一图表中创建具有多个图表区域和/或多个序列的高级图表。

As a last advice, don’t forget that a chart area will need you to define the same values for X axis along all the series when you want to plot something like this:

作为最后的建议,当您要绘制类似以下内容时,不要忘记图表区域将需要您沿所有系列为X轴定义相同的值:

In this example, I set values of 0 for occurrences of LOG backups in the “FULL-WITH-LOG backups occurrences” series and 0 for occurrences of FULL-WITH-LOG backups in the “LOG backups occurrences” series.

在此示例中,我将“ FULL-WITH-LOG备份事件”系列中的LOG备份的发生值设置为0,将“ LOG备份事件”系列中的FULL-WITH-LOG备份的值设置为0。

By the way, if you are willing to get a more in-depth knowledge on the DataVisualization namespace, there are two ways to do so. Firstly, you can go through the documentation and test yourself on concrete examples (that would add some value to your job, it’s always a better way to learn). Secondly, you could also get into Visual Studio, try to build charts using WYSIWYG tools and look at the code used behind.

顺便说一句,如果您愿意对DataVisualization名称空间有更深入的了解,可以通过两种方法。 首先,您可以浏览文档并在具体示例上进行测试(这将为您的工作增加一些价值,它始终是学习的一种更好的方法)。 其次,您也可以进入Visual Studio,尝试使用所见即所得工具构建图表,并查看背后使用的代码。

Previous article in this series:

本系列的上一篇文章:

翻译自: https://www.sqlshack.com/create-charts-from-sql-server-data-using-powershell/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值