Column charts with negative axis

A really good tutorial can be found here on how to create “clustered-stacked column charts”. It works very well in Excel 2007 also. The result looks something like this:

Clustered-stacked column chart

It took me a while to find how to accomplish this, I googled different variations of “Excel 2007 series values” minus, negative, stacked, “excel 2007 axis scale”, “same secondary scale”, “negative bar color”. I even attempted scripting it, but with no luck.

This chart is used to illustrate why a company is not necessarily profitable, despite that it looks that way first. The profits exceed the costs at a first look (above the zero), but adding costs for personnell and operating expenses, the actual result is negative, as seen above where the multi-colored bar (expense types) is longer than the blue (income).

Splitting string with delimiters

Splitting a string with delimiters, i.e. commas, into an array was easy.

I had an XML-file with multiple possible values for one start value. These would be used to populate a drop down:

<startValue>1</startValue>
<endValue>1,2,3,4,5</endValue>

Alas, I needed to split the endValue property into an array:

var tmp:String = xmlList.endValue.text();
var array:Array = tmp.split(",");
trace(array);

Even division with random numbers

I needed to generate random numbers for even division from a given range. Using the modulo operator (Wikipedia) gives the remainder of a division. This gave me some headache, but I thought I’d share what I found.

var a:Number = Math.round((randomNumber() % 8 + 1)*3);

The randomNumber() method looks like this:

return Math.round(Math.random() * (endIndex - startIndex)) + startIndex;

The above example with the hard coded values 8 and 3 generates numbers in the range 3 to 24, all with modulo 0.

If you would like numbers that can be divided with 5, the following code does the job:

var a:Number = Math.round((randomNumber() % 5 + 1)*5);

So, the maximum number that is generated is specified by the product of 5*5 or 8*3. 5 and 3 specify the denominator.

The quick fix I came up with is to create different variants of these code snippets, and randomly select on. In example, have one line of code for division with 2, 3, 4, 5 and so forth and then randomly select one line to use.