Chart.DataBindTable Method

Creates and binds series data to the specified data table, and optionally populates multiple x-values.

Namespace:  System.Web.Helpers
Assembly:  System.Web.Helpers (in System.Web.Helpers.dll)

public Chart DataBindTable(
	IEnumerable dataSource,
	string xField
)

Parameters

dataSource
Type: System.Collections.IEnumerable
The chart data source. This can be can be any IEnumerable object.
xField
Type: System.String
The name of the table column used for the series x-values.

Return Value

Type: System.Web.Helpers.Chart
The chart.

As an alternative to using the DataBindTable method, you can use the AddSeries method of the Chart class. The AddSeries method lets you set the xValue and yValues parameters.

The following example shows how to use the DataBindTable method to create a chart image based on an array of values.


@{ 
    var db = Database.Open("SmallBakery"); 
    var data = db.Query("SELECT Name, Price FROM Product"); 
    var basicChart = new Chart(width: 600, height: 400) 
        .AddTitle("Product Sales") 
        .DataBindTable(dataSource: data, xField: "Name") 
        .Write(); 
}


The code creates a new chart and passes a database query to it by calling the chart's DataBindTable method.

  • Medium trust for the immediate caller. This member can be used by partially trusted code.
Show: