Create beautiful charts with one line of JavaScript

Overview

Chartkick.js

Create beautiful charts with one line of JavaScript

See it in action

Supports Chart.js, Google Charts, and Highcharts

Also available for React, Vue.js, Ruby, Python, Elixir, and Clojure

Quick Start

Run

npm install chartkick chart.js

And add

import Chartkick from "chartkick"
import Chart from "chart.js"

Chartkick.use(Chart)

This sets up Chartkick with Chart.js. For other charting libraries, see detailed instructions.

Charts

Create a div for the chart

<div id="chart-1" style="height: 300px;"></div>

Line chart

new Chartkick.LineChart("chart-1", {"2017-01-01": 11, "2017-01-02": 6})

Pie chart

new Chartkick.PieChart("chart-1", [["Blueberry", 44], ["Strawberry", 23]])

Column chart

new Chartkick.ColumnChart("chart-1", [["Sun", 32], ["Mon", 46], ["Tue", 28]])

Bar chart

new Chartkick.BarChart("chart-1", [["Work", 32], ["Play", 1492]])

Area chart

new Chartkick.AreaChart("chart-1", {"2017-01-01": 11, "2017-01-02": 6})

Scatter chart

new Chartkick.ScatterChart("chart-1", [[174.0, 80.0], [176.5, 82.3], [180.3, 73.6]])

Geo chart - Google Charts

new Chartkick.GeoChart("chart-1", [["United States", 44], ["Germany", 23], ["Brazil", 22]])

Timeline - Google Charts

new Chartkick.Timeline("chart-1", [["Washington", "1789-04-29", "1797-03-03"], ["Adams", "1797-03-03", "1801-03-03"]])

Multiple series

data = [
  {name: "Workout", data: {"2017-01-01": 3, "2017-01-02": 4}},
  {name: "Call parents", data: {"2017-01-01": 5, "2017-01-02": 3}}
]
new Chartkick.LineChart("chart-1", data)

Multiple series stacked and grouped - Chart.js 2.5+ or Highcharts

data = [
  {name: "Apple", data: {"Tuesday": 3, "Friday": 4}, stack: "fruit"},
  {name: "Pear", data: {"Tuesday": 1, "Friday": 8}, stack: "fruit"},
  {name: "Carrot", data: {"Tuesday": 3, "Friday": 4}, stack: "vegetable"},
  {name: "Beet", data: {"Tuesday": 1, "Friday": 8}, stack: "vegetable"}
]
new Chartkick.BarChart("chart-1", data, {stacked: true})

Say Goodbye To Timeouts

Make your pages load super fast and stop worrying about timeouts. Give each chart its own endpoint.

new Chartkick.LineChart("chart-1", "/stocks")

Options

Min and max for y-axis

new Chartkick.LineChart("chart-1", data, {min: 1000, max: 5000})

min defaults to 0 for charts with non-negative values. Use null to let the charting library decide.

Min and max for x-axis - Chart.js

new Chartkick.LineChart("chart-1", data, {xmin: "2018-01-01", xmax: "2019-01-01"})

Colors

new Chartkick.LineChart("chart-1", data, {colors: ["#b00", "#666"]})

Stacked columns or bars

new Chartkick.ColumnChart("chart-1", data, {stacked: true})

You can also set stacked to percent or relative for Google Charts and percent for Highcharts

Discrete axis

new Chartkick.LineChart("chart-1", data, {discrete: true})

Label (for single series)

new Chartkick.LineChart("chart-1", data, {label: "Value"})

Axis titles

new Chartkick.LineChart("chart-1", data, {xtitle: "Time", ytitle: "Population"})

Straight lines between points instead of a curve

new Chartkick.LineChart("chart-1", data, {curve: false})

Hide points

new Chartkick.LineChart("chart-1", data, {points: false})

Show or hide legend

new Chartkick.LineChart("chart-1", data, {legend: true})

Specify legend position

new Chartkick.LineChart("chart-1", data, {legend: "bottom"})

Donut chart

new Chartkick.PieChart("chart-1", data, {donut: true})

Prefix, useful for currency - Chart.js, Highcharts

