Giter Club home page Giter Club logo

amcharts.rb's Introduction

AmCharts.rb Gem Version Build Status

Ruby on Rails wrapper for creating AmCharts charts.

Usage

A chart is represented by a subclass of AmCharts::Chart (ie. Pie or Serial), which is initialized with a data collection (should be an array of homogeneous hashes), and some settings passed into a block:

chart = AmCharts::Chart::Serial.new(data) do |c|
  add_title 'My Fancy Chart', size: 18
  loading_indicator!

  c.dimensions = '800x600'

  legends.new do |l|
    l.position = 'right'
    l.value_text = '[[percents]]%'
  end

  scrollbar {}
end

The setup block accepts a number of methods for ease of setting up a chart, and accepts any other assignment method as a setting that will be passed directly to AmCharts when the chart is rendered. Chart components including graphs, legends, axes, scrollbars and cursors can also be added and configured using inner blocks. Listeners can also be defined in a similar manner.

Values in the setup block can be numeric or string literals (which will be passed along as is); symbols (which will be given to I18n.t before being rendered); procs (which will be executed); or the function() method (which will act as a literal javascript function).

An AmCharts::Chart object can then be rendered using the amcharts helper:

amcharts(chart, 'id-of-container')

If the container ID given doesn't already exist in the page, it will be automatically created before the chart is rendered by AmCharts.

Chart setup methods

  • add_title(text, options: {}): Adds a title element to the chart. options can be used to specify size, bold (true or false), alpha (0..1), and/or color (a hex color code).
  • loading_indicator!: Displays a loading indicator over the chart until it has completed rendering. The indicator makes use of an I18n key (chart_data_loading) and an image (amcharts/loading.gif) which both can be customized within your application.
  • detach_legend(div: true): Makes the legend (if present) render in a separate div. div can either be true (which will cause the legend div to be given an ID the same as the main div, with a "_legend" suffix), or the ID you want to give the div.
  • dimensions=('widthxheight'): Allows width and height to be specified in one line

Defining a chart

  • Serial charts need to have their graphs defined on setup (this also allows multiple graphs to be defined; ie. for a multi-line chart). Graphs are initialized with (name, type), where type is a type of graph that AmCharts is aware of (:column, :line, :step, etc.)

      AmCharts::Chart::Serial.new(data) do
        [:col1, :col2, :col3].each do |column|
        graphs.new(column, :smoothedLine) do |g|
          g.title = column
        end
      end
    
  • Pie charts do not use graphs, as they get their values directly from the given data set.

Note about the Version Number

The version number correlates to the version of AmCharts that is included (ie. 3.1.1.0 is the first release of amcharts.rb which uses AmCharts v3.1.1)

Using a Commercial AmCharts license

If you have purchased the commercial version of AmCharts, place the amcharts.js file it comes with inside a javascripts/amcharts directory within (app|lib|vendor)/assets, so that it will be used instead of the free version that is included in this gem.

Installation

Add this line to your application's Gemfile:

gem 'amcharts.rb'

And then execute:

$ bundle

Or install it yourself as:

$ gem install amcharts.rb

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Acknowledgments

amcharts.rb's People

Contributors

dvandersluis avatar rvracaric avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

amcharts.rb's Issues

Responsive charts

Hi, first wanted to thank you for really great library! Nice work!

Is there a mapping for enabling responsive charts? Something like

c.responsive do |r|
  r.enabled = true
end

Was not able to find. I have to always specify the dimensions.

Thank you in advance!

Themes

Please add support for amcharts themes.

content for JavaScript causing issues

I was wondering if there is a reason for the content for calling JavaScript. I use the same content name through all my app in a different form and this is causing issues on the page where I want to display a chart.
It should ideally be prefixed with amchart or something to avoid clashes with other code bases

Paid version

Is there a whole different set of JS for the paid version or is there some way to easily turn off the advertisement with a setting? We already own the whole product, but I liked the idea of using the gem.

Requiring all throws an error

When I require amcharts/all I get an error saying it cannot find amstock, but works if if i include amcharts/amstock by itself in my application.js file.

