Skip to content

Commit

Permalink
Merge PR #33 from skyz04/main
Browse files Browse the repository at this point in the history
Add Pie Chart Support For Negative Values
  • Loading branch information
GeorgeBerdovskiy authored Mar 27, 2024
2 parents 486c24b + a182a85 commit ccd031f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
8 changes: 5 additions & 3 deletions app/views/_pie.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
<%=object[:percent_value] > 0.5 ? 1 : 0%> 1
<%= 150 - 125 * Math.cos(2 * Math::PI * cumulative_percent)%>
<%= 150 - 125 * Math.sin(2 * Math::PI * cumulative_percent)%> Z"
stroke="White"
fill="<%=object[:color]%>"
stroke-width="1" />
stroke="<%= object[:is_negative] == 1 ? object[:color] : 'white' %>"
stroke-dasharray= '<%= object[:is_negative] == 1 ? 30 : 0 %>'
fill="<%= object[:is_negative] == 1 ? 'white' : object[:color] %>"
stroke-width="<%= object[:is_negative] == 1 ? 5 : 1 %>" />
<%-arc_x = 150 - 125 * Math.cos(2 * Math::PI * cumulative_percent)%>
<%-arc_y = 150 - 125 * Math.sin(2 * Math::PI * cumulative_percent)%>

Expand All @@ -28,6 +29,7 @@
</svg>
</div>
<%# Insert the key here %>

</div>

<%= render :partial => '/styles' %>
22 changes: 19 additions & 3 deletions lib/purechart/chart_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,20 +126,36 @@ def column_chart
end

def pie_chart(data)
# check for negative values
has_negative_value = 0

# Find total value for calculating percentages
total_value = 0
data.each do |object|
total_value += object[:value]
total_value += (object[:value]).abs
end
# Calculate percentages for each data point
data.each do |object|
object[:percent_value] = Float(object[:value]) / total_value
object[:percent_value] = (Float(object[:value]) / total_value).abs
if object[:value] < 0
object[:is_negative] = 1
has_negative_value = 1
else
object[:is_negative] = 0
end
end

negative_value = {
negative: has_negative_value
}

ActionController::Base.render partial: '/pie', locals: {
data: data
data: data,
negative_value: negative_value
}
end



def box_plot(data, configuration = {}, path="")

Expand Down

0 comments on commit ccd031f

Please sign in to comment.