Back to Devexpress

Series.DataSource Property

corelibraries-devexpress-dot-xtracharts-dot-series-4e71f573.md

latest13.4 KB
Original Source

Series.DataSource Property

SECURITY-RELATED CONSIDERATIONS

Deserializing layout settings from untrusted resources may create security issues. Serializable System.Object properties that contain custom type values are not (de)serialized automatically. Review the following help topic for information on how to (de)serialize custom type values: Safe Deserialization.

Gets or sets the series’ data source.

Namespace : DevExpress.XtraCharts

Assembly : DevExpress.XtraCharts.v25.2.dll

NuGet Package : DevExpress.Charts

Declaration

csharp
[XtraChartsLocalizableCategory(XtraChartsCategory.Data)]
public object DataSource { get; set; }
vb
<XtraChartsLocalizableCategory(XtraChartsCategory.Data)>
Public Property DataSource As Object

Property Value

TypeDescription
Object

A Object representing the series’ data source.

|

Remarks

Use the DataSource property to specify the data source from which the current series will obtain information about the data points it displays.

The DataSource property is useful for series binding to bind each series separately. When binding an individual series object, the DataSource property of the series has a higher priority than the ChartControl.DataSource property of a chart control. For instance, if a chart control is bound to the dataView1 object via its ChartControl.DataSource property and a particular series of the chart control is bound to the dataView2 object via its DataSource property, the series will contain data points obtained from the dataView2 object according to the SeriesBase.ArgumentDataMember and SeriesBase.ValueDataMembers property settings of the series.

If the DataSource property of a series is not set (its value is null ) but the chart control that contains this series is bound to data via its ChartControl.DataSource property, the series’ SeriesBase.ArgumentDataMember and SeriesBase.ValueDataMembers properties can be defined so that the series will obtain data point information from the corresponding data fields of the chart control’s data source.

For more information, refer to Providing Data.

Example

The following example demonstrates how to bind a chart to data at runtime via binding its individual series to a particular datasource. It uses the same approach as the design-time example, but another data table is generated in this code to simplify the example.

Note

Don’t forget to include all necessary assemblies to the References list of your project.

Note

A complete sample project is available at https://github.com/DevExpress-Examples/winforms-charts-bind-individual-series-to-data

csharp
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.Visibility = DevExpress.Utils.DefaultBoolean.False;
            chart.Legend.Visibility = DevExpress.Utils.DefaultBoolean.False;

            // Dock the chart into its parent and add it to the current form.
            chart.Dock = DockStyle.Fill;
            this.Controls.Add(chart);
        }
    }
}
vb
Imports System
Imports System.Data
Imports System.Windows.Forms
Imports DevExpress.XtraCharts
' ...

Namespace BindIndividualSeriesRuntimeCS
    Partial Public Class Form1
        Inherits Form

        Public Sub New()
            InitializeComponent()
        End Sub

        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
            For i As Integer = 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.Visibility = DevExpress.Utils.DefaultBoolean.False
            chart.Legend.Visibility = DevExpress.Utils.DefaultBoolean.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
End Namespace

The following code snippets (auto-collected from DevExpress Examples) contain references to the DataSource property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-charts-plot-xy-series-with-histogram/CS/Form1.cs#L55

csharp
line.DataSource = NormalDistribution;
histogram.DataSource = Histogram;

winforms-dashboard-obtain-dashboard-item-client-data/CS/Dashboard_ClientDataCards_Win/Form1.cs#L70

csharp
foreach (Series series in chart.Series) {
    series.DataSource = dataSource; series.ArgumentDataMember = "Argument";
    series.ValueScaleType = ScaleType.Numerical;

winforms-charts-create-model-for-custom-chart-element/CS/CustomChartElementModel/Form1.cs#L106

csharp
Series series = new Series("Temperature", ViewType.Bar);
series.DataSource = DataPoint.GetDataPoints();
series.ArgumentDataMember = "Date";

winforms-charts-create-real-time-chart/CS/RealTimeChartUpdates/Form1.cs#L18

csharp
series.ChangeView(ViewType.Line);
series.DataSource = dataPoints;
series.DataSourceSorted = true;

winforms-charts-create-real-time-chart-and-collect-data-in-separate-thread/CS/RealTimeChartUpdates/Form1.cs#L27

csharp
series.ChangeView(ViewType.Line);
series.DataSource = viewportData;
series.DataSourceSorted = true;

winforms-chart-exclude-weekends-and-holidays-from-the-axis-range/VB/WeekendsExclusion/Form1.vb#L19

vb
series0.ArgumentScaleType = ScaleType.DateTime
series0.DataSource = CreateChartData()
series0.SetFinancialDataMembers("Argument", "Low", "High", "Open", "Close")

winforms-charts-plot-xy-series-with-histogram/VB/Form1.vb#L81

vb
lineView.AxisX = secondaryAxisX
line.DataSource = NormalDistribution
histogram.DataSource = Me.Histogram

winforms-dashboard-obtain-dashboard-item-client-data/VB/Dashboard_ClientDataCards_Win/Form1.vb#L69

vb
For Each series As Series In chart.Series
    series.DataSource = dataSource
    series.ArgumentDataMember = "Argument"

winforms-charts-create-model-for-custom-chart-element/VB/CustomChartElementModel/Form1.vb#L173

vb
Dim series As Series = New Series("Temperature", ViewType.Bar)
series.DataSource = DataPoint.GetDataPoints()
series.ArgumentDataMember = "Date"

winforms-charts-create-real-time-chart/VB/RealTimeChartUpdates/Form1.vb#L24

vb
series.ChangeView(ViewType.Line)
series.DataSource = dataPoints
series.DataSourceSorted = True

See Also

Provide Data

DataSource

ArgumentDataMember

ValueDataMembers

Series Class

Series Members

DevExpress.XtraCharts Namespace