npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

table-renderer

v0.1.30

Published

convert table or spreadsheet data into an image

Downloads

12

Readme

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;