Programmatically creating bar or column charts in Flex

Creating Flex charts and series is easy if you lay them out in MXML.  But creating them programmatically can be a little trickier.  Here's an issue that's not too difficult to figure out but that I found wasn't documented in any other articles online.

Every chart object holds an array of series objects: LineSeries, AreaSeries, BarSeries, etc.  One day I wanted to create a chart that showed columns of data so I wrote what I thought would be a chart with ColumnSeries.

However, I was stumped when no series showed up on my chart.  Where did they go?

Turns out, all ColumnSeries have to be included in a ColumnSet, and the same for BarSeries in a BarSet.  The *Set objects in turn have another array of all the *Series they hold.  So a chart that correctly houses ColumnSeries would look like this:

Although the idea of having an array of series objects within an array called series on a chart seems a little difficult, it actually makes a lot of sense. The *Series are stored within an overall *Set object because they are grouped together and are able to be stacked. Using multiple *Sets on one chart allows these groupings to stay separate and stacked charts to stack correctly without being lumped together.

Comments

TabNavigator labels getting ellipsed improperly

So the other day I was working with a TabNavigator when I noticed this:

My first tab's label was getting ellipsed! My first thought was that the tab was just too short and needed to be wider. So I made the tabs overly large. Still no luck, now I had a 500px tab that said "In..."

My next thought was that maybe my skin was causing some trouble. After resetting back to the default style I realized that if I moused over the tab with or without skin, the label would show correctly.

It looks as though the TabNavigator occasionally just slips and doesn't validate it's label display. So here's what I did for a quick fix.

This ended up being a pretty simple solution, I just invalidated the size of the tab after it was completely rendered and that took care of the problem. This is a pretty rare occurrence with the TabNavigator but it's a pretty handy fix to have just in case.

Comments

HtmlUnit is not just for unit testing

From the name it may appear that HtmlUnit is only used for unit testing. While it has extensive logging and error detection, it is really a browser client including Javascript and SSL support that can be used along with a testing framework.

A few uses of HtmlUnit without a testing framework would be to:

  1. monitor information from a web site that doesn't have a web services interface. An example would be an internal build servers status page.
  2. automate logging into a website and posting status reports on a regular schedule.

Here are a few tips to get HtmlUnit set up quickly for use in automation.
See documentation on the project page for more info

1. Download htmlunit-2.4.zip off of the project page: http://htmlunit.sourceforge.net/. Or if you are using Maven to build your project, include the following dependency:

    <dependency>
        <groupId>net.sourceforge.htmlunit</groupId>
        <artifactId>htmlunit</artifactId>
        <version>2.4</version>
    <dependency>

2. To turn off Javascript errors from stopping execution, add code as follows:

   WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3);
   webClient.setThrowExceptionOnScriptError(false);

