Build a Better Bar Chart Using SVG and HTML

How to Build a Better Bar Chart

NOTICE: Your browser must be able to render SVG for you to see the examples on this page.

Most people looking at SVG are trying to do some sort of charting. Starting off it looks simple but quickly quarks start piling up till your trivial task is slightly more convoluted. Looking over these examples of how you probably progressed I have a final example that allows you to remove some of the bss aackward ness from the process.

Starting with Intuition

You first try probably looked like this. I have actually seed this setup as example code for some D3 tutorials. No, they don’t leave it like this either. This is because it was decided that the layout would follow a screen scanning approach where the upper left corner is the start of the coordinate system. Unlike in Math where graphing starts in the lower left and positive values are up not down, but thats not going to get changed now…

Adjusting for Computer Science

This is where most other examples stop. You have solved the problem though computation. You spend some time thinking about it and come up with a way to calculate the way to place the columns on the bottom of the view. Offset each column by 100% minus its height and you get its correct position. We, however, can do better.

The Solution / Easy Fix

Thinking about what we did in step one, our problem was that the bars were flipped from the way we expected it. There is a transform for that but that’s not our entire solution. If we just flip the bars over the y axis then we will not be able to see them. We instead have to translate the chart down to the bottom of the viewport. After our scale and transform the chart looks like we expect and we don’t have to calculate any offsets and our heights are positive values. A win indeed.

Clean JavaScript consol.log() Shortcut

A while ago I posted a article about how to do something like string.Format() in C# .net. Recently I started using it more frequently for logging and had the idea to extend string again to save time when writing output to the console.

To review the original code looked like this.

String.prototype.format = function () {
    var formatted = this;
    for (var i = 0; i < arguments.length; i++) {
        formatted = formatted.replace(
            RegExp("\\{" + i + "\\}", 'g'), arguments[i]);
    }
    return formatted;
};

You use the code by using curly brackets to define where the parameters will replaced in the string.

//You can fill one value in multiple places
var s = 'This {1} is #{0} !!!{0}!'.format(1, 'blog');
//produces: This blog is #1 !!!1!

I know the mustache library can also do replacement and that _.template (underscore) does similar things. My reason for using this over them is that I don’t always want to pass a full object and seed the keys in my string. This syntax is a little more DRY (Don’t repeat yourself).

So once you have use the above code to extended the prototype for string you can use this next block of code to do the same syntax for logging.

String.prototype.log = function () {
    console.log(this.format.apply(this, arguments));
};

Now logging values out to the console is as easy as…

//Same syntax, just ends up on ur console
'Value of something: {0} / or this {1}'.log(a, b);

Try it for your self on JSBin.com

Personal project updates, github dump.

I’m a professional developer. I’m lucky enough that my job is also my hobby and as such I have tinkered with many side projects. Some of those are merely proof of concepts. Others develop into functioning programs. Those other projects come up from time to time in conversation so I finally figured that I would post them online. Now, besides being proof that they actually exists, I can also get comments on how they were designed and implemented.

SmithWiki

https://github.com/QueueHammer/SmithWiki

A JavaScript based Wiki. Built using jQuery, Underscore, Backbone, Mustache, and Amplify.

In the past I had used javascript wiki called TiddlyWiki. However modern browsers add restrictions to local file modification, disabling any permanent changes to TiddlyWiki. Looking at the idea of making my own I realized that a number of frameworks had been created to make developing such an app easier. Specifically jQuery to clean up working with the DOM, Backbone for managing Views / Local Routes, Mustache for templating, and Amplify for persistent storage.

CC-MineLib

https://github.com/QueueHammer/CC-MineLib

My friend hosts a Minecraft server. Over Christmas one year he convinced me to look into the Turtles Mod. It basically lets you program a block that can move left, right, up, down, forward, and backward to perform tasks. Watching how people were using the “Turtles” i came up with some utility function that I build into a library between Christmas and New Years.

