Archive for the ‘JavaScript’ Category

Library for easy sparklining

Wednesday, December 12th, 2007

I’m actually not planning on writing much about charts and graphs — but since we’re prototyping the back-end of Nosco’s prediction market application I’ve been reading a lot about the subject. For big and interactive charts we’ll be using either YUI Charts or some native Flex stuff depending on the level of ambitiousness, but John Resig’s JavaScript Sparklines demonstrates how to do something a little simpler in a much, much simpler fashion.

Basically, you insert an html element containing the data for the sparkline — and then the library does the rest for you by creating a <canvas> element and drawing the sparkline:

<span class="sparkline">10,8,20,5...</span>

Very, very cool… Although the library doesn’t seem to support baselines and highlighting of the min and max values, which takes some of the coolness out of the idea of sparklines.

Remember to use excanvas as well to emulate canvas support in Internet Explorer.

Custom select boxes with JavaScript [Work-in-progress]

Sunday, December 9th, 2007


It’s sunday and I’ve been looking for something reasonable to do while not actually working. So I though I’d address an issue we’ll be having at 23 within the next month or so. We want to let people choose their language from a drop-down select box, and we want each option in this box to have a flag icon representing the language. Such specific styling isn’t doable through plain HTML, so custom code is needed…

(We’re certainly not the first to address this issue, and a lot of people have begun to include custom select boxes in their web applications. Most notable in this crowd is Google in Google Reader and Gmail.)

I’m still a few hours from a result that’s usable in the real life, but the first prototype demonstrates where I’m going. Already the JavaScript and the accompanying stylesheet is well-commented, but since it seems like a great example of advanced web scripting in action I’ll be back with a detailed walk-through soon.

» See it in action

Charting is a nightmare…

Saturday, December 8th, 2007

It’s nice to see Google entering the scene for easy chart building — along with Yahoo’s Chart library and new JavaScript/Canvas-based stuff like Ole Laursen’s Flot. Frankly, I’ve been postponing doing a pretty chart based reporting tool for Nosco’s prediction markets for quite a while now (we do build nice charts for the web app though) but all of this could be becoming easier.

I’m still not jumping deliriously up and down. My first experiments with YUI Charts were mixed, and for everything it lets you do there’s two or three things out of reach. The same thing probably goes for Google’s hosted solution and it’s worth approaching the service with a bit of skepticism.

Charting with YUI

Wednesday, December 5th, 2007

Yahoo’s web programming framework, YUI, is out in a new version, which frankly wouldn’t be too exciting if it wasn’t for the new Charts library. Basically, quite a few people have been experimenting with doing charts and graphs with on html canvases (including myself). This method can work well for specific purposes, but it certainly doesn’t scale well, and nobody has come up with a good general library for all charting needs yet.

And the YUI team hasn’t either. Instead, they’ve taken a much more interesting approach by building a JavaScript library for accessing Flash’s (or Flex’s?) Charting engine. Hopefully (in time) we’ll get all the power of Flex Charting through YUI.

Update: Apparently, this is a joint project between the Flash and JavaScript teams at Yahoo, so the Flashy people have more info on the YUI utility.

Updated update: It also turns out that this isn’t based on Flex Charting, rather it’s built on top of a Yahoo-specific Flash library. This probably makes it less interesting?

Cross-browser hovering [Howto]

Tuesday, November 27th, 2007

The CSS :hover pseudoclass is nice and allows you to easily modify behaviour on web page elements when user moves her mouse, and pretty much every web programmer has written something like this in her stylesheet:
a {text-decoration:none;}
a:hover {text-decoration:underline;}

And all these programmers have found that CSS hovering doesn’t work well in Internet Explorer when it isn’t done on a straightforward link. For example this will not have the desired effect:
a:hover div {border-color:red;}
div:hover {font-weight:bold;}

With some light scripting however you can make you page work this way. Basically, make sure that an elements receives the class name .hover when the mouse moves over it; and that the class is removed when the mouse moves away. If you enable such behaviour you’ll see that this works on across different browsers:
a.hover div {border-color:red;}
div.hover {font-weight:bold;}

The code to set up this magic is simpe (example in Prototype)…

function makeHoverable(el) {
  // Setup mouseover and mouseout events to add and remove the 'hover' class
  Event.observe(el, 'mouseover', function(){el.addClassName('hover');});
  Event.observe(el, 'mouseout', function(){el.removeClassName('hover');});
}
// When the document is ready
document.observe('dom:loaded', function() {
// ... we loop through all element with the class name 'hoverable'
$$('.hoverable').each(makeHoverable);
// ... and all links to make them act nicely
$$('a').each(makeHoverable);
});

Now finally add the class name hoverable to all the elements you want to use this new behaviour for:

<div class="hoverable">
  I'll be bold when you move the mouse over me.
</div>

» See it in action

If you plan to use this method along with complex CSS positioning you should be aware that mouseover and mouseout sometimes behaves strangely when applied to two elements on top of each other. In this case, you could try to combine mouseenter and mouseleave for robust IE support with CSS pseudo-classes to support everything else. It might work.