Category: HTML5

  • Essential LAMP tools

    Essential LAMP tools

    For the last time am I going to utter the words ‘I wish I’d found this earlier’. There are so many great tools out there for developers that it can become overwhelming. I used to just accept the flaws in my text editor or database client. Often telling myself ‘it’s better the devil you know’. Recently, however, certain frustrations have caused me to reassess the tools I am using. At this current point in time, I have reached a state of LAMP developer nirvana! This article is to help you reach the same higher state of consciousness!

    OK, few points to know before we kick off. I am very tight with my cash. Despite this, I am a keen Apple advocate (maybe the two traits aren’t entirely unrelated). The tools I’ll talk about will be for OSx but I’ll try to mention some Windows tools along the way.

    MySQL
    Free GUIs for MySQL have come a long way since the free ones provided by MySQL. The latest incarnation is Workbench. The killer functionality here is the ERR modelling creation tool. You create your database, tables, initial inserts and relationships. Hit forward engineer and hey presto! Your schema is all up and running. And you also have a fully documented schema. As a habit, I try to add comments to all tables and some columns. This diagram process is one of my first tasks for any new project. The query editor for Workbench doesn’t include a decent auto-complete functionality. An essential feature if you ask me. For this try Sequel Pro. It’s snappy, free and highly intuitive.

    Text editor
    OK, the most delicate of subjects. I’ve never really used a full IDE to develop but I hear Eclipse is the front-runner on this. I had been recently using Komodo edit which is a great free text editor. My highlight feature on this is the scanning of other PHP DocBlock comments. Within your current file, it sees you’re calling a method from another class (in another file) and tells you what parameters the class expects. Pure black magic. My only gripe with the editor was that the shortcuts weren’t intuitive and some just didn’t work. I’d found Textmate was better for this so gave Textmate 2 a try. They’ve really dropped the ball on this. Textmate 2 is awful! That’s when I found Sublime Text. Sublime Text will cost you around £45 but it’s worth every penny. Once package control is installed you have a world of useful plugins at your fingertips! For Windows, I always find Notepad++ a decent free editor.

    Source control
    I’ve used CVS and Subversion. Both of which are quite similar. Recently, I’ve made the switch to GIT and I am amazed by it! I’ve implemented a GIT flow approach which means I have a solid workflow which doesn’t interfere with other feature branches on the same code base. Switching to different features is so easy and still makes me go ‘wow’ when I do it.

    Linux/Apache
    I work on the mac mainly as it’s a UNIX-based operating system. This allows me to use a lot of the Pre-built commands available with my personal/work Linux servers. The terminal app is very good on the Mac and likewise Putty on Windows is also very good. For package installation on the Mac Macports is essential and for Linux Yum is superb!

    I guess that’s it for the time being! To take the pain out of setting up your LAMP environment on your mac I would highly recommend Zend’s community server.

    Next time I’ll discuss my project process methodology.

  • HTML5 Dynamic Charting

    I recently attended an HTML5 workshop held by
    Mr Remy Sharp. I was the only honest schmo in the room to admit to having already
    purchased the HTML5 book on offer. I was waiting for the free T-shirt that
    never transpired 🙁 The workshop itself was really interesting and I left with
    the usual ‘I’m going to change the world with this new knowledge’ excitement.
    A few months have passed and I’ve finally got around to writing this blog (the
    changing the world part will have to wait).

    The part of the day that really got my juices flowing was the Canvas tag
    stuff. It’s perhaps embarrassing to admit but I’d not had any dealings with it
    beforehand. The bit that really blew my top was the traversing through each
    pixel of a transposed image on the canvas and altering each one. Simply
    amazing. During my lunch hours at work, I wanted to investigate the Canvas tag
    in more depth. I wanted to create a simple table of data, that using
    JavaScript, could be used to generate a simple line graph. I wasn’t even sure
    if it was possible.

    Just a quick mention of
    Zen-Coding
    which I was also introduced to at the Workshop. Imagine being able to write
    entire blocks of HTML shorthand and then convert them at the press of a
    button. So

    div.class-name becomes
    <div class="class-name"></div>and

    ul>li*3 becomes
    <ul><li></li><li></li><li></li></ul>
    (Tabbed in reality)Back to the point…

    Generating a line graph on the fly

    The final example can be seen
    here. I started off with a basic table of data within my HTML5 document.

    Day Jokes Learnt Girls Numbers
    Day 1 5 1
    Day 2 7 1
    Day 3 10 3
    Day 4 20 4
    Day 5 40 7

    Here we are studying the highly scientific effect of comedy on the fairer sex.
    The next thing I added was the container for my X and Y labels and a key
    holder which explained what the line represented.

    
      <div id="y-axis">
        <ul id="y-axis"> 
          <li id="label-1"></li> 
          <li id="label-2"></li> 
          <li id="label-3"></li> 
          <li id="label-4"></li> 
        </ul>
      </div> 
        <div id="canvas-holder"></div> 
        <div id="key-holder"> 
          <table id="key"> 
            <tbody> 
              <tr>
                <th>
                  Key
                </th> 
                <th>
                  Value
                </th> 
              </tr> 
            </tbody>
          </table> 
        </div>
      <div id="x-axis"> </div>
    
    

    I learnt at the workshop about document.querySelector and
    document.querySelectorAll. Both return the corresponding DOM
    elements of the queried parameters. I wanted this HTML file to be
    self-sufficient and not require any 3rd party JavaScript libraries. So using
    these DOM selectors I did the following.

      
        var can = document.querySelector('canvas'); 
        
        // Traverse each header to work out the keys required
        var thList = document.querySelector('#data').querySelectorAll('th');
        var dataKey = new Array();
        // Start count at 1 to avoid our blank cell
        for (i = 1; i < thList.length; i++) {
          dataKey[i] = thList[i].innerHTML;
        }
      
    

    The first line assigns our canvas element to the variable
    can. Next up we use the headers to define the number of lines
    we need on our graph and what they represent. Notice that the count starts at
    1 to ignore the first th cell which is empty. We assign these
    variables to an array (dataKey).

      
    // Use the td content to derive the data array
    var tdList = document.querySelector('#data').querySelectorAll('td');
    var dataArray = {};
    var dataCount = 0;
    var currentKey = '';
    
    // Variables for our X and Y labels
    var smallestFigure = 0;
    var largestFigure = 0;
    var xLabels = [];
    var xlableCnt = 0;
    
    // Traverse the td to derive our data array
    for (i = 0; i < tdList.length; i++) {
      // Set smallest/largest if value is a number
      if (!isNaN(tdList[i].innerHTML)) {
        var testNumber = parseFloat(tdList[i].innerHTML);
        if (!smallestFigure && !largestFigure) {
          smallestFigure = testNumber;
          largestFigure = testNumber;
        } else {
          if (testNumber < smallestFigure) {
            smallestFigure = testNumber;
          }
          if (testNumber > largestFigure) {
            largestFigure = testNumber;
          }
        }
      }
    
      // Col 1 is the key for this row
      if (dataCount == 0) {
        currentKey = tdList[i].innerHTML;
        dataArray[currentKey] = {};
        xLabels[xlableCnt] = tdList[i].innerHTML;
        xlableCnt++;
      } else {
        dataArray[currentKey][dataKey[dataCount]] = tdList[i].innerHTML;
      }
    
      // Count starts at zero
      if (dataCount == (dataKey.length - 1)) {
        dataCount = 0;
      } else {
        dataCount++;
      }
    }
    
    

    OK, this may look slightly overwhelming. Just take some deep breaths and I'll
    talk you through it. What this code is trying to do is to use the content of
    the table to form a data array. Firstly the tdList becomes a
    holder for all of the content within the td tags. Next, we
    define all of the variables we are going to require to get our array. We loop
    through all of the td DOM elements. For each
    td element, we check whether the content is a valid number.
    If it is we check the value against previous values to determine the largest
    and smallest numbers. This will be used to determine our Y axis range. Our X
    axis labels use the first (vertical) column in our table. Each label will also
    be used as our array key to determine the values plotted on the canvas.

    
      // Each value will be worth a units worth of pixel
      var unit = can.height / largestFigure;
    
      // Lets now get our side label
      document.querySelector('#label-1').innerHTML = largestFigure;
      document.querySelector('#label-2').innerHTML = (largestFigure / 4) * 3;
      document.querySelector('#label-3').innerHTML = (largestFigure / 4) * 2;
      document.querySelector('#label-4').innerHTML = (largestFigure / 4) * 1;
    
      // Our bottom label
      var xWidth = can.width / xLabels.length;
    
      // Lets fill our x labels
      for (i = 0; i < xLabels.length; i++) {
        xSpan = document.createElement('span');
        xSpan.innerHTML = xLabels[i];
        xSpan.className = 'spanBlock';
        xSpan.style.width = xWidth + 'px';
        document.querySelector('#ul-x-axis').appendChild(xSpan);
      }
    
    

    Next up we use our assigned variables to determine the X and Y axis labels. We
    find our unit (what each value is in pixels) by taking the length of the
    canvas and dividing it by the largest figure we have. The Y axis is split into
    4 so is not very dynamic, I'm sure you can do better. Also, you may have noted
    I've picked some nice round numbers in this example, barely the case in
    reality! Our X-axis uses the xLabels variable we assigned to
    early. We create a new span DOM element and set the inner HTML to match the
    value of the label. Now for the fun part...

    
      // Check whether we can use the canvas 
      if (can.getContext){ 
        // Get our object
        var ctx = can.getContext('2d');
        
        // Black out back ground 
        ctx.fillStyle = '#000'; 
        ctx.fillRect(0,0, ctx.canvas.width, ctx.canvas.height); 
        ctx.fillStyle = '#fff'; 
        ctx.strokeStyle = '#fff'; 
        
        // default drawing style 
        ctx.lineWidth = 5; 
        ctx.lineCap = 'round'; 
        ctx.save(); 
        
        // Define array of fill colours first element is 
        // blank so that our keys can be used 
        strokeArr =new Array('','#fff','#0000FF');
        
    

    The first line checks that we can actually use the
    getContext method of the object. We set up our ctx object
    which allows us to draw on our canvas tag. We create our black rectangle which
    covers the canvas, this is the base for our chart. The
    lineWidth and lineCap set up how our line
    graph will look on the canvas. Our strokeArr sets the colours
    for each of our lines. This isn't at all dynamic. You may ask
    'why is the first element blank' and
    'what if someone adds another column'. To which my response will be
    'Shut the hell up!'. Anyway, we are now ready to start drawing our
    lines.

    
      for (i=0;i<dataKey.length;i++) { 
        if (dataKey[i] != undefined) { 
          // Set Starting point 
          ctx.strokeStyle = strokeArr[i]; 
          ctx.beginPath(); 
          ctx.moveTo(0,500); 
          
          // Set defualt points 
          xPoint = 0; 
          yPoint = 500; 
          
          // Item in object
          xCount = 0; 
          
          // Loop through items 
          for (o in dataArray) { 
            // X point moves along with the count 
            xPoint  = xCount * xWidth; 
            
            // Use the unit calculated earlier to calc the y point 
            if (dataArray[o][dataKey[i]]) {
              yPoint = 500 - (dataArray[o][dataKey[i]] * unit); 
            } else { 
              yPoint = 500;
            } 
            
            // Start new line or join existing 
            if (xCount == 0) { 
              ctx.moveTo(xPoint,yPoint); 
            } else { 
              ctx.lineTo(xPoint, yPoint);
            } 
            
            xCount++; 
          } 
          
        // Add stroke
        ctx.stroke(); 
        
        // Add this row to key 
        keyTr = document.createElement('tr');
        keyTd = document.createElement('td'); 
        keyTd.style.backgroundColor = strokeArr[i]; 
        keyTr.appendChild(keyTd); 
        keyTd2 = document.createElement('td'); 
        keyTd2.innerHTML = dataKey[i];
        keyTr.appendChild(keyTd2);
        document.querySelector('#keytbody').appendChild(keyTr);
      } 
    }
    
    

    We are now ready to use our data array which we generated earlier. We loop
    through each item (line on the chart) which contains an object of points on
    the map. The strokeArr value is the colour of the line. The
    beginPath method is the equivalent of taking your pen off of
    the canvas. The moveTo method puts the pen back on the canvas
    at the correct point, in this case where the X and Y axis meet. We start
    looping the points on the canvas we need to draw.

    The xPoint (where on the X axis we need to plot our point) is
    calculated by using the xCount (number of iterations through
    our X-axis) times the xWidth (number of pixels between each X
    label). The Y axis is a little more complicated as the lowest Y point isn't 0
    it's 500 (because the canvas start at the top left not the bottom left). We
    use our data value and times it by the unit which gives us the number of
    pixels up we need to be. We have our X and Y points so we now just need to set
    them. We check whether this is a new line or an existing line and plot the
    point accordingly. The ctx.stroke() fills the line for us.
    Now we have our line we need to create a record in our key table. The first
    td is filled with the same colour of the line while the
    second td holds the label for that line. We append the new
    table row to the key table.

    And that's that. I was very impressed with the ease with which we can do this
    funky stuff and look forward to more of the same!