new Chartkick.LineChart("chart-1", data, {prefix: "$"})

Suffix, useful for percentages - Chart.js, Highcharts

new Chartkick.LineChart("chart-1", data, {suffix: "%"})

Set a thousands separator - Chart.js, Highcharts

new Chartkick.LineChart("chart-1", data, {thousands: ","})

Set a decimal separator - Chart.js, Highcharts

new Chartkick.LineChart("chart-1", data, {decimal: ","})

Set significant digits - Chart.js, Highcharts

new Chartkick.LineChart("chart-1", data, {precision: 3})

Set rounding - Chart.js, Highcharts

new Chartkick.LineChart("chart-1", data, {round: 2})

Show insignificant zeros, useful for currency - Chart.js, Highcharts

new Chartkick.LineChart("chart-1", data, {round: 2, zeros: true})

Friendly byte sizes - Chart.js 2.8+

new Chartkick.LineChart("chart-1", data, {bytes: true})

Show a message when data is empty

new Chartkick.LineChart("chart-1", data, {messages: {empty: "No data"}})

Refresh data from a remote source every n seconds

new Chartkick.LineChart("chart-1", url, {refresh: 60})

You can pass options directly to the charting library with:

new Chartkick.LineChart("chart-1", data, {library: {backgroundColor: "pink"}})

See the documentation for Chart.js, Google Charts, and Highcharts for more info.

To customize datasets in Chart.js, use:

new Chartkick.LineChart("chart-1", data, {dataset: {borderWidth: 10}})

You can pass this option to individual series as well.

Global Options

To set options for all of your charts, use:

Chartkick.options = {
  colors: ["#b00", "#666"]
}

Data

Pass data as an array or object

new Chartkick.PieChart("chart-1", {"Blueberry": 44, "Strawberry": 23})
new Chartkick.PieChart("chart-1", [["Blueberry", 44], ["Strawberry", 23]])

Times can be a Date or a string (strings are parsed)

new Chartkick.LineChart("chart-1", [[new Date(), 5], ["2017-01-01 00:00:00 UTC", 7]])

Data can also be a callback

function fetchData(success, fail) {
  success({"Blueberry": 44, "Strawberry": 23})
  // or fail("Data not available")
}

new Chartkick.LineChart("chart-1", fetchData)

Multiple Series

You can pass a few options with a series:

  • name
  • data
  • color
  • dataset - Chart.js only
  • points - Chart.js only
  • curve - Chart.js only

Code

If you want to use the charting library directly, get the code with:

new Chartkick.LineChart("chart-1", data, {code: true})

The code will be logged to the JavaScript console.

Note: JavaScript functions cannot be logged, so it may not be identical.

Download Charts

Chart.js only

Give users the ability to download charts. It all happens in the browser - no server-side code needed.

new Chartkick.LineChart("chart-1", data, {download: true})

Set the filename

new Chartkick.LineChart("chart-1", data, {download: {filename: "boom"}})

Note: Safari will open the image in a new window instead of downloading.

Set the background color

new Chartkick.LineChart("chart-1", data, {download: {background: "#fff"}})

Installation

Chart.js

Run

npm install chartkick chart.js

And add

import Chartkick from "chartkick"
import Chart from "chart.js"

Chartkick.use(Chart)

Google Charts

Run

npm install chartkick

And add

import Chartkick from "chartkick"

And include on the page

<script src="https://www.gstatic.com/charts/loader.js"></script>

To specify a language or Google Maps API key, use:

Chartkick.configure({language: "de", mapsApiKey: "..."})

before your charts.

Highcharts

Run

npm install chartkick highcharts

And add

import Chartkick from "chartkick"
import Highcharts from "highcharts"

Chartkick.use(Highcharts)

No Package Manager

Download chartkick.js directly.

For Chart.js (works with 2.1+), download the bundle and use:

<script src="/path/to/Chart.bundle.js"></script>
<script src="chartkick.js"></script>

For Google Charts, use:

<script src="https://www.gstatic.com/charts/loader.js"></script>
<script src="chartkick.js"></script>

For Highcharts (works with 2.1+), download it and use:

