Join Joseph Lowery for an in-depth discussion in this video Building bar charts, part of CSS: Formatting Visual Data.
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.
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.
- Adding custom number characters to lists
- Managing hanging indents
- Styling nested lists
- Understanding definition lists
- Highlighting table content
- Creating alternating row colors (zebra stripes)
- Building bar graphs and pie charts