Module: PureChart::ChartHelpers

Defined in:
lib/purechart/chart_helper.rb

Instance Method Summary collapse

Instance Method Details

#bar_chartObject



46
47
48
# File 'lib/purechart/chart_helper.rb', line 46

def bar_chart
    "<div>Bar chart will be rendered here.</div>".html_safe
end

#column_chartObject



50
51
52
# File 'lib/purechart/chart_helper.rb', line 50

def column_chart
    "<div>Column chart will be rendered here.</div>".html_safe
end

#lollipop_chart(data, configuration = { axes: { horizontal: "Value" } }, path = "") ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/purechart/chart_helper.rb', line 3

def lollipop_chart(data, configuration = { axes: { horizontal: "Value" } }, path = "")
    # Set default configuration file path
    default_config_path = File.join( File.dirname(__FILE__), 'styles/default.yml' )

    default_config_hash = YAML.load(File.read(default_config_path))
    user_config_hash = {}

    if path != ""
        # TODO - Implement better logic
        if File.file?("app/purechart/" + path + ".yml")
            user_config_hash = YAML.load(File.read("app/purechart/" + path + ".yml"))
        elsif File.file?("app/purechart/" + path + ".yaml")
            user_config_hash = YAML.load(File.read("app/purechart/" + path + ".yaml"))
        elsif File.file?("app/purechart/" + path + ".json")
            user_config_hash = JSON.load(File.read("app/purechart/" + path + ".json"))
        else
            raise "(PureChart) ERROR - Could not locate configuration file '" + path + ".[YML, YAML, JSON]'. Make sure this file exists in your 'app/purechart' directory."
        end
    end

    # Merge user's configuration with default
    style_config = default_config_hash.merge(user_config_hash)

    # Format data for chart generation
    largest_value = (data.map { |object| object[:value] }).max()

    data.each do |object|
        object[:width] = (Float(object[:value]) / largest_value) * 100
    end

    gridlines = {
        vertical_lines: 10,
        vertical_increment: (Float(largest_value) / 10).ceil
    }

    ActionController::Base.render partial: '/lollipop', locals: {
        data: data,
        gridlines: gridlines,
        configuration: configuration,
        style: style_config
    }
end

#pie_chartObject



54
55
56
# File 'lib/purechart/chart_helper.rb', line 54

def pie_chart
    ActionController::Base.render partial: '/pie'
end