Top Related Projects
Highcharts JS, the JavaScript charting framework
Simple HTML5 Charts using the <canvas> tag
Bring data to life with SVG, Canvas and HTML. :bar_chart::chart_with_upwards_trend::tada:
Open-source JavaScript charting library behind Plotly and Dash
📊 Interactive JavaScript Charts built on SVG
JavaScript 3D Library.
Quick Overview
Chartkick is a powerful and easy-to-use charting library for Ruby, JavaScript, and Python. It allows developers to create beautiful charts with minimal code, supporting various chart types and integrating seamlessly with popular web frameworks like Rails, Vue, and React.
Pros
- Simple and intuitive API for creating charts quickly
- Supports multiple backends (Chart.js, Google Charts, Highcharts)
- Integrates well with various web frameworks and languages
- Responsive and mobile-friendly charts out of the box
Cons
- Limited customization options compared to using chart libraries directly
- Dependency on external charting libraries may increase load times
- Some advanced chart types or features may not be supported
- Documentation could be more comprehensive for complex use cases
Code Examples
- Creating a line chart in Ruby:
<%= line_chart User.group_by_day(:created_at).count %>
- Creating a pie chart in JavaScript:
new Chartkick.PieChart("chart-1", {"Blueberry": 44, "Strawberry": 23, "Banana": 22, "Apple": 21})
- Creating a column chart with multiple series in Python:
column_chart([
{"name": "Workout", "data": {"2021-01-01": 3, "2021-01-02": 4, "2021-01-03": 3}},
{"name": "Call parents", "data": {"2021-01-01": 5, "2021-01-02": 3, "2021-01-03": 4}}
])
Getting Started
To use Chartkick in a Rails application:
-
Add to your Gemfile:
gem 'chartkick'
-
Run:
bundle install
-
In your application.js, add:
//= require chartkick //= require Chart.bundle
-
In your view, create a chart:
<%= line_chart @data %>
Competitor Comparisons
Highcharts JS, the JavaScript charting framework
Pros of Highcharts
- More extensive and feature-rich charting library with a wide variety of chart types
- Highly customizable with advanced styling and animation options
- Better suited for complex, interactive data visualizations
Cons of Highcharts
- Commercial license required for most use cases, which can be costly
- Steeper learning curve due to its extensive API and configuration options
- Larger file size, potentially impacting page load times
Code Comparison
Chartkick:
<%= line_chart User.group_by_day(:created_at).count %>
Highcharts:
Highcharts.chart('container', {
series: [{
data: [1, 2, 3, 4, 5]
}]
});
Key Differences
- Chartkick focuses on simplicity and ease of use, while Highcharts offers more advanced features
- Chartkick is open-source and free for all uses, whereas Highcharts requires a commercial license for most applications
- Chartkick provides a higher-level abstraction, making it easier to create charts with less code
- Highcharts offers more fine-grained control over chart appearance and behavior
Use Cases
- Chartkick: Ideal for quickly adding simple charts to web applications, especially in Ruby on Rails projects
- Highcharts: Better suited for complex data visualizations, dashboards, and applications requiring extensive customization
Simple HTML5 Charts using the <canvas> tag
Pros of Chart.js
- More customizable and flexible, offering a wide range of chart types and options
- Better performance for large datasets and complex visualizations
- Extensive documentation and a larger community for support
Cons of Chart.js
- Steeper learning curve, requiring more JavaScript knowledge
- Larger file size, which may impact page load times
- Requires more code to create basic charts compared to Chartkick
Code Comparison
Chartkick:
<%= line_chart User.group_by_day(:created_at).count %>
Chart.js:
new Chart(ctx, {
type: 'line',
data: {
labels: ['Day 1', 'Day 2', 'Day 3'],
datasets: [{
data: [12, 19, 3]
}]
}
});
Summary
Chart.js offers more advanced features and customization options, making it suitable for complex visualizations. However, it requires more JavaScript knowledge and code to implement. Chartkick provides a simpler, more concise approach for basic charts, especially when working with Ruby on Rails applications. The choice between the two depends on the project's requirements, the developer's expertise, and the desired level of customization.
Bring data to life with SVG, Canvas and HTML. :bar_chart::chart_with_upwards_trend::tada:
Pros of d3
- Highly flexible and customizable, allowing for complex and unique visualizations
- Extensive library with a wide range of chart types and data manipulation tools
- Large community and ecosystem with numerous extensions and resources
Cons of d3
- Steeper learning curve due to its low-level nature and complexity
- Requires more code and setup for basic charts compared to simpler libraries
- May be overkill for simple visualization needs
Code Comparison
d3:
const svg = d3.select("body").append("svg")
.attr("width", 400)
.attr("height", 300);
svg.selectAll("rect")
.data(data)
.enter()
.append("rect")
.attr("x", (d, i) => i * 70)
.attr("y", (d, i) => 300 - 10 * d)
.attr("width", 65)
.attr("height", (d, i) => d * 10);
Chartkick:
new Chartkick.BarChart("chart-1", data);
Summary
d3 offers unparalleled flexibility and power for creating complex, custom visualizations, but comes with a steeper learning curve. Chartkick, on the other hand, provides a simpler, more straightforward approach for creating common chart types with minimal code. While d3 excels in scenarios requiring highly customized or interactive visualizations, Chartkick is better suited for quickly implementing standard charts with less development overhead.
Open-source JavaScript charting library behind Plotly and Dash
Pros of Plotly.js
- More advanced and customizable charting options
- Supports interactive and animated visualizations
- Extensive documentation and community support
Cons of Plotly.js
- Steeper learning curve due to its complexity
- Larger file size, which may impact page load times
- Requires more setup and configuration
Code Comparison
Chartkick example:
<%= line_chart User.group_by_day(:created_at).count %>
Plotly.js example:
Plotly.newPlot('myDiv', [{
x: [1, 2, 3, 4],
y: [10, 15, 13, 17],
type: 'scatter'
}]);
Summary
Plotly.js offers more advanced features and customization options, making it suitable for complex data visualizations. However, it comes with a steeper learning curve and larger file size. Chartkick, on the other hand, provides a simpler and more straightforward approach to creating charts, making it ideal for quick implementations and basic visualizations. The choice between the two depends on the specific requirements of your project and the level of complexity needed in your charts.
📊 Interactive JavaScript Charts built on SVG
Pros of ApexCharts.js
- More extensive chart types and customization options
- Interactive features like zooming and panning
- Responsive and mobile-friendly design
Cons of ApexCharts.js
- Steeper learning curve due to more complex API
- Larger file size, which may impact page load times
- Requires more configuration for basic charts
Code Comparison
Chartkick:
<%= line_chart User.group_by_day(:created_at).count %>
ApexCharts.js:
var options = {
chart: { type: 'line' },
series: [{ data: [30, 40, 35, 50, 49, 60, 70] }],
xaxis: { categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul'] }
};
var chart = new ApexCharts(document.querySelector("#chart"), options);
chart.render();
Chartkick offers a simpler, more concise syntax for basic charts, making it easier to quickly generate visualizations with minimal code. ApexCharts.js provides more granular control over chart appearance and behavior but requires more verbose configuration.
Both libraries have their strengths: Chartkick excels in simplicity and ease of use, particularly for Ruby on Rails applications, while ApexCharts.js offers more advanced features and customization options for complex data visualizations across various frameworks.
JavaScript 3D Library.
Pros of three.js
- Powerful 3D graphics rendering capabilities
- Extensive documentation and large community support
- Wide range of features for complex 3D visualizations
Cons of three.js
- Steeper learning curve for beginners
- Larger file size and potentially higher performance overhead
- May be overkill for simple 2D chart creation
Code Comparison
three.js:
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
Chartkick:
new Chartkick.LineChart("chart-1", {"2021-01-01": 11, "2021-01-02": 6});
three.js is a comprehensive 3D graphics library, offering advanced features for creating complex 3D scenes and animations. It provides granular control over the rendering process and supports various 3D objects and effects.
Chartkick, on the other hand, is focused on simplifying the creation of charts and graphs. It offers a more straightforward API for generating common chart types with minimal code, making it easier for developers to quickly create data visualizations without dealing with low-level graphics programming.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Chartkick
Create beautiful JavaScript charts with one line of Ruby. No more fighting with charting libraries!
:fire: For admin charts and dashboards, check out Blazer, and for advanced visualizations, check out Vega
:two_hearts: A perfect companion to Groupdate, Hightop, and ActiveMedian
Quick Start
Add this line to your applicationâs Gemfile:
gem "chartkick"
Then follow the instructions for your JavaScript setup:
- Importmap (Rails 7 default)
- esbuild, rollup.js, or Webpack
- Webpacker (Rails 6 default)
- Sprockets
This sets up Chartkick with Chart.js. For other charting libraries and frameworks, see these instructions.
Importmap
In config/importmap.rb
, add:
pin "chartkick", to: "chartkick.js"
pin "Chart.bundle", to: "Chart.bundle.js"
And in app/javascript/application.js
, add:
import "chartkick"
import "Chart.bundle"
esbuild, rollup.js, or Webpack
Run:
yarn add chartkick chart.js
And in app/javascript/application.js
, add:
import "chartkick/chart.js"
Note: For rollup.js, this requires format: "iife"
in rollup.config.js
.
Webpacker
Run:
yarn add chartkick chart.js
And in app/javascript/packs/application.js
, add:
import "chartkick/chart.js"
Sprockets
In app/assets/javascripts/application.js
, add:
//= require chartkick
//= require Chart.bundle
Charts
Line chart
<%= line_chart User.group_by_day(:created_at).count %>
Pie chart
<%= pie_chart Goal.group(:name).count %>
Column chart
<%= column_chart Task.group_by_hour_of_day(:created_at, format: "%l %P").count %>
Bar chart
<%= bar_chart Shirt.group(:size).sum(:price) %>
Area chart
<%= area_chart Visit.group_by_minute(:created_at).maximum(:load_time) %>
Scatter chart
<%= scatter_chart City.pluck(:size, :population) %>
Geo chart - Google Charts
<%= geo_chart Medal.group(:country).count %>
Timeline - Google Charts
<%= timeline [
["Washington", "1789-04-29", "1797-03-03"],
["Adams", "1797-03-03", "1801-03-03"],
["Jefferson", "1801-03-03", "1809-03-03"]
] %>
Multiple series
<%= line_chart [
{name: "Workout", data: {"2021-01-01" => 3, "2021-01-02" => 4}},
{name: "Call parents", data: {"2021-01-01" => 5, "2021-01-02" => 3}}
] %>
or
<%= line_chart Feat.group(:goal_id).group_by_week(:created_at).count %>
Data
Data can be a hash, array, or URL.
Hash
<%= line_chart({"2021-01-01" => 2, "2021-01-02" => 3}) %>
Array
<%= line_chart [["2021-01-01", 2], ["2021-01-02", 3]] %>
URL
Make your pages load super fast and stop worrying about timeouts. Give each chart its own endpoint.
<%= line_chart completed_tasks_charts_path %>
And in your controller, pass the data as JSON.
class ChartsController < ApplicationController
def completed_tasks
render json: Task.group_by_day(:completed_at).count
end
end
For multiple series, add chart_json
at the end.
render json: Task.group(:goal_id).group_by_day(:completed_at).count.chart_json
Options
Id, width, and height
<%= line_chart data, id: "users-chart", width: "800px", height: "500px" %>
Min and max values
<%= line_chart data, min: 1000, max: 5000 %>
min
defaults to 0 for charts with non-negative values. Use nil
to let the charting library decide.
Min and max for x-axis - Chart.js
<%= line_chart data, xmin: "2021-01-01", xmax: "2022-01-01" %>
Colors
<%= line_chart data, colors: ["#b00", "#666"] %>
Stacked columns or bars
<%= column_chart data, stacked: true %>
Discrete axis
<%= line_chart data, discrete: true %>
Label (for single series)
<%= line_chart data, label: "Value" %>
Axis titles
<%= line_chart data, xtitle: "Time", ytitle: "Population" %>
Straight lines between points instead of a curve
<%= line_chart data, curve: false %>
Hide points
<%= line_chart data, points: false %>
Show or hide legend
<%= line_chart data, legend: false %>
Specify legend position
<%= line_chart data, legend: "bottom" %>
Donut chart
<%= pie_chart data, donut: true %>
Prefix, useful for currency - Chart.js, Highcharts
<%= line_chart data, prefix: "$" %>
Suffix, useful for percentages - Chart.js, Highcharts
<%= line_chart data, suffix: "%" %>
Set a thousands separator - Chart.js, Highcharts
<%= line_chart data, thousands: "," %>
Set a decimal separator - Chart.js, Highcharts
<%= line_chart data, decimal: "," %>
Set significant digits - Chart.js, Highcharts
<%= line_chart data, precision: 3 %>
Set rounding - Chart.js, Highcharts
<%= line_chart data, round: 2 %>
Show insignificant zeros, useful for currency - Chart.js, Highcharts
<%= line_chart data, round: 2, zeros: true %>
Friendly byte sizes - Chart.js
<%= line_chart data, bytes: true %>
Specify the message when data is loading
<%= line_chart data, loading: "Loading..." %>
Specify the message when data is empty
<%= line_chart data, empty: "No data" %>
Refresh data from a remote source every n
seconds
<%= line_chart url, refresh: 60 %>
You can pass options directly to the charting library with:
<%= line_chart data, library: {backgroundColor: "#eee"} %>
See the documentation for Chart.js, Google Charts, and Highcharts for more info. For Chart.js plugins, check out this guide.
To customize datasets in Chart.js, use:
<%= line_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, create an initializer config/initializers/chartkick.rb
with:
Chartkick.options = {
height: "400px",
colors: ["#b00", "#666"]
}
Customize the html
Chartkick.options[:html] = '<div id="%{id}" style="height: %{height};">%{loading}</div>'
You capture the JavaScript in a content block with:
Chartkick.options[:content_for] = :charts_js
Then, in your layout, use:
<%= yield :charts_js %>
For Padrino, use yield_content
instead of yield
.
This is great for including all of your JavaScript at the bottom of the page.
Multiple Series
You can pass a few options with a series:
name
data
color
dataset
- Chart.js onlypoints
- Chart.js onlycurve
- Chart.js only
Code
If you want to use the charting library directly, get the code with:
<%= line_chart data, code: true %>
The code will be logged to the JavaScript console. 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.
<%= line_chart data, download: true %>
Safari will open the image in a new window instead of downloading.
Set the filename
<%= line_chart data, download: {filename: "boom"} %>
Set the background color
<%= line_chart data, download: {background: "#ffffff"} %>
Set title
<%= line_chart data, title: "Awesome chart" %>
Additional Charting Libraries
Google Charts
In your layout or views, add:
<%= javascript_include_tag "https://www.gstatic.com/charts/loader.js" %>
For Importmap (Rails 7 default), in config/importmap.rb
, add:
pin "chartkick", to: "chartkick.js"
And in app/javascript/application.js
, add:
import "chartkick"
For Webpacker (Rails 6 default), run:
yarn add chartkick
And in app/javascript/packs/application.js
, add:
import "chartkick"
For Sprockets, in app/assets/javascripts/application.js
, add:
//= require chartkick
To specify a language or Google Maps API key, use:
Chartkick.configure({language: "de", mapsApiKey: "..."})
before your charts.
Highcharts
For Importmap (Rails 7 default), run:
bin/importmap pin highcharts --download
And in config/importmap.rb
, add:
pin "chartkick", to: "chartkick.js"
And in app/javascript/application.js
, add:
import "chartkick"
import Highcharts from "highcharts"
window.Highcharts = Highcharts
For Webpacker (Rails 6 default), run:
yarn add chartkick highcharts
And in app/javascript/packs/application.js
, add:
import "chartkick/highcharts"
For Sprockets, download highcharts.js into vendor/assets/javascripts
(or use yarn add highcharts
in Rails 5.1+), and in app/assets/javascripts/application.js
, add:
//= require chartkick
//= require highcharts
Multiple Libraries
If more than one charting library is loaded, choose between them with:
<%= line_chart data, adapter: "google" %> <!-- or highcharts or chartjs -->
Sinatra and Padrino
Download chartkick.js and include it manually.
<script src="chartkick.js"></script>
Then include the charting library.
Chart.js - download Chart.js and the date-fns adapter bundle
<script src="chart.js"></script>
<script src="chartjs-adapter-date-fns.bundle.js"></script>
Google Charts
<script src="https://www.gstatic.com/charts/loader.js"></script>
Highcharts - download highcharts.js
<script src="highcharts.js"></script>
JavaScript 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()
Destroy the chart with:
chart.destroy()
Loop over charts with:
Chartkick.eachChart(function (chart) {
// do something
})
Content Security Policy (CSP)
Check out how to configure CSP
Tutorials
- Charts with Chartkick and Groupdate
- Creando gráficos en Ruby on Rails con Chartkick y Chart.js
- Make Easy Graphs and Charts on Rails with Chartkick
- Practical Graphs on Rails: Chartkick in Practice
Upgrading
5.0
If you use Importmap or Sprockets, update the gem and youâre good to go!
If you use esbuild, Webpack, or Webpacker, run:
yarn upgrade chartkick --latest
If you use Chart.js with esbuild, Webpack, or Webpacker, also run:
yarn upgrade chart.js --latest
History
View the changelog
Contributing
Everyone is encouraged to help improve this project. Here are a few ways you can help:
- Report bugs
- Fix bugs and submit pull requests
- Write, clarify, or fix documentation
- Suggest or add new features
To get started with development:
git clone https://github.com/ankane/chartkick.git
cd chartkick
bundle install
bundle exec rake test
Top Related Projects
Highcharts JS, the JavaScript charting framework
Simple HTML5 Charts using the <canvas> tag
Bring data to life with SVG, Canvas and HTML. :bar_chart::chart_with_upwards_trend::tada:
Open-source JavaScript charting library behind Plotly and Dash
📊 Interactive JavaScript Charts built on SVG
JavaScript 3D Library.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot