下面的示例演示了如何在运行时刻通过把图表的各个系列绑定到特定数据源,从而把图表绑定到数据。 它使用与 设计时刻示例 相同的方法,但是在代码中生成了另一个数据表来简化示例。

Note注意

别忘了把所有 必需的程序集 包含到项目的“引用”列表中。

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

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

    // Add two columns to the table.
    table.Columns.Add("Argument", typeof(Int32));
    table.Columns.Add("Value", typeof(Int32));

    // Add data rows to the table.
    Random rnd = new Random();
    DataRow row = null;
    for (int i = 0; i < rowCount; i++) {
        row = table.NewRow();
        row["Argument"] = i;
        row["Value"] = rnd.Next(100);
        table.Rows.Add(row);
    }

    return table;
}

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

    // Create an empty Bar series and add it to the chart.
    Series series = new Series("Series1", ViewType.Bar);
    chart.Series.Add(series);

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

    // Specify data members to bind the series.
    series.ArgumentScaleType = ScaleType.Numerical;
    series.ArgumentDataMember = "Argument";
    series.ValueScaleType = ScaleType.Numerical;
    series.ValueDataMembers.AddRange(new string[] { "Value" });

    // Set some properties to get a nice-looking chart.
    ((SideBySideBarSeriesView)series.View).ColorEach = true;
    ((XYDiagram)chart.Diagram).AxisY.Visible = false;
    chart.Legend.Visible = false;

    // 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(ByVal rowCount As Integer) As DataTable
    ' Create an empty table.
    Dim Table As New DataTable("Table1")

    ' Add two columns to the table.
    Table.Columns.Add("Argument", GetType(Int32))
    Table.Columns.Add("Value", GetType(Int32))

    ' Add data rows to the table.
    Dim Rnd As New Random()
    Dim Row As DataRow = Nothing
    Dim i As Integer
    For i = 0 To rowCount - 1
        Row = Table.NewRow()
        Row("Argument") = i
    Row("Value") = Rnd.Next(100)
    Table.Rows.Add(Row)
    Next i

    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()

    ' Create an empty Bar series and add it to the chart.
    Dim Series As New Series("Series1", ViewType.Bar)
    Chart.Series.Add(Series)

    ' Generate a data table and bind the series to it.
    Series.DataSource = CreateChartData(50)

    ' Specify data members to bind the series.
    Series.ArgumentScaleType = ScaleType.Numerical
    Series.ArgumentDataMember = "Argument"
    Series.ValueScaleType = ScaleType.Numerical
    Series.ValueDataMembers.AddRange(New String() {"Value"})

    ' Set some properties to get a nice-looking chart.
    CType(Series.View, SideBySideBarSeriesView).ColorEach = True
    CType(Chart.Diagram, XYDiagram).AxisY.Visible = False
    Chart.Legend.Visible = False

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

Expand image参阅