My favorite programs is “atn” or Advanced Turtle Notation. This program took a string of characters to drive the Turtle based on the keys used in first person games; asdw. So to have the Turtle go forward 3 and turn left and go backward 2 your would type “atn wwwass”. Now, that was a big improvement of having to write a custom program for that or call different programs for forward and turn, but it could be better. That’s where the advanced part of the Turtle notation comes in. I made it so you could queue up commands and that it was fully recursive. To go in a circle 4 times you could just write “atn 4(4(wd))”. With the ability to dig and place blocks added in its uses were endless, endless.

Other good programs were fuel, and consume.

Fuel was designed to refuel and display the current fuel of a Turtle. I like it cause of its simple interface. Type the command “fuel” and you see the fuel level, pass in a parameter for where the fuel is in the inventory and it would consume it to refuel the Turtle.

Consume was fun because it leverages the functional nature of Lua. Its not exceptionally hard to write a program to recursively search out the next touching block and consume it, even in three space, but to to it with calls to an function that is a template for an action and pass the steps in as a function was a challenge. I honestly look at it and laugh sometimes as I remember how it works.

WPF 3D Hex Grid

https://github.com/QueueHammer/WPF3DGrid

A WPF 3D demo I made to try out ray tracing to a background element. When you click the tile will change to an inverse color scheme. The tiles are hexes and require that they be laid in an offset pattern. Most WPF / Silverlight examples the view is almost entirely done in markup this I built though code at runtime. I was really excited about property assignment at construction and lambdas at the time so I attempted to make the code look as if C# was a functional language.

Arduino 3×8 Pixel Scroller

https://github.com/QueueHammer/Arduino3x8PixelScroller

The source to a 3×8 pixel LED scroller. Full design and write up is here on this blog!

Regex Design and Problem Solving

There is a joke about regular expressions that goes like this; “Once, when a person was confronted with a problem they though,
‘I know, I’ll use regular expressions’. Then they had two problems”. Unfortunately this is likely true in most cases. Regular Expressions are fairly far from being a common skill. Frequently the harder a person thinks about the problem the more exaggerated and complex the regex becomes. If you take these steps when designing you solution they will make it much simpler.

A complex Regular Expression:
^[a-zA-Z0-9\.]@[a-zA-Z0-9\.]\.[a-zA-Z]{2,4}$|^[a-zA-Z0-9\.]@[a-zA-Z0-9\.]\.[a-zA-Z]{2,4}([;,]\s[a-zA-Z0-9\.]@[a-zA-Z0-9\.]\.[a-zA-Z]{2,4})+$

The Steps

Clearly Define Your Objective

Understand what your trying to do with your regular expression and understand if it’s the correct took for the job. If your trying to match parenthesis then an regex is will not do what you need. Understand how what it is you need to match. This may seem obvious but there can be a magnitude of difference in complexity between the objective of “Match an email address” and “Match a RFC 2822 email address”. Know where you need to draw the line.

For the example we will be trying to validate an email field. The field can contain one email address or more than one email address separated by colons or commas. Furthermore they can be decorated by periods. So “john.smith@whateva.com” is a valid and “Phillip@whateva.com, Margret.T.Meed@otherdomain.com; JonesSmith85@some.other.com” is also valid. Now we wont hold ourselves to the standard for email, just the cases we have above. The email is being entered for the persons own benefit. The validation is just helping out with obvious typos. Know what you need to and don’t need to accomplish. As always when programming it helps to have clearly defined scope.

Break Your Problem Into Pieces

This is true for any problem, programming, regular expression or not. Find the smaller pieces that your problem is made out of and solve it piece by piece. I’ll show this by using substation in my example of the steps. The key is to find the correct parts so that you can design them, test them and reuse them in the larger problem.

So we want to validate “john.smith@whateva.com” and “Phillip@whateva.com, Margret.T.Meed@otherdomain.com; JonesSmith85@some.other.com”. Step one is to see that we are trying to match emails. So lets rewrite this with place holders: “email” and “email, email; email”. Broken down, it’s much less daunting. Now can we break this down any smaller? How about an email it’s self? Think charactersAndPeriods@charactersAndPeriods.domain . This produces two parts that we can reuse and a guide of how to put it all together.

