├── image.png ├── .gitignore ├── .editorconfig ├── rollup.config.js ├── license ├── src ├── index.js ├── layout.js ├── milestones.js ├── tasks.js └── style.js ├── package.json ├── demo └── index.html └── readme.md /image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nextbitlabs/gantt-chart/HEAD/image.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | dist 4 | npm-debug.log* 5 | .vscode 6 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | indent_style = tab 6 | indent_size = 2 7 | end_of_line = lf 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | -------------------------------------------------------------------------------- /rollup.config.js: -------------------------------------------------------------------------------- 1 | import resolve from '@rollup/plugin-node-resolve'; 2 | import commonjs from 'rollup-plugin-commonjs'; 3 | import pkg from './package.json'; 4 | 5 | export default [ 6 | { 7 | input: 'src/index.js', 8 | output: { 9 | name: 'ganttChart', 10 | file: pkg.browser, 11 | format: 'umd' 12 | }, 13 | plugins: [ 14 | resolve(), 15 | commonjs() 16 | ] 17 | }, 18 | { 19 | input: 'src/index.js', 20 | external: ['ms'], 21 | output: [ 22 | { file: pkg.main, format: 'cjs' }, 23 | { file: pkg.module, format: 'es' } 24 | ] 25 | } 26 | ]; 27 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | Copyright 2019 Riccardo Scalco 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import {html, define, property} from 'hybrids'; 2 | import style from './style'; 3 | import { 4 | margin, 5 | trimWidth, 6 | trimHeight, 7 | globalTransform, 8 | taskHeight, 9 | taskVerticalPosition, 10 | latestEnd, 11 | unitWidth, 12 | taskHorizontalPosition, 13 | taskWidth 14 | } from './layout'; 15 | import {tasks} from './tasks'; 16 | import {milestones} from './milestones'; 17 | 18 | function render({ 19 | width, 20 | height, 21 | tasks, 22 | milestones, 23 | globalTransform 24 | }) { 25 | return html` 26 | ${style} 27 | 28 | 29 | ${tasks} 30 | ${milestones} 31 | 32 | 33 | `; 34 | } 35 | 36 | export const GanttChart = { 37 | data: property({ 38 | tasks: [] 39 | }), 40 | width: 800, 41 | height: 600, 42 | marginRight: 20, 43 | marginBottom: 20, 44 | marginLeft: 20, 45 | marginTop: 20, 46 | margin, 47 | trimWidth, 48 | trimHeight, 49 | globalTransform, 50 | taskHeight, 51 | taskVerticalPosition, 52 | latestEnd, 53 | unitWidth, 54 | taskHorizontalPosition, 55 | taskWidth, 56 | tasks, 57 | milestones, 58 | render 59 | }; 60 | 61 | define('gantt-chart', GanttChart); 62 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@nextbitlabs/gantt-chart", 3 | "version": "1.3.2", 4 | "description": "Web component of a gantt chart.", 5 | "main": "dist/gantt-chart.cjs.js", 6 | "module": "dist/gantt-chart.esm.js", 7 | "browser": "dist/gantt-chart.umd.js", 8 | "scripts": { 9 | "build": "rollup -c", 10 | "dev": "rollup -c -w", 11 | "pretest": "npm run build", 12 | "test": "xo src/*", 13 | "lint-fix": "xo --fix src/*", 14 | "release": "np" 15 | }, 16 | "keywords": [ 17 | "gantt", 18 | "plot", 19 | "chart", 20 | "webcomponent", 21 | "component" 22 | ], 23 | "author": { 24 | "name": "Riccardo Scalco", 25 | "email": "riccardoscalco@gmail.com", 26 | "url": "http://riccardoscalco.github.io/" 27 | }, 28 | "license": "MIT", 29 | "repository": "nextbitlabs/gantt-chart", 30 | "homepage": "https://github.com/nextbitlabs/gantt-chart/", 31 | "bugs": "https://github.com/nextbitlabs/gantt-chart/issues", 32 | "devDependencies": { 33 | "@rollup/plugin-node-resolve": "^7.1.1", 34 | "hybrids": "^4.0.3", 35 | "np": "^6.2.0", 36 | "rollup": "^1.27.13", 37 | "rollup-plugin-commonjs": "^10.1.0", 38 | "xo": "^0.27.2" 39 | }, 40 | "files": [ 41 | "dist" 42 | ], 43 | "publishConfig": { 44 | "access": "public" 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/layout.js: -------------------------------------------------------------------------------- 1 | export const margin = { 2 | get: ({marginRight, marginBottom, marginLeft, marginTop}) => ({ 3 | top: marginTop, 4 | right: marginRight, 5 | bottom: marginBottom, 6 | left: marginLeft 7 | }) 8 | }; 9 | 10 | export const trimWidth = { 11 | get: ({width, margin}) => width - margin.left - margin.right 12 | }; 13 | 14 | export const trimHeight = { 15 | get: ({height, margin}) => height - margin.top - margin.bottom 16 | }; 17 | 18 | export const globalTransform = { 19 | get: ({margin}) => `translate(${margin.left}, ${margin.top})` 20 | }; 21 | 22 | export const taskHeight = { 23 | get: ({trimHeight, data}) => trimHeight / Math.max(10, data.tasks.length) 24 | }; 25 | 26 | export const taskVerticalPosition = { 27 | get: ({taskHeight}) => index => taskHeight * index 28 | }; 29 | 30 | export const latestEnd = { 31 | get: ({data}) => data.tasks.length > 0 ? 32 | data.tasks 33 | .map(el => el.start + el.duration) 34 | .reduce((p, c) => c > p ? c : p, 0) : 35 | 1 36 | }; 37 | 38 | export const unitWidth = { 39 | get: ({trimWidth, latestEnd}) => trimWidth / latestEnd 40 | }; 41 | 42 | export const taskHorizontalPosition = { 43 | get: ({unitWidth}) => start => unitWidth * start 44 | }; 45 | 46 | export const taskWidth = { 47 | get: ({unitWidth}) => duration => unitWidth * duration 48 | }; 49 | -------------------------------------------------------------------------------- /src/milestones.js: -------------------------------------------------------------------------------- 1 | import {svg} from 'hybrids'; 2 | 3 | const taskVerticalPadding = 2; 4 | 5 | const shift = (tasks, task, index) => { 6 | const isInTaskInterval = (c, task) => 7 | (c.start >= task.start) && (c.start <= (task.start + task.duration)); 8 | return tasks 9 | .reduce( 10 | (p, c, i) => (i >= index) && isInTaskInterval(c, task) ? p + 1 : p, 11 | 0 12 | ); 13 | }; 14 | 15 | export const milestones = { 16 | get: ({ 17 | data, 18 | taskVerticalPosition, 19 | taskHorizontalPosition, 20 | taskWidth, 21 | taskHeight 22 | }) => { 23 | const sortedTasks = data.tasks 24 | .map(el => el) 25 | .sort((a, b) => 26 | (a.start < b.start) || 27 | ((a.start === b.start) && (a.duration < b.duration)) ? 28 | -1 : 29 | 1 30 | ); 31 | return data.milestones.map(milestone => { 32 | const index = sortedTasks.findIndex(task => task.id === milestone.taskId); 33 | const task = sortedTasks[index]; 34 | return svg` 35 | 41 | 44 | ${milestone.title} 48 | ${milestone.date || ''} 53 | 54 | `; 55 | }); 56 | } 57 | }; 58 | -------------------------------------------------------------------------------- /src/tasks.js: -------------------------------------------------------------------------------- 1 | import {svg} from 'hybrids'; 2 | 3 | const taskVerticalPadding = 2; 4 | 5 | const ticks = (numberOfTicks, taskWidth, taskHeight) => 6 | [...new Array(numberOfTicks).keys()].map(index => svg` 7 | 13 | `); 14 | 15 | export const tasks = { 16 | get: ({ 17 | data, 18 | taskVerticalPosition, 19 | taskHorizontalPosition, 20 | taskWidth, 21 | taskHeight 22 | }) => { 23 | const sortedTasks = data.tasks 24 | .map(el => el) 25 | .sort((a, b) => 26 | (a.start < b.start) || 27 | ((a.start === b.start) && (a.duration < b.duration)) ? 28 | -1 : 29 | 1 30 | ); 31 | return sortedTasks.map((task, index) => svg` 32 | 38 | 45 | 52 | 53 | ${ticks(task.duration - 1, taskWidth(task.duration), taskHeight)} 54 | 55 | ${task.title} 59 | w${task.start} 64 | 65 | `); 66 | } 67 | }; 68 | -------------------------------------------------------------------------------- /src/style.js: -------------------------------------------------------------------------------- 1 | import {html} from 'hybrids'; 2 | 3 | export default html` 4 | 100 | `; 101 | -------------------------------------------------------------------------------- /demo/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 22 | 23 | 31 | 32 | 150 | 151 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # <gantt-chart> 2 | 3 | ![Latest Release](https://badgen.net/github/release/nextbitlabs/gantt-chart) [![Published on webcomponents.org](https://img.shields.io/badge/webcomponents.org-published-blue.svg)](https://www.webcomponents.org/element/gantt-chart) 4 | 5 | Web component implementation of a Gantt chart. 6 | 7 |
8 | 9 | 10 | 11 |
12 | 13 | ## Data model 14 | 15 | ```js 16 | { 17 | milestones: [ 18 | { 19 | id: 1, 20 | taskId: 6, // Graphical hint, it may be latest task in the milestone. 21 | title: 'Milestone A', 22 | date: 'July 2019', // Optional. 23 | additionalVerticalShift: 1 // Optional. 24 | }, 25 | ... 26 | ], 27 | tasks: [ 28 | { 29 | id: 1, 30 | title: 'task 1', 31 | start: 0, // Week number. 32 | duration: 4, // Duration in weeks. 33 | class: 'c1', 34 | progress: // Optional, defaults to 0. 35 | }, 36 | ... 37 | ] 38 | } 39 | ``` 40 | 41 | ## Exposed attributes 42 | 43 | * `width`: chart width in pixels 44 | * `height`: chart height in pixels 45 | 46 | Margins attributes help to ensure task and milestone titles are visible. 47 | 48 | * `margin-top`: space in pixel between the top border and the first task. 49 | * `margin-bottom`: space in pixel between the bottom border and the last task. 50 | * `margin-left`: space in pixel between the left border and the first task. 51 | * `margin-right`: space in pixel between the right border and the last task. 52 | 53 | See section [Usage](#usage) for an example. 54 | 55 | ## Exposed CSS Custom Properties 56 | 57 | 58 | * `--font-family` defaults to `sans-serif` 59 | * `--font-size` defaults to `12px` 60 | * `--background-color` defaults to `white` 61 | * `--visibility-ticks` defaults to `visible`, set to `hidden` to hide the week ticks 62 | 63 | Up to 10 classes can be passed: 64 | 65 | * `--c1`, the task color defaults to `purple` 66 | * `--c2`, the task color defaults to `blue` 67 | * `--c3`, the task color defaults to `gold` 68 | 69 | See section [Usage](#usage) for an example. 70 | 71 | ## Usage 72 | 73 | In an html file 74 | 75 | ```html 76 | 77 | 78 | 79 | 80 | 83 | 84 | 96 | 97 | 98 | 106 | 107 | 131 | 132 | ``` 133 | 134 | With npm: 135 | 136 | ```sh 137 | npm i @nextbitlabs/gantt-chart 138 | ``` 139 | 140 | See [demo](https://stackblitz.com/edit/gantt-chart-example). 141 | 142 | ## Development 143 | 144 | Install dependencies with 145 | 146 | ```sh 147 | npm install 148 | ``` 149 | 150 | Update build on changes with 151 | 152 | ```sh 153 | npm run dev 154 | ``` 155 | 156 | and see updates on `./demo/index.html` running a local server from `./`. 157 | 158 | ## Release 159 | 160 | ```sh 161 | npm run release 162 | ``` 163 | 164 | ## License 165 | 166 | This project is licensed under the MIT License. See the [license](license) file for details. 167 | --------------------------------------------------------------------------------