HighTables
HighTables makes it trivial to render charts from existing HTML tables using jQuery and Highcharts.
- Quick Example
- Basic Usage
- Line Charts
- Area & Stack Charts
- Bar & Column Charts
- Pie Charts
- Installation
- Options
- API
Quick Example
Here's a quick example of what you can do with HighTables.
Domestic Box Office Performance of the Batman Movie Franchise
The graphs above were rendered automatically (i.e., without any custom JavaScript) from a hidden HTML table (show).
Day | Batman Begins | Batman Begins (Total) | The Dark Knight | The Dark Knight (Total) | The Dark Knight Rises | The Dark Knight Rises (Total) |
---|---|---|---|---|---|---|
Wednesday | $15,068,368 | $15,068,368 | ||||
Thursday | $9,083,178 | $24,151,546 | ||||
Friday | $15,072,242 | $39,223,788 | $67,165,092 | $67,165,092 | $75,754,897 | $75,754,897 |
Saturday | $18,017,047 | $57,240,835 | $47,650,240 | $114,815,332 | $44,931,966 | $120,686,863 |
Sunday | $15,656,151 | $72,896,986 | $43,596,151 | $158,411,483 | $40,200,432 | $160,887,295 |
Monday | $6,627,412 | $79,524,398 | $24,493,313 | $182,904,796 | $19,389,129 | $180,276,424 |
Tuesday | $5,404,419 | $84,928,817 | $20,868,722 | $203,773,518 | $17,762,472 | $198,038,896 |
Wednesday | $5,518,167 | $90,446,984 | $18,377,288 | $222,150,806 | $13,770,092 | $211,808,988 |
Thursday | $4,475,440 | $94,922,424 | $16,464,405 | $238,615,211 | $13,202,371 | $225,011,359 |
Friday | $8,269,743 | $103,192,167 | $23,232,292 | $261,847,503 | $17,734,545 | $242,745,904 |
Saturday | $10,664,330 | $113,856,497 | $28,272,494 | $290,119,997 | $24,712,417 | $267,458,321 |
Sunday | $8,655,316 | $122,511,813 | $23,661,680 | $313,781,677 | $19,654,489 | $287,112,810 |
Monday | $4,010,038 | $126,521,851 | $10,518,116 | $324,299,793 | $8,160,046 | $295,272,856 |
Tuesday | $3,727,338 | $130,249,189 | $9,629,366 | $333,929,159 | $8,773,116 | $304,045,972 |
Wednesday | $2,574,479 | $132,823,668 | $8,755,141 | $342,684,300 | $7,333,325 | $311,379,297 |
Thursday | $2,637,269 | $135,460,937 | $8,402,546 | $351,086,846 | $6,818,467 | $318,197,764 |
Basic Usage
To render a chart from any table on your page using HighTables, you have two options.
Table-based approach
<table class="render-to-[_____]-chart">
<!-- ... -->
</table>
Fill in the blank above with a valid chart type, e.g. line
. A chart will be rendered automatically, just above the table, when the page loads.
Div-based approach
<div class="[_____]-chart" data-source="#chart-data"></div>
<!-- elsewhere on the page -->
<table id="chart-data">
<!-- ... -->
</table>
The value of data-source
should be a valid CSS selector (such as "#foo"
or ".bar"
) which identifies the <table>
element used to render the chart.
The second approach is more flexible than the first as it allows you to render multiple charts from the same table with different custom options. It also decouples the logic used to render your tables from your charting logic, making it possible to (for example) load tables asynchronously from one website and render charts from them on a completely different website.
Line Charts
<table class="render-to-line-chart">
<!-- ... -->
</table>
<!-- or: -->
<div class="line-chart" data-source="#line-chart-source"></div>
<table id="line-chart-source">
</table>
By default, the first column will be used to label the X-axis of the chart, and each column after that will be represented as a data series.
For a spline, or smoothed line, use spline
instead of line
.
Web Browser Market Share
Month | IE | Firefox | Chrome |
---|---|---|---|
2012-01 | 37.45 | 24.78 | 28.4 |
2012-02 | 35.75 | 24.88 | 29.84 |
2012-03 | 34.81 | 24.98 | 30.87 |
2012-04 | 34.07 | 24.87 | 31.23 |
2012-05 | 32.12 | 25.55 | 32.43 |
2012-06 | 32.31 | 24.56 | 32.76 |
2012-07 | 32.04 | 23.73 | 33.81 |
2012-08 | 32.85 | 22.85 | 33.59 |
2012-09 | 32.7 | 22.4 | 34.21 |
2012-10 | 32.08 | 22.32 | 34.77 |
2012-11 | 31.23 | 22.37 | 35.72 |
2012-12 | 29.84 | 22.24 | 36.66 |
Area Charts
<table class="render-to-area-chart">
<!-- ... -->
</table>
<!-- or: -->
<div class="area-chart" data-source="#area-chart-source"></div>
<table id="area-chart-source">
</table>
Area charts work basically the same as line charts. For a stack chart, use stack
instead of area
.
Mobile OS Market Share
Quarter | iOS | Android | BlackBerry | Windows Phone |
---|---|---|---|---|
2012 Q3 | 23,550 | 122,480 | 8,947 | 4,058 |
2012 Q2 | 28,935 | 98,529 | 7,991 | 4,087 |
2012 Q1 | 33,121 | 81,067 | 9,939 | 2,713 |
2011 Q4 | 35,456 | 75,906 | 13,185 | 2,759 |
2011 Q3 | 17,295 | 60,490 | 12,701 | 1,702 |
2011 Q2 | 19,629 | 46,776 | 12,652 | 1,724 |
2011 Q1 | 16,883 | 36,350 | 13,004 | 1,600 |
Bar & Column Charts
<table class="render-to-bar-chart">
<!-- ... -->
</table>
<!-- or: -->
<div class="bar-chart" data-source="#bar-chart-source"></div>
<table id="bar-chart-source">
</table>
By default, the first column of the table will be used for bar labels, and each remaining column will be rendered as a group of bars. The data-transpose
option reverses this, using the first row for labels and each subsequent row as a group of bars.
Use column
instead of bar
to produce a bar chart with vertical bars, i.e., a column chart.
World's Top Oil Producers
Nation | Oil Produced | CO2 Emissions |
---|---|---|
Russia | 10,540,000 | 1,688,688 |
Saudi Arabia | 10,270,000 | 493,726 |
United States | 9,688,000 | 5,492,170 |
Iran | 4,252,000 | 574,667 |
China | 4,073,000 | 8,240,958 |
Pie Charts
<table class="render-to-pie-chart">
<!-- ... -->
</table>
<!-- or: -->
<div class="pie-chart" data-source="#pie-chart-source"></div>
<table id="pie-chart-source">
</table>
By default, the first column of the table will be used to name the slices of the pie, and the values in the last column will be used to determine the width of each slice.
U.S. National Budget
Category | Budget (in billions) |
---|---|
Medicare/Medicaid | $835 |
Social Security | $725 |
Defense | $700 |
Discretionary | $646 |
Other Mandatory | $465 |
Net Interest | $227 |
Installation
To use HighTables on your website, simply include hightables.min.js (or hightables.js) after including jQuery and Highcharts:
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="highcharts.min.js"></script>
<script type="text/javascript" src="hightables.min.js"></script>
Options
Support for custom chart options is pretty limited right now:
-
Title: to give a chart a title, add the
data-title
attribute to the element responsible for rendering the element (the<table>
or the<div>
). -
Order: to render series in the opposite order they appear in a table, set the
data-order
attribute to "descending". -
Limit: to only render the first N records in a chart, set
data-limit="[N]"
. -
Threshold: to skip values below a certain threshold, use the
data-threshold
attribute. -
X-Axis: to specify a minimum and tick interval for the X-axis, use the
data-x-min
anddata-x-interval
attributes. -
Y-Axis: to specify a minimum and tick interval for the Y-axis, use the
data-y-min
anddata-y-interval
attributes. -
Columns: to use only certain columns for a table's data, set the
data-value-columns
attribute to a comma-delimited list of the (zero-based) column indices you want to use. Use"..."
to represent a range; for example:-
"5,6"
would use columns 5 and 6 (obviously) -
"2,...,5"
would use columns 2, 3, 4, and 5 -
"3,..."
would use every column starting with column 3 -
"...,3"
would use every column from 0 to 3
-
In addition to these options, there is support for a more general customization mechanism: add the data-options
attribute, and specify the name of a JavaScript function which returns an object with any Highcharts options you like.
Options Example
For instance, the chart on the right below is rendered with the attribute data-options="customOptions"
, where customOptions
is the following JavaScript function:
function customOptions() {
return {
title: { text: "Gloomy Autumn Donut Version" },
colors: ["#74A6BD", "#7195A3", "#D4E7ED", "#EB8540", "#B06A3B", "#AB988B"],
plotOptions: {
pie: {
borderColor: "#000",
borderWidth: 3,
innerSize: "25%",
shadow: false
}
}
};
}
Category | Budget (in billions) |
---|---|
Medicare/Medicaid | $835 |
Social Security | $725 |
Defense | $700 |
Discretionary | $646 |
Other Mandatory | $465 |
Net Interest | $227 |
API
HighTables will automatically render charts when the page loads. However, you can also render charts manually, e.g. if the content of your page is dynamic and/or you want to make charts update and re-render based on user actions.
To render a chart directly above a table, call HighTables.renderChartFromTable(table)
, where table
is a raw <table>
DOM element (not a jQuery object) with an appropriate render-to-[*]-chart
class.
To render a chart within any arbitrary <div>
with a [*]-chart
class, call HighTables.renderChart(div)
.
You can also immediately re-render all charts on a page by calling HighTables.renderCharts()
.
So far there isn't much you can do to customize the HighTables library's default behavior. This will almost certainly change, but for now there's really only one configurable default option: the display of Highcharts links (off by default). To show Highcharts some love and turn the display on:
HighTables.includeHighchartsLinks = true;
API Example
Click the button below to load an HTML table via AJAX and render a chart from it.