Skip to content

Commit

Permalink
vault backup: 2023-10-03 11:29:20
Browse files Browse the repository at this point in the history
Affected files:
content/.obsidian/plugins/recent-files-obsidian/data.json
content/.obsidian/workspace.json
content/_templates/default-note.md
content/attachments/chart-js-example-plugin.png
content/chart-js/Chart.js.md
content/chart-js/Getting Started with Chart.js.md
content/chart-js/Plugins of Chart.js.md
content/chart-js/Tree-shaking of Chart.js.md
content/chart-js/react-chartjs-2.md
  • Loading branch information
windsuzu committed Oct 3, 2023
1 parent 2b9c5e1 commit 215383c
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 126 deletions.
28 changes: 22 additions & 6 deletions content/.obsidian/plugins/recent-files-obsidian/data.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
{
"recentFiles": [
{
"basename": "Getting Started with Chart.js",
"path": "chart-js/Getting Started with Chart.js.md"
"basename": "react-chartjs-2",
"path": "chart-js/react-chartjs-2.md"
},
{
"basename": "Chart.js",
"path": "chart-js/Chart.js.md"
},
{
"basename": "Getting Started with Chart.js",
"path": "chart-js/Getting Started with Chart.js.md"
},
{
"basename": "Plugins of Chart.js",
"path": "chart-js/Plugins of Chart.js.md"
},
{
"basename": "chart-js-example-plugin",
"path": "attachments/chart-js-example-plugin.png"
},
{
"basename": "Tree-shaking of Chart.js",
"path": "chart-js/Tree-shaking of Chart.js.md"
},
{
"basename": "default-note",
"path": "_templates/default-note.md"
},
{
"basename": "PWA (Progressive Web Apps)",
"path": "pwa/PWA (Progressive Web Apps).md"
Expand Down Expand Up @@ -36,10 +56,6 @@
"basename": "index",
"path": "index.md"
},
{
"basename": "default-note",
"path": "_templates/default-note.md"
},
{
"basename": "pwa-passed",
"path": "attachments/pwa-passed.png"
Expand Down
26 changes: 16 additions & 10 deletions content/.obsidian/workspace.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
"type": "split",
"children": [
{
"id": "40a3cf1cc11e754b",
"id": "c1a750abe73de0fd",
"type": "tabs",
"children": [
{
"id": "6826f27d9dc27c8d",
"id": "d66aaaaefaa52e83",
"type": "leaf",
"state": {
"type": "markdown",
"state": {
"file": "chart-js/Getting Started with Chart.js.md",
"file": "chart-js/react-chartjs-2.md",
"mode": "source",
"source": false
}
Expand Down Expand Up @@ -102,7 +102,7 @@
"state": {
"type": "localgraph",
"state": {
"file": "chart-js/Getting Started with Chart.js.md",
"file": "chart-js/react-chartjs-2.md",
"options": {
"collapse-filter": true,
"search": "",
Expand Down Expand Up @@ -152,7 +152,7 @@
"state": {
"type": "outline",
"state": {
"file": "chart-js/Getting Started with Chart.js.md"
"file": "chart-js/react-chartjs-2.md"
}
}
},
Expand Down Expand Up @@ -191,7 +191,7 @@
"state": {
"type": "file-properties",
"state": {
"file": "chart-js/Getting Started with Chart.js.md"
"file": "chart-js/react-chartjs-2.md"
}
}
},
Expand All @@ -201,7 +201,7 @@
"state": {
"type": "backlink",
"state": {
"file": "chart-js/Getting Started with Chart.js.md",
"file": "chart-js/react-chartjs-2.md",
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical",
Expand All @@ -218,7 +218,7 @@
"state": {
"type": "outgoing-link",
"state": {
"file": "chart-js/Getting Started with Chart.js.md",
"file": "chart-js/react-chartjs-2.md",
"linksCollapsed": false,
"unlinkedCollapsed": false
}
Expand All @@ -243,10 +243,17 @@
"cmdr:Obsidian Git: Create backup": false
}
},
"active": "6826f27d9dc27c8d",
"active": "d66aaaaefaa52e83",
"lastOpenFiles": [
"chart-js/Chart.js.md",
"chart-js/react-chartjs-2.md",
"chart-js/Getting Started with Chart.js.md",
"chart-js/Plugins of Chart.js.md",
"attachments/chart-js-example-plugin.png",
"chart-js/Tree-shaking of Chart.js.md",
"tree-shaking.md",
"chart-js/Untitled.md",
"_templates/default-note.md",
"pwa/PWA (Progressive Web Apps).md",
"pwa/Create a PWA.md",
"pwa/Create a PWA with Next.js 13 App Router.md",
Expand All @@ -259,7 +266,6 @@
"pwa/Untitled.md",
"Configure ServiceWorker.md",
"Untitled.md",
"_templates/default-note.md",
"attachments/pwa-passed.png",
"attachments",
"Pasted image 20230913181543.png",
Expand Down
7 changes: 2 additions & 5 deletions content/_templates/default-note.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
---
title: <% tp.file.title %>
draft: false
date: 2023-09-13 18:23
date: 2023-10-03 10:52
tags:
- reading
- learning
---

> [!tldr] TL;DR
> Write some tldr
write some details or insights
write some quick thoughts

> [!info] References
> - put some references
Binary file added content/attachments/chart-js-example-plugin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions content/chart-js/Chart.js.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Chart.js
draft: false
date: 2023-10-02 17:03
date: 2023-10-03 11:12
tags:
- learning
- chart-js
Expand All @@ -11,7 +11,7 @@ Chart.js is a widely-used charting library for JavaScript developers, boasting a

In addition to its well-maintained, easy-to-use, and out-of-the-box functionalities, Chart.js offers two compelling advantages that users appreciate: a developer-friendly experience and high performance.

For developer experience, Chart.js includes built-in TypeScript typings and is compatible with [React](https://github.com/reactchartjs/react-chartjs-2), Vue, Svelte, and Angular. Furthermore, Chart.js offers comprehensive [documentation](https://www.chartjs.org/docs/latest/), an extensive [API reference](https://www.chartjs.org/docs/latest/api/), and a variety of [examples](https://www.chartjs.org/docs/latest/samples/information.html). Developers can also find numerous solved questions on Stack Overflow tagged with `chart.js`.
For developer experience, Chart.js includes built-in TypeScript typings and is compatible with [[react-chartjs-2|React]], Vue, Svelte, and Angular. Furthermore, Chart.js offers comprehensive [documentation](https://www.chartjs.org/docs/latest/), an extensive [API reference](https://www.chartjs.org/docs/latest/api/), and a variety of [examples](https://www.chartjs.org/docs/latest/samples/information.html). Developers can also find numerous solved questions on Stack Overflow tagged with `chart.js`.

For performance, Chart.js renders chart elements on an HTML5 canvas instead of as SVG, which can require managing thousands of SVG nodes in the DOM tree. This approach makes Chart.js well-suited for handling large datasets, even without the need for [data parsing and normalization](https://www.chartjs.org/docs/latest/general/performance.html). Additionally, developers have the option to implement [data decimation](https://www.chartjs.org/docs/latest/configuration/decimation.html) to sample the dataset and reduce the chart's size before rendering. Chart.js also supports tree-shaking, allowing you to further reduce the bundle size.

Expand Down
105 changes: 2 additions & 103 deletions content/chart-js/Getting Started with Chart.js.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
---
title: Getting Started with Chart.js
draft: false
date: 2023-10-03 10:32
date: 2023-10-03 10:57
tags:
- learning
- chart-js
---

You can follow the [Step-by-step guide from Chart.js](https://www.chartjs.org/docs/latest/getting-started/usage.html), learn how to create a chart from scratch, and explore all the fundamental concepts of [[Chart.js]], including chart types, elements, datasets, customization, plugins, components, and tree-shaking.


You can follow the [Step-by-step guide from Chart.js](https://www.chartjs.org/docs/latest/getting-started/usage.html), learn how to create a chart from scratch, and explore all the fundamental concepts of [[Chart.js]], including chart types, elements, datasets, customization, [[Plugins of Chart.js|plugins]], components, and [[Tree-shaking of Chart.js|tree-shaking]].
## Create a new project
The first step is to create a project with this folder structure:
```bash
Expand Down Expand Up @@ -133,105 +131,6 @@ Let's apply further customization to our bar chart. For instance, I want to cate
You can discover other options that you can fine-tune in the options object, such as aspect ratio, maximum or minimum values for the x and y scales, and custom tick formatting.
![[chart-js-example-2.png|400]]

---
## Plugins
Another way to customize the chart is by using plugins. You can employ plugins [created by other developers](https://github.com/chartjs/awesome#plugins) or create your own. The tutorial demonstrates how to create an ad-hoc one that adds a border to the chart.
```js title="src/draw.js" {1-18,24,31-38}
const chartAreaBorder = {
id: "chartAreaBorder",

beforeDraw(chart, args, options) {
const {
ctx,
chartArea: { left, top, width, height },
} = chart;

ctx.save();
ctx.strokeStyle = options.borderColor;
ctx.lineWidth = options.borderWidth;
ctx.setLineDash(options.borderDash || []);
ctx.lineDashOffset = options.borderDashOffset;
ctx.strokeRect(left, top, width, height);
ctx.restore();
},
};

(async function () {
const data = [];
new Chart(document.getElementById("draw"), {
type: "bar",
plugins: [chartAreaBorder],
options: {
aspectRatio: 1,
scales: {
x: { stacked: true },
y: { max: 40, ticks: { callback: (value) => `${value} times` } },
},
plugins: {
chartAreaBorder: {
borderColor: "red",
borderWidth: 2,
borderDash: [5, 5],
borderDashOffset: 2,
},
},
},
})();
```
---
## Tree-shaking
Finally, you can apply tree-shaking when you finish development and ready to push your project to production. Tree-shaking is a term for removing unused code from the JavaScript bundle.
When we build the project without tree-shaking, which implies that we imported all available components from `"chart.js/auto"`, we receive the following bundle result:
```bash
> chartjs-example@1.0.0 build
> parcel build src/index.html

✨ Built in 7.05s

dist\index.html 313 B 5.26s
dist\index.7311b552.js 209.1 KB 964ms
```
To achieve tree-shaking is simple. We only need to replace `import Chart from "chart.js/auto";` with the following method of importing only the necessary components to create our chart.
```js
import {
Chart,
BarController,
CategoryScale,
LinearScale,
BarElement,
Colors,
Legend,
} from "chart.js";

Chart.register(
BarController,
CategoryScale,
LinearScale,
BarElement,
Colors,
Legend,
);
```
And the result shows our bundle size is reduced.
```bash
> chartjs-example@1.0.0 build
> parcel build src/index.html

✨ Built in 2.58s

dist\index.html 313 B 832ms
dist\index.e65aa5c3.js 197.19 KB 634ms
```
> [!tip]
> If you don't know which components you need for creating your chart, you can open the browser console and learn from the warning message, such as:
> ```
> Unhandled Promise Rejection: Error: "bar" is not a registered controller.
> ```
> [!info] References
> - [Step-by-step guide | Chart.js (chartjs.org)](https://www.chartjs.org/docs/latest/getting-started/usage.html)
56 changes: 56 additions & 0 deletions content/chart-js/Plugins of Chart.js.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
title: Plugins of Chart.js
draft: false
date: 2023-10-03 11:03
tags:
- learning
- chart-js
---

Another way to customize the chart is by using plugins. You can employ plugins [created by other developers](https://github.com/chartjs/awesome#plugins) or create your own. The tutorial demonstrates how to create an ad-hoc one that adds a border to the chart.
```js title="src/draw.js" {1-18,24,31-38}
const chartAreaBorder = {
id: "chartAreaBorder",

beforeDraw(chart, args, options) {
const {
ctx,
chartArea: { left, top, width, height },
} = chart;

ctx.save();
ctx.strokeStyle = options.borderColor;
ctx.lineWidth = options.borderWidth;
ctx.setLineDash(options.borderDash || []);
ctx.lineDashOffset = options.borderDashOffset;
ctx.strokeRect(left, top, width, height);
ctx.restore();
},
};

(async function () {
const data = [];
new Chart(document.getElementById("draw"), {
type: "bar",
plugins: [chartAreaBorder],
options: {
aspectRatio: 1,
scales: {
x: { stacked: true },
y: { max: 40, ticks: { callback: (value) => `${value} times` } },
},
plugins: {
chartAreaBorder: {
borderColor: "red",
borderWidth: 2,
borderDash: [5, 5],
borderDashOffset: 2,
},
},
},
})();
```
![[chart-js-example-plugin.png]]
> [!info] References
> - put some references
Loading

0 comments on commit 215383c

Please sign in to comment.