Quantcast
Channel: 簡睿隨筆
Viewing all articles
Browse latest Browse all 897

Obs134|用Dataviewjs製作簡單的建檔統計圖-使用Charts外掛

$
0
0

h使用Charts外掛可以輕鬆地產生統計表,資料來源則使用Dataviewjs來查詢一段時間內的建檔筆記數量統計。

1. Charts基本使用

Charts外掛安裝後可以調用Insert new Chart命令,輸入三個欄位資料:

  • X-Axis (X軸標題): Aug. 10, Aug. 11, Aug. 12
  • Y-Axis Name (Y軸標題): Number of created
  • Y-Axis Data (Y軸資料): 10, 5, 3
  • Start at Zero: 勾選

gh|700

```chart
type: bar
labels: [Aug. 10, Aug. 11, Aug. 12]
series:
  - title: Number of Created
    data: [10, 5, 3]
tension: 0.2
width: 80%
labelColors: false
fill: false
beginAtZero: true
bestFit: false
bestFitTitle: undefined
bestFitNumber: 0
```

gh

2. Dataviewjs統計

gh

gh

---
aliases: []
created: 2023-05-22 18:32:55
modified: 2023-08-12 12:27:05
number headings: first-level 1, max 6, _.1.1.
tags: []
type:
  - 專案筆記
folder: '""'
daysback: 3
chartDays: 100
---
# Chart Test

```dataviewjs
const iDaysback = parseInt(dv.current().daysback) || 2;
const iChartDays = parseInt(dv.current().chartDays) || 10;

const lStartTime = moment();
const sFolder = dv.current().folder || '';  // 要統計的資料夾
// const dateField = 'created'; // 使用YAML欄位時需要
const sChartColor = '#4e9a06';
const oDocs = dv.pages(sFolder);

function getRecentDocs(iNumDays) {
  return oDocs.where(f => {
    //if (!f[dateField]) return false;  // 如果沒有日期欄位則不繼續處理
    const lPastTime = moment().subtract(iNumDays, 'days');
    let lDocTime = f.file.ctime;   // moment(f[dateField]);  //.toISO());
    let valid = lDocTime >= lPastTime && lStartTime >= lDocTime;  //.isAfter(lPastTime) && lDocTime.isBefore(start);
    return valid;
  });
}

// creating the table
var oChartDocs = getRecentDocs(iDaysback);

// 輸出表格樣式的符合條件筆記清單
dv.span(iDaysback + " days ago");
dv.table(['link', 'Created on'], oChartDocs
    .sort(file => file.file.ctime, 'desc')
    .map(page => [page.file.link, page.file.ctime]));

// creating the charts
var oChartDocs = getRecentDocs(iChartDays).sort(f => f.file.ctime);
var daysData = [];
var totalcount = 0;

dv.paragraph(iChartDays + " days ago");
// formatting the data
for (var i=0; i<=oChartDocs.length; i++) {
  var f = oChartDocs[i];
  if (f) {
    var itemDate = moment(f.file.ctime.ts);  //moment(f[dateField]);
    var newDate = moment(itemDate).format(&#039;MM-DD&#039;);
     var index = daysData.findIndex(d => d.label === newDate);
    if (index !== -1) {
        daysData[index].num += 1;  // 該日數值遞增
    } else {
        daysData.push({label: newDate, num: 1});  // 該日啟始為1
    }
    totalcount += 1;
  }
};

var labels = [], data = [], aggData = [];

daysData.map(el => {
  labels.push(el.label);
  data.push(el.num);

  if (aggData.length) {
    var lastNum = aggData[aggData.length - 1];
    aggData.push(el.num + lastNum);
  } else {
    aggData.push(el.num);
  }
});

const lineChart = {
  type: &#039;line&#039;,
  data: {
    labels: labels,
    datasets: [{
      label: &#039;Docs created&#039;,
      data: data,
      backgroundColor: [
        sChartColor
      ],
      borderColor: [
        sChartColor
      ],
      borderWidth: 1
    }]
  }
}

const aggregateChart = {
  type: &#039;line&#039;,
  data: {
    labels: labels,
    datasets: [{
      label: &#039;Aggregate Docs Created&#039;,
      data: aggData,
      backgroundColor: [
          sChartColor
      ],
      borderColor: [
          sChartColor
      ],
      borderWidth: 1
    }]
  }
}

dv.paragraph(&#039;Created Chart&#039;);
window.renderChart(lineChart, this.container);

dv.paragraph(&#039;Growth Chart&#039;);
window.renderChart(aggregateChart, this.container);

dv.paragraph(&#039;Total: &#039; + totalcount);
```

3. 相關鏈接

4. 教學影片

##

您可能也會有興趣的類似文章


Viewing all articles
Browse latest Browse all 897

Trending Articles