對,每個禮拜都要畫
而且是逼員工加班畫圖呢!!
picasso.js
套件
picasso.chart
函式複製過來並稍做修改,程式碼如下:
picasso.chart({
element: document.querySelector('#container'),
data,
settings: {
scales: {
y: {
data: { extract: { field: 'Category' } }
},
t: {
data: { fields: ['Started', 'Ended'] },
expand: 0.1
}
},
components: [{
type: 'grid-line',
x: 't'
},{
type: 'axis',
dock: 'left',
scale: 'y'
},{
type: 'axis',
dock: 'bottom',
scale: 't',
formatter: {
type: 'd3-time',
format: '%Y-%m'
}
},{
key: 'bars',
type: 'box',
data: {
extract: {
field: 'Category',
props: {
start: { field: 'Started' },
end: { field: 'Ended' }
}
}
},
settings: {
orientation: 'horizontal',
major: { scale: 'y' },
minor: { scale: 't' },
box: {
fill: function(d){
let colors = ["blue", "green", "purple", "orange", "yellow"];
return colors[Math.floor(Math.random() * colors.length)];
},
width: 0.8
}
}
}, {
type: 'labels',
displayOrder: 2,
settings: {
sources: [{
component: 'bars',
selector: 'rect',
strategy: {
type: 'bar',
settings: {
direction: 'right',
labels: [{
placements: [{
position: 'outside'
}],
label: node => `${Math.round((node.data.end.value - node.data.start.value) / 864e5)} days`
}]
}
}
}]
}
}]
}
})
data
裝 Array
Array
中裝了 type
和 data
Task 1
改成 工作名字
,後面的兩個數字,就是 js 的 Date()
格式換成毫秒數14...
開頭的,大概可以猜到他就是時間。data = {
var jobs = [
['宮廟修繕招標', '2022-09-15', '2022-09-30'],
['得標公告', '2022-10-01', '2022-10-05'],
['開工典禮', '2022-10-05', '2022-10-06'],
['建築師勘查', '2022-10-06', '2022-10-08'],
['繪製工程圖', '2022-10-07', '2022-10-09'],
['拆除作業', '2022-10-11', '2022-10-13'],
['整修作業', '2022-10-13', '2022-10-20'],
['裝回作業', '2022-10-15', '2022-10-25'],
['驗收作業', '2022-10-26', '2022-10-31'],
];
var arr = [
['Category', 'Started', 'Ended'],
];
for (var i = 0; i < jobs.length; i++) {
var job_start = new Date(jobs[i][1]).valueOf(); // 起始日期換算成毫秒
var job_end = new Date(jobs[i][2]).valueOf(); // 結束日期換算成毫秒
arr.push([
jobs[i][0],
job_start,
job_end
]);
}
return [{
type: 'matrix',
data: arr
}];
}
picasso.js
接受的格式(毫秒型態的時間格式)