Giter VIP home page Giter VIP logo

table-renderer's Introduction

table-renderer

convert table or spreadsheet data into an image

Background

One day, I had to build a slack slash command which reports marketing reports to our company slack channel. I wanted to format the command results look like table, but I could not find a simple way to do that. I decided to build a table-like view using markdown text, and struggled to do that. However, the layout was broken with small windows or with CJK charaters. So I decided to build the report as an image.

table-renderer

I hope this module will help someone who wants to convert a simple spreadsheet data into an image,

Install

npm install table-renderer canvas

node-canvas module is peer-dependency. You have to install it, too.

Examples

Usage

import path from 'path';
import TableRenderer, { saveImage } from 'table-renderer';

const renderTable = TableRenderer().render;

const canvas = renderTable({
	title: 'Marketing Summary',
	columns: [
		{ width: 200, title: 'Campaign', dataIndex: 'campaign' },
		{ width: 100, title: 'Install', dataIndex: 'install', align: 'right' },
		{ width: 100, title: 'Cost', dataIndex: 'cost', align: 'right' },
	],
	dataSource: [
		'-',
		{ campaign: 'Google CPC', install: '12', cost: '$ 400' },
		{ campaign: 'Facebook CPC', install: '3', cost: '$ 60' },
		{ campaign: 'Youtube Video', install: '131', cost: '$ 1,230' },
		'-',
		{ campaign: 'Total', install: '146', cost: '$ 1,690' },
	],
});

saveImage(canvas, path.join(__dirname, 'example.png'));

single table

API

TableRenderer

TableRenderer(options = {}) => ({ render: function });

options

  • cellWidth {number} default width for a table cell. default = 100
  • cellHeight {number} default height for a table cell. default = 40
  • offsetLeft {number} default text offset from left border of table cell. default = 8
  • offsetTop {number} default text offset from top border of table cell. default = 26
  • spacing {number} spacing between tables. default = 20
  • titleSpacing {number} spacing between title and a table. default = 10
  • fontFamily {string} default = 'sans-serif'
  • paddingVertical {number} vertical padding of a page. default = 0
  • paddingHorizontal {number} horizontal padding of a page. default = 0
  • backgroundColor {string} page background color. default = '#ffffff'

TableRenderer#render

render((tables: Object | Array)) => Canvas;

tables parameter is either Object or Array. Single table is comprised of title, columns, and dataSource, where title is optional. Parameters of render function resembles ant-design Table paramters.

The function returns Canvas object, which is an instance of node-canvas. So, you can add canvas operations to it.

render({
    title: 'Marketing Summary',
    columns: [...],
    dataSource: [...]
});

saveImage

saveImage((canvas: Canvas), (filepath: String)) => Promise;

table-renderer's People

Contributors

dependabot[bot] avatar eve0415 avatar idw111 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

table-renderer's Issues

Common JS support

Hi, I just recently found this useful package and would love to use it in my code!

However, it seems like there's some error while trying to use it as common js.

const path = require('path');
const TableRenderer = require('table-renderer');
const {saveImage} = require('table-renderer');
const renderTable = TableRenderer().render;

module.exports = {
        //code...
}

The error

Uncaught TypeError TypeError: TableRenderer is not a function

The example I based off of

import path from 'path';
import TableRenderer, { saveImage } from 'table-renderer';

const renderTable = TableRenderer().render;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.