
上図のような時間のガントチャート(?)を、列の書式設定で作成してみました。
以下、ガントチャートの作成方法を記載します。
よし!それっぽくできた😆✨ #SharePoint #ListFormatting #SVG pic.twitter.com/1156Kt4X08
— てっちゃん (@techan_k) 2020年11月2日
リスト定義
リストを作成し、次のフィールドを作成します。
| 列名(Column Name) | 列の種類(Type) |
|---|---|
| Start | 1 行テキスト(Single Line Text) or 選択肢(Choice) |
| End | 1 行テキスト(Single Line Text) or 選択肢(Choice) |
| Schedule | 何でもOK(Any) |
※ Start と End には、必ず hh:mm 形式で値が入力されている必要があります。
列の書式設定
Schedule 列の書式設定に、次の JSON を設定します。
設定が完了すると、 Schedule 列に時間単位のガントチャートが表示されます。

{
"$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
"elmType": "div",
"style": {
"display": "flex",
"flex-direction": "column",
"width": "288px",
"height": "70px",
"margin": "5px",
"fill": "currentColor"
},
"children": [
{
"elmType": "div",
"style": {
"display": "flex",
"justify-content": "space-between",
"width": "105%",
"padding-bottom": "5px"
},
"attributes": {
"class": "ms-fontColor-themePrimary ms-fontWeight-bold"
},
"children": [
{
"elmType": "span",
"txtContent": "00"
},
{
"elmType": "span",
"txtContent": "03"
},
{
"elmType": "span",
"txtContent": "06"
},
{
"elmType": "span",
"txtContent": "09"
},
{
"elmType": "span",
"txtContent": "12"
},
{
"elmType": "span",
"txtContent": "15"
},
{
"elmType": "span",
"txtContent": "18"
},
{
"elmType": "span",
"txtContent": "21"
},
{
"elmType": "span",
"txtContent": "24"
}
]
},
{
"elmType": "div",
"style": {
"width": "100%",
"border": "2px solid",
"height": "40px"
},
"attributes": {
"class": "ms-fontColor-themePrimary"
},
"children": [
{
"elmType": "svg",
"children": [
{
"elmType": "path",
"attributes": {
"d": "= 'M ' + (Number(substring([$Start],0,2)) * 60 + Number(substring([$Start],3,5))) * 0.2 + ',0 ' + (Number(substring([$End],0,2)) * 60 + Number(substring([$End],3,5))) * 0.2 + ',0 ' + (Number(substring([$End],0,2)) * 60 + Number(substring([$End],3,5))) * 0.2 + ',40 ' + (Number(substring([$Start],0,2)) * 60 + Number(substring([$Start],3,5))) * 0.2 + ', 40 Z'"
}
}
]
}
]
}
]
}
書式設定の SVG について解説
ガントチャートは、SVG の d 属性を利用して表示しています。
d 属性の計算式は次の通りです。
"d": "= 'M ' + (Number(substring([$Start],0,2)) * 60 + Number(substring([$Start],3,5))) * 0.2 + ',0 ' + (Number(substring([$End],0,2)) * 60 + Number(substring([$End],3,5))) * 0.2 + ',0 ' + (Number(substring([$End],0,2)) * 60 + Number(substring([$End],3,5))) * 0.2 + ',40 ' + (Number(substring([$Start],0,2)) * 60 + Number(substring([$Start],3,5))) * 0.2 + ', 40 Z'"
この計算式の要素を分解すると、次のようになっています。
| 点 | x 座標 | y 座標 |
|---|---|---|
| 始点 M | 0 | 0 |
| 点A | (Number(substring([$Start],0,2)) * 60 + Number(substring([$Start],3,5))) * 0.2 | 0 |
| 点B | (Number(substring([$End],0,2)) * 60 + Number(substring([$End],3,5))) * 0.2 | 0 |
| 点C | (Number(substring([$End],0,2)) * 60 + Number(substring([$End],3,5))) * 0.2 | 40 |
| 点D | (Number(substring([$Start],0,2)) * 60 + Number(substring([$Start],3,5))) * 0.2 | 40 |

x 座標のごちゃごちゃした数式は何なの?というと、次のように時間を分に直して、大きさ調整のための定数をかけています。

今回は、大きさ調整の定数を 0.2 としたため、x 座標の最大値は 288(24 [h] * 60 [m] * 0.2)となります。
そのため、div 要素の width を 288px としています。