undefined method `helper' for ActionController::API:Class

Hello,
I have a strange issue after adding draper gem:

/usr/local/bundle/gems/amcharts.rb-3.11.2.17/lib/amcharts.rb:27:in `block (3 levels) in <module:AmCharts>': undefined method `helper' for ActionController::API:Class (NoMethodError)
        from /usr/local/bundle/gems/activesupport-5.0.2/lib/active_support/lazy_load_hooks.rb:43:in `instance_eval'
        from /usr/local/bundle/gems/activesupport-5.0.2/lib/active_support/lazy_load_hooks.rb:43:in `execute_hook'
        from /usr/local/bundle/gems/activesupport-5.0.2/lib/active_support/lazy_load_hooks.rb:33:in `block in on_load'
        from /usr/local/bundle/gems/activesupport-5.0.2/lib/active_support/lazy_load_hooks.rb:32:in `each'
        from /usr/local/bundle/gems/activesupport-5.0.2/lib/active_support/lazy_load_hooks.rb:32:in `on_load'
        from /usr/local/bundle/gems/amcharts.rb-3.11.2.17/lib/amcharts.rb:26:in `block (2 levels) in <module:AmCharts>'
        from /usr/local/bundle/gems/railties-5.0.2/lib/rails/initializable.rb:30:in `instance_exec'
        from /usr/local/bundle/gems/railties-5.0.2/lib/rails/initializable.rb:30:in `run'
        from /usr/local/bundle/gems/railties-5.0.2/lib/rails/initializable.rb:55:in `block in run_initializers'
        from /usr/local/lib/ruby/2.3.0/tsort.rb:228:in `block in tsort_each'
        from /usr/local/lib/ruby/2.3.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
        from /usr/local/lib/ruby/2.3.0/tsort.rb:431:in `each_strongly_connected_component_from'
        from /usr/local/lib/ruby/2.3.0/tsort.rb:349:in `block in each_strongly_connected_component'
        from /usr/local/lib/ruby/2.3.0/tsort.rb:347:in `each'
        from /usr/local/lib/ruby/2.3.0/tsort.rb:347:in `call'
        from /usr/local/lib/ruby/2.3.0/tsort.rb:347:in `each_strongly_connected_component'
        from /usr/local/lib/ruby/2.3.0/tsort.rb:226:in `tsort_each'
        from /usr/local/lib/ruby/2.3.0/tsort.rb:205:in `tsort_each'
        from /usr/local/bundle/gems/railties-5.0.2/lib/rails/initializable.rb:54:in `run_initializers'
        from /usr/local/bundle/gems/railties-5.0.2/lib/rails/application.rb:352:in `initialize!'
        from /app/config/environment.rb:5:in `<top (required)>'
        from /app/config.ru:3:in `require_relative'
        from /app/config.ru:3:in `block in <main>'
        from /usr/local/bundle/gems/rack-2.0.1/lib/rack/builder.rb:55:in `instance_eval'
        from /usr/local/bundle/gems/rack-2.0.1/lib/rack/builder.rb:55:in `initialize'
        from /app/config.ru:in `new'
        from /app/config.ru:in `<main>'
        from /usr/local/bundle/gems/rack-2.0.1/lib/rack/builder.rb:49:in `eval'
        from /usr/local/bundle/gems/rack-2.0.1/lib/rack/builder.rb:49:in `new_from_string'
        from /usr/local/bundle/gems/rack-2.0.1/lib/rack/builder.rb:40:in `parse_file'
        from /usr/local/bundle/gems/rack-2.0.1/lib/rack/server.rb:318:in `build_app_and_options_from_config'
        from /usr/local/bundle/gems/rack-2.0.1/lib/rack/server.rb:218:in `app'
        from /usr/local/bundle/gems/railties-5.0.2/lib/rails/commands/server.rb:59:in `app'
        from /usr/local/bundle/gems/rack-2.0.1/lib/rack/server.rb:353:in `wrapped_app'
        from /usr/local/bundle/gems/railties-5.0.2/lib/rails/commands/server.rb:124:in `log_to_stdout'
        from /usr/local/bundle/gems/railties-5.0.2/lib/rails/commands/server.rb:77:in `start'
        from /usr/local/bundle/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:90:in `block in server'
        from /usr/local/bundle/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:85:in `tap'
        from /usr/local/bundle/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:85:in `server'
        from /usr/local/bundle/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
        from /usr/local/bundle/gems/railties-5.0.2/lib/rails/commands.rb:18:in `<top (required)>'
        from /app/bin/rails:9:in `require'
        from /app/bin/rails:9:in `<top (required)>'
        from /usr/local/bundle/gems/spring-2.0.1/lib/spring/client/rails.rb:28:in `load'
        from /usr/local/bundle/gems/spring-2.0.1/lib/spring/client/rails.rb:28:in `call'
        from /usr/local/bundle/gems/spring-2.0.1/lib/spring/client/command.rb:7:in `call'
        from /usr/local/bundle/gems/spring-2.0.1/lib/spring/client.rb:30:in `run'
        from /usr/local/bundle/gems/spring-2.0.1/bin/spring:49:in `<top (required)>'
        from /usr/local/bundle/gems/spring-2.0.1/lib/spring/binstub.rb:31:in `load'
        from /usr/local/bundle/gems/spring-2.0.1/lib/spring/binstub.rb:31:in `<top (required)>'
        from /app/bin/spring:15:in `require'
        from /app/bin/spring:15:in `<top (required)>'
        from bin/rails:3:in `load'
        from bin/rails:3:in `<main>'

I think it could be solved by replacing line 27 in lib/amcharts.rb
was

helper AmCharts::AmChartsHelper

should be

::ActionController::Base.helper AmCharts::AmChartsHelper

Processing data in a chart changes the underlying data

When process_data is called on data, any changes made change the underlying data rather than just applying to the chart being rendered. This can be a problem if the same data set is being used to drive multiple charts, as the initial values are lost.

AmCharts with Turbolinks

AmCharts.ready works fine if you don't use Turbolinks, but we need to have a way to trigger what's inside of AmCharts.ready. What about creating something inside of the AmCharts.helper that gets triggered using document ready and the page load events from turbolinks?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.