如何:为 PerformancePoint Services 创建记分卡转换

上次修改时间: 2011年8月30日

在 Microsoft SharePoint Server 2010 中的 PerformancePoint Services 中,记分卡转换会在记分卡视图呈现在仪表板中之前,更改这些视图的外观、内容或功能。

适用范围: SharePoint Server 2010

备注

建议您将记分卡转换示例用作自定义转换的模板。该示例演示如何调用 PerformancePoint Services API 中的对象,并阐释有关 PerformancePoint Services 开发的最佳实践。

创建记分卡转换

以下过程基于 AddColumnTransform 示例,该示例将列添加到记分卡视图。本主题的第一个示例中提供了该类的完整代码。

创建记分卡转换

  1. 安装 PerformancePoint Services,或者将随 PerformancePoint Services一起安装的 DLL 复制到您的计算机。有关详细信息,请参阅 开发方案中使用的 PerformancePoint Services DLL

  2. 在 Visual Studio 中创建 C# 类库。如果已经为您的扩展创建了类库,则添加一个新的 C# 类。

  3. 将 Microsoft.PerformancePoint.Scorecards.Client.dll 作为程序集引用添加到项目中。

  4. 为以下命名空间添加 using 指令:

    • Microsoft.PerformancePoint.Scorecards

    • Microsoft.PerformancePoint.Scorecards.Extensions

    • System.Collections.Generic

  5. 实现 IGridViewTransform 接口。

  6. 重写 GetId 方法以返回您的转换的字符串标识符。GetId 必须返回与在 PerformancePoint Services web.config 文件中为转换注册的 key 属性相同的字符串。有关注册记分卡转换的详细信息,请参阅如何:手动注册 PerformancePoint Services 扩展

  7. 重写 GetTransformType 方法以指定何时运行转换。转换的运行时间点取决于由 GridViewTransformType 枚举定义的类型:PreQuery、PostQuery 或 PreRender。有关详细信息,请参阅PerformancePoint Services 记分卡转换概述

  8. 重写 Execute 方法以定义如何转换记分卡。下面的代码示例演示如何向记分卡视图添加列以及如何更改空白记分卡单元格的格式。

对 DLL 进行签名和生成之后,按如何:手动注册 PerformancePoint Services 扩展中所述安装扩展。

示例

示例 1:向记分卡视图添加列

下面的代码示例来自记分卡转换示例。它创建一个 PreQuery 转换,该转换向在列的叶级别处包含 KPI 的已呈现记分卡视图中添加一列。(如果记分卡视图在 KPI 下包含成员,则不会添加该列。)

备注

必须先按"创建记分卡转换"过程中所述配置开发环境,然后才能编译此代码示例。

using System;
using System.Collections.Generic;
using Microsoft.PerformancePoint.Scorecards;
using Microsoft.PerformancePoint.Scorecards.Extensions;

namespace Microsoft.PerformancePoint.SDK.Samples.ScorecardTransforms.PreQuery
{

    // Represents the class that adds columns of data to a scorecard view. 
    public class AddColumnTransform : IGridViewTransform
    {

        // Set transform type to PreQuery.
        public GridViewTransformType GetTransformType()
        {
            return GridViewTransformType.PreQuery;
        }

        // Return the string identifier of your transform. This value must
        // match the key attribute registered in the web.config file.
        public string GetId()
        {
            return "AddColumn";
        }