Test the Parts of the Whole

Once you have the problem broken down into smaller parts and regular expressions for matching them, test those parts. Testing and debugging the smaller pieces will be much faster than working out why your larger expression is failing. Especially when your problem is not something trivial.

So now we develop our regular expressions for the problem at hand. This fiddle shows the expressions used for each part and how they were tested.

Put the Pieces Together; Then Test

Take the smaller proven parts of your regular expression and put it together. If they are assembled the correct way and you tested them thoroughly then your final tests should be a much easier.

Once we have the smaller pieces we can put them together to solve the full problem. Because we tested the parts of the whole we can be confident that they will likely work when they are put together. To be sure we test. The resulting full regular expression is not easily read or understood but our parts are. That’s how one ends up solving their problem with a regex and not ending up with two.

Leap Motion Unboxing

What is Leap?

Leap Motion is a cool start up that has basically developed what is a Kinect for your monitor. It allows you to capture gestures from your hands to manipulate the screen. It turns any screen into a touch screen with out all the messy finger prints!

Why Me?

Best of all of this, they have a developers program. For anyone who was selected they would send out free dev units. When I was accepted in the developers program I was very excited! I was notified that soon I would have a leap motion sensor sent to me to experience this amazing technology first hand. So here goes the unboxing.

The Box

LeapBox
The box inside the box, the one that it was shipped in, was black with the company name stuck to the top. A little industrial but this is a dev-kit after all.

Inside the Box

DevelopersMessage
The first thing that you see when you open the box is a message folded into a slim pouch. Its from the President of the company explaining the hopes of the development program. The strength of the company is in its developers. They see the development program as key to the next killer app that will drive the growth of the company.

In the Bag

UnitInBag
Under the first layer of protective foam is the device and USB cable in their respective bags.

The Leap Capture Device

ShinyNew
The unit is smooth, shiny, metal and glass. Very iDevice-isque. If you look carefully you can see three LEDS under the glass. Most likely to be used in poor lighting conditions.

The Details

Bottom
The bottom of the device is made of non slip rubber. If you look you can see the company name etched in the bottom. It’s little details like this that show they took their time in the design of the whole product.

Ports

Ports
The device allows connection though USB2 or USB3. The USB3 ports allow for some amazing capture rates on the device. Up to 240 fps! Unreal for hand gestures. I imagine it’s intended for an industrial setting.

Connector

USBCable
They include a USB2 cable to connect the device. This one is very pretty but just slightly flaky. They already knew about this by the time mine arrived and they were sending out replacements to anyone who wanted one.

Next?

I’ll post more about the using the unit when I get it setup and running. Reading over the API while waiting for the unit to arrive I have found it very easy to understand. Can’t wait to see how it works with real data!

Intro to Backbone Routing

Routing is a method of triggering state. In your site you have paths that bring a user to different pages that provide specific content. Routes are just like that but for a single page. They provide a way to trigger a specific state on that page. A good example is search.

Assume that a page may shows a list of items the user has ordered. For that the path could look like “www.mySite.com/user/orders/2012/”. We assume there is a view in that page that will show the details the orders that match a search term a modal. The page could have script that searches though the data, using Underscore.js possibly, to produce the select orders and then launch a modal to show them. A route can be the trigger to perform the search and show the modal. Afterward the route in the app can then be used by the user to book mark the result with out the general mess of a search/query string in the URI. The resulting URI would look something like this “www.mySite.com/user/orders/2012/#search/orange” for the users search for orders with the keyword “orange” in them. As a application gets more complicated in its views and states routes become a good way to trigger and return to specific sates in your application.

So how do you get started with Routes?

First start to extend the route class:

var PageRouter = Backbone.Router.extend({});

The route extend function takes a hash as its parameter. The hash has key with all the routes and keys for the functions the routes will call when they are matched:

