Class: SVG::Graph::C3js
- Inherits:
-
Object
- Object
- SVG::Graph::C3js
- Defined in:
- lib/SVG/Graph/C3js.rb
Overview
This class provides a lightweight generator for html code indluding c3js based graphs specified as javascript.
Instance Method Summary collapse
-
#add_chart_spec(javascript, js_chart_variable_name = "") ⇒ Object
Adds a javascript/json C3js chart definition into the div tag.
-
#add_javascript(attrs = {}, &block) ⇒ REXML::Element
Appends a
By default, the generated html code links the javascript and css dependencies to the d3 and c3 libraries in the
element. The latest versions of d3 and c3 available at the time of gem release are used through cdnjs. Custom versions of d3 and c3 can easily be used by specifying the corresponding keys in the optional Hash argument.If the dependencies are http(s) urls a simple href / src link is inserted in the html header. If you want to create a fully offline capable html file, you can do this by downloading the (minified) versions of d3.js, c3.css, c3.js to disk and then point to the files instead of http links. This will then inline the complete script and css payload directly into the generated html page.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
# File 'lib/SVG/Graph/C3js.rb', line 48 def initialize(opts = {}) default_opts = { "inline_dependencies" => false, "d3_js" => "https://cdnjs.cloudflare.com/ajax/libs/d3/5.12.0/d3.min.js", "c3_css" => "https://cdnjs.cloudflare.com/ajax/libs/c3/0.7.11/c3.min.css", "c3_js" => "https://cdnjs.cloudflare.com/ajax/libs/c3/0.7.11/c3.min.js" } @opts = default_opts.merge(opts) if @opts["inline_dependencies"] # we replace the values in the opts Hash by the referred file contents ["d3_js", "c3_css", "c3_js"].each do |key| if !File.file?(@opts[key]) raise "opts[\"#{key}\"]: No such file - #{File.(@opts[key])}" end @opts[key] = File.read(@opts[key]) end # ["d3_js", "c3_css", "c3_js"].each end # if @opts["inline_dependencies"] start_document() end