Building bar charts

show more Building bar charts provides you with in-depth training on Web. Taught by Joseph Lowery as part of the CSS: Formatting Visual Data show less
please wait ...

Building bar charts

The first thing that comes to mind when you hear the term, visual data, may very well be charts. Throughout this chapter, we'll take a look at ways to visualize data through a number of different graphs. Our first type covered in this lesson is the bar chart. Of course, you can create a bar chart, or any other type of chart in a graphics program like Photoshop. But it's far more interesting for the web designer to do it where you have a larger measure of control. And can change it at the drop of a client's hat.

For that, we need to turn to the HTML 5 innovation canvas. Canvas objects are drawn programmatically through java script, which means it's much easier to change the things like the data values and labels. For this chart, I'm going to use a code developed by a gentleman named William Malone, and available on his site. Let's start. The first task is to link to the Java script file that contains the majority of the functionality. I'm going to place my cursor after the link to main.css. And enter the needed script tag.

I'll set the source to my scripts folder. Where I've already downloaded HTML5-canvas-bar-graph.js. Now let me set the type to text JavaScript and close the tag. Now I am going to scroll down the page a little bit just to point out a div that we have on line 36. You'll see a div there already marked up with the id of bar chart. That is where our bar chart is going to go.

Let's go do to the bottom of the page, and we'll put in the script tag that we'll call all the canvas drawing functions. I am going to place this just above the closing body tag and I'll start by entering a script tag, and then in the center create a little space. Because we're not using jQuery or its equivilent, we don't have something like the jQuery document ready function, so we'll need to wrap the entire fucntion in parantheses. After I type in the word function, I'll enter another set of parenthese. And then following that between the two closing parenthesis, I'll put in my curly brace pair, and hit return to open up a line. Next, we'll create the canvas object.

So I'll enter in var and I'm going to call this variable CTX, and set that equal to, Create Canvas this is a function that's in the library that we link to. And we'll give it the name of the divs ID, bar chart, so we can tell where to place that canvas. On the next line we'll create a new bar graph object, and I'm just going to call this graph, and set that equal to new bar graph and will use the canvass CTX.

Now we need to specify a good number of values. First, we'll set the maximum value and we'll target graph the property max value, and make that 1,000. Then the margin between each of the bars, I'm going to set the graph.margin equal to 5. Next up the colors will be defined in an array. For an array we'll use the square brackets.

And each value within it is a string, so it'll be contained within a pair of quotes. All of these values that I'll enter are six digit hexadecimal numbers, 0263ae is the first one. Next will be a858a3. Our next colored number, fbae2c, and then the final one, df4c27. Let's close off that entire array with a semicolon, and now we're ready for the labels.

These particular labels will go along the x axis, so x axis Label Arr, short for array, and for an array we'll use the square brackets. And the first in the array is Painting, followed by Sculpture, next Digital, and then Studio. Don't forget your closing semicolon. Our final line are the values that we want to pass in for the data.

And for that we use the method update. And because it's a method, a set of parenthesis follows it. And within it, the square brackets of the array. These are number values so they don't need quotes. So Painting is 950, Sculpture 650, Digital 700, and Studio 885. Looks like I need to add another pair of parentheses here, following the function call, and a closing semi-colon. Let's Save the file and Browse it. As you can see, this bar chart has gradients built in to give it a very polished result.

If you change the number of elements in the array, you can alter the number of bars shown. All in all, I think it's a pretty flexible, easy to use system, and I hope you find it so.

Building bar charts
Video duration: 5m 40s 1h 37m Intermediate


Building bar charts provides you with in-depth training on Web. Taught by Joseph Lowery as part of the CSS: Formatting Visual Data

please wait ...