'routes': { //Key for routes followed by the route map
    'pathOne/someThing/:aValue': 'RouteOne', //path and trigger name
    'pathTwo/*everything': 'RouteTwo'
},
//Each trigger name and action for the path mapping
'RouteOne': function (aValue) {
    //Do Something
}

The last part is to create an instance of your router and then to start the backbone history module.

var pRte = new PageRouter();
Backbone.history.start();

Navigating to a route will trigger the function its bound to to run. The function will be passed all the keys defined in the path as and parameters. Key can be either specific parts of the path or splats of what ever is left. Keys are defined like “reports/:year/:month/:day/” where the function that is called when that path is matched could have the parameters (year, month, day).

The last thing to note is that routes are matched from first to last. So if you define “search/ducks”, above “search/*term” the latter will never respond to a path of “#search/ducks” because the first route will catch it. That being said making the last route “*anything” is a good way to catch all the routes that don’t match anything else.

Introduction to Backbone

Backbone is a framework aimed at making some of the higher level design patterns in web app development easier and the code cleaner. From the projects page:

Backbone.js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface.

So, yeah, it dose all that. Here’s what it means in English as a developer. The content on your page, the list of contacts, groups of images, descriptions of places or events. All of that is what make up the views. There is also data that populates those views. That data is either rendered into your page or its requested and then pushed into your views. As the views change or as users interacts with you app you provide a way to return to the state they have left it. Backbone addresses all of that and makes it simpler to do and easier to maintain.

There roughly there are 4 major classes in the framework that you will extend to use those features in your app. There are more, like Events, and a few others but I’m not planning to cover those at this time.

  • Router - Allows uses to save and return to your app in a specific state. e.g. Viewing a specif product from a larger list or a search view loaded with their results
  • View - The views for the data, using templates, and the events that you bind for interaction
  • Model - The data that is pushed into the views. Triggers can be defined for get, set, changed, fetch, and save.
  • Collection - Is a list of Models it has add and remove events that can be defined as well as a change event that responds to any Model in the list. The collection can also be build from calling a RESTful target.

Backbone is really the library that enables you to graduate from static single views between server requests for updates or refreshes to a dynamic web application running client side that calls your API for updates and to commit changes. I will provide direct links to each post on these classes here.

Backbone Routing


The Ternary Operator as If / Else Shorthand

In my last post i created the following piece of code to group names from a list into a hash by their first letter. Looking at I though about how it could be reduced to something simpler with the Ternary Operator. I left it be for the time since I wanted to only talk about one concept at a time. Now that there is time, here it goes.

Ternary Operators

I have talked briefly about ternary operators before. They work basically like this.

//(test ? value if true : value if false)

//Usually used like this
var someVar = (10 % 2) == 0 ? "Even" : "Odd";

Thoughts

That is the pattern you almost always see. So I looked at the code in the last post realized, if I don’t use it in an assignment statement it’s really close to if / else. So the I took it one step further. If not assigning a value the result from the “value if true” and “value if false” statements don’t matter either.

Result

//The Original
_.reduce(
    list,
    function(output, name){
        var lCase = name[0].toLowerCase();
        
        if(output[lCase])
            output[lCase].push(name);
        else
            output[lCase] = [name];
            
        return output;
    },
    {}
);

//Could be rewritten to
 _.reduce(
    list,
    function(output, name){
        var lCase = name[0].toLowerCase();    
        
        output[lCase] ? output[lCase].push(name) : output[lCase] = [name];
        
        return output;
    },
    {}
);

It’s a narrow call on which one is more readable or justifying the revision by it being fewer lines. However I think it’s was a good though process to realise false constraints in the use of the ternary operator and find a application for it where with out those old constraints.

Grouping Items in a List Using Underscore and Reduce

List comprehension functions make solving problems with groups or lists much easier. The two most basic of the functions are Map and Reduce. From these two you can build any other operation you want. Luckily, most times you don’t have to and functions that sort or group will already exist.

A word on Underscore ‘_’

Underscore is a library for JavaScript that provides these basic list comprehension function that are native to most other functional languages. It can be used in a functional sense where you call a reduce, filter, find, etc and pass in the grouping or list you want to process. Underscore also extends the base JavaScript objects so you can use a syntax that may feel more natural to people who are used to object oriented languages. Other features of the library include tests or base object types and even a basic templating system which though really nice are not related to the rest of this post.

How about Map and Reduce

So back to the two basic functions that I mentioned earlier. Map is a function that will apply an operation to every element in a set, this can be a list of values or a collection of key value pairs. Imagine it like a station on a assembly line where it paints each part that comes down the line. Reduce is different in that it takes a collection and will return a single value. This is useful when you have a list and you want to find the largest number, it’s sum, or to test if a value exists in the collection. Reduce takes the first element, does something with it and then takes the result and looks at the next element. Think about that when computing the sum. You take one value add it to your total then take your new total and look at the next value, over and over, till you are done. There is one caveat with Reduce that need to be mentioned though, and, it’s that is it can return a collection as it’s one result.

To Group By Reduce

So Working with Reduce for fun I wanted to break a list into groups. For the record Underscore has a function that will do this .groupBy() and Reduce is supported in JavaScript in most of the Modern Browsers without needing Underscore. For this that doesn’t matter, cause, is for fun. :)

Sort Name by First Letter

For this example we are going to work with a list of names. Our goal will be to take those names and group them by the first letter in the name.

var list = 
    ["Dan", "Wayne", "Steph",  "Marc", "Duchess",
     "Deric", "Crystal", "Alex", "Wesley", "Dort"];

There are two ways we can use Underscore to reduce. We could just call .reduce() on our list since Underscore extends all our lists with its extra features. The other way is to call it from the library its self.

//Underscore extends all lists
//(Now a standard list function in modern browsers / html5)
list.reduce();

//Or on the Underscore object
_.reduce();

When calling reduce from the Underscore object we need to pass the function three parameters. The list to work on, the function to perform on each element, and the starting value for the result. The last param seeds your working function, after the first iteration that value will be the functions result. You can see this in the exampel from the underscore documentation.

//The result will be 6
_.reduce([1, 2, 3], function(memo, num){ return memo + num; }, 0);

Now like I said above the function can return a list instead of a single value. For that matter it can return a hash. Now when that’s the case your seed should be the same type. What our reduce function will do is to check a hash for a key that’s the first letter of the current item. If it exists it will append it to that key, other wise it will create a new key with our item as the first value in a list. This will then produce a sorted set of names.

_.reduce(
//Our input list
    list,
//The function is called on each item in the list
//The first parameter is the value the function returned
//    for the previous item
    function(output, name){
    
        var lCase = name[0].toLowerCase();
        
        if(output[lCase]) //if lCase is a key
            output[lCase].push(name); //Add name to its list
        else
            output[lCase] = [name]; // Else add a key
            
        return output;
    },
//The seed for the first time the function is called
    {}
);
/* Returns
Object {
    a: Array[1]
    c: Array[1]
    d: Array[4]
    m: Array[1]
    s: Array[1]
    w: Array[2]
}
*/

