class GithubChart::Chart
Object for parsing and outputing Github stats data
Convert stats into SVG
Constants
- CUBE_SIZE
- SVG_MONTH_STYLE
Define Style for month labels
- SVG_SHARED_STYLE
Define shared label style
- SVG_WEEKDAY_STYLE
Define style for weekday labels
- X_PAD
- Y_PAD
Attributes
colors[RW]
stats[R]
Public Class Methods
new(params = {})
click to toggle source
Create a new chart object Passes the username through to GithubStats Uses colors rather than default, if provided
# File lib/githubchart.rb, line 59 def initialize(params = {}) params = { user: params } unless params.is_a? Hash @stats = load_stats(params[:data], params[:user]) @colors = params[:colors] || :default @colors = COLOR_SCHEMES[@colors] unless @colors.is_a? Array end
Public Instance Methods
render(type)
click to toggle source
# File lib/githubchart.rb, line 66 def render(type) raise(NameError, "Format #{type} is unsupported.") unless GithubChart.supports? type send("render_#{type}".to_sym) end
Private Instance Methods
load_stats(data, user)
click to toggle source
Load stats from provided arg or github
# File lib/githubchart.rb, line 76 def load_stats(data, user) return data if data raise('No data or user provided') unless user stats = GithubStats.new(user).data raise("Failed to find data for #{user} on GitHub") unless stats stats end
matrix(fill_value = -1)
click to toggle source
Convert the data into a matrix of weeks The fill value is used to pad the front and back
# File lib/githubchart.rb, line 88 def matrix(fill_value = -1) Matrix[*@stats.pad(fill_value).each_slice(7).to_a.transpose] end
render_svg()
click to toggle source
# File lib/githubchart/svg.rb, line 21 def render_svg grid = matrix chart = SVGPlot.new(width: (CUBE_SIZE * grid.column_size) + X_PAD, height: (CUBE_SIZE * grid.row_size) + Y_PAD) svg_add_points grid, chart svg_add_weekdays chart svg_add_months chart chart.to_s end
render_svg_square()
click to toggle source
# File lib/githubchart/svg.rb, line 31 def render_svg_square grid = matrix.minor(0, 7, -7, 7) chart = SVGPlot.new(width: (CUBE_SIZE * grid.column_size) - 2, height: (CUBE_SIZE * grid.row_size) - 2) svg_add_points grid, chart, 0, 0 chart.to_s end
svg_add_months(chart)
click to toggle source
# File lib/githubchart/svg.rb, line 102 def svg_add_months(chart) offsets = svg_get_month_offsets offsets.shift if [1, 2].include? offsets[1].last offsets.each do |month, offset| next if offset > 50 x = (CUBE_SIZE * offset) + X_PAD chart.text(x, 10, style: SVG_MONTH_STYLE) { raw month } end end
svg_add_points(grid, chart, xpadding = X_PAD, ypadding = Y_PAD)
click to toggle source
# File lib/githubchart/svg.rb, line 67 def svg_add_points(grid, chart, xpadding = X_PAD, ypadding = Y_PAD) grid.each_with_index do |point, y, x| next if point.score == -1 chart.rectangle( (x * CUBE_SIZE) + xpadding, (y * CUBE_SIZE) + ypadding, 10, 10, data: { score: point.score, date: point.date }, style: svg_point_style(point) ) end end
svg_add_weekday(chart, point)
click to toggle source
# File lib/githubchart/svg.rb, line 78 def svg_add_weekday(chart, point) index = point.date.wday letter = point.date.strftime('%a') style = SVG_WEEKDAY_STYLE.dup style[:display] = 'none' unless [1, 3, 5].include? index shift = index > 3 ? 29 : 28 chart.text(0, (CUBE_SIZE * index) + shift, style: style) { raw letter } end
svg_add_weekdays(chart)
click to toggle source
# File lib/githubchart/svg.rb, line 87 def svg_add_weekdays(chart) @stats.raw.first(7).each { |point| svg_add_weekday chart, point } end
svg_get_month_offsets()
click to toggle source
# File lib/githubchart/svg.rb, line 91 def svg_get_month_offsets # rubocop:disable Metrics/AbcSize list = @stats.raw.select { |x| x.date.sunday? } list.unshift(@stats.raw.first) unless @stats.raw.first.date.sunday? list.map! { |x| x.date.strftime('%b') } acc = 0 list.chunk { |x| x }.map do |month, offset| acc += offset.size [month, acc - offset.size] end end
svg_point_style(point)
click to toggle source
# File lib/githubchart/svg.rb, line 60 def svg_point_style(point) { fill: @colors[@stats.quartile(point.score)], 'shape-rendering': 'crispedges' } end