2024年终活动

DevExpress控件使用交流,DevExpress中国社区Dev联系电话 联系电话:023-68661681

DevExpress XtraCharts示例三:运行时绑定单个图表系列到外部数据源

来源:本站原创   发布时间:2012-09-18   浏览:4030次

本示例主要演示了如何在运行时通过绑定单个系列到特定的数据源,从而实现将图表绑定到数据中。

C#:Form1.cs
using System;
using System.Data;
using System.Windows.Forms;
using DevExpress.XtraCharts;
// ...
namespace BindIndividualSeriesRuntimeCS {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
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);
}
}
}


VB:Form1.vb
Imports System
Imports System.Data
Imports System.Windows.Forms
Imports DevExpress.XtraCharts
' ...

Public Class Form1
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
End Class

本站文章除注明转载外,均为本站原创或翻译
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明:文章转载自:DevExpress控件中文网 [https://www.devexpresscn.com/]
本文地址:https://www.devexpresscn.com/post/347.html
扫码咨询
电话咨询
023-68661681
返回
顶部