You can see quickly that how useful this function can be. I’m not going to go back in to Map / Reduce but the reduce concept is a good tool to have when trying to solve problems. Furthermore Underscore is a good tool kit to have when your solution involves working with collections in JavaScript.

One Query to Inner Join or Left Join Based on a Flag

I ran into a situation today where the issue at hand was to I had to update a query from a Left Join to an Inner Join. One concern was that some of the consumers of the query needed the Left Join while others now needed an Inner Join of the data.

create table One (
    id int,
    noun vchar(5)
)

insert into One
values 
    (1, 'Car'),
    (1, 'Bus'),
    (1, 'Bike'),
    (2, 'Boat'),
    (3, 'Book'),
    (4, 'Sock'),
    (5, 'Rock')

create table Two (
    id int,
    verb vchar(10)
)

insert into Two
values 
    (1, 'Rolls'),
    (2, 'Floats'),
    (3, 'Reads...?')

select o.noun, t.verb
from One o
left join Two t
on o.id == t.id
where len(noun) = 4

The Obvious Solutions

There are a couple of solutions that come to mind first off.

  1. Create two procedures, One with an Inner Join and another with an Left Join.
  2. Modify the procedure to take a flag and switch between the two.

Two Separate Procs

The first solution is probably the worst. You not have nearly the exact same code in two places. Where the odds are that in the any future changes to either procedures will not be propagated between them. Likely causing an bug or unintended side effect.

