Skip to content

Commit

Permalink
Merge PR #18 from matthewh8/bar-chart-branch
Browse files Browse the repository at this point in the history
Add Bar Chart Helper
  • Loading branch information
GeorgeBerdovskiy authored Feb 24, 2024
2 parents b287b67 + 5582a73 commit 8b0dec7
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 2 deletions.
49 changes: 49 additions & 0 deletions app/views/_bar.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<div class="chart-flex-container" style="width: fit-content">
<div class="chart" style="border-left: <%= style['axes']['style'] %>; border-bottom: <%= style['axes']['style'] %>">
<div class="chart-elements">
<%- data.each do |object| %>
<div class="bar bar-chart-bar" style="width: <%= object[:width] %>%; background-color: <%= style['colors'][object[:color]] || object[:color] %>;">
<p class="bar-title bar-chart-bar-title" style="font-family: <%= style['labels']['font'] %>; font-weight: <%= style['labels']['weight'] %>; color: <%= style['labels']['color'] %>"><%= object[:name] %></p>
<div class="tooltip">
<p style="color: <%= style['colors'][object[:color]] || object[:color] %>;"><%= object[:name] %></p>
<p><%= configuration[:symbol] ? configuration[:symbol] : ''%><%= object[:value] %></p>
</div>
</div>
<% end %>
</div>
<div class="gridlines">
<div style="width: 10%; border-right: <%= style['gridlines']['style'] %>">
</div>
<div style="width: 10%; border-right: <%= style['gridlines']['style'] %>">
</div>
<div style="width: 10%; border-right: <%= style['gridlines']['style'] %>">
</div>
<div style="width: 10%; border-right: <%= style['gridlines']['style'] %>">
</div>
<div style="width: 10%; border-right: <%= style['gridlines']['style'] %>">
</div>
<div style="width: 10%; border-right: <%= style['gridlines']['style'] %>">
</div>
<div style="width: 10%; border-right: <%= style['gridlines']['style'] %>">
</div>
<div style="width: 10%; border-right: <%= style['gridlines']['style'] %>">
</div>
<div style="width: 10%; border-right: <%= style['gridlines']['style'] %>">
</div>
<div style="width: 10%; border-right: <%= style['gridlines']['style'] %>">
</div>
</div>
</div>
<div class="gridlines-ticks">
<%- (1..10).each do |index| %>
<div class="tick" style="width: 10%; border-right: <%= style['gridlines']['style'] %>">
<p style="margin-right: 4px; font-family: <%= style['ticks']['font'] %>; font-weight: <%= style['ticks']['weight'] %>; color: <%= style['ticks']['color'] %>"><%= index * gridlines[:vertical_increment] %></p>
</div>
<% end %>
</div>
<div style="margin-left: 96px">
<h4 class="purechart" style="text-align: center; font-family: <%= style['labels']['font'] %>; font-weight: <%= style['labels']['weight'] %>; color: <%= style['labels']['color'] %>"><%= configuration[:axes][:horizontal] %><%= configuration[:symbol] ? " (" + configuration[:symbol] + ")" : ''%></h4>
</div>
</div>

<%= render :partial => '/styles' %>
8 changes: 8 additions & 0 deletions app/views/_styles.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ h4.purechart {
z-index: 1;
}

.bar-chart-bar {
height: 16px;
}

.lollipop-end {
width: 16px;
height: 16px;
Expand Down Expand Up @@ -70,6 +74,10 @@ h4.purechart {
padding-right: 8px;
}

.bar-chart-bar-title {
top: -6px;
}

.tooltip {
position: absolute;
top: 50%;
Expand Down
51 changes: 49 additions & 2 deletions lib/purechart/chart_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,55 @@ def dot_svg_render
</svg>'''.html_safe
end

def bar_chart
"<div>Bar chart will be rendered here.</div>".html_safe
def bar_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 == "professional_light"
# TODO - Instead of loading our own by default, try/catch to see if they defined their own
# style using the same name
style_config_path = File.join( File.dirname(__FILE__), 'styles/professional_light.yml' )
default_config_hash = YAML.load(File.read(style_config_path))
elsif path == "professional_dark"
style_config_path = File.join( File.dirname(__FILE__), 'styles/professional_dark.yml' )
default_config_hash = YAML.load(File.read(style_config_path))
elsif 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: '/bar', locals: {
data: data,
gridlines: gridlines,
configuration: configuration,
style: style_config
}
end

def column_chart
Expand Down

0 comments on commit 8b0dec7

Please sign in to comment.