<commit_before>require 'json/ext'

module ActionView
  module Helpers
    module ScriptaculousHelper
      
      # Insert a flot chart into the page.  <tt>placeholder</tt> should be the 
      # name of the div that will hold the chart, <tt>collections</tt> is a hash 
      # of legends (as strings) and datasets with options as hashes, <tt>options</tt>
      # contains graph-wide options.
      # 
      # Example usage:
      # <tt>chart("graph_div", <br>
      # { "January" => { :collection => @january, :x => :day, :y => :sales, 
      # :options => { :bar => :show } }, <br>
      # "February" => { :collection => @february, :x => :day, :y => :sales, 
      # :options => { :points => :show }},<br>
      # :grid => { :backgroundColor => %{"#fffaff"} })</tt>
      #
      def chart(placeholder, series, options = {})

        data, x_is_date, y_is_date = series_to_json(series)
        if x_is_date
          options[:xaxis] ||= {}
          options[:xaxis].merge!({ :mode => :time })
        end
        if y_is_date
          options[:yaxis] ||= {}
          options[:yaxis].merge!({ :mode => :time })
        end

        javascript = <<EOF
        <!--[if IE]><script language="javascript" type="text/javascript" src="/javascripts/excanvas.pack.js"></script><![endif]-->
        <script language="javascript" type="text/javascript" src="/javascripts/jquery.flot.pack.js"></script>
        <script type="text/javascript">
          $.plot($('##{placeholder}'), #{data}, #{options.to_json});
        </script>
EOF
      end

      private

      def series_to_json(series)
        data_sets = []<commit_msg>encode all values as strings for compatibility with json/pure<commit_after>require 'json'

module ActionView
  module Helpers
    module ScriptaculousHelper
      
      # Insert a flot chart into the page.  <tt>placeholder</tt> should be the 
      # name of the div that will hold the chart, <tt>collections</tt> is a hash 
      # of legends (as strings) and datasets with options as hashes, <tt>options</tt>
      # contains graph-wide options.
      # 
      # Example usage:
      # <tt>chart("graph_div", <br>
      # { "January" => { :collection => @january, :x => :day, :y => :sales, 
      # :options => { :bar => :show } }, <br>
      # "February" => { :collection => @february, :x => :day, :y => :sales, 
      # :options => { :points => 'show' }},<br>
      # :grid => { :backgroundColor => %{"#fffaff"} })</tt>
      #
      def chart(placeholder, series, options = {})

        data, x_is_date, y_is_date = series_to_json(series)
        if x_is_date
          options[:xaxis] ||= {}
          options[:xaxis].merge!({ :mode => 'time' })
        end
        if y_is_date
          options[:yaxis] ||= {}
          options[:yaxis].merge!({ :mode => 'time' })
        end

        javascript = <<EOF
        <!--[if IE]><script language="javascript" type="text/javascript" src="/javascripts/excanvas.pack.js"></script><![endif]-->
        <script language="javascript" type="text/javascript" src="/javascripts/jquery.flot.pack.js"></script>
        <script type="text/javascript">
          $.plot($('##{placeholder}'), #{data}, #{options.to_json});
        </script>
EOF
      end

      private

      def series_to_json(series)
        data_sets = []
{'fraction_non_alphanumeric': 0.1726027397260274, 'fraction_numerical': 0.0, 'mean_word_length': 2.5069370330843115, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 73, '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': '2796996', 'n_tokens_mistral': 1039, 'n_tokens_neox': 1003, 'n_words': 250}