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

table2canvas

v2.0.5

Published

Draw table on canvas for node and browser!

Downloads

7

Readme

Draw table on canvas! Can be used both of node and browser.

Install

  • node (Must install canvas) npm install canvas table2canvas or yarn add canvas table2canvas

  • browser npm install table2canvas or yarn add table2canvas

Usage

  • node
import { Canvas } from 'canvas';
import Table2canvas from 'table2canvas';
import fs from 'fs';

const columns = [
    {
        title: 'name',
        children: [
            {
                title: 'first',
                dataIndex: 'first'
            },
            {
                title: 'last',
                dataIndex: 'last',
                render: (text, row, i) => {
                    if (i === 0) {
                        return { text, rowSpan: 3 }
                    }
                    if (i === 1) {
                        return { text, rowSpan: 0 }
                    }
                    if (i === 2) {
                        return { text, rowSpan: 0 }
                    }
                    return text;
                }
            }
        ]
    },
    { title: 'age', dataIndex: 'age', textAlign: 'center', textColor: 'blue' },
    { title: 'weight', dataIndex: 'weight', render: '{c}kg' },
    { title: 'address', dataIndex: 'address', width: 200 },
    {
        title: 'other-abcd',
        children: [
            {
                title: 'a',
                dataIndex: 'a',
                render: (text, row, i) => {
                    if (i === 2) {
                        return { text, colSpan: 2, rowSpan: 2 }
                    }
                    if (i === 3) {
                        return { text, colSpan: 0, rowSpan: 0 }
                    }
                    return text;
                }
            },
            {
                title: 'b',
                dataIndex: 'b',
                render: (text, row, i) => {
                    if (i === 2 || i === 3) {
                        return { text, colSpan: 0, rowSpan: 0 }
                    }
                    return text;
                }
            },
            {
                title: 'c+d',
                children: [
                    {
                        title: 'c',
                        dataIndex: 'c'
                    },
                    {
                        title: 'd',
                        dataIndex: 'd'
                    }
                ]
            }
        ]
    }
]

const dataSource: any[] = [
    { first: 'Jack', last: 'smith', age: 16, weight: 50, address: '1.somewhere\n2.somewhere', a: 'a1', b: 'b1', c: 'c1', d: 'd1' },
    { first: 'Jack', last: 'smith', age: 26, weight: 60, address: 'street9527123456789no.,it is a to long adress!', a: 'a2', b: 'b2', c: 'c2', d: 'd2' },
    { first: 'Jack', last: 'last', age: 36, weight: 70, address: 'where', a: 'merge-a+b\nline2\nline3', b: 'merge-a+b', c: 'c3', d: 'd3' },
    { first: 'Tom', last: 'last', age: 46, weight: 80, address: 'where', a: 'merge-a+b', b: 'merge-a+b', c: 'c4', d: 'd4' },
]

const table = new Table2canvas({
    canvas: new Canvas(2, 2),
    columns: columns,
    dataSource: dataSource,
    bgColor: '#fff',
    text: 'This is table title!',
})

const buffer = table.canvas.toBuffer();

fs.writeFileSync('demo.png', buffer);

result:demo.png basic usage

  • browser
import Table2canvas from 'table2canvas';

const columns = [...]

const dataSource = [...]

const table = new Table2canvas({
    canvas: document.createElement('canvas'),
    ...     
});

document.body.appendChild(table.canvas);

Options

TableOpt

| Property | Description | type | Default | Required | | :----: | :----: | :----: | :----: | :----: | | canvas | Draw table to whitch canvas | Canvas | HTMLCanvasElement | - | yes | | width | canvas's width | number | 'auto' | 'auto' | no | | height | canvas's height | number | 'auto' | 'auto' | no | | bgColor | canvas's background color | string | 'transparent' | no | | padding | Table in canvas's padding | number | number[] | 10 | no | | columns | columns config | IColumn[] | - | yes | | dataSource | dataSource for table | Record<string,any> | - | - | | style | Table's style | TableStyle | ... | no | | text | Table's title | string | - | no | | textStyle | Table's title style(color,fontSize,fontFamily,textAlign,lineHeight) | TextStyle | ... | no |

IColumn

| Property | Description | type | Default | Required | | :----: | :----: | :----: | :----: | :----: | | title | Table's td text | string | - | yes | | dataIndex | Display field of the data recordg | string | - | no | | titleColor | title's color | string | 'rgba(0,0,0,0.85)' | no | | titleFontWeight | title's font weight | string | 'bold' | no | | titleFontSize | title's font size | string | inherit TableStyle.fontSize | no | | textAlign | The specify which way that column is aligned | 'left' | 'right' | 'center' | 'left' | no | | textColor | Color of column's body | string | 'rgba(0,0,0,0.85)' | no | | textFontWeight | font weight of column's body | string | '' | no | | textFontSize | font size of column's body | string | inherit TableStyle.fontSize | no | | textOverflow | text overflow show type | 'auto' | 'ellipsis' | 'auto' | no | | width | Width of this column | number | defaultStyle.columnWidth | no | | children | Group table head | IColumn[] | - | no | | render | custom render,if is string,{c} is row[dataIndex] | string | Function | - | no |

TableStyle

| Property | Description | type | Default | Required | | :----: | :----: | :----: | :----: | :----: | | columnWidth | default width of all columns | number | 150 | no | | borderColor | color of border | string | '#e8e8e8' | no | | textAlign | The specify which way that column is aligned | string | 'left' | no | | color | color of table | string | 'rgba(0,0,0,0.85)' | no | | fontSize | fontSize of table | string | '14px' | no | | fontFamily | fontFamily of table | string | 'sans-serif' | no | | headerBgColor | background color of header cell | string | 'rgba(0,0,0,0.02)' | no | | background | background color of table | string | - | no| | padding | padding of table cell | number | number[] | 16 | no | | lineHeight | text lineHeight | number | 22 | no |