<script src="/path/to/highcharts.js"></script>
<script src="chartkick.js"></script>

Multiple Libraries

If more than one charting library is loaded, choose between them with:

new Chartkick.LineChart("chart-1", data, {adapter: "google"}) // or highcharts or chartjs

API

Access a chart with:

var chart = Chartkick.charts["chart-id"]

Get the underlying chart object with:

chart.getChartObject()

You can also use:

chart.getElement()
chart.getData()
chart.getOptions()
chart.getAdapter()

Update the data with:

chart.updateData(newData)

You can also specify new options:

chart.setOptions(newOptions)
// or
chart.updateData(newData, newOptions)

Refresh the data from a remote source:

chart.refreshData()

Redraw the chart with:

chart.redraw()

Loop over charts with:

Chartkick.eachChart( function(chart) {
  // do something
})

Custom Adapters

Note: This feature is experimental.

Add your own custom adapter with:

var CustomAdapter = {
  name: "custom",
  renderLineChart: function (chart) {
    chart.getElement().innerHTML = "Hi";
  }
};

Chartkick.adapters.unshift(CustomAdapter);

Examples

To run the files in the examples directory, you’ll need a web server. Run:

npm install -g serve
serve

and visit http://localhost:5000/examples/

Upgrading

3.0

Breaking changes

  • Removed xtype option - numeric axes are automatically detected
  • Removed window.Chartkick = {...} way to set config - use Chartkick.configure instead
  • Removed support for the Google Charts jsapi loader - use loader.js instead

2.0

Breaking changes

  • Chart.js is now the default adapter if multiple are loaded - yay open source!
  • Axis types are automatically detected - no need for discrete: true
  • Better date support - dates are no longer treated as UTC

Credits

Chartkick uses iso8601.js to parse dates and times.

History

View the changelog

Chartkick.js follows Semantic Versioning

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

See the Contributing Guide for more info.

