#!/usr/bin/ruby

# Author: Triskaideka  ( https://triskaideka.net/ )
# License: The author hereby places this script in the public domain.
#          You can do whatever you want with it, to the extent allowed by law.

require 'csv'
require 'gruff'

### Load the records
records = CSV.read('./drafts.csv')

headers = records.shift

c = Hash.new
headers.each_with_index {|item, index|
  c[item] = index
}

# delete rows with no tournament in them
records.delete_if{ |row| row[c['Date']].nil? }

# convert cells from strings to numbers or booleans where appropriate
records.map{|row|
  row.map!{|cell|
    if (cell =~ /^[0-9]+$/) then
      cell = cell.to_i
    elsif (cell == 'FALSE' || cell == '')
      cell = false
    elsif (cell == 'TRUE' || cell == 'x')
      cell = true
    else
      cell = cell
    end
  }
  row.pop
}

# complete_records contains only those tournaments that have been completed
complete_records = records.reject{ |row| !row[c['Complete?']] }

# nonww_records contains only tournaments that weren't part of Weekend Warrior events
nonww_records = records.reject{ |row| row[c['WW?']] }

### Constants
num_drafts = records.length
pct = 100.0 / num_drafts  # can multiply by this number to turn things into percentages
max_wins_per_draft = 4
possible_finishes = { 0 => '0-4', 1 => '1-3', 2 => '2-2', 3 => '3-1', 4 => '4-0' }
days = %w(Sun Mon Tue Wed Thu Fri Sat)
factions = ['Alloyin', 'Nekrium', 'Tempys', 'Uterra']
facpairs = ['A/N', 'A/T', 'A/U', 'N/T', 'N/U', 'T/U']
color_a = '#999999'
color_n = '#990099'
color_t = '#993333'
color_u = '#339933'
f_colors = [color_a, color_n, color_t, color_u]
color_std = '#333399'
releasedates = {
  "Rise of the Forgeborn (Set 2) & earlier" => DateTime.new(2012,9,10),  # this is actually the date the SolForge Kickstarter ended
  "Secrets of Solis (Set 3)" => DateTime.new(2014,8,20),
  "Secrets of Solis: Unveiled (3.1) & later" => DateTime.new(2014,9,25)
}

