Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Building bar charts

From: CSS: Formatting Visual Data

Video: 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.

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.

Show transcript

This video is part of

Image for CSS: Formatting Visual Data
CSS: Formatting Visual Data

21 video lessons · 7874 viewers

Joseph Lowery
Author

 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed CSS: Formatting Visual Data.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.