Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

chartkick

ankane361.5kMIT5.0.1

Create beautiful charts with one line of JavaScript

charts

readme

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

Build Status

Quick Start

Run

npm install chartkick chart.js

And add

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

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

Charts

Create a div for the chart

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

Line chart

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

Pie chart

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

Column chart

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

Bar chart

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

Area chart

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

Scatter chart

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

Geo chart - Google Charts

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

Timeline - Google Charts

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

Multiple series

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

Multiple series stacked and grouped - Chart.js 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", data, {stacked: true})

Data

Data can be an array, object, callback, or URL.

Array

new Chartkick.LineChart("chart", [["2021-01-01", 2], ["2021-01-02", 3]])

Object

new Chartkick.LineChart("chart", {"2021-01-01": 2, "2021-01-02": 3})

Callback

function fetchData(success, fail) {
  success({"2021-01-01": 2, "2021-01-02": 3})
  // or fail("Data not available")
}

new Chartkick.LineChart("chart", fetchData)

URL

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

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

Options

Min and max for y-axis

new Chartkick.LineChart("chart", 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", data, {xmin: "2021-01-01", xmax: "2022-01-01"})

Colors

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

Stacked columns or bars

new Chartkick.ColumnChart("chart", 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", data, {discrete: true})

Label (for single series)

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

Axis titles

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

Straight lines between points instead of a curve

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

Hide points

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

Show or hide legend

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

Specify legend position

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

Donut chart

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

Prefix, useful for currency - Chart.js, Highcharts

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

Suffix, useful for percentages - Chart.js, Highcharts

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

Set a thousands separator - Chart.js, Highcharts

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

Set a decimal separator - Chart.js, Highcharts

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

Set significant digits - Chart.js, Highcharts

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

Set rounding - Chart.js, Highcharts

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

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

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

Friendly byte sizes - Chart.js

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

Specify the message when the chart is loading

new Chartkick.LineChart("chart", data, {loading: "Loading..."})

Specify the message when data is empty

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

Refresh data from a remote source every n seconds

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

You can pass options directly to the charting library with:

new Chartkick.LineChart("chart", 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", 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"]
}

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", 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", data, {download: true})

Set the filename

