Sha256: fe1b7e4742d83f5ffbedd5516f4bb0a745e7426ed27d35acbb88e319fc6cc727

Contents?: true

Size: 1.5 KB

Versions: 2

Compression:

Stored size: 1.5 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_s
      Spec.new(spec).to_s
    end

    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

2 entries across 2 versions & 1 rubygems

Version Path
vega-0.2.5 lib/vega/base_chart.rb
vega-0.2.4 lib/vega/base_chart.rb