Sha256: 3d79299563fd531bbe460ccecd6d5affb223e69f69105ca194ce2d56e60133cd

Contents?: true

Size: 1.45 KB

Versions: 5

Compression:

Stored size: 1.45 KB

Contents

require 'json'
require 'csv'
require 'erb'

module Asciinurse
  module Chart

    class CSVData
      def initialize(engine, attrs, csv_content)
        defaults = Asciinurse.config "charts.#{engine}.defaults"
        options = defaults.merge attrs if defaults
        options ||= attrs
        @engine = engine
        @title = (options['title'] or '')
        @type = options['type']
        @width = options['width']
        @height = options['height']
        @csv = CSV.parse(csv_content, :converters => :all)
        parse_data
      end

      def to_chart_json
        ERB.new(get_template).result binding
      end

      private

      def get_template(default = 'generic.json.erb')
        template = Asciinurse.find_resource "#{@engine}/templates/charts/#{@type}.json.erb"
        template ||= Asciinurse.find_resource "#{@engine}/templates/charts/#{default}"
        raise "Template not found for #{@engine}:#{@type}" unless template
        IO.read template
      end

      def parse_data
        @target = @csv[0][0]
        @elements = @csv.size - 1
        @series = []
        @header = []
        @elements.times do |n|
          @header << @csv[n + 1].first
        end
        (@csv[0].size - 1).times do |n|
          row = {
              name: @csv[0][n + 1],
              data: []
          }
          @elements.times do |el|
            row[:data] << @csv[el + 1][n + 1]
          end
          @series << row
        end
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
asciinurse-0.1.7 lib/asciinurse/extensions/charts/chart_data.rb
asciinurse-0.1.6 lib/asciinurse/extensions/charts/chart_data.rb
asciinurse-0.1.5 lib/asciinurse/extensions/charts/chart_data.rb
asciinurse-0.1.4 lib/asciinurse/extensions/charts/chart_data.rb
asciinurse-0.1.3 lib/asciinurse/extensions/charts/chart_data.rb