Sha256: 9efe396803a597324662c85e56833de1865af3ebd977fad86e7715f3d50425d7

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

require "node/echarts/version"
require "json"

module Node
  module Echarts
    # Generate chart picture
    #
    # Example:
    #  >> chart("/home/foo/sample.png", {titile: {text: 'T1'},
    #                          xAxis: ['c'],
    #                          series: [{type: 'bar', data: [100]}]
    #                          },
    #           400, 400)
    #
    #  Arguments:
    #    path: (string)
    #    data: (hash)
    #    width: integer
    #    height: integer
    def self.chart(path, data, width=400, height=400)
      cmd_str = "var echarts = require('echarts'); var Canvas = require('canvas'); var fs = require('fs'); #{@theme}; echarts.setCanvasCreator(function () { var canvas = new Canvas(#{width}, #{height}); return canvas; }); var chart = echarts.init(new Canvas(#{width}, #{height}), 'infographic'); chart.setOption(#{data.to_json.gsub("\"", "'")}); fs.writeFileSync('#{path}', chart.getDom().toBuffer()); process.exit()"
      `export export NODE_PATH=$(npm config get prefix)/lib/node_modules; node -e "#{cmd_str}"`
    end

    # Register echarts theme by file
    #
    # Example:
    #  >> Node::Echarts.register_theme("/home/infographic")
    #
    # Arguments:
    #  path: (string)
    def self.register_theme(path)
      @theme = File.read(path).
        gsub(/\/\/.*$/, '').  # remove comment
        gsub("\n", "").       # remove \n
        gsub(/\s+/, " ").     # compact blank
        gsub("\"", "'")       # replace " with '
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
node-echarts-0.1.1 lib/node/echarts.rb