mandag den 29. april 2013

Javascript "static" class - make javascript code more readable

I usually use static classes for my C# helper-functions, when there're no properties to expose, nor any need to inherit from them. So this increases code readability. Javascript, however, doesn't have that feature, so how to immitate it? I usually do the below:

// declare object - recall that 'function' is an object in javascript
var WebServiceFunctions = function() {

// declare method on the object
WebServiceFunctions.webServiceMethod = function (parameter1, parameter2) {
// method code here

// return value if any
return xyz;

With the above code, I call my helper-method in my code like this:

var resultFromCall = WebServiceFunctions.webServiceMethod("foo","bar");

This has the odour of a static class. Of course behind the scene the "var WebServiceFunctions = function() {}" declaration signifies the creation of an object ("function" is an object in javascript), but I find that given the generic name - 'WebServiceFunctions' - my mind abstracts from this.

The javascript increases in readability, which may be an issue with a multitude of referenced javascript files.

There's a slightly better way, though. If the helper-functions are so generic in nature that they're applicable to many different purposes, they may be used in many different files. As such, the above will declare the "WebServiceFunctions"-object with every import of the javascript file. Better would be to check beforehand if the object has already been created, in which case there's no need to create it again:

(function () {
    this.WebServiceFunctions = this.WebServiceFunctions || {};

    var ns = this.WebServiceFunctions;

    ns.getMobilityPeriodId = function (serviceUrl) {
             // function code here


The above this.WebServiceFunctions = this.WebServiceFunctions || {}; is a conditional that checks if this.WebServiceFunctions has any value, in which case the already created object is returned, or - represented by the two pipes '||' - a new object is created. var ns is merely a reference, to avoid having to write 'this.WebServiceFunctions' repeatedly.

So that's a way to reference javascript in a 'namespace' sort of way. I find the above helps me keep my references handy and maintain the readability of my code, especially as a project starts to become clotted with javascript. If you wish to go depper into the subject matter, the above is what is known as the Javascript Module Pattern.

tirsdag den 23. april 2013

Simple jquery stacked bar chart

One way of doing an easy and simple stacked bar chart is to style a DIV to a desired length, then have its left-border represent the second data range. If you need more ranges you can always stack several DIVs on top of each other.

Here's example HTML for the graph itself...:

<div id="Graphs">
<h2>How are we doing?</h2>
<h4>&nbsp;Exchanges at a Glance</h4>
<p style="clear: both; padding-top: 1em;">
The below bar charts indicate, percentage-wise, the total exchange grant as compared to the allotted application grants.

And here's the annotated javascript to add the data as stacked bar charts:

// dummy example data
var exchangeGraphData = new Object([
{ allottedGrant: 70, totalGrant: 90, exchangeName: "exchangeNo1" },
{ allottedGrant: 30, totalGrant: 40, exchangeName: "exchangeNo2" }]

// render it
if (exchangeGraphData != false) {

// get width of the graph container. This will ensure our stacked bar charts do not exceed the container width limitation
var widthOfMainBar = Number($('#Graphs').css("width").replace("px", "")) - 16 /* 1em */;

// iterate over the example data
$.each(exchangeGraphData, function () {

// add a stacked bar chart for each 'record'
var div = document.createElement("div");
// add a class-attribute to the bar chart, so as to enable styling it
div.setAttribute('class', 'mainGraphBarDiv');

// add the bar chart to the container

// establish fill-percentage for 'sub-bar', i.e. the second data range
var fillPercentage = (Number(this.allottedGrant) * 100) / (Number(this.totalGrant));

// translate the fill-percentage into actual pixels
var fillPixels = widthOfMainBar * (fillPercentage / 100);

// ... and apply those pixels as a left-border to the bar chart = fillPixels + "px solid #00f500";

// lastly, subtract the same number of pixels from the bar chart's width, or the added border will extend the bar chart unnecessarily = (widthOfMainBar - fillPixels) + "px";

// apply a little animation, if so desired

Below is my CSS for the 'Graphs' contaner and the 'mainGraphBarDiv' elements:

#Graphs {
position: relative;
float: left;
width: 350px;
text-align: center;
margin-top: 5em;
border: 1px dotted white;
padding: 16px;
background-color: gray;

.mainGraphBarDiv {
color: #0d0d0d;
text-align: center;
display: none;
height: 3em;
margin: 1em;
background-color: #92ff92;

The above HTML, javascript and CSS renders like the below:

How are we doing?

ERAMOB-2012-041 Exchanges at a Glance

The below bar charts indicate, percentage-wise, the total exchange grant as compared to the allotted application grants.

tirsdag den 16. april 2013

iTextSharp - multi-line form filling

For those of us using iTextSharp to generate dynamic PDF documents, one caveat I found was in controlling line breaks. In as much as the PDF standard has supported RTF since version 6.1, I was trying to add line breaks via RTF-code - "\par", i.e. Yet it turns out the iTextSharp parser appreciates the ever reliable System.Environment.Newline.

So format your PDF form field thus...:

... and you'll be able to reference your field and add line breaks thus:

PdfReader reader = null;
AcroFields af = null;

PdfStamper ps = null;

reader = new PdfReader(new RandomAccessFileOrArray(Request.MapPath(@"~/App_Data/your_pdf_document.pdf")), null);

ps = new PdfStamper(reader, stream);

AcroFields af = ps.AcroFields;

af.SetField("Your PDF form field name", @"This line is " + System.Environment.Newline + "broken");

// make resultant PDF read-only for end-user
ps.FormFlattening = true;

// forget to close() PdfStamper, you end up with
// a corrupted file!

That's all there is to it.

onsdag den 3. april 2013

Having an astable trigger a monostable

I wanted to have an astable pulse trigger a monostable, using the 555 timer. The main issue here is in that the trigger-pulse that's sent to the monostable 555 can't be longer than the output pulse, unless you add a 'trigger guard' of sorts.

The below schematic does the trick:

The 555 on the left is the astable, it pulses every approx 4-5 seconds. Its output is sent to the input of the 555 to the right, the monostable, which pulses for a significantly lower duration as opposed to its input pulse.

This could be used for an egg-timer, for example, where a variable resistor sounds a buzzer at a selected interval.