        // Run the transform to add a column. 
        public void Execute(GridViewData viewData, PropertyBag parameters, IGlobalCache cache)
        {
            // Verify the scorecard definition.
            if (viewData == null)
            {
                throw new ArgumentException("Parameter cannot be null", "viewData");
            }
                     
            List<GridHeaderItem> leafRowHeaders = viewData.RootRowHeader.GetAllLeafHeadersInTree();

            foreach (GridHeaderItem rowHeader in leafRowHeaders)
            {
                if (rowHeader.HeaderType == ScorecardNodeTypes.Kpi)
                {
                    Kpi kpi = cache.GetKpi(rowHeader.LinkedKpiLocation);
                    if (kpi != null && viewData.RootColumnHeader != null)
                    {

                        // Create the column header and add it to the root.
                        GridHeaderItem theNewColumn = GridHeaderItem.CreateDetailsHeader(kpi.Owner.DisplayName);

                        // Set the GUIDs for the data headers.
                        // Setting the DefinitionGuid property to the
                        // same value as the root column header enables
                        // Dashboard Designer to display the scorecard. 
                        // Note: Do not try to modify or delete the new
                        // column from within Dashboard Designer.
                        theNewColumn.DefinitionGuid = viewData.RootColumnHeader.DefinitionGuid;
                        theNewColumn.Parent = viewData.RootColumnHeader;
                        theNewColumn.Guid = new Guid();

                        // Insert the column at the end of the collection
                        // of child elements.
                        if (viewData.RootColumnHeader.Children.Count != 0)
                        {
                            viewData.RootColumnHeader.Children.Insert(viewData.RootColumnHeader.Children.Count, theNewColumn);
                        }
                                                                         
                        break;
                    }
                }
            }
            viewData.RootColumnHeader.LinkAndIndexTreeFromRoot();
        }
    }
}

示例 2:更改空白记分卡单元格的格式

下面的代码示例创建一个 PreQuery 转换,该转换向空白记分卡单元格应用灰色背景色。

备注

您必须先按"创建记分卡转换"过程所述配置开发环境,然后才能编译此代码示例。此外,还必须向项目中添加对 System.Drawing 程序集的引用。

using System;
using System.Collections.Generic;
using Microsoft.PerformancePoint.Scorecards;
using Microsoft.PerformancePoint.Scorecards.Extensions;

namespace Microsoft.PerformancePoint.SDK.Samples
{

    // Represents a transform that applies a grey background color to empty scorecard cells.
    public class GreyEmptiesTransform : IGridViewTransform
    {

        // Set the transform type to "PreRender".
        public GridViewTransformType GetTransformType()
        {
            return GridViewTransformType.PreRender;
        }

        // Return the string identifier of the transform.
        public string GetId()
        {
            return "GreyEmptyCells";
        }

        // Run the transform.
        public void Execute(GridViewData viewData, PropertyBag parameters, IGlobalCache cache)
        {
            // Validate parameters. 
            if (viewData == null)
            {
                throw new ArgumentException("Parameter cannot be null", "viewData");
            }

            // Get the headers under the root row header.
            List<GridHeaderItem> nonLeafRowHeaders = viewData.RootRowHeader.GetAllHeadersInTree();

            // Get the leaf headers under the root column header.
            List<GridHeaderItem> leafColumnHeaders = viewData.RootColumnHeader.GetAllLeafHeadersInTree();

            foreach (GridHeaderItem rowHeader in nonLeafRowHeaders)
            {
                foreach (GridHeaderItem columnHeader in leafColumnHeaders)
                {
                    // Get scorecard cells.
                    GridCell cell = viewData.Cells[rowHeader, columnHeader];

                    if (cell.IsCellEmpty || string.IsNullOrEmpty(cell.ActualValue.ToString()))
                    {
                        GridFormatInfo emptyFormat = new GridFormatInfo(viewData.DefaultCellFormatInfo);
                        emptyFormat.BackColor = new GridColor(System.Drawing.Color.Gray);
                        cell.FormatInfo = emptyFormat;
                    }
                    viewData.Cells[rowHeader, columnHeader] = cell;
                }
            }
        }
    }
}

编译代码

必须先按"创建记分卡转换"过程中所述配置开发环境,然后才能编译这些代码示例。

安全性

您必须使用强名称对 DLL 进行签名。此外,确保 DLL 引用的所有程序集都具有强名称。有关如何使用强名称对程序集进行签名以及如何创建公钥/私钥对的信息,请参阅How to: Create a Public/Private Key Pair

请参阅

概念

PerformancePoint Services 记分卡转换概述

其他资源

PerformancePoint Services 记分卡

如何实现 - PerformancePoint Services

SharePoint Server 2010 中的 PerformancePoint Services 的代码示例