How to bind a dataset with date values to a WinForms Chart?
To bind a dataset containing date values to a Syncfusion® WinForms Charts, you should use the ChartDataBindModel class. This involves creating a table with a Datetime column and then binding it to the dataset.
C#
//Create the DataSet and DataTable
DataSet dataSet1 = new DataSet("DataSet1");
DataTable demographicsTable = new DataTable("Demographics");
demographicsTable.Columns.Add("ID", typeof(int));
demographicsTable.Columns.Add("TimeStamp", typeof(DateTime));
demographicsTable.Columns.Add("Population", typeof(int));
demographicsTable.Rows.Add(1, new DateTime(2019, 1, 1), 8000000);
demographicsTable.Rows.Add(2, new DateTime(2020, 1, 1), 12400000);
demographicsTable.Rows.Add(3, new DateTime(2021, 1, 1), 11000000);
demographicsTable.Rows.Add(4, new DateTime(2022, 1, 1), 8500000);
demographicsTable.Rows.Add(5, new DateTime(2023, 1, 1), 6800000);
demographicsTable.Rows.Add(6, new DateTime(2024, 1, 1), 4500000);
dataSet1.Tables.Add(demographicsTable);
//Configure the data binding model
ChartDataBindModel model = new ChartDataBindModel(dataSet1, "Demographics");
// XName is "ID"
model.XName = "ID";
// YNames is "Population"
model.YNames = new string[] { "Population" };
//Configure the chart series
ChartSeries series = new ChartSeries();
series.Type = ChartSeriesType.Line;
series.Style.Symbol.Shape = ChartSymbolShape.Circle;
series.Style.Symbol.Color = Color.Blue;
series.Style.Interior = new Syncfusion.Drawing.BrushInfo(Color.Blue);
series.SeriesModelImpl = model;
this.chartControl1.Series.Add(series);
ChartDataBindAxisLabelModel xAxisLabelModel = new ChartDataBindAxisLabelModel(dataSet1, "Demographics");
//The columns that has the label values corresponding X values
xAxisLabelModel.LabelName = "TimeStamp";
this.chartControl1.PrimaryXAxis.LabelsImpl = xAxisLabelModel;
this.chartControl1.PrimaryXAxis.ValueType = ChartValueType.Custom;
this.chartControl1.PrimaryXAxis.Range = new MinMaxInfo(1, 6, 1);
this.chartControl1.PrimaryXAxis.EdgeLabelsDrawingMode = ChartAxisEdgeLabelsDrawingMode.Shift;
VB
Dim dataSet1 As New DataSet("DataSet1")
Dim demographicsTable As New DataTable("Demographics")
demographicsTable.Columns.Add("ID", GetType(Integer))
demographicsTable.Columns.Add("TimeStamp", GetType(DateTime))
demographicsTable.Columns.Add("Population", GetType(Integer))
'Add data rows with DateTime values
demographicsTable.Rows.Add(1, New DateTime(2019, 1, 1), 8000000)
demographicsTable.Rows.Add(2, New DateTime(2020, 1, 1), 12400000)
demographicsTable.Rows.Add(3, New DateTime(2021, 1, 1), 11000000)
demographicsTable.Rows.Add(4, New DateTime(2022, 1, 1), 8500000)
demographicsTable.Rows.Add(5, New DateTime(2023, 1, 1), 6800000)
demographicsTable.Rows.Add(6, New DateTime(2024, 1, 1), 4500000)
dataSet1.Tables.Add(demographicsTable)
'Configure the data binding model
Dim model As New ChartDataBindModel(dataSet1, "Demographics")
' XName is "ID"
model.XName = "ID"
' YNames is "Population"
model.YNames = New String() {"Population"}
'Configure the chart series
Dim series As New ChartSeries()
series.Type = ChartSeriesType.Line
series.Style.Symbol.Shape = ChartSymbolShape.Circle
series.Style.Symbol.Color = Color.Blue
series.Style.Interior = New Syncfusion.Drawing.BrushInfo(Color.Blue)
series.SeriesModelImpl = model
lineChart.Series.Add(series)
'Use ChartDataBindAxisLabelModel to display dates as axis labels
Dim xAxisLabelModel As New ChartDataBindAxisLabelModel(dataSet1, "Demographics")
' The column that has the label values corresponding to the X values
xAxisLabelModel.LabelName = "TimeStamp"
lineChart.PrimaryXAxis.LabelsImpl = xAxisLabelModel
'Configure the X-axis
lineChart.PrimaryXAxis.ValueType = ChartValueType.Custom
lineChart.PrimaryXAxis.Range = New MinMaxInfo(1, 6, 1)
lineChart.PrimaryXAxis.EdgeLabelsDrawingMode = ChartAxisEdgeLabelsDrawingMode.Shift
Output:
Conclusion
I hope you enjoyed learning about how to bind a dataset with date values to a WinForms Chart.
You can refer to our WinForms Chart feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our WinForms Chart examples to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!