Oracle® Application Express Advanced Tutorials Release 3.2 Part Number E11945-01 |
|
|
View PDF |
A stacked bar chart displays the results of multiple queries stacked on top of one another, either vertically or horizontally. Using a stacked bar chart is an effective way to present the absolute values of data points represented by the segments of each bar, as well as the total value represented by data points from each series stacked in a bar.
Although Application Builder includes built-in wizards for generating HTML, Scalable Vector Graphics (SVG), and Flash charts, only SVG and Flash charts support stacked bar charts.
This tutorial describes how to create a Flash stacked bar chart. Before you begin, you need to import and install the OEHR Sample Objects application in order to access the necessary sample database objects. See "About Loading Sample Objects".
This section contains the following topics:
See Also:
"Creating Charts" in Oracle Database Application Express User's GuideFor additional examples on this and related topics, please visit the following Oracle by Examples (OBEs):
Using Some Additional New Features in Oracle Application Express 3.1
http://www.oracle.com/technology/obe/apex/apex31nf/apex31misc.htm
Adding Additional Components to your Existing Application
http://www.oracle.com/technology/obe/hol08/apexintro/apex3.1.1_c/apex3.1.1_c_otn.htm
The syntax for the select statement of a chart is:
SELECT link, label, value FROM ...
Where:
link
is a URL. This URL will be called if the user clicks on the that point on the resulting chart.
label
is the text that displays in the bar.
value
is the numeric column that defines the bar size.
You must have all three items in your select statement. In the next example, the link is defined as null because there is no appropriate page to link to.
For example:
SELECT null link, last_name label, salary value FROM employees WHERE DEPARTMENT_ID = :P101_DEPARTMENT_ID
See Also:
"Creating Charts" in Oracle Database Application Express User's GuideFirst, you create an application using the Create Application Wizard.
To create an application using the Create Application Wizard:
On the Workspace home page, click the Application Builder icon.
The Application Builder home page appears.
Click Create.
Select Create Application and click Next.
For Name:
Name - Enter Bar Chart
.
Application - Accept the default.
Create Application - Select From scratch.
Schema - Select the schema where you installed the OEHR sample objects.
Click Next.
Next, you need to add a page. You have the option of adding a blank page, a report, a form, a tabular form, or a report and form. For this exercise, you add a blank page.
Add a blank page:
Under Select Page Type, select Blank and click Add Page.
The new page appears in the list at the top of the page.
Click Next.
For Tabs, accept the default, One Level of Tabs, and click Next.
For Copy Shared Components from Another Application, accept the default, No, and click Next.
For Attributes, accept the defaults for Authentication Scheme, Language, and User Language Preference Derived From and click Next.
For User Interface, select Theme 2 and click Next.
Review your selections and click Create.
The Application home page appears.
To create your chart, you can either add a region to an existing page and define it as a stacked bar chart, or you can create a new page. In this exercise, you create a new page within the Bar Chart application you just created.
The chart will display the sum for sales by product category. It will contain sales for the twelve months prior to the current month. In the following exercise, you use a wizard to create the chart and the first query. Then, you add additional queries (or series) for other product categories to make it stacked.
To create a new page:
On the Application home page, click Create Page.
For page, select Chart and click Next.
Select Flash Chart and click Next.
For Page Attributes:
For Page Number, enter 2
.
For Page Name, enter Revenue by Category
.
For Region Template, accept the default.
For Region Name, enter Revenue by Category
.
For Breadcrumb, accept the default.
Click Next.
For Tab Options, accept the default, Do not use Tabs, and then click Next.
The Chart Preview appears. Use Chart Preview to configure chart attributes. Click Update to refresh the preview image.
On Chart Preview, specify the following:
Chart Type - Select Stacked 3D Column.
Show Legend - Select Right.
Click Update.
Notice the changes to the preview.
Click Next.
For Query:
Enter the following query:
SELECT NULL link, sales_month value, revenue "Hardware" FROM ( SELECT TO_CHAR(o.order_date,'Mon YY') sales_month, SUM(oi.quantity * oi.unit_price) revenue, TO_DATE(to_char(o.order_date,'Mon YY'),'Mon YY') sales_month_order FROM OEHR_PRODUCT_INFORMATION p, OEHR_ORDER_ITEMS oi, OEHR_ORDERS o, OEHR_CATEGORIES_TAB ct WHERE o.order_date <= (trunc(sysdate,'MON')-1) AND o.order_date > (trunc(sysdate-365,'MON')) AND o.order_id = oi.order_id AND oi.product_id = p.product_id AND p.category_id = ct.category_id AND ct.category_name like '%hardware%' GROUP BY TO_CHAR(o.order_date,'Mon YY') ORDER BY sales_month_order )
The value label (in this instance, Hardware
) is displayed in the legend of stacked charts.
Tip:
You can also create a chart query interactively by clicking the Build Query button.For When No Data Found Message, enter:
No orders found in the past 12 months.
Click Next.
Review your selections and click Finish.
The Success page appears.
Now that you have created a page with a region defining the query, you need to add additional series. In the following exercise, you add a series for the categories software and office equipment.
To add additional series:
On the Success Page, click Edit Page.
The Page Definition for page 2 appears.
Under Regions, click Flash Chart next to Revenue by Category.
The Flash Chart page appears with the Chart Attributes tab selected. Scroll down to Chart Series. Note that only one series appears.
To change the name the existing series:
Click the Edit icon.
In Series Name, enter Hardware
.
Click Apply Changes.
Add a chart series for software:
Scroll down to Chart Series and then click Add Series.
For Series Name, enter Software
.
Scroll down to Series Query.
In SQL, enter:
SELECT NULL link, sales_month value, revenue "Software" FROM ( SELECT TO_CHAR(o.order_date,'Mon YY') sales_month, SUM(oi.quantity * oi.unit_price) revenue, TO_DATE(to_char(o.order_date,'Mon YY'),'Mon YY') sales_month_order FROM OEHR_PRODUCT_INFORMATION p, OEHR_ORDER_ITEMS oi, OEHR_ORDERS o, OEHR_CATEGORIES_TAB ct WHERE o.order_date <= (trunc(sysdate,'MON')-1) AND o.order_date > (trunc(sysdate-365,'MON')) AND o.order_id = oi.order_id AND oi.product_id = p.product_id AND p.category_id = ct.category_id AND ct.category_name like '%software%' GROUP BY TO_CHAR(o.order_date,'Mon YY') ORDER BY sales_month_order )
The value label (in this instance, Software
) is displayed in the legend of stacked charts. Note that this SQL matches the previous series. The only difference is the category in the WHERE
clause.
For When No Data Found Message, enter:
No orders found in the past 12 months.
At the top of the page, click Apply Changes.
Add a chart series for office equipment:
Under Chart Series, click Add Series.
For Series Name, enter Office Equipment
.
Scroll down to Series Query.
In SQL, enter:
SELECT NULL link, sales_month value, revenue "Office Equipment" FROM ( SELECT TO_CHAR(o.order_date,'Mon YY') sales_month, SUM(oi.quantity * oi.unit_price) revenue, TO_DATE(to_char(o.order_date,'Mon YY'),'Mon YY') sales_month_order FROM OEHR_PRODUCT_INFORMATION p, OEHR_ORDER_ITEMS oi, OEHR_ORDERS o, OEHR_CATEGORIES_TAB ct WHERE o.order_date <= (trunc(sysdate,'MON')-1) AND o.order_date > (trunc(sysdate-365,'MON')) AND o.order_id = oi.order_id AND oi.product_id = p.product_id AND p.category_id = ct.category_id AND ct.category_name like '%office%' GROUP BY TO_CHAR(o.order_date,'Mon YY') ORDER BY sales_month_order )
The value label (in this instance, Office Equipment
) is displayed in the legend of stacked charts.
For When No Data Found Message, enter:
No orders found in the past 12 months.
Scroll up to the top of the page and click Apply Changes.
The sample data that installed with the OEHR Sample Objects application is not current. To make the data current, you need to update the dates in the sample data. You will accomplish this by running an update statement in SQL Commands
See Also:
"Using SQL Commands" in Oracle Database Application Express User's GuideTo update the dates in the seed data:
Return to the Workspace home page. Click the Home breadcrumb link at the top of the page.
On the Workspace home page, click SQL Workshop and then SQL Commands.
The SQL Commands page appears.
Enter the following in the SQL editor pane:
DECLARE l_date_offset number; BEGIN FOR c1 IN (SELECT TRUNC(max(order_date)) max_date FROM oehr_orders) LOOP l_date_offset := round(sysdate - c1.max_date); END LOOP; UPDATE oehr_orders set order_date = order_date + l_date_offset; COMMIT; END; /
Click Run (Ctrl+Enter) to execute the command.
Now that the chart is complete, you can view it.
To run the chart:
Return to page 2, Revenue by Category:
Click the Home breadcrumb link at the top of the page.
Click Application Builder and then click your Bar Chart application.
Click 2 - Revenue by Category.
Click the Run Page icon in the upper right corner of the page.
If prompted for a user name and password, enter your workspace user name and password and click Login. See "About Application Authentication".
Your bar chart should resemble Figure 8-1.
Figure 8-1 Revenue by Category Stacked Bar Chart
The chart displays the revenue for each product category by month. A legend that defines the color associated with each product appears at the top of the page. Note that the text in X Axis is spaced too closely together. In the next section, you edit the chart attributes to correct this issue.
In this exercise, you change the appearance of your chart by editing chart attributes.
To edit chart attributes:
Click Edit Page 2 on the Developer toolbar.
Under Regions, click Flash Chart.
The Chart Attributes page appears.
Under Chart Settings, edit the chart width. In Chart Width, enter 800.
Scroll down to Display Settings. From Animation, select Dissolve.
Scroll down to Axes Settings. From Show Group Separator, select Yes.
Scroll down to Font Settings. For X Axis Title, select the Font Size 10.
Click Apply Changes to a the top of the page.
Click the Run Page icon in the upper right corner of the page.
You chart should resemble Figure 8-2.
Note that the chart displays on-screen gradually using a dissolve and the X Axis displays correctly.