Code Monkey home page Code Monkey logo

charts.js's Introduction

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');

Column Titles

You may either specify the column titles of each series in your array using the columnTitles item, or they may be included in the JSON you pass in, or by the data returned by the server at the URL.

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.

charts.js's People

Contributors

joshdover avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

kpatt1011

charts.js's Issues

Add verbose option

Add ability to see charts.js events in console log. (ie. JSON loaded from URL)

Update options names

For the 1.0 release we will be using more natural options names. Here are the tentative changes:

"chartType" => "type"
"jsonData" => "array"
"columnTitles" => "titles"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.