Sha256: d517e14e8ac7d851d0c6f2d5136cc9b34a40181e992d2aaf1bbf45bb38356a0f
Contents?: true
Size: 1.56 KB
Versions: 1
Compression:
Stored size: 1.56 KB
Contents
module AsciiChart class Line DEFAULTS = { offset: 3, format: '%8.2f ', height: nil } def initialize(series, options = {}) @series = series @options = DEFAULTS.merge(options) end def plot max, min = @series.max, @series.min interval = (max - min).abs @options[:height] ||= interval radio = @options[:height].to_f / interval offset = @options[:offset] intmax, intmin = (max * radio).ceil, (min * radio).floor rows = (intmax - intmin).abs width = @series.length + offset result = (0..rows).map { [' '] * width } (intmin..intmax).each do |y| label = @options[:format] % (max - (((y - intmin) * interval).to_f / rows)) result[y - intmin][[offset - label.length, 0].max] = label result[y - intmin][offset - 1] = y == 0 ? '┼' : '┤' end highest = (@series.first * radio - intmin).to_i result[rows - highest][offset - 1] = '┼' (0...@series.length - 1).each do |x| _curr = ((@series[x + 0] * radio).round - intmin).to_i _next = ((@series[x + 1] * radio).round - intmin).to_i if _curr == _next result[rows - _curr][x + offset] = '-' else result[rows - _curr][x + offset] = _curr > _next ? '╮' : '╯' result[rows - _next][x + offset] = _curr > _next ? '╰' : '╭' ([_curr, _next].min + 1...[_curr, _next].max).each do |y| result[rows - y][x + offset] = '|' end end end result.map(&:join).join("\n") end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
ascii_chart-0.2.0 | lib/ascii_chart/line.rb |