Skip to main content

Like No Other

APEX pie charts allow you to limit the number of records that are displayed in the chart. This can be controlled by editing the Series and setting the Maximum Rows parameter. However, APEX will automatically add an "Other" slice to your chart, which represents all of the other data from the query. Consider this example: you have 10 different widget types, and 10 of each type for a total of 100 units. Here's some SQL to quickly create this scenario, if you want to follow along:
CREATE TABLE widgets (widget_type VARCHAR2(10))
/

DECLARE
  z NUMBER := 1;
BEGIN
FOR y IN 1..10
LOOP
  FOR x IN 1..10
  LOOP
    INSERT INTO widgets VALUES ('Type ' || z);
    z := z + 1;
  END LOOP;
  z := 1;
END LOOP;
END;
/
If you wanted to sum them based on type and just see the first five types, you can use a simple SQL statement like this:
SELECT 
  NULL link,
  widget_type label, 
  COUNT(*) value
FROM 
  widgets
GROUP BY 
  widget_type 
ORDER BY 2
Which will in turn, produce a chart that looks like this: SafariScreenSnapz001 Clearly, not what you may expected, as you only wanted to see the top 5 widgets, not the top 5 plus all additional records grouped into an "other" slice. Visually, this may be misleading to the user. To get just the top 5 records, we have to use an inline SQL statement that will select from our original SQL statement and limit the results to just 5 records:
SELECT
  link,
  label,
  value
FROM
  (
  SELECT 
    NULL link,
    widget_type label, 
    COUNT(*) value
  FROM 
    widgets
  GROUP BY 
    widget_type 
  ORDER BY 2
  )
WHERE
  rownum < 6
With this approach, we let APEX create the chart and append the Other slice to the results. We then siphon off just the first five records, which in this case, will not include the Other slice. The result is something closer to what many users would expect: SafariScreenSnapz002 Using this approach, the value of the Maximum Rows can be set anything greater than or equal to the value compared to ROWNUM in the last line of the second SQL statement, as we will be using the WHERE clause to limit how many records are displayed.

Comments

jts194 said…
Scott,
We're using APEX 4.1 and APEX Listener 1.1.3. Have you ever had a problem with stored procedures parameter/signature caching. For example, if I create a stored procedure with two parameters (default null), I can access via browser through /apex/schema_name.procedure_name?p_one=1&p_tw0=2. Now, if I had a third parameter and change my URL signature to p_one=1&p_tw0=2&p_three=3, p_three is always null. Whenever the procedure is created or accessed, it seems its caching the signature and not acknowledging the new parameters. Any insight would be appreciated. Thanks
Josh S.
Scott said…
Josh,

Not sure how this comment relates to this post.

- Scott -

Popular posts from this blog

Custom Export to CSV

It's been a while since I've updated my blog. I've been quite busy lately, and just have not had the time that I used to. We're expecting our 1st child in just a few short weeks now, so most of my free time has been spent learning Lamaze breathing, making the weekly run to Babies R Us, and relocating my office from the larger room upstairs to the smaller one downstairs - which I do happen to like MUCH more than I had anticipated. I have everything I need within a short walk - a bathroom, beer fridge, and 52" HD TV. I only need to go upstairs to eat and sleep now, but alas, this will all change soon... Recently, I was asked if you could change the way Export to CSV in ApEx works. The short answer is, of course, no. But it's not too difficult to "roll your own" CSV export procedure. Why would you want to do this? Well, the customer's requirement was to manipulate some data when the Export link was clicked, and then export it to CSV in a forma

Refreshing PL/SQL Regions in APEX

If you've been using APEX long enough, you've probably used a PL/SQL Region to render some sort of HTML that the APEX built-in components simply can't handle. Perhaps a complex chart or region that has a lot of custom content and/or layout. While best practices may be to use an APEX component, or if not, build a plugin, we all know that sometimes reality doesn't give us that kind of time or flexibility. While the PL/SQL Region is quite powerful, it still lacks a key feature: the ability to be refreshed by a Dynamic Action. This is true even in APEX 5. Fortunately, there's a simple workaround that only requires a small change to your code: change your procedure to a function and call it from a Classic Report region. In changing your procedure to a function, you'll likely only need to make one type of change: converting and htp.prn calls to instead populate and return a variable at the end of the function. Most, if not all of the rest of the code can rem

Logging APEX Report Downloads

A customer recently asked how APEX could track who clicked “download” from an Interactive Grid.  After some quick searching of the logs, I realized that APEX simply does not record this type of activity, aside from a simple page view type of “AJAX” entry.  This was not specific enough, and of course, led to the next question - can we prevent users from downloading data from a grid entirely? I knew that any Javascript-based solution would fall short of their security requirements, since it is trivial to reconstruct the URL pattern required to initiate a download, even if the Javascript had removed the option from the menu.  Thus, I had to consider a PL/SQL-based approach - one that could not be bypassed by a malicious end user. To solve this problem, I turned to APEX’s Initialization PL/SQL Code parameter.  Any PL/SQL code entered in this region will be executed before any other APEX-related process.  Thus, it is literally the first place that a developer can interact with an APEX p