--Proc One
select o.noun, t.verb
from One o
left join Two t
on o.id == t.id
where len(noun) = 4

--Proc Two
select o.noun, t.verb
from One o
join Two t
on o.id == t.id
where len(noun) = 4

If Not One Then the Other

The second solution is better because it keeps the code in the same logical place. That way if you update one you will likely think: “Hey, maybe I should update that select too”. Still, it is essentially the same code in two different places, be it in the same proc.

declare @flag bit
select @flag = 0

if @flag
begin
    --Proc One
    select o.noun, t.verb
    from One o
    left join Two t
    on o.id == t.id
where len(noun) = 4
end
else
begin
    --Proc Two
    select o.noun, t.verb
    from One o
    join Two t
    on o.id == t.id
where len(noun) = 4
end

We Can Do Better

So the next two things that came to mind were

  1. Take the results from the first select, the one with the Left Join, and using an “if” block select the rows in the result with out the nulls produced by the left join.
  2. Use some sort of logical selection in the query clause or a case statement to auto-magically switch between the two behaivors

Now the first option is not a bad solution. Given you know that the data set will always be small and that the cost of the second query is negligible in CPU and the memory.

Select “if / then” select again

declare @flag bit
select @flag = 0

select o.noun, t.verb
into #tempJoin
from One o
left join Two t
on o.id == t.id
where len(noun) = 4

if @flag
begin
    select *
    from #tempJoin
    where verb is not null
end
else
begin
    select *
    from #tempJoin
end

But…

But…

We Should Do Better

Usually any solution that involves “auto-magically” can be flagged right off as impossible, impractical, or a “bad idea” but something about the solution… if possible… seemed like it could be elegant, concise, and practical.

The answer lies in the select “if / then” select again solution. We have a query that returns the results we need in both cases. In one of those cases we just don’t need all of it and we have a if statement select out the nulls returned by the left join.

That logic of if “flag” then “remove the nulls” just has to be moved into the query. Typically there are three ways to do that.

One is to move the logic into a case statement. We could use the case to omit rows when they don’t match what we want but that would produce nulls. Again, what we don’t want.

The second and third are to embed the logic into the Join or the Where clauses of the statement. For the final solution both work but I chose to use the where clause because I believe it was more readable and simpler for other maintainers to understand.

So when you think about it you want to remove the row or keep the row based on the flag. The first thing you should think is AND. You want to mask the data or keep it so an AND will do the job. The next part of your logic will be to match what you want to filter out. That filter with the AND will turn your Left Join into an Inner Join. With that solved you have to find a way to switch that behavior on and off. The way to do that is to use an OR, not at the same level as your AND but inside with the filter. To ignore it, filter or true, or use it, filter or false. If your having trouble following that read though the code and look though the comments.

Both Left and Inner Join in One Select

select o.noun, t.verb
into #tempJoin
from One o
left join Two t
on o.id == t.id
--Our original where clause
where len(noun) = 4
--AND to filter
and (
    --This is our filter; remove nulls
    t.verb is not null 
    or
    --When this is true the filter is ignored
    --When this is false the filter works
    @flag = 1
)

So that, in a nut shell is it. You can switch between a Left Join and an Inner Join using a flag using only one select statement and no if blocks or case statements. The real advantage is that there is no code duplication across procedures or inside the proc it’s self.