Comments
  • Allowing custom date formatting for google charts and highcharts

    Allowing custom date formatting for google charts and highcharts

    Following up with the issue #10 I raised a couple of days ago I'm submitting this pull request.

    These changes allow custom date formatting for both highcharts and google charts.

    opened by alterx 14
  • Is it possible to link to an url when click on the chart ?

    Is it possible to link to an url when click on the chart ?

    Hello,

    Thank you for this tool! it's amazing! I'm newbie in rails / chartkick.

    I'm currently using pie charts, and I'm wondering if there is a way I can get each section of my pie chart to navigate to an url.

    I would like to know if it is possible / there is a way to , on clicking on the graph, it open an url link ? So if is there a way let me know. thanks!!!

        <h1>Pie Chart</h1>
        <div id="chart-2" style="height: 300px;"></div>
        <script>
          new Chartkick.PieChart("chart-2", [["Blueberry",44, "http://link1.com"],
                                                                 ["Strawberry",23, "http://link2.com"],
                                                                 ["Banana",22, "http://link3.com"],
                                                                 ["Apple",21, "http://link4.com"],
                                                                 ["Grape",13, "http://link5.com"]]);
        </script>
    
    opened by rafaeljneves 11
  • Add Calendar HeatMap

    Add Calendar HeatMap

    It would be great to add a calendar chart to this awesome library. Docs: https://developers.google.com/chart/interactive/docs/gallery/calendar?hl=en http://www.highcharts.com/demo/heatmap I'll be trying to implement this

    new chart 
    opened by chimeno 10
  • Adds xtitle & ytitle options

    Adds xtitle & ytitle options

    Support for hAxisTitle & vAxisTitle options for all charts.

    As per https://github.com/ankane/chartkick.js/pull/47#issuecomment-134474115

    Do you like the option names? I had some trouble choosing what to use.. xAxis, xAxisLabel, xAxisTitle etc etc...

    opened by buren 7
  • Support callback as data source

    Support callback as data source

    I initially need to add an Authorization header contains a bearer token. Since my project is already using a configured axios, supporting callback as data source would solve my problem and provide most flexibility meanwhile.

    opened by xinyifly 6
  • Make date and time formats configurable through options

    Make date and time formats configurable through options

    Hi @ankane

    This is with regards to https://github.com/ankane/chartkick/issues/445

    I've added two options:

    • date_format
    • time_format

    And kept the default values in case they aren't being used.

    I'm not sure if the naming is clear like this. Happy to hear your feedback.

    opened by rept 6
  • Add updateChart and updateAllCharts methods

    Add updateChart and updateAllCharts methods

    Two methods have been added to Chartkick:

    // Update one chart
    Chartkick.updateChart('chart-id');               // Update chart from the current dataSource
    Chartkick.updateChart('chart-id', newRemoteUrl); // Update chart from URL
    
    // Update all charts
    Chartkick.updateAllCharts(); // Updates from current `dataSource`
    Chartkick.updateAllCharts(function(chart) {
      return chart.dataSource + '?some_param=abc';
    }); // Updates all charts and appends ?some_param=abc to each chart's dataSource
    

    This also allows for simple pull-based updates of any chart:

    // Updates user-registration-chart every 10 seconds
    setInterval(function() {
      Chartkick.updateChart('user-registration-chart');
    }, 10000);
    

    Thank you for a great library!

    opened by buren 6
  • Automatic destroy all charts on turbo:before-render causing issues

    Automatic destroy all charts on turbo:before-render causing issues

    Currently Chartkick.js automatically adds a listener which removes all Chartkick.js charts upon turbo:before-render event. Unfortunately it is causing issues when there is a turbo form with data-turbo-frame and data-turbo-action specified. The latter causes that turbo:before-render is fired, and even though only contents of a single frame were updated, charts on the whole page are destroyed.

    Frankly speaking, there is a chance that it is a Turbo bug, but at least from our point of view it was easier to fix that on Chartkick.js side, by disabling the automatic turbo:before-render and adding a code which removes charts in a different way (see the comment).

    Anyway, maybe it is worth to check it, or at least provide some easy option of disabling automatic turbo:before-render listener?

    opened by ocher 5
  • 2.2.2 gives error on scatter_chart

    2.2.2 gives error on scatter_chart

    I updated chartkick via bower from 2.2.1 to 2.2.2 and now I get the error

    Type mismatch. Value 2016-06-26T12:30:00.000Z does not match type number in column index 0

    My setup is chartkick in Gemfile set to 2.2.2 and I install the js file via bower, version 2.2.2. This works perfectly with js file 2.2.1.

    opened by arni1981 5
  • Add support for 2nd y-axis

    Add support for 2nd y-axis

    Possible design (comments welcome)

    new Chartkick.LineChart(id, [{name: "Series 1", data: data1}, {name: "Series 2", data: data2, y2: true}])
    

    To set min and max values

    new Chartkick.LineChart(id, series, y2: {min: 0, max: 1000})
    
    enhancement 
    opened by ankane 5
  • Previously 0-values were not plotted into chart, now they do.

    Previously 0-values were not plotted into chart, now they do.

    For ChartJS, data with 0 values were not being plotted into the chart, so there were gaps in places where it shouldn't. That leads to confusion, as the 0 values were confused with null values (a.k.a no data). An use case is for example a chart that measures the heartbeats of an application, 0 values NEED to be plotted as that data exists and is valuable.

    opened by cizambra 5
Owner
Andrew Kane
Andrew Kane
Create beautiful JavaScript charts with one line of Ruby

Chartkick Create beautiful JavaScript charts with one line of Ruby. No more fighting with charting libraries! See it in action Chartkick 4.0 was recen

Andrew Kane 6.1k Jan 8, 2023
Smoothie Charts: smooooooth JavaScript charts for realtime streaming data

Smoothie Charts is a really small charting library designed for live streaming data. I built it to reduce the headaches I was getting from watching ch

Joe Walnes 2.2k Dec 13, 2022
Beautiful and interactive javascript charts for Java-based web applications.

Wicked Charts Beautiful and interactive JavaScript charts for Java-based web applications. Check out the Changelog Check out the Feature Overview with

adesso SE 85 Aug 23, 2022
Beautiful charts for Angular based on Chart.js

