本示例演示了在运行时刻如何访问和定制 窗格

假设在图表中有两个 系列 都是 折线视图类型 的,在下列代码中,我们创建一个附加的窗格,并把第二个系列指派到其中。 另外,创建一个 辅助轴 并指派到第二个系列,也在附加的窗格中显示。

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

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

    // Create two series.
    Series series1 = new Series("Series 1", ViewType.Bar);
    Series series2 = new Series("Series 2", ViewType.Line);

    // Add points to them, with their arguments different.
    series1.Points.Add(new SeriesPoint("A", 10));
    series1.Points.Add(new SeriesPoint("B", 12));
    series1.Points.Add(new SeriesPoint("C", 17));
    series1.Points.Add(new SeriesPoint("D", 14));
    series2.Points.Add(new SeriesPoint("I", 2500));
    series2.Points.Add(new SeriesPoint("II", 3800));
    series2.Points.Add(new SeriesPoint("III", 1500));
    series2.Points.Add(new SeriesPoint("IV", 1300));

    // Add both series to the chart.
    chartControl1.Series.AddRange(new Series[] { series1, series2 });

    // Hide the legend (optional).
    chartControl1.Legend.Visible = false;

    // Cast the chart's diagram to the XYDiagram type, 
    // to access its axes and panes.
    XYDiagram diagram = (XYDiagram)chartControl1.Diagram;

    // Add secondary axes to the diagram, and adjust their options.
    diagram.SecondaryAxesX.Add(new SecondaryAxisX("My Axis X"));
    diagram.SecondaryAxesY.Add(new SecondaryAxisY("My Axis Y"));
    diagram.SecondaryAxesX[0].Alignment = AxisAlignment.Near;
    diagram.SecondaryAxesY[0].Alignment = AxisAlignment.Near;

    // Add a new additional pane to the diagram.
    diagram.Panes.Add(new XYDiagramPane("My Pane"));

    // Assign both the additional pane and, if required,
    // the secondary axes to the second series. 
    LineSeriesView myView = (LineSeriesView)series2.View;
    myView.AxisX = diagram.SecondaryAxesX[0];
    myView.AxisY = diagram.SecondaryAxesY[0];
    // Note that the created pane has the zero index in the collection,
    // because the existing Default pane is a separate entity.
    myView.Pane = diagram.Panes[0];
    
    // Customize the layout of the diagram's panes.
    diagram.PaneDistance = 10;
    diagram.PaneLayoutDirection = PaneLayoutDirection.Horizontal;
    diagram.DefaultPane.SizeMode = PaneSizeMode.UseWeight;
    diagram.DefaultPane.Weight = 1.2;

    // Add the chart to the form.
    chartControl1.Dock = DockStyle.Fill;
    this.Controls.Add(chartControl1);
}
Visual BasicCopyCode image复制代码
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports DevExpress.XtraCharts
' ...

Private Sub Form1_Load(ByVal sender As Object, _ 
ByVal e As EventArgs) Handles MyBase.Load
    ' Create a new chart.
    Dim chartControl1 As New ChartControl()

    ' Create two series.
    Dim series1 As New Series("Series 1", ViewType.Bar)
    Dim series2 As New Series("Series 2", ViewType.Line)

    ' Add points to them, with their arguments different.
    series1.Points.Add(New SeriesPoint("A", 10))
    series1.Points.Add(New SeriesPoint("B", 12))
    series1.Points.Add(New SeriesPoint("C", 17))
    series1.Points.Add(New SeriesPoint("D", 14))
    series2.Points.Add(New SeriesPoint("I", 2500))
    series2.Points.Add(New SeriesPoint("II", 3800))
    series2.Points.Add(New SeriesPoint("III", 1500))
    series2.Points.Add(New SeriesPoint("IV", 1300))

    ' Add both series to the chart.
    chartControl1.Series.AddRange(New Series() { series1, series2 })

    ' Hide the legend (optional).
    chartControl1.Legend.Visible = False

    ' Cast the chart's diagram to the XYDiagram type, 
    ' to access its axes and panes.
    Dim diagram As XYDiagram = CType(chartControl1.Diagram, XYDiagram)

    ' Add secondary axes to the diagram, and adjust their options.
    diagram.SecondaryAxesX.Add(New SecondaryAxisX("My Axis X"))
    diagram.SecondaryAxesY.Add(New SecondaryAxisY("My Axis Y"))
    diagram.SecondaryAxesX(0).Alignment = AxisAlignment.Near
    diagram.SecondaryAxesY(0).Alignment = AxisAlignment.Near

    ' Add a new additional pane to the diagram.
    diagram.Panes.Add(New XYDiagramPane("My Pane"))

    ' Assign both the additional pane and, if required,
    ' the secondary axes to the second series. 
    Dim myView As LineSeriesView = CType(series2.View, LineSeriesView)
    myView.AxisX = diagram.SecondaryAxesX(0)
    myView.AxisY = diagram.SecondaryAxesY(0)
    ' Note that the created pane has the zero index in the collection,
    ' because the existing Default pane is a separate entity.
    myView.Pane = diagram.Panes(0)

    ' Customize the layout of the diagram's panes.
    diagram.PaneDistance = 10
    diagram.PaneLayoutDirection = PaneLayoutDirection.Horizontal
    diagram.DefaultPane.SizeMode = PaneSizeMode.UseWeight
    diagram.DefaultPane.Weight = 1.2

    ' Add the chart to the form.
    chartControl1.Dock = DockStyle.Fill
    Me.Controls.Add(chartControl1)
End Sub

在下面的插图中显示了结果。

CodeCentralShow Me

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

Expand image参阅