Skip to content

Commit

Permalink
Add ability to turn off coloring
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Jul 28, 2018
1 parent d908276 commit 538d04e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
10 changes: 7 additions & 3 deletions lib/tty/pie_chart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def data_items
angle = percent * FULL_CIRCLE_DEGREES / 100.to_f
color_fill = item[:fill] || fill
DataItem.new(item[:name], item[:value], percent, angle,
item[:color], color_fill)
item.fetch(:color, false), color_fill)
end
end

Expand Down Expand Up @@ -116,11 +116,15 @@ def draw
output << ' ' * (center_x - width) if top.nil?
(-width..width).each do |x|
angle = radian_to_degree(Math.atan2(x, y))
idx = select_data_item(angle, angles)
item = items[select_data_item(angle, angles)]
if !top.nil?
output << cursor.move_to(center_x + x, center_y + y)
end
output << @pastel.decorate(items[idx].fill, items[idx].color)
if item.color
output << @pastel.decorate(item.fill, item.color)
else
output << item.fill
end
end

if legend
Expand Down
3 changes: 2 additions & 1 deletion lib/tty/pie_chart/data_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ def initialize(name, value, percent, angle, color, fill)
# @api private
def to_label
percent_fmt = '%.2f' % percent
"#{@pastel.decorate(fill, color)} #{name} #{percent_fmt}%"
label = color ? @pastel.decorate(fill, color) : fill
"#{label} #{name} #{percent_fmt}%"
end
end
end # PieChart
Expand Down
22 changes: 22 additions & 0 deletions spec/unit/color_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

RSpec.describe TTY::PieChart, ':color option' do
it "draws a pie chart without colors" do
data = [
{ name: 'BTC', value: 5977, fill: '*' },
{ name: 'BCH', value: 3045, fill: '+' },
{ name: 'LTC', value: 2030, fill: 'x' }
]
pie = TTY::PieChart.new(data, radius: 2)

output = pie.draw

expect(output).to eq([
" x** * BTC 54.08%\n",
" +xx****\n",
"++++***** + BCH 27.55%\n",
" +++****\n",
" +** x LTC 18.37%\n"
].join)
end
end

0 comments on commit 538d04e

Please sign in to comment.