这个示例展示了如何在各自的 窗格 中显示自动创建的系列。

为了使此示例能正确工作,需执行下列操作。

  1. 启动 MS Visual Studio (2005、2008 或 2010),并且新建一个或者打开一个现有的 Windows 窗体应用程序

  2. 把所有 必需的程序集 包含到项目的 引用 列表中。
  3. 打开 解决方案资源管理器 窗口 (例如敲击 CTRL+ALT+L 组合键),使用鼠标右键单击 WindowsApplication1 项,然后在被调用的上下文菜单中,指向 Add(添加) 菜单项并单击 Existing Item(现有项)...。 然后在弹出的“添加现有项”对话框中,找到 gsp.mdb 文件 (与 XtraCharts 套件一起提供,位于 DevExpress 演示 的安装目录中) 并单击 Add(添加) 按钮。 在弹出的“数据源配置向导”对话框中,选中数据库中的 GSP 表。

    在执行完上述步骤之后,会自动创建一个数据集 (命名为 gspDataSet)。

  4. 把一个按钮拖放到窗体上,并把下列代码添加到 Button1.Click 事件处理程序。
    (译者注: 以 C# 为例,在下面的示例代码中,“using gspDataSetTableAdapters;”语句应该放置在 Form1.cs 代码文件中的“namespace WindowsFormsApplication1”内部,或者使用“using WindowsFormsApplication1.gspDataSetTableAdapters;”语句来替代。 否则会显示编译错误“找不到类型或命名空间名称 gspDataSetTableAdapters (是否缺少 using 指令或程序集引用?)”。)

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

private void button1_Click(object sender, EventArgs e) {
    // Create and customize a form.
    XtraForm form = new XtraForm();
    form.Text = "Multiple Panes and Chart Binding";
    form.Size = new Size(800, 600);

    // Create a chart.
    ChartControl chart = new ChartControl();

    // Handle the chart's BoundDataChanged event.
    chart.BoundDataChanged += new BoundDataChangedEventHandler(chart_BoundDataChanged);

    // Initialize a dataset. Initialize and fill data adapter.
    gspDataSet dataSet = new gspDataSet();
    GSPTableAdapter dataAdapter = new GSPTableAdapter();
    dataAdapter.Fill(dataSet.GSP);

    // Assign the chart's datasource to the created dataset.
    chart.DataSource = dataSet.GSP;
    // Define a data member for the chart's series.
    chart.SeriesDataMember = "Year";
    // Define an argument and value data members for the chart's series template,
    // so that the created series inherit these properties.
    chart.SeriesTemplate.ArgumentDataMember = "Region";
    chart.SeriesTemplate.ValueDataMembers[0] = "GSP";

    // Add the chart to the form's controls collection and fit the chart to the form's dimensions.
    chart.Dock = DockStyle.Fill;
    form.Controls.Add(chart);

    // Show the result.
    form.Show();
}

private void chart_BoundDataChanged(object sender, EventArgs e) {
    ChartControl chart = (ChartControl)sender;

    // Check whether the chart contains series.
    if(chart.Series.Count > 0) {
        // Obtain a diagram and clear its collection of panes.
        XYDiagram diagram = (XYDiagram)chart.Diagram;
        diagram.Panes.Clear();
        // Create a pane for each series.
        for(int i = 1; i < chart.Series.Count; i++) {
            XYDiagramPane pane = new XYDiagramPane("The Pane's Name");
            diagram.Panes.Add(pane);
            XYDiagramSeriesViewBase view = (XYDiagramSeriesViewBase)chart.Series[i].View;
            view.Pane = pane;
        }
    }
}
Visual BasicCopyCode image复制代码
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports DevExpress.XtraEditors
Imports DevExpress.XtraCharts
Imports gspDataSetTableAdapters
' ...

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles button1.Click
    ' Create and customize a form.
    Dim form As XtraForm = New XtraForm()
    form.Text = "Multiple Panes and Chart Binding"
    form.Size = New Size(800, 600)

    ' Create a chart.
    Dim chart As ChartControl = New ChartControl()

    ' Handle the chart's BoundDataChanged event.
    AddHandler chart.BoundDataChanged, AddressOf chart_BoundDataChanged

    ' Initialize a dataset. Initialize and fill data adapter.
    Dim dataSet As gspDataSet = New gspDataSet()
    Dim dataAdapter As GSPTableAdapter = New GSPTableAdapter()
    dataAdapter.Fill(dataSet.GSP)

    ' Assign the chart's datasource to the created dataset.
    chart.DataSource = dataSet.GSP

    ' Define a data member for the chart's series.
    chart.SeriesDataMember = "Year"

    ' Define an argument and value data members for the chart's series template,
    ' so that the created series inherit these properties.
    chart.SeriesTemplate.ArgumentDataMember = "Region"
    chart.SeriesTemplate.ValueDataMembers(0) = "GSP"

    ' Add the chart to the form's controls collection and fit the chart to the form's dimensions.
    chart.Dock = DockStyle.Fill
    form.Controls.Add(chart)

    ' Show the result.
    form.Show()
End Sub
Private Sub chart_BoundDataChanged(ByVal sender As Object, ByVal e As EventArgs)
    Dim chart As ChartControl = CType(sender, ChartControl)

    ' Check whether the chart contains series.
    If chart.Series.Count > 0 Then

        ' Obtain a diagram and clear its collection of panes.
        Dim diagram As XYDiagram = CType(chart.Diagram, XYDiagram)
        diagram.Panes.Clear()

        ' Create a pane for each series.
        Dim i As Integer = 1
        Do While i < chart.Series.Count
            Dim pane As XYDiagramPane = New XYDiagramPane("The Pane's Name")
            diagram.Panes.Add(pane)
            Dim view As XYDiagramSeriesViewBase = CType _
                (chart.Series(i).View, XYDiagramSeriesViewBase)
            view.Pane = pane
            i += 1
        Loop
    End If
End Sub

CodeCentralShow Me

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

Expand image参阅