这个示例展示了如何在单独的 窗格 中显示图表的系列。

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

  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";
    form.Size = new Size(800, 600);
    
    // Create a chart.
    ChartControl chart = new ChartControl();

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

    // Define the chart's datasource.
    chart.DataSource = dataSet.GSP;

    // Create a series. Define its data member and create a data filter, to display a required year.
    Series series1 = new Series();
    series1.Name = "2003";
    series1.ArgumentDataMember = "Region";
    series1.ValueDataMembers[0] = "GSP";
    series1.DataFilters.Add(new DataFilter("Year", "System.Int32", 
        DataFilterCondition.Equal, 2003));

    // Create a second series in a similar manner.
    Series series2 = new Series();
    series2.Name = "2004";
    series2.ArgumentDataMember = "Region";
    series2.ValueDataMembers[0] = "GSP";
    series2.DataFilters.Add(new DataFilter("Year", "System.Int32", 
        DataFilterCondition.Equal, 2004));

    // Add both series to the chart's collection.
    chart.Series.Add(series1);
    chart.Series.Add(series2);

    // Initialize a chart's diagram.
    XYDiagram diagram = (XYDiagram)chart.Diagram;

    // Create a new pane.
    XYDiagramPane secondPane = new XYDiagramPane("The Pane's Name");

    // Add the pane to the diagram's collection.
    diagram.Panes.Add(secondPane);
    ((XYDiagramSeriesViewBase)series2.View).Pane = secondPane;

    // Add the chart to the form and fit it to the parent form's size.
    chart.Dock = DockStyle.Fill;
    form.Controls.Add(chart);

    // Display the form containing the chart.
    form.Show();
}
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"
    form.Size = New Size(800, 600)

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

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

    ' Define the chart's datasource.
    chart.DataSource = dataSet.GSP

    ' Create a series. Define its data member and create a data filter, to display a required year.
    Dim series1 As Series = New Series()
    series1.Name = "2003"
    series1.ArgumentDataMember = "Region"
    series1.ValueDataMembers(0) = "GSP"
    series1.DataFilters.Add(New DataFilter("Year", "System.Int32", _
        DataFilterCondition.Equal, 2003))

    ' Create a second series in a similar manner.
    Dim series2 As Series = New Series()
    series2.Name = "2004"
    series2.ArgumentDataMember = "Region"
    series2.ValueDataMembers(0) = "GSP"
    series2.DataFilters.Add(New DataFilter("Year", "System.Int32", _
        DataFilterCondition.Equal, 2004))

    ' Add both series to the chart's collection.
    chart.Series.Add(series1)
    chart.Series.Add(series2)

    ' Initialize a chart's diagram.
    Dim diagram As XYDiagram = CType(chart.Diagram, XYDiagram)

    ' Create a new pane.
    Dim secondPane As XYDiagramPane = New XYDiagramPane("The Pane's Name")

    ' Add the pane to the diagram's collection.
    diagram.Panes.Add(secondPane)
    CType(series2.View, XYDiagramSeriesViewBase).Pane = secondPane

    ' Add the chart to the form and fit it to the parent form's size.
    chart.Dock = DockStyle.Fill
    form.Controls.Add(chart)

    ' Display the form containing the chart.
    form.Show()
End Sub

CodeCentralShow Me

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

Expand image参阅