|
| 1 | +<script lang="ts"> |
| 2 | + import type { TAbstractFile } from "obsidian"; |
| 3 | + import type BetterWordCount from "src/main"; |
| 4 | + import { |
| 5 | + getAllFileContentInFolder, |
| 6 | + getAllFilesInFolder, |
| 7 | + } from "src/utils/FileUtils"; |
| 8 | + import { ArcElement, Chart, PieController, Tooltip } from "chart.js"; |
| 9 | + import { getWordCount } from "src/utils/StatUtils"; |
| 10 | + // Enable minimal chart js |
| 11 | + Chart.register(ArcElement, PieController); |
| 12 | + Chart.register(Tooltip); |
| 13 | +
|
| 14 | + export let file: TAbstractFile; |
| 15 | + export let plugin: BetterWordCount; |
| 16 | +
|
| 17 | + let chartContainer: HTMLCanvasElement; |
| 18 | +
|
| 19 | + // Function to map the word count of each file to a chart js data object |
| 20 | + const getFolderWordStats = async () => { |
| 21 | + // Get all files in the folder |
| 22 | + const allFiles = getAllFilesInFolder(plugin, file.path); |
| 23 | +
|
| 24 | + // Get the content of all files in the folder |
| 25 | + const content = await getAllFileContentInFolder(allFiles); |
| 26 | +
|
| 27 | + // Get the word count of all files in the folder |
| 28 | + const wordCounts = content.map((c) => getWordCount(c)); |
| 29 | +
|
| 30 | + return { |
| 31 | + labels: allFiles.map((file) => file.name), |
| 32 | + datasets: [ |
| 33 | + { |
| 34 | + label: "Word Count", |
| 35 | + data: wordCounts, |
| 36 | + backgroundColor: "rgba(255, 99, 132, 0.2)", |
| 37 | + borderColor: "rgba(255, 99, 132, 1)", |
| 38 | + borderWidth: 1, |
| 39 | + }, |
| 40 | + ], |
| 41 | + }; |
| 42 | + }; |
| 43 | +
|
| 44 | + // Function to get all the data and render the chart |
| 45 | + async function renderChart(): Promise<number> { |
| 46 | + const options = { |
| 47 | + title: { |
| 48 | + display: true, |
| 49 | + text: "All Files and there Word Count", |
| 50 | + position: "top", |
| 51 | + }, |
| 52 | + rotation: -0.7 * Math.PI, |
| 53 | + legend: { |
| 54 | + display: false, |
| 55 | + }, |
| 56 | + }; |
| 57 | + const data = await getFolderWordStats(); |
| 58 | +
|
| 59 | + new Chart(chartContainer, { |
| 60 | + type: "pie", |
| 61 | + data: data, |
| 62 | + options: options, |
| 63 | + }); |
| 64 | +
|
| 65 | + return data.datasets[0].data.reduce( |
| 66 | + (acc, current) => (acc += current), |
| 67 | + 0 |
| 68 | + ); |
| 69 | + } |
| 70 | +</script> |
| 71 | + |
| 72 | +<div> |
| 73 | + <h1>{file.name}</h1> |
| 74 | + |
| 75 | + {#await renderChart()} |
| 76 | + <p>Counting</p> |
| 77 | + {:then data} |
| 78 | + <p>Total: {data} words</p> |
| 79 | + {:catch error} |
| 80 | + <p style="color: red">{error.message}</p> |
| 81 | + {/await} |
| 82 | + |
| 83 | + <canvas class="pieChart" bind:this={chartContainer} /> |
| 84 | +</div> |
0 commit comments