3. To optionally add a proxy server, which can be useful to monitor traffic while debugging with a debug proxy, add:

   if (PROXY) {
       ProxyConfig config = new ProxyConfig();
       config.setProxyHost("127.0.0.1");
       config.setProxyPort(8888);
       webClient.setProxyConfig(config);
       //...

4. To allow bypassing invalid SSL certificates (may be needed for an SSL debug proxy or for accessing sites with expired certificates):

      //...
      try {
	   webClient.setUseInsecureSSL(true);
      } catch (GeneralSecurityException e) {
	   e.printStackTrace();
     }

5. To shut off all the warning messages that are generated by default:
(from the htmlunit docs)

    System.getProperties().put("org.apache.commons.logging.simplelog.defaultlog", "error");

or if using log4j, add the following lines to log4j.conf:

log4j.logger.httpclient.wire=ERROR
log4j.logger.org.apache.commons=ERROR
log4j.logger.com.gargoylesoftware.htmlunit=ERROR
log4j.logger.com.gargoylesoftware.htmlunit.WebTestCase=INFO

Refer to the HtmlUnit project page for info on finding elements, submitting forms, using Javascript, etc.

other pages:
How to write a simple internet/web robot in Java?
htmlunit project page

Comments

Playing Audio Files using the iPhone SDK

Playing sound is a big part of any rich media application and it's very likely you'll need to do it in your future iPhone apps. Before version 2.2 of the SDK this was somewhat difficult -- you needed to either roll your own player or use AudioQueueServices. To use the latter you have to create a bunch of C-style structures and callbacks to feed the data manually byte by byte. It's a very programming intensive process while all you want to do is just load and play a sound!

To remedy this problem Apple introduced a new Framework: AVFoundation and a new audio player: AVAudioPlayer. It has the simplicity you desire but the features you need in order to play your audio files: play, stop, seek, and pause. You can also choose to register a delegate to get certain events like errors in playback, notification when playback ends, and interruption events for when phone calls occur. Pretty cool stuff.

I've written up an example application which is checked into SVN at http://svn.9mmedia.com/public/iphone/audio-example/ but here are the key points:

//Make sure you're building for 2.2 and you are also including AVFoundation.framework
 
#import <AVFoundation/AVFoundation.h>
AVAudioPlayer* player;
...
/* Both these actions are hooked up to buttons in IB */
- (IBAction)startPlayback:(UIButton *)sender {
    if(!player){
        /*
         * Here we grab our path to our resource
         */
        NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
        resourcePath = [resourcePath stringByAppendingString:@"/grabbag.m4a"];
        NSLog(@"Path to play: %@", resourcePath);
        NSError* err;
 
        //Initialize our player pointing to the path to our resource
        player = [[AVAudioPlayer alloc] initWithContentsOfURL:
                            [NSURL fileURLWithPath:resourcePath] error:&err];
 
        if( err ){
            //bail!
            NSLog(@"Failed with reason: %@", [err localizedDescription]);
        }
        else{
            //set our delegate and begin playback
            player.delegate = self;
            [player play];
        }
    }
}
- (IBAction)pausePlayback:(UIButton*)sender {
    NSLog(@"Player paused at time: %f", player.currentTime);
    [player pause];
}

That's all you need to do to play a sound... create a file URL, init, and play! Pausing is done by a simple method call, and you can monitor the location in the song by checking the currentTime property of the object.

Make sure to check out the project to see the delegate methods implemented, and the implementation of the code above! Also make sure to check the iPhone documentation for these classes as well:
  • AVAudioPlayer Documentation (apple.com)
  • AVAudioPlayerDelegate Documentation (apple.com)

19 Comments

UIWebView and anchor links

Today I needed to load a local html file into a UIWebView and jump to a specific section in the file. I thought this is simple enough all I have to do before creating a NSURL is make sure the url string has the correct anchor appended at the end of it. So I tried:

UIWebView *myWebView = [[UIWebView alloc] initWithFrame:frame];
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *stringUrl = [mainBundle pathForResource:@"mypage" ofType:@"html"];
stringUrl = [stringUrl stringByAppendingString:@"#jumpto"];
NSURL *baseUrl = [NSURL fileURLWithPath:stringUrl];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:baseUrl];
 
[myWebView loadRequest:urlRequest];

Unfortunately it turns out that [NSURL fileURLWithPath:stringUrl]; encodes the string converting the # symbol into %23 escape code, which prevents the page from loading. Finally I found the solution by first creating a NSURL variable and then appending the anchor link to it. Here is the code that got the job done:

UIWebView *myWebView = [[UIWebView alloc] initWithFrame:frame];
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *stringUrl = [mainBundle pathForResource:@"mypage" ofType:@"html"];
NSURL *baseUrl = [NSURL fileURLWithPath:stringUrl];
NSURL *fullURL = [NSURL URLWithString:@"#jumpto" relativeToURL:baseUrl];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:fullURL];
 
[myWebView loadRequest:urlRequest];
Comment




  • buy generic viagra
  • buy cialis from canada
  • price of acomplia
  • buy synthroid without prescription
  • synthroid online cheap
  • cialis from india
  • viagra order
  • accutane generic
  • cheapest levitra prices
  • buy clomid online
  • generic cialis cheap
  • buy cialis lowest price
  • buy acomplia
  • buy levitra online
  • cheap cialis pill
  • buy cheapest cialis online
  • discount cialis
  • where to order viagra
  • viagra no prescription
  • order cialis no prescription required
  • soma generic
  • cialis tablet
  • levitra
  • cialis for order
  • order acomplia online
  • cheap generic soma
  • order levitra
  • discount cialis no rx
  • find cialis no prescription required
  • viagra online
  • cialis malaysia
  • viagra uk
  • soma pharmacy
  • buy cheap cialis
  • levitra cheap
  • buy lasix without prescription
  • lasix without prescription
  • order propecia online
  • cheap cialis
  • viagra free delivery
  • viagra overnight
  • cheap clomid online
  • lasix without a prescription
  • viagra online review
  • find viagra online
  • cialis generic
  • purchase viagra
  • propecia pharmacy
  • find cheap cialis
  • cheapest cialis prices
  • buy soma cheap
  • buy lasix cheap
  • cialis overnight
  • sale cialis
  • clomid pills
  • order cialis on internet
  • soma sale
  • cheapest lasix prices
  • viagra no online prescription
  • buy viagra from us
  • buy generic cialis online
  • cheap viagra tablet
  • soma without prescription
  • buy zithromax cheap
  • order propecia
  • buy cheap synthroid
  • find cheap cialis online
  • buy cheap lasix
  • order soma
  • lowest price propecia
  • discount propecia
  • purchase viagra online
  • accutane for sale
  • soma pills