# Chartkick Create beautiful Javascript charts with one line of Ruby. No more fighting with charting libraries! [See it in action](http://ankane.github.io/chartkick/) Works with Rails, Sinatra and most browsers (including IE 6) :two_hearts: A perfect companion to [groupdate](https://github.com/ankane/groupdate), [hightop](https://github.com/ankane/hightop), and [active_median](https://github.com/ankane/active_median) :speech_balloon: Get [handcrafted updates](http://chartkick.us7.list-manage.com/subscribe?u=952c861f99eb43084e0a49f98&id=6ea6541e8e&group[0][1]=true) for new features ## Charts Line chart ```erb <%= line_chart User.group_by_day(:created_at).count %> ``` Pie chart ```erb <%= pie_chart Goal.group(:name).count %> ``` Column chart ```erb <%= column_chart Task.group_by_hour_of_day(:created_at, format: "%l %P").count %> ``` Bar chart ```erb <%= bar_chart Shirt.group(:size).sum(:price) %> ``` Area chart ```erb <%= area_chart Visit.group_by_minute(:created_at).maximum(:load_time) %> ``` Geo chart ```erb <%= geo_chart Medal.group(:country).count %> ``` Timeline ```erb <%= timeline [ ["Washington", "1789-04-29", "1797-03-03"], ["Adams", "1797-03-03", "1801-03-03"], ["Jefferson", "1801-03-03", "1809-03-03"] ] %> ``` Multiple series ```erb <%= line_chart @goals.map{|goal| {name: goal.name, data: goal.feats.group_by_week(:created_at).count} } %> ``` or ```erb <%= line_chart Feat.group(:goal_id).group_by_week(:created_at).count %> ``` ### Say Goodbye To Timeouts Make your pages load super fast and stop worrying about timeouts. Give each chart its own endpoint. ```erb <%= line_chart completed_tasks_charts_path %> ``` And in your controller, pass the data as JSON. ```ruby class ChartsController < ApplicationController def completed_tasks render json: Task.group_by_day(:completed_at).count end end ``` **Note:** This feature requires [jQuery](http://jquery.com/) or [Zepto](http://zeptojs.com/) at the moment. For multiple series, add `chart_json` at the end. ```ruby render json: Task.group(:goal_id).group_by_day(:completed_at).count.chart_json ``` ### Options Id and height ```erb <%= line_chart data, id: "users-chart", height: "500px" %> ``` Min and max values ```erb <%= 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. Colors ```erb <%= line_chart data, colors: ["pink", "#999"] %> ``` Stacked columns or bars ```erb <%= column_chart data, stacked: true %> ``` Discrete axis ```erb <%= line_chart data, discrete: true %> ``` You can pass options directly to the charting library with: ```erb <%= line_chart data, library: {backgroundColor: "#eee"} %> ``` See the documentation for [Google Charts](https://developers.google.com/chart/interactive/docs/gallery) and [Highcharts](http://api.highcharts.com/highcharts) for more info. ### Global Options To set options for all of your charts, create an initializer `config/initializers/chartkick.rb` with: ```ruby Chartkick.options = { height: "400px", colors: ["pink", "#999"] } ``` Customize the html ```ruby Chartkick.options[:html] = '