Easier Visual Data in the Browser With Variance Charts
Today, we will be diving into Variance Charts; a unique JavaScript based "grammar of graphics" which provides an abstract, declarative markup style to create what would otherwise be fairly complex charts. Variance Charts sits somewhere between D3.js and HighCharts, providing flexibility while retaining an approachable syntax.
Using Variance Charts does not rely on deeper knowledge of JavaScript. Let's take a look!
Getting the Library
Variance provides a non-commercial version, hosted through its own CDN, which you should include in your page like so:
1 |
<script src="http://variancecharts.com/cdn/variance-noncommercial-standalone-9bc0f5e.min.js" charset="UTF-8"></script> |
You'll want to include a style.css
file as well. We won't go through the process of explaining the basic setup of the HTML file, as most of these details are straightforward.
Our First Chart
Variance can read both JSON and CSV; we will create a simple bar chart using the following unofficial CSV data about bacon popularity:
1 |
<csv id="yearly-bacons"> year,bacons 1990,20 1995,231 2000,639 2005,1625 2010,7000 2015,16000 </csv> |
As we can see from these value pairs, bacon is getting exponentially more and more popular. For example, the pair 1990,20
shows that bacon was 20
popular (whatever the heck that means) in 1990
, but then by 1995
popularity had risen to 231
.
But really, to see this visually, we'd like to present it using a bar chart. Here's an example of how we'd markup our chart (we'll go over the various elements in a moment).
1 |
<div class="bacon-chart"> |
2 |
<h3>Bacon data</h3> |
3 |
<chart data="#yearly-bacons" scale-y-linear="0 20000"> |
4 |
<guide-x></guide-x>
|
5 |
<guide-y></guide-y>
|
6 |
<repeat>
|
7 |
<annotation class="bottom">{{year}}</annotation> |
8 |
<bar map-length="bacons"></bar> |
9 |
</repeat>
|
10 |
</chart>
|
11 |
</div>
|
We will also need some accompanying CSS to set the size of the chart. To do this, we will wrap all of our markup in a <div class='container'>
and add the following styles to a style.css
file:
1 |
.container { |
2 |
width:50%; |
3 |
margin:60px auto; |
4 |
}
|
5 |
|
6 |
.container chart { |
7 |
width:100%; |
8 |
height:400px; |
9 |
}
|
Notice a few things here:
- The chart markup looks a lot like HTML. In fact, it is valid XML which Variance uses to build our bar chart. We also used this custom tagging to wrap the CSV data. We reference that CSV element in the
chart
tag. -
scale-y-linear
tells us what the scale of the y direction should be. In our case, our data set goes to 16000, but starts as low as 20. To cover the range, we set our bottom end at 0, and our top at 20000. -
guide-x / guide-y
creates a tick-mark guide on the respective axes. -
<repeat>
loops over our data and creates items based on each data point. -
annotation
is a label that can be placed around a particular item, such as abar
. Curly braces allow you to display a piece of data; in our case,year
is in our CSV, so we can easily output year. -
bar
creates a bar element, andmap-length
defines the width of the barbacons
maps to the CSV.
Great! Now how do we make it less ugly?
Styling the Chart
To style the chart, we simply style as we would normal elements in CSS. The bar elements of the chart are block level normal DOM elements and are positioned using flexbox. We will add some styles to our CSS, which should then look something like this.
1 |
body { |
2 |
color:#3f130c; |
3 |
height:100%; |
4 |
font-family:sans-serif; |
5 |
background-image:url(../bg.jpg); |
6 |
background-size:cover; |
7 |
background-position:center; |
8 |
background-repeat:no-repeat; |
9 |
background-attachment:fixed; |
10 |
}
|
11 |
|
12 |
h1,h2,h3,h4 { |
13 |
font-family:Tauri, sans-serif; |
14 |
}
|
15 |
|
16 |
.container { |
17 |
width:50%; |
18 |
position:relative; |
19 |
background-image:url(../bg-blur.jpg); |
20 |
background-size:cover; |
21 |
background-position:center; |
22 |
background-repeat:no-repeat; |
23 |
background-attachment:fixed; |
24 |
margin:100px auto; |
25 |
padding:100px; |
26 |
}
|
27 |
|
28 |
.bacon-chart { |
29 |
position:relative; |
30 |
z-index:999; |
31 |
}
|
32 |
|
33 |
.bacon-chart h3 { |
34 |
margin:0 0 1em; |
35 |
}
|
36 |
|
37 |
.container:after { |
38 |
background-color:rgba(255,255,255,0.4); |
39 |
position:absolute; |
40 |
top:0; |
41 |
left:0; |
42 |
width:100%; |
43 |
height:100%; |
44 |
content:""; |
45 |
}
|
46 |
|
47 |
.container chart { |
48 |
width:100%; |
49 |
height:400px; |
50 |
}
|
51 |
|
52 |
.bacon-chart bar { |
53 |
background-color:rgba(96,28,18,0.7); |
54 |
border:none; |
55 |
}
|
Note: we've added a Google font to the index file above our style.css link:
1 |
<link href='http://fonts.googleapis.com/css?family=Tauri' rel='stylesheet' type='text/css'> |
We've included a little bonus here: the "blurred window" effect can be seen created between the container and body elements. We make use of a second background image to achieve this.
Now we have a more useful chart. But it gets much, much better.
Animation and Tooltips
Time for some flourishes, beginning with some tooltips on the bars. To kick things off, we'll add a new annotation for each bar, so that our chart markup looks like this:
1 |
<chart data="#yearly-bacons" scale-y-linear="0 20000" scale-y-linear="1990 2015"> |
2 |
<guide-x></guide-x>
|
3 |
<guide-y></guide-y>
|
4 |
<repeat>
|
5 |
<annotation class="bottom">{{year}}</annotation> |
6 |
<annotation class="tooltip" map-position="bacons">{{bacons}}</annotation> |
7 |
<bar map-length="bacons"></bar> |
8 |
</repeat>
|
9 |
</chart>
|
After that, we will add some new styles to our CSS that will allow for some interesting hover effects and simple transitions for the tooltip:
1 |
.bacon-chart bar { |
2 |
background-color:rgba(96,28,18,0.7); |
3 |
border:none; |
4 |
margin:0!important; |
5 |
}
|
6 |
|
7 |
datum { |
8 |
overflow:hidden; |
9 |
}
|
10 |
|
11 |
.tooltip { |
12 |
width:auto; |
13 |
height:auto; |
14 |
background-color:#444; |
15 |
color:#fff; |
16 |
opacity:0; |
17 |
left:-100%; |
18 |
font-size:.6em; |
19 |
-webkit-transition:all .4s; |
20 |
-moz-transition:all .4s; |
21 |
-ms-transition:all .4s; |
22 |
-o-transition:all .4s; |
23 |
transition:all .4s; |
24 |
padding:6px; |
25 |
}
|
26 |
|
27 |
datum:hover { |
28 |
background-color:rgba(255,255,255,0.1); |
29 |
}
|
30 |
|
31 |
datum:hover .tooltip { |
32 |
display:block; |
33 |
opacity:1; |
34 |
left:0; |
35 |
}
|
Animation
Finally, we want to animate our bars in. We'll accomplish this with a keyframe animation:
1 |
@-webkit - keyframes barsIn { |
2 |
0 % { |
3 |
bottom: -100 % ; |
4 |
}
|
5 |
100 % { |
6 |
bottom: 0; |
7 |
}
|
8 |
}
|
Which we then will apply to our bar element:
1 |
.bacon - chart bar { |
2 |
background - color: rgba(96, 28, 18, 0.7); |
3 |
margin: 0!important; |
4 |
border: none; |
5 |
height: 0; - webkit - animation: barsIn 1s; |
6 |
}
|
This can also be accomplished using something like-webkit-transform: scale( 1, 0 )
, but we wanted to keep it as simple as possible. You should also make sure you add the proper prefixes as necessary.
More About Variance
Variance does a great job of explaining more complex examples, and also provides a great index of documentation. You will find that Variance is capable of far more complex visualizations. We'll touch briefly on some of the main points here.
Variance utilizes names of different core parts of a chart to make building the charts easy, including point
,dot
, range
, bar
, boxplot
, and line
. Each of these have their own determinable inputs. These can all be used in combination with one another, and can be created in iterative fashion with the repeat
block.
Variance can also use linked data, which allows for API-driven visualizations that have dynamically updated information.
Conclusion
Today, you've learned how to create a completely declarative, animated, styled chart using Variance Charts. In an effort to get you started quickly, we've left out a lot of the functionality available in Variance, so be sure to check it out for yourself!
Bacon photo by jeffreyww on Flickr, edited. Creative Commons 2.0