All Projects → ankane → Chartkick

ankane / Chartkick

Licence: mit
Create beautiful JavaScript charts with one line of Ruby

Programming Languages

ruby
36898 projects - #4 most used programming language
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Chartkick

chart
📊📉 Add beautiful and reusable charts with one line of ruby for Rails 5.x
Stars: ✭ 42 (-99.29%)
Mutual labels:  highcharts, chartjs, google-charts
Chartkick.py
Create beautiful Javascript charts with minimal code
Stars: ✭ 695 (-88.23%)
Mutual labels:  charts, chartjs, highcharts
scalajs-highcharts
Scala.js static typed facades for Highcharts library
Stars: ✭ 30 (-99.49%)
Mutual labels:  charts, highcharts
robo-chart-web
📊 Transform Google sheets to pretty charts!
Stars: ✭ 28 (-99.53%)
Mutual labels:  charts, chartjs
dashblocks-template
Dashblocks Vue Material Admin Template
Stars: ✭ 143 (-97.58%)
Mutual labels:  charts, chartjs
Highcharts Ng
AngularJS directive for Highcharts
Stars: ✭ 1,741 (-70.51%)
Mutual labels:  charts, highcharts
Chartbrew
Open-source web platform for creating charts out of different data sources (databases and APIs) 📈📊
Stars: ✭ 199 (-96.63%)
Mutual labels:  charts, chartjs
vue3-chartjs
Vue3 wrapper for ChartJS
Stars: ✭ 122 (-97.93%)
Mutual labels:  charts, chartjs
laravel-chartjs
No description or website provided.
Stars: ✭ 13 (-99.78%)
Mutual labels:  charts, chartjs
React Jsx Highcharts
Highcharts built with proper React components
Stars: ✭ 336 (-94.31%)
Mutual labels:  charts, highcharts
React Chartjs 2
React components for Chart.js, the most popular charting library
Stars: ✭ 4,667 (-20.94%)
Mutual labels:  charts, chartjs
Yii2 Chartjs Widget
ChartJs Widget For Yii2
Stars: ✭ 95 (-98.39%)
Mutual labels:  charts, chartjs
Dashblocks
Enable Analytics in your Apps
Stars: ✭ 48 (-99.19%)
Mutual labels:  charts, chartjs
React Native Highcharts
📈 Add Highcharts charts to react-native app for IOS and Android
Stars: ✭ 247 (-95.82%)
Mutual labels:  charts, highcharts
Aachartcore
📈📊☕️☕️☕️An elegant modern declarative data visualization chart framework for Android. Extremely powerful, supports line, spline, area, areaspline, column, bar, pie, scatter, angular gauges, arearange, areasplinerange, columnrange, bubble, box plot, error bars, funnel, waterfall and polar chart types.极其精美而又强大的 Android 数据可视化图表框架,支持柱状图、条形图、折线图、曲线图、折线填充图、曲线填充图、气泡图、扇形图、环形图、散点图、雷达图、混合图等各种类型的多达几十种的信息图图表,完全满足工作所需.
Stars: ✭ 424 (-92.82%)
Mutual labels:  charts, highcharts
ipychart
The power of Chart.js with Python
Stars: ✭ 48 (-99.19%)
Mutual labels:  charts, chartjs
Chartjs.blazor
Brings Chart.js charts to Blazor
Stars: ✭ 402 (-93.19%)
Mutual labels:  charts, chartjs
Awesome
A curated list of awesome Chart.js resources and libraries
Stars: ✭ 621 (-89.48%)
Mutual labels:  charts, chartjs
Devextreme Angular
Angular UI and data visualization components
Stars: ✭ 497 (-91.58%)
Mutual labels:  charts
Highcharter
R wrapper for highcharts
Stars: ✭ 583 (-90.12%)
Mutual labels:  highcharts

Chartkick

Create beautiful JavaScript charts with one line of Ruby. No more fighting with charting libraries!

See it in action

Chartkick 4.0 was recently released - see how to upgrade

🔥 For admin charts and dashboards, check out Blazer, and for advanced visualizations, check out Vega

💕 A perfect companion to Groupdate, Hightop, and ActiveMedian

Build Status

Quick Start

Add this line to your application’s Gemfile:

gem "chartkick"

For Rails 6 / Webpacker, run:

yarn add chartkick chart.js

And in app/javascript/packs/application.js, add:

import "chartkick/chart.js"

For Rails 5 / Sprockets, in app/assets/javascripts/application.js, add:

//= require chartkick
//= require Chart.bundle

For Rails 7 / Importmap (experimental), 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"

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

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.

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 only
  • points - Chart.js only
  • curve - 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" %>

Installation

Add this line to your application's Gemfile:

gem "chartkick"

Next, choose your charting library.

Chart.js

For Rails 6 / Webpacker, run:

yarn add chartkick chart.js

And in app/javascript/packs/application.js, add:

import "chartkick/chart.js"

For Rails 5 / Sprockets, in app/assets/javascripts/application.js, add:

//= require chartkick
//= require Chart.bundle

For Rails 7 / Importmap (experimental), 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"

Google Charts

In your layout or views, add:

<%= javascript_include_tag "https://www.gstatic.com/charts/loader.js" %>

For Rails 6 / Webpacker, run:

yarn add chartkick

And in app/javascript/packs/application.js, add:

import "chartkick"

For Rails 5 / Sprockets, in app/assets/javascripts/application.js, add:

//= require chartkick

For Rails 7 / Importmap (experimental), in config/importmap.rb, add:

pin "chartkick", to: "chartkick.js"

And in app/javascript/application.js, add:

import "chartkick"

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

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

before your charts.

Highcharts

For Rails 6 / Webpacker, run:

yarn add chartkick highcharts

And in app/javascript/packs/application.js, add:

import "chartkick/highcharts"

For Rails 5 / 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

For Rails 7 / Importmap (experimental), in config/importmap.rb, add:

pin "chartkick", to: "chartkick.js"

And run:

bin/importmap pin highcharts --download

And in app/javascript/application.js, add:

import "chartkick"
import Highcharts from "highcharts"

window.Highcharts = Highcharts

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>

Multiple Libraries

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

<%= line_chart data, adapter: "google" %> <!-- or highcharts or chartjs -->

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

No Ruby? No Problem

Check out chartkick.js

Tutorials

Upgrading

4.0

If you use Sprockets, update the gem and you’re good to go!

If you use Webpacker, run:

yarn upgrade chartkick --latest

If you use Chart.js with Webpacker, also run:

yarn upgrade chart.js --latest

And in app/javascript/packs/application.js, change:

require("chartkick")
require("chart.js")

to:

require("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.git
cd chartkick
bundle install
bundle exec rake test
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].