下面的示例演示了如何在运行时刻使用系列模板把图表绑定到数据。 它使用与 设计时刻示例 相同的方法,但是在代码中生成了另一个数据表来简化示例。 要使本示例正确工作,则不要忘了把所有 必需的程序集 包含到项目的“引用”列表中。

C#CopyCode image复制代码
using System;
using System.Data;
using System.Windows.Forms;
using DevExpress.XtraCharts;
// ...

private DataTable CreateChartData() {
    // Create an empty table.
    DataTable table = new DataTable("Table1");

    // Add three columns to the table.
    table.Columns.Add("Month", typeof(String));
    table.Columns.Add("Section", typeof(String));
    table.Columns.Add("Value", typeof(Int32));

    // Add data rows to the table.
    table.Rows.Add(new object[] { "Jan", "Section1", 10 });
    table.Rows.Add(new object[] { "Jan", "Section2", 20 });
    table.Rows.Add(new object[] { "Feb", "Section1", 20 });
    table.Rows.Add(new object[] { "Feb", "Section2", 30 });
    table.Rows.Add(new object[] { "March", "Section1", 15 });
    table.Rows.Add(new object[] { "March", "Section2", 25 });

    return table;
}

private void Form1_Load(object sender, EventArgs e) {
    // Create a chart.
    ChartControl chart = new ChartControl();

    // Generate a data table and bind the chart to it.
    chart.DataSource = CreateChartData();

    // Specify data members to bind the chart's series template.
    chart.SeriesDataMember = "Month";
    chart.SeriesTemplate.ArgumentDataMember = "Section";
    chart.SeriesTemplate.ValueDataMembers.AddRange(new string[] {"Value"});

    // Specify the template's series view.
    chart.SeriesTemplate.View = new StackedBarSeriesView();

    // Specify the template's name prefix.
    chart.SeriesNameTemplate.BeginText = "Month: ";

    // Dock the chart into its parent, and add it to the current form.
    chart.Dock = DockStyle.Fill;
    this.Controls.Add(chart);
}
Visual BasicCopyCode image复制代码
Imports System
Imports System.Data
Imports System.Windows.Forms
Imports DevExpress.XtraCharts
' ...

Private Function CreateChartData() As DataTable
    ' Create an empty table.
    Dim Table As New DataTable("Table1")

    ' Add three columns to the table.
    Table.Columns.Add("Month", GetType([String]))
    Table.Columns.Add("Section", GetType([String]))
    Table.Columns.Add("Value", GetType(Int32))

    ' Add data rows to the table.
    Table.Rows.Add(New Object() {"Jan", "Section1", 10})
    Table.Rows.Add(New Object() {"Jan", "Section2", 20})
    Table.Rows.Add(New Object() {"Feb", "Section1", 20})
    Table.Rows.Add(New Object() {"Feb", "Section2", 30})
    Table.Rows.Add(New Object() {"March", "Section1", 15})
    Table.Rows.Add(New Object() {"March", "Section2", 25})

    Return Table
End Function

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) _
Handles MyBase.Load
    ' Create a chart.
    Dim Chart As New ChartControl()

    ' Generate a data table and bind the chart to it.
    Chart.DataSource = CreateChartData()

    ' Specify data members to bind the chart's series template.
    Chart.SeriesDataMember = "Month"
    Chart.SeriesTemplate.ArgumentDataMember = "Section"
    Chart.SeriesTemplate.ValueDataMembers.AddRange(New String() {"Value"})

    ' Specify the template's series view.
    Chart.SeriesTemplate.View = New StackedBarSeriesView()

    ' Specify the template's name prefix.
    Chart.SeriesNameTemplate.BeginText = "Month: "

    ' Dock the chart into its parent, and add it to the current form.
    Chart.Dock = DockStyle.Fill
    Me.Controls.Add(Chart)
End Sub

CodeCentralShow Me

在 DevExpress Code Central 数据库中可以找到完整的示例项目,网址是 http://www.devexpress.com/example=E3。 取决于目标平台类型 (ASP.NET、WinForms 等),可以在线运行本示例,或者下载自动可执行的示例。

Expand image参阅