Archive for the ‘JavaScript’ Category

Custom colors for Ext.chart.*

Thursday, October 8th, 2009

One of the main reasons I like ExtJS more than YUI is the documentation – I happen to find the ExtJS docs much easier to understand, though there’s a number of people who disagree with me.

With the release of ExtJS 3.0 earlier this year, developers can now create and use charts of various kinds. The ExtJS chart classes (Ext.chart.*) are basically extensions of the YUI charts – which has made my life difficult because neither YUI nor ExtJS does a fantastic job documenting how to use them. To be fair ExtJS has done a great job updating their 3.0 docs with the help of the community (charts included), but the chart documentation is still far from ideal.

My biggest beef with the Ext.chart.* docs has to do with the ability to change the default colors. While some references on how to do this can be found on the ExtJS forums and by searching Google, no solid examples exist as far as I can tell in either the ExtJS or YUI documentation. I only stumbled across the solution to this problem while searching Google.

The key to changing a chart’s colors lies in the “series” attribute of a given chart. This config option takes an array of Object literals defining each series on the chart – things like “displayName” and (most importantly) “style”. The “style” attribute has a “colors” property which takes an array of color values.

For example, let’s create a Pie Chart. A Pie Chart will only have one series, and doesn’t need much defined for the series’ attributes (it really only needs the “style” attribute to define the colors we’re going to use).

Ext.chart.PieChart() with custom colors

As you can see, the series style defines colors for the Pie Chart in the order they’re going to be used. Since our data store contains three records, “Pending” will get a blue color, “Passed” will get a green color, and “Failed” will get a red color.

See an example implemented within my ExtJS Unit Testing demo.

ExtJS 3.0 Themes

Thursday, August 27th, 2009

One of the best improvements offered in ExtJS 3.0 over previous versions is the new CSS structure. JC Bize (an ExtJS Development Team member) had an excellent presentation at the 2009 Ext Conference discussing what had changed and offered some tips on how to build your own themes. I recently migrated my application from 2.x to 3.0, and I’d like to share my experience with creating a new theme.

In 3.0, the CSS data is divided into two logical groups: structural and visual. This makes creating custom themes much easier than it had been in 2.x, as the developer (in most cases) isn’t interested in changing structural CSS definitions. By isolating the visual CSS, developers can play with colors, fonts, and other presentational elements without worrying about breaking their application.

ExtJS 3.0 CSS Theme Structure

The only problem with the improvement is that it left many of the cool 2.x templates behind. New CSS rules prevent some things from rendering correctly – though which “things” depends entirely on which template you might have been using.

Searching the ExtJS message boards, you’ll notice a handful of 3.0 templates – but not an overwhelming number. To save you some time, I compiled a small list:

Of course, the standard “blue” theme comes with 3.0 as a default.

I had been using the Slickness Theme for my application, and I was dreading the thought of having to give it up as I migrated to 3.0. Much to my surprise, the Slickness theme (built for 2.x) works almost perfectly in 3.0! I had to make a few minor CSS adjustments (available here), but here’s what my customized Slickness theme looks like:

Custom ExtJS 3.0 Theme

If you’d like to try out my customized Slickness theme, simply include the “xtheme-slickness.css” file (as you would for any theme), but also include my “xtheme-symphony.css” file AFTER the Slickness CSS file. This way, my CSS styles override the necessary parts of the Slickness theme.

I also attempted to create my own custom theme for ExtJS 3.0 – though I’ll admit it didn’t look nearly as good as the Slickness theme. I basically fake my way through Photoshop, so editing the CSS sprites was a pain in the ass for me. All things considered though, I had a fully customized theme in under 4 hours of work. If you’ve never worked with CSS, 4 hours for a fully customized theme is pretty quick. In 2.x, it probably would have taken days.

Anyone want to tackle creating their own theme for ExtJS 3.0? Here’s the basics:

  1. Open your ExtJS 3.0 folder, and go to /resources/css/
  2. Make a copy of “xtheme-blue.css” and name it whatever you’d like. (e.g. “xtheme-mytheme.css”)
  3. Go to the /resources/images/ folder.
  4. Make a copy of the “/default/” folder and name it whatever you’d like. (e.g. “/mytheme/”)
  5. Open your new CSS file.
  6. Find/Replace all references to “../images/default/” with the path to your new images folder. (e.g. “../images/mytheme/”)
  7. Include your new CSS file in the header of your webpage.

