下面的示例演示了如何在运行时刻通过把图表的各个系列绑定到特定数据源,从而把图表绑定到数据。 它使用与 设计时刻示例 相同的方法,但是在代码中生成了另一个数据表来简化示例。
注意 |
---|
别忘了把所有 必需的程序集 包含到项目的“引用”列表中。 |
C# | 复制代码 |
---|
using System;
using System.Data;
using System.Windows.Forms;
using DevExpress.XtraCharts;
private DataTable CreateChartData(int rowCount) {
DataTable table = new DataTable("Table1");
table.Columns.Add("Argument", typeof(Int32));
table.Columns.Add("Value", typeof(Int32));
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) {
ChartControl chart = new ChartControl();
Series series = new Series("Series1", ViewType.Bar);
chart.Series.Add(series);
series.DataSource = CreateChartData(50);
series.ArgumentScaleType = ScaleType.Numerical;
series.ArgumentDataMember = "Argument";
series.ValueScaleType = ScaleType.Numerical;
series.ValueDataMembers.AddRange(new string[] { "Value" });
((SideBySideBarSeriesView)series.View).ColorEach = true;
((XYDiagram)chart.Diagram).AxisY.Visible = false;
chart.Legend.Visible = false;
chart.Dock = DockStyle.Fill;
this.Controls.Add(chart);
}
|
Visual Basic | 复制代码 |
---|
Imports System
Imports System.Data
Imports System.Windows.Forms
Imports DevExpress.XtraCharts
Private Function CreateChartData(ByVal rowCount As Integer) As DataTable
Dim Table As New DataTable("Table1")
Table.Columns.Add("Argument", GetType(Int32))
Table.Columns.Add("Value", GetType(Int32))
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
Dim Chart As New ChartControl()
Dim Series As New Series("Series1", ViewType.Bar)
Chart.Series.Add(Series)
Series.DataSource = CreateChartData(50)
Series.ArgumentScaleType = ScaleType.Numerical
Series.ArgumentDataMember = "Argument"
Series.ValueScaleType = ScaleType.Numerical
Series.ValueDataMembers.AddRange(New String() {"Value"})
CType(Series.View, SideBySideBarSeriesView).ColorEach = True
CType(Chart.Diagram, XYDiagram).AxisY.Visible = False
Chart.Legend.Visible = False
Chart.Dock = DockStyle.Fill
Me.Controls.Add(Chart)
End Sub
|
参阅