<commit_before>module IRuby
  module Output
    module HTML
      require "gruff"
      require "base64"
      def self.table(data)
        #
        # data = {a: 1, b:2}

        if data.respond_to?(:keys)
          d = data
        else
          d = data
        end

        r = "<table>"
        if d.respond_to?(:keys) # hash
          columns = [0,1]
        else
          columns = d.first.keys
          r << "<tr>#{columns.map{|c| "<th>#{c}</th>"}.join("\n")}</tr>"
        end
        d.each{|row|
          r << "<tr>"
          columns.each{|column|
            r << "<td>#{row[column]}</td>"
          }
          r << "</tr>"
        }
        r << "</table>"
        r
      end

      def self.image(image)
        data = image.respond_to?(:to_blob) ? image.to_blob : image
        "<img src='data:image/png;base64,#{Base64.encode64(data)}'>"
      end

      def self.chart_pie(o)
        data=o.delete(:data)
        title=o.delete(:title)
        size=o.delete(:size) || 300
        g = Gruff::Pie.new(size)
        g.title = title if title
        data.each do |data|
          g.data(data[0], data[1])
        end
        image(g.to_blob)
      end

      def self.chart_bar(o)
        data=o.delete(:data)
        title=o.delete(:title)
        size=o.delete(:size) || 300
        g = Gruff::Bar.new(size)
        g.title = title if title
        data.each do |data|
          g.data(data[0], data[1])
        end
        image(g.to_blob)

      end

      module Gmaps
        def self.points2latlng(points)
          "[" + points.reject{|p| not p.lat or not p.lon}.map{|p| 
            "  {location: new google.maps.LatLng(#{p.lat.to_f}, #{p.lon.to_f}) #{", weight: #{p.weight.to_i}" if p.respond_to?(:weight) and p.weight} } "
          }.join(',') + "]"
        end<commit_msg>stacked bar graphs<commit_after>module IRuby
  module Output
    module HTML
      require "gruff"
      require "base64"
      def self.table(data)
        #
        # data = {a: 1, b:2}

        if data.respond_to?(:keys)
          d = data
        else
          d = data
        end

        r = "<table>"
        if d.respond_to?(:keys) # hash
          columns = [0,1]
        else
          columns = d.first.keys
          r << "<tr>#{columns.map{|c| "<th>#{c}</th>"}.join("\n")}</tr>"
        end
        d.each{|row|
          r << "<tr>"
          columns.each{|column|
            r << "<td>#{row[column]}</td>"
          }
          r << "</tr>"
        }
        r << "</table>"
        r
      end

      def self.image(image)
        data = image.respond_to?(:to_blob) ? image.to_blob : image
        "<img src='data:image/png;base64,#{Base64.encode64(data)}'>"
      end

      def self.chart_pie(o)
        data=o.delete(:data)
        title=o.delete(:title)
        size=o.delete(:size) || 300
        g = Gruff::Pie.new(size)
        g.title = title if title
        data.each do |data|
          g.data(data[0], data[1])
        end
        image(g.to_blob)
      end

      def self.chart_bar(o)
        data=o.delete(:data)
        title=o.delete(:title)
        size=o.delete(:size) || 300

        klass = o.delete(:stacked) ? Gruff::StackedBar : Gruff::Bar
        g = klass.new(size)

        if labels=o.delete(:labels)
          if ! labels.respond_to?(:keys)
            labels = Hash[labels.map.with_index{|v,k| [k,v]}]
          end
          g.labels = labels
        end

        g.title = title if title
        data.each do |data|
          g.data(data[0], data[1])
        end
        image(g.to_blob)

      end

      module Gmaps
        def self.points2latlng(points)
          "[" + points.reject{|p| not p.lat or not p.lon}.map{|p| 
            "  {location: new google.maps.LatLng(#{p.lat.to_f}, #{p.lon.to_f}) #{", weight: #{p.weight.to_i}" if p.respond_to?(:weight) and p.weight} } "
          }.join(',') + "]"
        end
{'fraction_non_alphanumeric': 0.16391326794011357, 'fraction_numerical': 0.011874032008260196, 'mean_word_length': 1.5645268034414295, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5290898', 'n_tokens_mistral': 1375, 'n_tokens_neox': 1328, 'n_words': 258}