def set_defaults(chart)
  chart.theme = {
    :colors => [
      '#045a8d',
      '#f1eef6',
      '#006600',
      '#0e110a',
      '#660000',
      '#777777',
    ],
    :marker_color => '#555555',
    :font_color => '#000000',
    :background_colors => %w(#E3A869 #E3A869)
  }

  unless chart.is_a?(Gruff::Pie) then chart.hide_legend = true end
  if chart.respond_to?(:minimum_value) then chart.minimum_value = 0 end
  if chart.respond_to?(:legend_position) then chart.legend_position = :right end
  if chart.respond_to?(:bar_spacing) then chart.bar_spacing = 0.7 end
end

page_title = "Statistics from #{complete_records.length} SolForge drafts"
html = <<END
<!doctype html>
<html><head>
<title>#{page_title}</title>
<style type="text/css"><!--
BODY {
  font-family: "Verdana", sans-serif;
  margin-left: 1em;
  margin-right: 4em;
}

ul.menu {
  transform: rotate(90deg);
  transform-origin: right bottom 0;

  position: fixed;
  right: 2em;
  bottom: 1em;

  line-height: 2em;

  padding: 0;
  margin: 0;
  border: 0;

  font-weight: bold;
  font-size: 1.2em;
}

ul.menu li {
  display: inline;
  list-style-type: none;
  margin: 0;
  padding: .7em;
  border-right: 3px solid black;
  border-bottom: 3px solid black;
  background-color: #E3A869;
}

ul.menu li:first-child {
  border-left: 3px solid black;
}

.figure {
  display: inline-block;
  width: 500px;
  margin-right: 2em;
}

.data-note {
  font-style: italic;
  font-size: smaller;
}
--></style>
</head>
<body>
<h1>#{page_title}</h1>
END

# links to subsections
html += "<ul class='menu'>\n"
['Finishes', 'Factions', 'Rarity', 'Card type'].each { |sect|
  # don't allow any whitespace between list items because it'll screw up the CSS
  html += "<li><a href='##{sect.gsub(/\s/, '').downcase}'>#{sect}</a></li>"
}
html += "</ul>\n\n"


### Template
=begin
chart = Gruff::Bar.new(500)
chart.title = ''
chart.data '',
chart.labels = {
}
set_defaults(chart)
fname = dir+'.png'
chart.write(fname)
html += "<div class='figure'>\n<h3>#{chart.title}</h3>\n<img src='#{fname}' alt='chart graphic'>\n</div>\n\n"
=end


### Finishes
dir = "fin/"

# calculate longest winning and losing streaks
results_in_order = records.reduce([]){ |memo, d| memo.push( d[ c['Round 1'] .. c['Round 4'] ] ) }.flatten.reject{|a| a.nil?}
win_streaks = [0]
loss_streaks = [0]
results_in_order.each do |r|
  if r == 'W' then
    loss_streaks.push(0)
    win_streaks[-1] += 1
  elsif r == "L" then
    win_streaks.push(0)
    loss_streaks[-1] += 1
  end
end

html += <<END
<h2 id='finishes'>Finishes</h2>
<ul>
<li>Decks drafted: #{records.length}</li>
<li>Tournaments completed: #{complete_records.length}</li>
<li>Longest winning streak: #{win_streaks.max} games</li>
<li>Longest losing streak: #{loss_streaks.max} games</li>
</ul>
END


## Win Ratio
chart = Gruff::Pie.new(500)
chart.title = "Win ratio"
chart.data 'Wins',   records.reduce(0){ |memo,d| d[c['Wins']]   + memo }
chart.data 'Losses', records.reduce(0){ |memo,d| d[c['Losses']] + memo }
set_defaults(chart)
fname = dir+'win_ratio.png'
chart.write(fname)
html += "<div class='figure'>\n<h3>#{chart.title}</h3>\n<img src='#{fname}' alt='chart graphic'>\n</div>\n\n"


## finishes %
chart = Gruff::Bar.new(500)
chart.title = 'Finishes'
chart.data 'Finishes', [
  records.count{|d| d[c['Wins']] == 0},
  records.count{|d| d[c['Wins']] == 1},
  records.count{|d| d[c['Wins']] == 2},
  records.count{|d| d[c['Wins']] == 3},
  records.count{|d| d[c['Wins']] == 4}
]
chart.labels = possible_finishes
chart.x_axis_label = "Wins-Losses"
chart.y_axis_label = "Number of tournaments"

# these next few lines are to avoid fractional markers
chart.maximum_value = (0..4).reduce(0){ |memo,n| records.count{|d| d[c['Wins']] == n} > memo ? records.count{|d| d[c['Wins']] == n} : memo }
if (chart.maximum_value % 2) then chart.maximum_value += 1 end
chart.marker_count = chart.maximum_value / 2

set_defaults(chart)
fname = dir+'finishes.png'
chart.write(fname)
html += "<div class='figure'>\n<h3>#{chart.title}</h3>\n<img src='#{fname}' alt='chart graphic'>\n</div>\n\n"


## Win ratio by day of week
chart = Gruff::Bar.new(500)
chart.title = 'Win ratio by day of week'

recbyday = []
days.each{ |day|
  recbyday.push( records.select{|d| d[c['Day of week']] == day } )
}

# if no games on a certain day, remove that day from the records
recbyday.reject!{ |r| r.length <= 0 }

chart.data 'Wins by Day', recbyday.map{ |r|
  r.reduce(0){ |memo,d| memo + d[c['Wins']] } * 100.0 /
  r.reduce(0){ |memo,d| memo + d[c['Wins']] + d[c['Losses']] }
}

# label the bars
# should account for the possibility of no games having been played on a certain day...
chart.labels = Hash[ (0...days.length).zip( days ) ]

# label the axes
chart.x_axis_label = "Day of week"
chart.y_axis_label = "% of games won"

chart.maximum_value = 100
chart.marker_count = 10

set_defaults(chart)
fname = dir+'win_ratio_by_day.png'
chart.write(fname)
html += "<div class='figure'>\n<h3>#{chart.title}</h3>\n<img src='#{fname}' alt='chart graphic'>\n</div>\n\n"


### Progress on win ratio
chart = Gruff::Line.new(500)
chart.title = 'Cumulative win ratio at intervals'

progress = []
labels = {}

(records.length/10..records.length).step(records.length/10) { |n|
  progress.push(
    records[0...n].reduce(0){ |memo,d| memo + d[c['Wins']] } * 100.0 /
    records[0...n].reduce(0){ |memo,d| memo + d[c['Wins']] + d[c['Losses']] }
  )
  labels[labels.length] = n.to_s
}

chart.data 'Cumulative ratio', progress
chart.labels = labels

chart.x_axis_label = "Total number of tournaments played"
chart.y_axis_label = "Win ratio"

set_defaults(chart)

chart.maximum_value = progress.max.ceil
chart.minimum_value = progress.min.floor

fname = dir+'win_ratio_progress.png'
chart.write(fname)
html += "<div class='figure'>\n<h3>#{chart.title}</h3>\n<img src='#{fname}' alt='chart graphic'>\n</div>\n\n"


## round lost in 3-1
chart = Gruff::Bar.new(500)
chart.title = 'When going 3-1, which round was lost?'

rec31 = records.select{|d| d[c['3-1']]}
data_points = [
  rec31.count{|d| d[c['Round 1']] == 'L'},
  rec31.count{|d| d[c['Round 2']] == 'L'},
  rec31.count{|d| d[c['Round 3']] == 'L'},
  rec31.count{|d| d[c['Round 4']] == 'L'}
]

chart.data 'Finishes', data_points

chart.labels = {0 => '1', 1 => '2', 2 => '3', 3 => '4'}
chart.x_axis_label = "Round"
chart.y_axis_label = "Number of tournaments"

set_defaults(chart)

# these next few lines are to avoid fractional markers
chart.maximum_value = data_points.max
if (chart.maximum_value % 2) then chart.maximum_value += 1 end
#chart.marker_count = chart.maximum_value / 2

fname = dir+'losses_in_3-1.png'
chart.write(fname)
html += "<div class='figure'>\n<h3>#{chart.title}</h3>\n<img src='#{fname}' alt='chart graphic'>\n</div>\n\n"



### Factions
dir = "fac/"
html += "\n<h2 id='factions'>Factions</h2>\n\n"

## % of decks with fac
chart = Gruff::Bar.new(500)
chart.title = '% of decks containing each faction'
chart.data :Alloyin, records.count{ |d| d[c['Alloyin']] } * pct, color_a
chart.data :Nekrium, records.count{ |d| d[c['Nekrium']] } * pct, color_n
chart.data :Tempys,  records.count{ |d| d[c['Tempys']]  } * pct, color_t
chart.data :Uterra,  records.count{ |d| d[c['Uterra']]  } * pct, color_u
chart.y_axis_label = "% of decks"
set_defaults(chart)
chart.hide_legend = false
chart.show_labels_for_bar_values = true
chart.maximum_value = 100
chart.marker_count = 10
fname = dir+'pct_decks_with_fac.png'
chart.write(fname)
html += "<div class='figure'>\n<h3>#{chart.title}</h3>\n<img src='#{fname}' alt='chart graphic'>\n</div>\n\n"


## % of decks with faction pair
chart = Gruff::Pie.new(500)
chart.title = "% of decks with each faction pair"
chart.data 'Alloyin/Nekrium', records.count{ |d| d[c['A/N']] }
chart.data 'Alloyin/Tempys',  records.count{ |d| d[c['A/T']] }
chart.data 'Alloyin/Uterra',  records.count{ |d| d[c['A/U']] }
chart.data 'Nekrium/Tempys',  records.count{ |d| d[c['N/T']] }
chart.data 'Nekrium/Uterra',  records.count{ |d| d[c['N/U']] }
chart.data 'Tempys/Uterra',   records.count{ |d| d[c['T/U']] }
set_defaults(chart)
fname = dir+'pct_decks_with_pair.png'
chart.write(fname)
html += "<div class='figure'>\n<h3>#{chart.title}</h3>\n<img src='#{fname}' alt='chart graphic'>\n</div>\n\n"


## win ratio with each faction
chart = Gruff::Bar.new(500)
chart.title = 'Win ratio with each faction'

recbyfac = []
factions.each{ |f|
  recbyfac.push( records.select{|d| d[c[f]] } )
}

# if no decks had a certain faction, remove that faction from the records
recbyfac.reject!{ |r| r.length <= 0 }

winsbyfac = recbyfac.map{ |r|
  r.reduce(0){ |memo,d| memo + d[c['Wins']] } * 100.0 /
  r.reduce(0){ |memo,d| memo + d[c['Wins']] + d[c['Losses']] }
}
chart.data :Alloyin, winsbyfac[0], color_a
chart.data :Nekrium, winsbyfac[1], color_n
chart.data :Tempys,  winsbyfac[2], color_t
chart.data :Uterra,  winsbyfac[3], color_u

# label the axes
#chart.x_axis_label = "Faction"
chart.y_axis_label = "% of games won"

set_defaults(chart)

chart.maximum_value = 100
chart.marker_count = 10

chart.hide_legend = false
chart.show_labels_for_bar_values = true

fname = dir+'win_ratio_by_fac.png'
chart.write(fname)
html += "<div class='figure'>\n<h3>#{chart.title}</h3>\n<img src='#{fname}' alt='chart graphic'>\n</div>\n\n"


## win ratio with each faction pair
chart = Gruff::Bar.new(500)
chart.title = 'Win ratio with each faction pair'

recbypair = []
facpairs.each{ |p|
  recbypair.push( records.select{|d| d[c[p]] } )
}

# if no decks had a certain faction pair, remove that pair from the records
recbypair.reject!{ |r| r.length <= 0 }

chart.data 'Wins by Pair', recbypair.map{ |r|
  r.reduce(0){ |memo,d| memo + d[c['Wins']] } * 100.0 /
  r.reduce(0){ |memo,d| memo + d[c['Wins']] + d[c['Losses']] }
}

# label the bars
chart.labels = Hash[ (0...facpairs.length).zip(facpairs) ]

# label the axes
chart.x_axis_label = "Faction pair"
chart.y_axis_label = "% of games won"

set_defaults(chart)

chart.maximum_value = 100
chart.marker_count = 10

chart.show_labels_for_bar_values = true

fname = dir+'win_ratio_by_pair.png'
chart.write(fname)
html += "<div class='figure'>\n<h3>#{chart.title}</h3>\n<img src='#{fname}' alt='chart graphic'>\n</div>\n\n"


## win ratio with each faction, by format
chart = Gruff::Bar.new(500)
chart.title = 'Win ratio with each faction, by format'

recbypair = []
factions.each{ |f|
  recbyfac.push( nonww_records.select{|d| d[c[f]] } )
}

# if no decks had a certain faction pair, remove that pair from the records
recbyfac.reject!{ |r| r.length <= 0 }

rec_list = nonww_records.clone
recbydate = {}
recbydate['Overall'] = rec_list.clone

releasedates.keys.reverse.each { |date|
  recbydate[date] = rec_list.select{ |rec|
    releasedates[date] <= DateTime.parse( rec[c['Date']] )
  }
  rec_list -= recbydate[date]
}

recbydateandfac = {}
recbydate.keys.each{ |date|
  recbydateandfac[date] = []
  factions.each{ |fac|
    recbydateandfac[date].push( recbydate[date].select{ |d| d[c[fac]] } )
  }
}

(releasedates.keys + ['Overall']).each { |date|
  chart.data date, recbydateandfac[date].map{ |r|
    x = r.reduce(0){ |memo,d| memo + d[c['Wins']] } * 100.0 /
        r.reduce(0){ |memo,d| memo + d[c['Wins']] + d[c['Losses']] }
    # return 0 if the result is NaN, which could happen in case of division by zero
    x.nan? ? 0 : x
  }
}

# label the bars
chart.labels = Hash[ (0...factions.length).zip(factions) ]

# label the axes
chart.x_axis_label = "Faction"
chart.y_axis_label = "% of games won"

set_defaults(chart)

chart.maximum_value = 100
chart.marker_count = 10

chart.hide_legend = false

fname = dir+'win_ratio_by_fac_and_date.png'
chart.write(fname)
html += "<div class='figure'>\n<h3>#{chart.title}</h3>\n<img src='#{fname}' alt='chart graphic'>\n" +
        "<p class='data-note'>This chart excludes Weekend Warrior drafts, since they may use non-standard card pools.</p>\n" +
        "</div>\n\n"


## win ratio with each faction pair, by format
chart = Gruff::Bar.new(500)
chart.title = 'Win ratio with each faction pair, by format'

recbypair = []
facpairs.each{ |p|
  recbypair.push( nonww_records.select{|d| d[c[p]] } )
}

# if no decks had a certain faction pair, remove that pair from the records
recbypair.reject!{ |r| r.length <= 0 }

rec_list = nonww_records.clone
recbydate = {}
recbydate['Overall'] = rec_list.clone

releasedates.keys.reverse.each { |date|
  recbydate[date] = rec_list.select{ |rec|
    releasedates[date] <= DateTime.parse( rec[c['Date']] )
  }
  rec_list -= recbydate[date]
}

recbydateandpair = {}
recbydate.keys.each{ |date|
  recbydateandpair[date] = []
  facpairs.each{ |pair|
    recbydateandpair[date].push( recbydate[date].select{ |d| d[c[pair]] } )
  }
}

(releasedates.keys + ['Overall']).each { |date|
  chart.data date, recbydateandpair[date].map{ |r|
    x = r.reduce(0){ |memo,d| memo + d[c['Wins']] } * 100.0 /
        r.reduce(0){ |memo,d| memo + d[c['Wins']] + d[c['Losses']] }
    # return 0 if the result is NaN, which could happen in case of division by zero
    x.nan? ? 0 : x
  }
}

# label the bars
chart.labels = Hash[ (0...facpairs.length).zip(facpairs) ]

# label the axes
chart.x_axis_label = "Faction pair"
chart.y_axis_label = "% of games won"

set_defaults(chart)

chart.maximum_value = 100
chart.marker_count = 10

chart.hide_legend = false

fname = dir+'win_ratio_by_pair_and_date.png'
chart.write(fname)
html += "<div class='figure'>\n<h3>#{chart.title}</h3>\n<img src='#{fname}' alt='chart graphic'>\n" +
        "<p class='data-note'>This chart excludes Weekend Warrior drafts, since they may use non-standard card pools.</p>\n" +
        "</div>\n\n"


### Rarity
dir = "rar/"
html += <<END
<h2 id='rarity'>Rarity</h2>
<ul>
<li>Most heroics in one deck: #{records.reduce(0){|memo, d| d[c['# Heroics']] > memo ? d[c['# Heroics']] : memo } }</li>
<li>Average heroics per deck: #{ ( records.reduce(0){|memo, d| memo + d[c['# Heroics']]} / records.length.to_f ).round(2) }</li>
</ul>
END

## % with Legendary
chart = Gruff::Pie.new(500)
chart.title = "% of decks with at least one legendary"
chart.data 'Had a legendary', records.count{|d| d[c['# Legendaries']] > 0}
chart.data 'No legendaries', records.count{|d| d[c['# Legendaries']] == 0}
set_defaults(chart)
fname = dir+'legendary_or_not.png'
chart.write(fname)
html += "<div class='figure'>\n<h3>#{chart.title}</h3>\n<img src='#{fname}' alt='chart graphic'>\n</div>\n\n"


## win ratio by # of legendaries
chart = Gruff::Bar.new(500)
chart.title = 'Win ratio with vs. without legendaries'

records.reject!{ |d| d[c['# Legendaries']].nil? }  # eliminate drafts for which no # of legendaries was recorded
most_l = records.reduce(0){|memo,d| d[c['# Legendaries']] > memo ? d[c['# Legendaries']] : memo }

recbynuml = []
recbynuml[0] = records.select{|d| d[c['# Legendaries']] == 0}
recbynuml[1] = records.select{|d| d[c['# Legendaries']] > 0}

# if no decks had a certain number of heroics, remove that number from the records
recbynuml.reject!{ |r| r.length <= 0 }

chart.data 'Win ratio with vs. without legendaries', recbynuml.map{ |r|
  r.reduce(0){ |memo,d| memo + d[c['Wins']] } * 100.0 /
  r.reduce(0){ |memo,d| memo + d[c['Wins']] + d[c['Losses']] }
}

# label the bars
chart.labels = {0 => 'No', 1 => 'Yes'}

# label the axes
chart.x_axis_label = "Legendary in deck?"
chart.y_axis_label = "% of games won"

chart.maximum_value = 100
chart.marker_count = 10
chart.show_labels_for_bar_values = true

set_defaults(chart)
fname = dir+'win_ratio_by_num_leg.png'
chart.write(fname)
html += "<div class='figure'>\n<h3>#{chart.title}</h3>\n<img src='#{fname}' alt='chart graphic'>\n</div>\n\n"


## win ratio by # of heroics
chart = Gruff::Bar.new(500)
chart.title = 'Win ratio by number of heroics'

records.reject!{ |d| d[c['# Heroics']] == nil }  # eliminate drafts for which no # of heroics was recorded
most_h = records.reduce(0){|memo,d| d[c['# Heroics']] > memo ? d[c['# Heroics']] : memo }

recbynumh = []
(0..most_h).each{ |n|
  recbynumh[n] = records.select{|d| d[c['# Heroics']] == n}
}

# if no decks had a certain number of heroics, remove that number from the records
recbynumh.reject!{ |r| r.length <= 0 }

chart.data 'Wins by # Heroics', recbynumh.map{ |r|
  r.reduce(0){ |memo,d| memo + d[c['Wins']] } * 100.0 /
  r.reduce(0){ |memo,d| memo + d[c['Wins']] + d[c['Losses']] }
}

# label the bars
chart.labels = Hash[ (0...recbynumh.length).zip( recbynumh.collect{ |r| r[0][c['# Heroics']].to_s + " (n=#{r.length})" } ) ]

# label the axes
chart.x_axis_label = "# of Heroics in deck (# of decks with that many Heroics)"
chart.y_axis_label = "% of games won"

chart.maximum_value = 100
chart.marker_count = 10

set_defaults(chart)
fname = dir+'win_ratio_by_num_heroics.png'
chart.write(fname)
html += "<div class='figure'>\n<h3>#{chart.title}</h3>\n<img src='#{fname}' alt='chart graphic'>\n</div>\n\n"


### Card type
dir = "typ/"
spell_counts = records.reject{|d| d[c['# Spells']].nil?}.map{|d| d[c['# Spells']]}
html += <<END
<h2 id='cardtype'>Card Type</h2>
<ul>
<li>Fewest spells in one deck: #{ spell_counts.min }</li>
<li>Most spells in one deck:   #{ spell_counts.max }</li>
<li>Average number of spells per deck: #{ (spell_counts.reduce(0,:+) / spell_counts.length.to_f ).round(2) }</li>
</ul>
END


## win ratio by # of spells
chart = Gruff::Bar.new(500)
chart.title = 'Win ratio by number of spells'

records.reject!{ |d| d[c['# Spells']] == nil }  # eliminate drafts for which no # of spells was recorded
most_s = records.reduce(0){|memo,d| d[c['# Spells']] > memo ? d[c['# Spells']] : memo }

recbynums = []
(0..most_s).each{ |n|
  recbynums[n] = records.select{|d| d[c['# Spells']] == n}
}

# if no decks had a certain number of spells, remove that number from the records
recbynums.reject!{ |r| r.length <= 0 }

chart.data 'Wins by # spells', recbynums.map{ |r|
  r.reduce(0){ |memo,d| memo + d[c['Wins']] } * 100.0 /
  r.reduce(0){ |memo,d| memo + d[c['Wins']] + d[c['Losses']] }
}

# label the bars
chart.labels = Hash[ (0...recbynums.length).zip( recbynums.collect{ |r| r[0][c['# Spells']].to_s } ) ]

# label the axes
chart.x_axis_label = "# of spells in deck"
chart.y_axis_label = "% of games won"

chart.maximum_value = 100
chart.marker_count = 10

set_defaults(chart)
fname = dir+'win_ratio_by_num_spells.png'
chart.write(fname)
html += "<div class='figure'>\n<h3>#{chart.title}</h3>\n<img src='#{fname}' alt='chart graphic'>\n</div>\n\n"


## Mean spells by finish
chart = Gruff::Bar.new(500)
chart.title = 'Average spells per draft by finish'

recbynumwins = []
(0..max_wins_per_draft).each{ |n|
  recbynumwins[n] = records.select{|d| d[c['Wins']] == n}
}

# if no decks had a certain number of wins, remove that number from the records
recbynumwins.reject!{ |r| r.length <= 0 }

chart.data 'Mean spells by finish', recbynumwins.map{ |r|
  r.reduce(0){ |memo,d| memo + d[c['# Spells']] }.to_f / r.length
}

chart.labels = possible_finishes

# label the axes
chart.x_axis_label = "Wins-Losses"
chart.y_axis_label = "Average # of spells in deck"

chart.show_labels_for_bar_values = true

set_defaults(chart)
fname = dir+'mean_spells_by_finish.png'
chart.write(fname)
html += "<div class='figure'>\n<h3>#{chart.title}</h3>\n<img src='#{fname}' alt='chart graphic'>\n</div>\n\n"


### Write the web page

html += "\n</body></html>\n"
# This overwrites the file every time, so if you want to edit it, it's safest to rename it.
outfile = File.open("draft_charts.html", "w")
outfile.write(html)
outfile.close