At this point, you have a new theme… albeit an exact copy of the standard “blue” theme. Now it’s just a matter of changing colors in your CSS file and Photoshop-ing the images in your new “/mytheme/” folder.

Granted, I’m making this sound easier than it actually is. Photoshop-ing the CSS sprites and background images is still a tedious process – and if you’re not a Photoshop wizard, it can be pretty frustrating. That being said, I’ll leave the rest of the details as an exercise for the reader. . . besides, the whole point of making a custom theme is to do whatever you want with it. My input beyond this step is really irrelevant.

If anyone out there attempts to create their own theme I’d love to hear your thoughts!

JavaScript Inheritance

Wednesday, July 22nd, 2009

Over the past few weeks, I’ve been doing a lot of reading into how JavaScript inheritance actually works. I’ve known for some time that objects in JavaScript inherit from this magical thing called “prototype” but I never fully understood what was really happening under-the-hood.

With more than nine million Google search results for the phrase “javascript prototype inheritence” you would think that it would be easy to find an explanation that was both (1) easy to understand and (2) technically correct. In this case, you’d be wrong.

Douglas Crockford has an explanation which is technically correct but uses examples which aren’t very clear (in my opinion). Wikipedia has a nice generic explanation of prototypal inheritance, but it’s not JavaScript specific and has no technical examples.

After a good deal of searching I found a blog post by Sergio Pereira that was far and away the best article I read. While technically spot-on, Sergio’s diagrams and example code set his post apart from every other article I’ve read. I highly suggest subscribing to his RSS feed as Sergio also has a host of other great JavaScript tips.

Ext-Doc: I’m now a project member

Thursday, July 2nd, 2009

Over the past several weeks, I’ve been evaluating a new JavaScript documentation tool called Ext-Doc. I wrote a post about my experience with it shortly after I downloaded the source code, and I have the highest recommendations for this tool.

After speaking with oxxxymoron (the project owner, aka Andrey Zubkov) I have agreed to join the project and help update the Wiki documentation. Granted, this isn’t a glamorous job – but I think every JavaScript developer should use Ext-Doc. In order to attract more users, the Wiki desperately needs updates as it currently isn’t that helpful.

Over the next few weeks, I’ll be adding examples and explanations so that using Ext-Doc is easier to get running.

Ext-Doc: ExtJS-style JavaScript Documentation

Thursday, June 25th, 2009

A while back, I posted about my struggle to find a good JavaScript documentation tool. Over the past week, I began exploring one more option which is very promising.

Ext-Doc is a JavaScript documentation tool which creates by far the best looking documentation. Created and maintained by several members of the ExtJS community, Ext-Doc outputs your documentation using a template mimicking the ExtJS documentation. The tool is compatible on both Windows and Mac/Linux (it runs off Java) and fairly easy to setup.

So far, I’ve only encountered three issues:

  1. The instructional Wiki on Ext-Doc site (hosted on Google Code) is simply terrible. It’s clear that the focus has been on developing the tool itself, but there’s close to zero documentation supporting it. If you need help, you have to search the ExtJS message boards.
  2. The documentation output by the tool cannot be viewed locally – it must be uploaded to a server as it relies on Ajax.
  3. The documentation tags are different from some of the other JS documentation tools

The first issue is obviously frustrating, particularly as the Java JAR file will hit errors and give you no reason why anything fails. Searching through the ExtJS forum and talking with the community has helped me get started, but this is a huge flaw and is (in my opinion) preventing Ext-Doc from being the defacto JavaScript documentation tool available. If they can get their Wiki up to date then this tool will certainly make an impact.

The second issue is actually pretty easy to work around on Microsoft Vista. Since Vista comes packaged with IIS7, you can simply tell the .BAT file to output the documentation to your local host’s root. This way, you can update and view your docs without having to copy/paste or FTP files to a web server.

Lastly, the tags used to document your JavaScript code are different in Ext-Doc than in JSDoc Toolkit (which is what I had been using). I don’t know if either tool is actually consistent with documentation standards in JAVA, but I wish that all of these JavaScript tools would use the same set of tags. It would make testing other tools much easier, and consistency in documentation is really important. Douglas Crockford (if you’re reading this), can we PLEASE get this discussion in front of the ECMA Standards board?

At the very least, I’m biased towards this new Ext-Doc tool (not really so new, actually) because I use ExtJS and I love their documentation. Once I get my tags updated, I think I’ll be very happy with this tool.