Skip to content

A jQuery plugin to make using Google Charts super simple, without losing any customizability.

Notifications You must be signed in to change notification settings

kpatt1011/charts.js

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 

Repository files navigation

Charts.js 0.3

A jQuery plugin to make using Google Charts super simple, without losing customizability.

Basic Setup

Include the appropriate libraries in your <head> tag

<!-- jQuery -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    
<!-- Google JS-API -->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
    
<!-- Charts.js -->
<script type="text/javascript" src="js/charts.js"></script>

Load the Google Charts Library

<script type="text/javascript">
// Load the visualization library
google.load('visualization', '1.0', {'packages':['corechart']});
</script>

On the Google Charts callback, draw your charts using Charts.js

<script type="text/javascript">
google.setOnLoadCallback(makeCharts);
function makeCharts() {
  $("#chart-bar").chart({
    chartType:'bar',
    columnTitles:['Answer', 'Frequency'],
    jsonData: [["1",1],
              ["2",4],
              ["3",1],
              ["4",3]]
  });
}
</script>

Specifying Data

You may either pass json directly upon creating the chart, or give the chart a url to make an AJAX request for the data. Currently, all data injected into the chart using the google.visualization.arrayToDataTable() method which you can read about here.

Passing JSON

JSON needs to be in a format where each row represents a data point, and each column represents a series. Column titles may be included in either the JSON or the columnTitles attribute. Column data types are implicit.

$('#chart').chart({
  chartType:'bar',
  columnTitles:['Answer', 'Frequency'],
  jsonData: [
              ["1",1],
              ["2",4],
              ["3",1],
              ["4",3]
            ]
});

Passing a URL

Make sure the JSON returned by the server matches the format above.

$('#chart').chart({
  chartType:'bar',
  columnTitles:['Answer', 'Frequency'],
  url: 'demo.json'
});

If you have used a URL for data, you can ask the chart to refresh its data by calling update.

$('#chart').chart('update');

Customizing

Google Charts allows for tons of customization, you and Charts.js supports all of these customization options.

Chart Types

Charts.js supports 'bar', 'column', 'line', 'pie', 'combo', 'area', 'bubble', 'candlestick', and 'scatter' graphs. Simply specify the type of graph you want on creation. You can also change it later and redraw the chart to see the changes.

// specify at creation
$('#chart').chart({
  chartType:'bar',
  columnTitles:['Answer', 'Frequency'],
  url: 'demo.json'
});

// or change it later
$('#chart').data('chart').chartType = 'bar';

// but don't forget to redraw the chart
$('#chart').chart('draw');

Google Charts Options

You may specify any options from the Google Charts API by passing them to the options item.

$('#chart').chart({
  chartType:'bar',
  url: 'demo.json',
  options: {
    height: 600,
    width: 1200
  }
});

Options can also be changed after creation by updating the options object and redrawing the chart

$('#chart').data('chart').options.height = 1000;
$('#chart').chart('draw');

Changelog

0.3

  • Fixed animating of charts when data is changed. Animations will work on $('#chart').chart('update'). Animations can be customized by changing options.animation item.
  • Fixed chainability issues with jQuery

0.2

  • Added all chart types included in the Google Charts corechart library
  • Added ability to call draw to update chart to reflect changed options rather than using update for less data polling.

About

A jQuery plugin to make using Google Charts super simple, without losing any customizability.

Resources

Stars

Watchers

Forks

Packages

No packages published