Chart Class

Displays data in the form of a graphical chart.


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

public class Chart

The Chart type exposes the following members.

Public methodChartInitializes a new instance of the Chart class.

Public propertyFileNameGets or sets the name of the file that contains the chart image.
Public propertyHeightGets or sets the height, in pixels, of the chart image.
Public propertyWidthGets or set the width, in pixels, of the chart image.

Public methodAddLegendAdds a legend to the chart.
Public methodAddSeriesProvides data points and series attributes for the chart.
Public methodAddTitleAdds a title to the chart.
Public methodDataBindCrossTableBinds a chart to a data table, where one series is created for each unique value in a column.
Public methodDataBindTableCreates and binds series data to the specified data table, and optionally populates multiple x-values.
Public methodEquals (Inherited from Object.)
Protected methodFinalize (Inherited from Object.)
Public methodGetBytesReturns a chart image as a byte array.
Public methodStatic memberGetFromCacheRetrieves the specified chart from the cache.
Public methodGetHashCode (Inherited from Object.)
Public methodGetType (Inherited from Object.)
Protected methodMemberwiseClone (Inherited from Object.)
Public methodSaveSaves a chart image to the specified file.
Public methodSaveToCacheSaves a chart in the system cache.
Public methodSaveXmlSaves a chart as an XML file.
Public methodSetXAxisSets values for the horizontal axis.
Public methodSetYAxisSets values for the vertical axis.
Public methodToString (Inherited from Object.)
Public methodToWebImageCreates a WebImage object based on the current Chart object.
Public methodWriteRenders the output of the Chart object as an image.
Public methodStatic memberWriteFromCacheRenders 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") 
            name: "Employee", 
            xValue: new[] {  "Peter", "Andrew", "Julie", "Mary", "Dave" }, 
            yValues: new[] { "2", "6", "4", "5", "3" }) 

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") 

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.