Giter VIP home page Giter VIP logo

table2canvas's Introduction

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

table2canvas's People

Contributors

cx690 avatar

Stargazers

 avatar kiki-kanri avatar Zhazha_JiaYiZhen avatar Code You avatar 地瓜 avatar Yang Woodu avatar

Watchers

 avatar

table2canvas's Issues

Request for support on a few features

Hi, im wondering if you can add a few features that could make this repo more usable for me to handle some of my projects

Currently i am working on replicating one of my tables i generate with another repo canvas-tables but theres issues i am not able to deal with unless the dev adds a few more functions.
image

Likewise for here i am hoping if you are able to further develop this repo to make it better suit for some of my applications.

These are my features ill like to ask if you could implement

  • Allow Table color change on the frame
  • Allow automatic fitting of table to the canvas size, and if needed block out some of the words like in the image

Hope to hear from you soon

有辦法支持自訂字體嗎

我在linux上產生表格,請問有辦法使用載入檔案的方式載入字體嗎,還是系統上一定要安裝字體檔

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.