Bit bot hour of code 2-6


This article explains how to use a chart to display data in an ASP. When you want to display your data in graphical form, you can use Chart helper. The Chart helper can render an image that displays data in a variety of chart types. It supports many options for formatting and labeling.

The Chart helper can render more than 30 types of charts, including all the types of charts that you might be familiar with from Microsoft Excel or other tools — area charts, bar charts, column charts, line charts, and pie charts, along with more specialized charts like stock charts. Charts show data and additional elements like legends, axes, series, and so on.

The following picture shows many of the chart elements that you can customize when you use the Chart helper. This article shows you how to set some not all of these elements. The data you display in a chart can be from an array, from the results returned from a database, or from data that's in an XML file.

As explained in Introduction to ASP. You can use arrays to contain the data that you want to include in your chart. This procedure shows how you can create a chart from data in arrays, using the default chart type.

Bit bot hour of code 2-6 also shows how to display the chart within the page. The code first creates a new chart and bit bot hour of code 2-6 its width and height.

You specify the chart title by using the AddTitle method. To add data, you use the AddSeries method. In this example, you use the namexValueand yValues parameters of the AddSeries method. The name parameter is displayed in the chart legend. The xValue parameter contains an array of data that's displayed along the horizontal axis of the chart.

The yValues parameter contains an array of data that's used to plot the vertical points of the chart. The Write method actually renders the chart. In this case, because you didn't specify a chart type, the Chart helper renders its default chart, which is a column chart.

If the information you want to chart is in a database, you can run a database query and then use data from the results to create the chart.

This procedure shows you how to read and display the data from the database created in the article Introduction to Working with a Database in ASP. The code first opens the SmallBakery database and assigns it to a variable named db. This variable represents a Database object that can be used to read from and write to the database. Next, the code runs a SQL query to get the bit bot hour of code 2-6 and price of each product. The code creates a new chart and passes the database query to it by calling the chart's DataBindTable method.

This method takes two parameters: The AddSeries method lets you set the xValue and yValues parameters. For example, instead of using the DataBindTable method like this:. Both render the same results. The AddSeries method is more flexible because you can specify the chart type and data more explicitly, but the Bit bot hour of code 2-6 method is easier to use if you don't bit bot hour of code 2-6 the extra flexibility. The third option for charting is to use an XML file as the data for the chart.

This requires that the XML file also have a schema file. This procedure shows you how to read data from an XML file. Note that the extension this time is.

The code first creates a DataSet object. This object is used to manage the data that's read from the XML file and organize it according to the information in the schema file. Notice that the top of the code includes the statement using SystemData. This is required in order to be able to work with the DataSet object.

Next, the code creates a DataView object based on the dataset. The data view provides an object that the chart can bind to — that is, read and plot. Bit bot hour of code 2-6 chart binds to the data using the AddSeries method, as you saw earlier when charting the array data, except that this time the xValue and yValues parameters are set to the DataView object. This example also shows you how to specify a particular chart type. When the data is added in the AddSeries method, the chartType parameter is also set to display a pie chart.

To make it manageable to work with all these classes, they're organized into namespaceswhich are somewhat like libraries. For example, the System.

Xml namespace contains classes that are used to create and read XML files, and the System. Data namespace contains classes that let you work with data. In order to access any given class in the.

NET Framework, code needs to know not just the class name, but also the namespace that the class bit bot hour of code 2-6 in. For example, in order to bit bot hour of code 2-6 the Chart helper, code needs to find the System. Chart class, which combines the namespace System. Helpers with the class name Chart. This is known as the class's fully-qualified name — its complete, unambiguous location within the vastness of the.

In code, this would look like the following:. However, it's cumbersome and error prone to have to use these long, fully-qualified names every time you want to refer to a class or helper. Therefore, to make it easier to use class names, bit bot hour of code 2-6 can import the namespaces you're interested in, which is usually is just a handful from among the many namespaces in the.

If you've imported a namespace, you can use just a class name Chart instead of the fully qualified name System. When your code runs and encounters a class name, it can look in just the namespaces you've imported to find that class. When you use ASP. NET Web Pages with Razor syntax to create web pages, you typically use the same set of classes each time, including the WebPage class, bit bot hour of code 2-6 various helpers, and so on.

To save you the work of importing the relevant namespaces every time you create a website, ASP. NET is configured so it automatically imports a set of core namespaces for every website.

That's why bit bot hour of code 2-6 haven't had to deal with namespaces or importing up to now; all the classes you've worked with are in namespaces that are already imported for you. However, sometimes you need to work with a class that isn't in a namespace that's automatically imported for you. In that case, you can either use that class's fully-qualified name, or you can manually import the namespace that contains the class.

To import a namespace, you use the using statement import in Visual Basicas you saw in an example earlier the article. For example, the DataSet class is in the System. Data namespace is not automatically available to ASP. Therefore, to work with the DataSet class using its fully qualified name, you can use code like this:. If you have to use the DataSet class repeatedly you can import a namespace like this and then use just the class name in code:.

You can add using statements for any other. NET Framework namespaces that you want to reference. However, as noted, you won't need to do this often, because most of the classes that you'll work with are in namespaces that are imported automatically by ASP. NET for use in. In the examples you've seen so far, you create a chart and then the chart is rendered directly to the browser as a graphic.

In many cases, though, you want to display a chart as part of a page, not just by itself in the browser. To do that requires a two-step process. The first step is to create a page that generates the chart, as you've already seen. The second step is to display the resulting image in another page. However, instead of referencing a.

Run the web page in a browser. The Chart helper supports a large number of options that let you customize the appearance of the chart. You can set colors, fonts, borders, and so on. An easy way to customize the appearance of a chart is to use a theme. Themes are collections of information that specify how to render a chart using fonts, colors, labels, palettes, borders, and effects.

Note that the style of a chart does not indicate the type of chart. This code is the same as the earlier example that uses the database for data, but adds the theme parameter when it creates the Chart object. The following shows the changed code:.

When you use the Chart helper as you've bit bot hour of code 2-6 so far in this article, the helper re-creates the chart from scratch each time it's invoked. If necessary, the code for the chart also re-queries the database or re-reads the XML file to get the data. In some cases, doing this can be a complex operation, such as if the database that you're querying is large, or if the XML file contains a lot of data.

Even if the chart doesn't involve a lot of data, bit bot hour of code 2-6 process of dynamically creating an image takes up server resources, and if many people request the page or pages that display the chart, there can be an impact on the performance of your website.

To help you reduce the potential performance impact of creating a chart, you can create a chart the first time you need it and then save it.

When the chart is needed again, rather than regenerating it, you can just fetch the saved version and render that. After you've created a chart, you can cache it. Caching a chart means that it doesn't have to be re-created if it needs to be displayed again.