XtraCharts 提供了对系列值计算多个内置汇总函数的功能 (MIN、MAX、SUM、AVERAGE、COUNT)。 除了所提供的汇总函数之外,还可以创建自定义汇总,用于在应用程序中以任何所需的方式来计算汇总值。 此外,使用把自定义汇总函数注册到特定图表实例的实用方法,可以让最终用户在 图表向导 中使用自定义汇总函数。
下面的示例演示了如何创建一个自定义汇总函数,它返回两个值的乘积 (Price * Count)。 要完成此任务,需要创建一个汇总函数委托,并通过 ChartControl.RegisterSummaryFunction (或 WebChartControl.RegisterSummaryFunction) 方法注册它。
下面的代码说明了如何完成此任务。
C# | 复制代码 |
---|---|
using System; using System.Windows.Forms; using DevExpress.XtraCharts; // ... namespace WindowsApplication1 { public partial class Form1 : Form { // Declare the Product summary function. private static SeriesPoint[] CalcProductValue(Series series, object argument, string[] functionArguments, DataSourceValues[] values) { // Create an array of the resulting series points. SeriesPoint[] points = new SeriesPoint[values.Length]; // Calculate the resulting series points as Price * Count. for (int i = 0; i < values.Length; i++) points[i] = new SeriesPoint(argument, Convert.ToDouble(values[i][functionArguments[0]]) * Convert.ToDouble(values[i][functionArguments[1]])); // Return the result. return points; } public Form1() { InitializeComponent(); // Create argument descriptions for the summary function. SummaryFunctionArgumentDescription argument1Description = new SummaryFunctionArgumentDescription("Price", ScaleType.Numerical); SummaryFunctionArgumentDescription argument2Description = new SummaryFunctionArgumentDescription("Count", ScaleType.Numerical); // Register the summary function in a chart. chartControl1.RegisterSummaryFunction("PRODUCT", "PRODUCT", 1, new SummaryFunctionArgumentDescription[] { argument1Description, argument2Description }, CalcProductValue); // Provide a datasource for the chart. chartControl1.DataSource = nwindDataSet.Products; } private void Form1_Load(object sender, EventArgs e) { // This line of code loads data into the 'nwindDataSet.Products' table. this.productsTableAdapter.Fill(this.nwindDataSet.Products); } } } |
Visual Basic | 复制代码 |
---|---|
Imports System Imports System.Windows.Forms Imports DevExpress.XtraCharts ' ... Public Class Form1 ' Declare the Product summary function. Private Shared Function CalcProductValue(ByVal series As Series, _ ByVal argument As Object, ByVal functionArguments() As String, _ ByVal values() As DataSourceValues) As SeriesPoint() ' Create an array of the resulting series points. Dim points(values.Length - 1) As SeriesPoint ' Calculate the resulting series points as Price * Count. For i As Integer = 0 To values.Length - 1 points(i) = New SeriesPoint(argument, _ Convert.ToDouble(values(i)(functionArguments(0))) * _ Convert.ToDouble(values(i)(functionArguments(1)))) Next i ' Return the result. Return points End Function Public Sub New() InitializeComponent() ' Create argument descriptions for the summary function. Dim argument1Description As New SummaryFunctionArgumentDescription("Price", _ ScaleType.Numerical) Dim argument2Description As New SummaryFunctionArgumentDescription("Count", _ ScaleType.Numerical) ' Register the summary function in a chart. ChartControl1.RegisterSummaryFunction("PRODUCT", "PRODUCT", 1, _ New SummaryFunctionArgumentDescription() {argument1Description, _ argument2Description}, AddressOf CalcProductValue) ' Provide a datasource for the chart. ChartControl1.DataSource = ProductsBindingSource End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load ' This line of code loads data into the 'NwindDataSet.Products' table. Me.ProductsTableAdapter.Fill(Me.NwindDataSet.Products) End Sub End Class |
Show Me |
---|
在 DevExpress Code Central 数据库中可以找到完整的示例项目,网址是 http://www.devexpress.com/example=E368。 取决于目标平台类型 (ASP.NET、WinForms 等),可以在线运行本示例,或者下载自动可执行的示例。 |