Chart Class
Displays data in the form of a graphical chart.
Assembly: System.Web.Helpers (in System.Web.Helpers.dll)
The Chart type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | AddLegend | Adds a legend to the chart. |
![]() | AddSeries | Provides data points and series attributes for the chart. |
![]() | AddTitle | Adds a title to the chart. |
![]() | DataBindCrossTable | Binds a chart to a data table, where one series is created for each unique value in a column. |
![]() | DataBindTable | Creates and binds series data to the specified data table, and optionally populates multiple x-values. |
![]() | Equals | (Inherited from Object.) |
![]() | Finalize | (Inherited from Object.) |
![]() | GetBytes | Returns a chart image as a byte array. |
![]() ![]() | GetFromCache | Retrieves the specified chart from the cache. |
![]() | GetHashCode | (Inherited from Object.) |
![]() | GetType | (Inherited from Object.) |
![]() | MemberwiseClone | (Inherited from Object.) |
![]() | Save | Saves a chart image to the specified file. |
![]() | SaveToCache | Saves a chart in the system cache. |
![]() | SaveXml | Saves a chart as an XML file. |
![]() | SetXAxis | Sets values for the horizontal axis. |
![]() | SetYAxis | Sets values for the vertical axis. |
![]() | ToString | (Inherited from Object.) |
![]() | ToWebImage | Creates a WebImage object based on the current Chart object. |
![]() | Write | Renders the output of the Chart object as an image. |
![]() ![]() | WriteFromCache | Renders the output of a Chart object that has been cached as an image. |
This class represents a helper, which is a component that simplifies web programming in ASP.NET Web Pages. You can use the Chart class to display data in the form of a graphical chart. The class can render data using a variety of chart types, such as bar charts, column charts and pie charts. For a complete list of chart types, see Chart Types (Chart Controls).
The Chart class can be used to customize the appearance of a chart. For instance, you can set colors, fonts, and borders of a chart. Use the ChartTheme class to quickly set the theme of a chart. Themes are collections of information that specify how to render a chart using fonts, colors, labels, palettes, borders, and effects. For more information about setting a chart theme, see the "Styling a Chart" section in Displaying Data in a Chart.
To help reduce the potential performance impact of creating a chart, you can create a chart the first time you use it and then save it. When you have to display the chart again, instead of regenerating it, you can fetch the saved version and render that. You can save a chart using one of the following methods:
As a cache entry.
As an image file.
As an XML file.
For more information about saving a chart, see the "Saving a Chart" section in Displaying Data in a Chart.
The following example shows how to use the Chart class to create a chart image based on an array of values.
@{
var basicChart = new Chart(width: 600, height: 400)
.AddTitle("Chart Title")
.AddSeries(
name: "Employee",
xValue: new[] { "Peter", "Andrew", "Julie", "Mary", "Dave" },
yValues: new[] { "2", "6", "4", "5", "3" })
.Write();
}
The following example shows how to use the Chart class to create a chart image by running a database query and then binding the selected data from the results of the query.
@{
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();
}
