Sha256: 249b11131df7bd30f52023fdc1c343b5d10edcf21fc6f6648295170542cb571f

Contents?: true

Size: 1.56 KB

Versions: 3

Compression:

Stored size: 1.56 KB

Contents

module Vega
  class BaseChart
    extend MethodHelpers

    def initialize
      @spec = {
        "$schema": @schema,
        width: "container",
        height: "container"
        # maybe add later
        # config: {mark: {tooltip: true}}
      }
    end

    def embed_options!(value)
      usermeta!(embedOptions: value)
      self
    end
    immutable_method :embed_options

    def to_html(nonce: nil)
      Spec.new(spec).to_html(nonce: nonce)
    end
    alias_method :to_s, :to_html

    def to_iruby
      Spec.new(spec).to_iruby
    end

    # for https://github.com/SciRuby/iruby/issues/314
    def to_iruby_mimebundle(**)
      [[to_iruby].to_h, {}]
    end

    def to_json
      spec.to_json
    end

    private

    def initialize_dup(*)
      # dup one-level up
      @spec = @spec.transform_values(&:dup)
      super
    end

    def data_value(value)
      value = value.to_a if defined?(Rover::DataFrame) && value.is_a?(Rover::DataFrame)
      value = value.to_a[0] if defined?(Daru::DataFrame) && value.is_a?(Daru::DataFrame)
      case value
      when Array
        {values: value}
      when String
        {url: value}
      else
        value
      end
    end

    def export(cmd)
      require "open3"

      pkg = cmd.start_with?("vg") ? "vega-cli" : "vega-lite"
      # use --no to prevent automatic installs
      stdout, stderr, status = Open3.capture3("npm", "exec", "--no", "--package=#{pkg}", "--", cmd, stdin_data: to_json, binmode: true)
      raise "Command failed: #{stderr}" unless status.success? && stdout.bytesize > 0
      stdout
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
vega-0.3.0 lib/vega/base_chart.rb
vega-0.2.7 lib/vega/base_chart.rb
vega-0.2.6 lib/vega/base_chart.rb