这个示例展示了如何在运行时刻把图表绑定到 MDB 数据库。

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

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

    // Create data objects.
    OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM GSP",
        @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\gsp.mdb");
    DataSet ds = new DataSet();
    //adapter.FillSchema(ds, SchemaType.Source);
    adapter.Fill(ds);

    // Bind the chart to data.
    chart.DataSource = ds;
    chart.DataAdapter = adapter;

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

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

    // Adjust the X-axis and series labels' appearance.
    chart.SeriesTemplate.Label.Visible = false;
    ((StackedBarSeriesView)chart.SeriesTemplate.View).AxisX.Label.Angle = 25;
    ((StackedBarSeriesView)chart.SeriesTemplate.View).AxisX.Label.Antialiasing = true;

    // 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.Data.OleDb
Imports System.Windows.Forms
Imports DevExpress.XtraCharts
' ...

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

    ' Create data objects.
    Dim adapter As New OleDbDataAdapter("SELECT * FROM GSP", _ 
        "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\gsp.mdb")
    Dim ds As New DataSet()
    'adapter.FillSchema(ds, SchemaType.Source);
    adapter.Fill(ds)

    ' Bind the chart to data.
    chart.DataSource = ds
    chart.DataAdapter = adapter

    ' Specify data members to bind the chart's series template.
    chart.SeriesDataMember = "Table.Year"
    chart.SeriesTemplate.ArgumentDataMember = "Table.Region"
    chart.SeriesTemplate.ValueDataMembers.AddRange(New String() { "Table.GSP" })

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

    ' Adjust the X-axis and series labels' appearance.
    chart.SeriesTemplate.Label.Visible = False
    CType(chart.SeriesTemplate.View, StackedBarSeriesView).AxisX.Label.Angle = 25
    CType(chart.SeriesTemplate.View, StackedBarSeriesView).AxisX.Label.Antialiasing = True

    ' 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=E1617。 取决于目标平台类型 (ASP.NET、WinForms 等),可以在线运行本示例,或者下载自动可执行的示例。