new Chartkick.LineChart("chart", 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", data, {download: {background: "#fff"}})

Installation

Chart.js

Run

npm install chartkick chart.js

And add

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

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 "chartkick/highcharts"

No Package Manager

Download chartkick.js directly.

For Chart.js (works with 4+), download it and the date-fns adapter bundle and use:

<script src="/path/to/chart.js"></script>
<script src="/path/to/chartjs-adapter-date-fns.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", 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

4.0

Run:

npm install chartkick@latest

For Chart.js, also run:

npm install chart.js@latest

And change:

import Chart from "chart.js"

Chartkick.use(Chart)

to:

import "chartkick/chart.js"

History

View the changelog

Contributing

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

To get started with development:

git clone https://github.com/ankane/chartkick.js.git
cd chartkick.js
npm install
npm run build

# start web server
npm install -g serve
serve

changelog

5.0.1 (2023-01-19)

  • Fixed issue with monthly and yearly data with Chart.js
  • Fixed tooltip and formatting for numeric x-axis with Chart.js

5.0.0 (2023-01-18)

  • Added support for Chart.js 4
  • Fixed tooltip for negative values with bytes
  • Dropped support for AJAX through jQuery and Zepto
  • Dropped support for Chart.js 3

4.2.0 (2022-05-25)

  • Added autoDestroy config

4.1.1 (2021-11-06)

  • Fixed xmin and xmax for Chart.js

4.1.0 (2021-10-23)

  • Added support for Turbo

4.0.5 (2021-07-07)

  • Fixed x-axis type and position library options
  • Fixed pointFormat for Highcharts

4.0.4 (2021-05-01)

  • Fixed error with destroy function
  • Fixed error with hidden charts

4.0.3 (2021-04-10)

  • Fixed error with background option for downloads
  • Fixed null values in stacked column and bar charts

4.0.2 (2021-04-06)

  • Fixed error with time parsing

4.0.1 (2021-04-06)

  • Fixed time parsing logic

4.0.0 (2021-04-04)

  • Added support for Chart.js 3
  • Added loading option
  • Added destroyAll function
  • Added chartkick:load event
  • Improved colors option for single-series column and bar charts
  • Increased hover radius for line, area, scatter, and bubble charts
  • Prefer empty over messages: {empty: ...}
  • Stopped refresh when chart is destroyed
  • Fixed gridline orientation for bar charts

Breaking changes

  • Removed support for Chart.js 2
  • Charts with no data show message instead of empty chart
  • Dates are shown in local time instead of UTC for Highcharts to be consistent with other adapters

3.2.1 (2020-07-23)

  • Added support for petabytes and exabytes
  • Fixed error with xmin and xmax and empty data
  • Fixed error with GeoChart with latest Google Charts release

3.2.0 (2019-11-09)

  • Fixed prototype pollution - see #117
  • Added bytes option for Chart.js
  • Added precision option
  • Added round option
  • Added zeros option

3.1.3 (2019-10-27)

  • Removed Error Loading Chart prefix for callback errors

3.1.2 (2019-10-27)

  • Added support for callbacks as data
  • Fixed stacked option for Highcharts area charts
  • Fixed error with jQuery slim
  • Fixed deprecation warning with Chart.js 2.9.0+

3.1.1 (2019-07-15)

  • Fixed missing bar chart labels with Chart.js 2.8.0

3.1.0 (2019-05-26)

  • Improved require so default is no longer needed
  • Added use function
  • Added xmin and xmax for Chart.js
  • Fixed Highcharts tooltip colors
  • Fixed error when passing images to options

3.0.2 (2019-01-03)

  • Added Chartkick.setDefaultOptions function
  • Added points and curve options for individual series with Chart.js
  • Added support for download background color
  • Raise an error for refresh option and non-URL data source

3.0.1 (2018-08-13)

  • Fixed numeric axes for Chart.js column and bar charts

3.0.0 (2018-08-08)

  • Added code option
  • Fixed dataset option for scatter charts

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.3.6 (2018-05-31)

  • Added dataset option for Chart.js
  • Added destroy function

2.3.5 (2018-03-26)

  • Added addAdapter function

2.3.4 (2018-03-25)

  • Fixed module build
  • Fixed Cannot read property 'config' of undefined error
  • Fixed error when switching between data and no data with empty message with Chart.js

2.3.3 (2018-02-26)

  • Fixed error when minified

2.3.2 (2018-02-24)

  • Fixed export for modules
  • Added more stacked options

2.3.1 (2018-02-23)

  • Fixed issues with thousands option
  • Friendlier error messages when charting library not found
  • Refresh interval now updates when refresh option changed
  • Removed experimental Chartkick.createChart function
  • Stopped pushing updates to Bower

2.3.0 (2018-02-21)

  • Added prefix and suffix options for Chart.js and Highcharts
  • Added thousands and decimal options for Chart.js and Highcharts
  • Added messages option
  • Fixed boolean labels for column chart
  • Clean up charts before refresh

2.2.4 (2017-05-14)

  • Added multiple series stacked and grouped charts - Chart.js and Highcharts
  • Fixed refreshData after updateData
  • Fixed redraw issue with HighCharts

2.2.3 (2017-02-22)

  • Added xtype option
  • Added points option
  • Added mapsApiKey option to configure

2.2.2 (2017-01-07)

  • Fixed missing scatter points for Google Charts
  • Fixed scatter chart for Highcharts
  • Limit concurrent requests to avoid overloading servers
  • Added ability to specify color with series for Chart.js and Highcharts
  • Added bubble chart for Chart.js [experimental]

2.2.1 (2016-12-05)

  • Added curve option
  • Added legend option
  • Added title option
  • Added getAdapter function
  • Added setOptions function
  • Added redraw function
  • Fixed column order for Google Charts and Highcharts

2.2.0 (2016-12-03)

  • Added global options
  • Added download option - Chart.js only
  • Added updateData function
  • Added refreshData function
  • Added refresh option
  • Added stopRefresh function
  • Added getDataSource function
  • Added donut option to pie chart
  • Added eachChart function
  • Remove colors from tooltips for Chart.js pie chart

2.1.2 (2016-11-29)

  • Fix for missing zero values for Chart.js

2.1.1 (2016-11-28)

  • Fix for missing values for multiple series column chart with sparse data
  • Remove colors from tooltips for Chart.js

2.1.0 (2016-09-10)

  • Added basic support for new Google Charts loader
  • Added configure function
  • Dropped jQuery and Zepto dependencies for AJAX
  • Fixed legend colors on scatter chart for Chart.js

2.0.1 (2016-08-11)

  • Added scatter chart for Chart.js
  • Fixed error with xtitle and ytitle on column and bar charts
  • Fixed all zeros with Chart.js
  • Fixed odd tick spacing with Chart.js

2.0.0 (2016-05-30)

  • Chart.js is now the default adapter - yay open source!
  • Axis types are automatically detected - no need for discrete: true
  • Better date support
  • New official API
  • Fixed min and max for Chart.js bar charts

1.5.1 (2016-05-03)

  • Added bar chart for Chart.js
  • Added library option for series
  • Better tick selection for time and discrete scales

1.5.0 (2016-05-01)

  • Added Chart.js adapter beta
  • Added smarterDates option (temporary until 2.0)
  • Added smarterDiscrete option (temporary until 2.0)
  • Fixed line height on timeline charts

1.4.2 (2016-02-29)

  • Added label option
  • Better tooltip for dates for Google Charts

1.4.1 (2015-09-07)

  • Fixed regression with min: null

1.4.0 (2015-08-31)

  • Added scatter chart
  • Added axis titles

1.3.0 (2014-10-09)

  • Added timelines
  • Added adapter option

1.2.2 (2014-03-29)

  • Added colors option

1.2.1 (2014-03-23)

  • Added discrete option

1.2.0 (2014-02-23)

  • Added geo chart
  • Added stacked option

1.1.1 (2013-12-08)

  • Made sure options can be overridden
  • Added support for Google Charts localization

1.1.0 (2013-06-27)

  • Added bar chart and area chart
  • Resize charts when window is resized

1.0.2 (2013-06-11)

  • Added library option

1.0.1 (2013-05-23)

  • Added support for Highcharts 2.1+
  • Fixed sorting for line chart with multiple series and Google Charts

1.0.0 (2013-05-15)

  • First major release