<commit_before>module SmartChart
  module Axes

    def self.included(base)
      base.class_eval do
      
        # grid axis style
        attr_accessor :axis
        
        # grid axis labels
        attr_accessor :labels
      end
    end
    
    ##
    # Determine a nice interval between labels based on the range of
    # axis values.
    #
    def self.auto_label_interval(range)
      return nil if range == 0
      # position of decimal point
      exp = Math.log10(range).floor
      # first two non-zero digits
      digits = (range.to_s + "00").sub(/^[\.0]*/, "")[0,2].to_i
      # the digits are in either 10..55 or 56..99
      # divide the base in half if they're in the lower half
      10**exp * (digits > 55 ? 1 : 0.5)
    end


    private # ---------------------------------------------------------------
  
    ##
    # Labels parameter.
    #
    def chdl
      unless bare_data_set?
        labels = data.map{ |d| d.is_a?(Hash) ? d[:label] : nil }
        labels.compact.size > 0 ? labels.join("|") : nil
      end
    end
  
    ##
    # Axis type parameter.
    #
    def chxt
      return nil unless show_axes?
      values = {
        :left   => "y",
        :right  => "r",
        :top    => "t",
        :bottom => "x"
      }
      axis.keys.map{ |i| values[i] }.join(',')
    end

    ##
    # Axis style parameter.
    #
    def chxs
      return nil unless show_axes?
      data = []
      axis.values.each_with_index do |side,i|
        data << [
          i + 1,                        # index
          side[:color],                 # color
          side[:font_size],             # font size
          alignment(side[:text_align]), # alignment
          (side[:color] ? "l" : "") +
            (side[:ticks] ? "t" : ""),  # drawing control
          side[:ticks]                  # tick mark color
        ].map{ |i| i.to_s }
      end
      out = data.map{ |i| i.join(',') }.join("|")
      out == "" ? nil : out
    end
    
    ##
    # Axis label text parameter.
    #
    def chxl
      return nil unless show_axes?
      labels = []
      axis.values.each_with_index do |a,i|
        a[:labels].each do |pos,text|
          labels[i] ||= "#{i}:"
          labels[i] << "|" + text.to_s
        end
      end
      labels.join('|')
    end
    
    ##
    # Axis label positions parameter.
    #
    def chxp
      return nil unless show_axes?
      labels = []
      axis.values.each_with_index do |a,i|
        a[:labels].each do |pos,text|
          # figure out whether are we on a vertical or horizontal axis
          dir = [:left, :right].include?(axis.keys[i]) ? "y" : "x"
          p = eval("#{dir}_axis_position(pos)")
          labels[i] ||= i.to_s
          labels[i] << "," + SmartChart.decimal_string(p)
        end
      end
      labels.join('|')
    end
    
    ##
    # Translate an alignment name into a URL parameter value.
    #
    def alignment(name)
      {
        "left"   => -1,
        "center" => 0,
        "right"  => 1<commit_msg>Refactor in preparation for auto-labels.<commit_after>module SmartChart
  module Axes

    def self.included(base)
      base.class_eval do
      
        # grid axis style
        attr_accessor :axis
        
        # grid axis labels
        attr_accessor :labels
      end
    end
    
    ##
    # Determine a nice interval between labels based on the range of
    # axis values.
    #
    def self.auto_label_interval(range)
      return nil if range == 0
      # position of decimal point
      exp = Math.log10(range).floor
      # first two non-zero digits
      digits = (range.to_s + "00").sub(/^[\.0]*/, "")[0,2].to_i
      # the digits are in either 10..55 or 56..99
      # divide the base in half if they're in the lower half
      10**exp * (digits > 55 ? 1 : 0.5)
    end


    private # ---------------------------------------------------------------
  
    ##
    # Labels parameter.
    #
    def chdl
      unless bare_data_set?
        labels = data.map{ |d| d.is_a?(Hash) ? d[:label] : nil }
        labels.compact.size > 0 ? labels.join("|") : nil
      end
    end
  
    ##
    # Axis type parameter.
    #
    def chxt
      return nil unless show_axes?
      values = {
        :left   => "y",
        :right  => "r",
        :top    => "t",
        :bottom => "x"
      }
      axis.keys.map{ |i| values[i] }.join(',')
    end

    ##
    # Axis style parameter.
    #
    def chxs
      return nil unless show_axes?
      data = []
      axis.values.each_with_index do |side,i|
        data << [
          i + 1,                        # index
          side[:color],                 # color
          side[:font_size],             # font size
          alignment(side[:text_align]), # alignment
          (side[:color] ? "l" : "") +
            (side[:ticks] ? "t" : ""),  # drawing control
          side[:ticks]                  # tick mark color
        ].map{ |i| i.to_s }
      end
      out = data.map{ |i| i.join(',') }.join("|")
      out == "" ? nil : out
    end
    
    ##
    # Axis label text parameter.
    #
    def chxl
      return nil unless show_axes?
      labels = []
      axis.values.each_with_index do |a,i|
        a[:labels].each do |pos,text|
          labels[i] ||= "#{i}:"
          labels[i] << "|" + text.to_s
        end
      end
      labels.join('|')
    end
    
    ##
    # Axis label positions parameter.
    #
    def chxp
      return nil unless show_axes?
      labels = []
      axis.values.each_with_index do |a,i|
        if [:left, :right].include?(axis.keys[i])
          l = label_positions_by_values(a)
        else
          l = label_positions_by_data_points(a)
        end
        labels << "#{i}," + l.join(',')
      end
      labels.join('|')
    end
    
    ##
    # Generate label position values, interpreting given label positions
    # as data point indices.
    #
    def label_positions_by_data_points(axis)
      axis[:labels].map do |pos,text|
        SmartChart.decimal_string(x_axis_position(pos))
      end
    end
    
    ##
    # Generate label position values, interpreting given label positions
    # as data point values.
    #
    def label_positions_by_values(axis)
      axis[:labels].map do |pos,text|
        SmartChart.decimal_string(y_axis_position(pos))
      end
    end
    
    ##
    # Translate an alignment name into a URL parameter value.
    #
    def alignment(name)
      {
        "left"   => -1,
        "center" => 0,
        "right"  => 1
{'fraction_non_alphanumeric': 0.14376158122297714, 'fraction_numerical': 0.008647313156269302, 'mean_word_length': 1.4949922958397535, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4418321', 'n_tokens_mistral': 2107, 'n_tokens_neox': 2034, 'n_words': 535}