ng2-charts slack Beautiful charts for Angular based on Chart.js Usage & Demo Samples using ng2-charts https://valor-software.com/ng2-charts/ Installat

Valor Software 2.2k Jan 4, 2023
Progressive pie, donut, bar and line charts

Peity Peity (sounds like deity) is a jQuery plugin that converts an element's content into a mini <svg> pie, donut, line or bar chart. Basic Usage HTM

Ben Pickles 4.2k Jan 1, 2023
Chart.js plugin to create charts with a hand-drawn, sketchy, appearance

chartjs-plugin-rough Chart.js plugin to create charts with a hand-drawn, sketchy, appearance Version 0.2 requires Chart.js 2.7.0 or later, and Rough.j

Akihiko Kusanagi 73 Dec 1, 2022
Using ASP.NET Core, SignalR, and ChartJs to create real-time updating charts

Real-time Charts with ASP.NET Core, SignalR, and Chart.js This project shows how to update a real-time chart in your web browser using technologies li

Khalid Abuhakmeh 11 Nov 25, 2022
JavaScript diagramming library for interactive flowcharts, org charts, design tools, planning tools, visual languages.

GoJS, a JavaScript Library for HTML Diagrams GoJS is a JavaScript and TypeScript library for creating and manipulating diagrams, charts, and graphs. S

Northwoods Software Corporation 6.6k Dec 30, 2022
Attractive JavaScript charts for jQuery

flot About flot is a JavaScript plotting library for engineering and scientific applications derived from Flot: http://www.flotcharts.org/ Take a look

Flot 5.9k Dec 22, 2022
A plugin for the jQuery javascript library to generate small sparkline charts directly in the browser

jQuery Sparklines This jQuery plugin makes it easy to generate a number of different types of sparklines directly in the browser, using online a line

Gareth Watts 1.2k Jan 4, 2023
Ember Charts 3.5 2.3 L2 JavaScript A powerful and easy to use charting library for Ember.js

Ember Charts A charting library built with the Ember.js and d3.js frameworks. It includes time series, bar, pie, and scatter charts which are easy to

Addepar 793 Dec 7, 2022
Reusable JavaScript library for creating sketchy/hand-drawn styled charts in the browser.

roughViz.js is a reusable JavaScript library for creating sketchy/hand-drawn styled charts in the browser, based on D3v5, roughjs, and handy. Why? Use

Jared Wilber 6.4k Jan 4, 2023
📊 Interactive JavaScript Charts built on SVG

A modern JavaScript charting library to build interactive charts and visualizations with simple API. Our Partner ApexCharts is now a partner of Fusion

ApexCharts 12.1k Jan 3, 2023
Emprise Javascript Charts

EJSChart 100% Powerful, Clean & Functional Javascript Charts Whether at home, a medium sized or enterprise venture, EJSCharts will seamlessly help you

Emprise Corporation 34 Jun 3, 2021
A simple script for pure javascript charts.

MK Charts A simple pure Javascript for displaying circle charts. Demo: https://mkirschen.de/mk-scripts/mk-charts/ Circle charts To insert a chart all

Marcus Kirschen 6 Dec 14, 2022
Simple HTML5 Charts using the tag

Simple yet flexible JavaScript charting for designers & developers Documentation Currently, there are two versions of the library (2.9.4 and 3.x.x). V

Chart.js 59.4k Jan 7, 2023
Simple responsive charts

Big welcome by the Chartist Guy Checkout the documentation site at http://gionkunz.github.io/chartist-js/ Checkout this lightning talk that gives you

Gion Kunz 26 Dec 30, 2022
Simple, responsive, modern SVG Charts with zero dependencies

Frappe Charts GitHub-inspired modern, intuitive and responsive charts with zero dependencies Explore Demos » Edit at CodePen » Contents Installation U

Frappe 14.6k Jan 4, 2023
📊 A highly interactive data-driven visualization grammar for statistical charts.

English | 简体中文 G2 A highly interactive data-driven visualization grammar for statistical charts. Website • Tutorial Docs • Blog • G2Plot G2 is a visua

AntV team 11.5k Dec 30, 2022