Skip to content

Commit

Permalink
vault backup: 2023-10-03 16:30:38
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/chart-js/react-chartjs-2/Datasets in react-chartjs-2.md
content/chart-js/react-chartjs-2/Tree-shaking in react-chartjs-2.md
content/chart-js/react-chartjs-2/Tree-shaking with react-chartjs-2.md
content/chart-js/react-chartjs-2/datasetIdKey in react-chartjs-2.md
content/chart-js/react-chartjs-2/react-chartjs-2.md
quartz.layout.ts
  • Loading branch information
windsuzu committed Oct 3, 2023
1 parent 32b39e5 commit e895abd
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 73 deletions.
16 changes: 8 additions & 8 deletions content/.obsidian/plugins/recent-files-obsidian/data.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
{
"recentFiles": [
{
"basename": "datasetIdKey in react-chartjs-2",
"path": "chart-js/react-chartjs-2/datasetIdKey in react-chartjs-2.md"
"basename": "Tree-shaking in react-chartjs-2",
"path": "chart-js/react-chartjs-2/Tree-shaking in react-chartjs-2.md"
},
{
"basename": "react-chartjs-2",
"path": "chart-js/react-chartjs-2/react-chartjs-2.md"
},
{
"basename": "Tree-shaking with react-chartjs-2",
"path": "chart-js/react-chartjs-2/Tree-shaking with react-chartjs-2.md"
"basename": "Datasets in react-chartjs-2",
"path": "chart-js/react-chartjs-2/Datasets in react-chartjs-2.md"
},
{
"basename": "Tree-shaking of Chart.js",
"path": "chart-js/Tree-shaking of Chart.js.md"
},
{
"basename": "Chart.js",
Expand All @@ -24,10 +28,6 @@
"basename": "Plugins of Chart.js",
"path": "chart-js/Plugins of Chart.js.md"
},
{
"basename": "Tree-shaking of Chart.js",
"path": "chart-js/Tree-shaking of Chart.js.md"
},
{
"basename": "react-chart-js-2",
"path": "attachments/react-chart-js-2.png"
Expand Down
46 changes: 12 additions & 34 deletions content/.obsidian/workspace.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,15 @@
"type": "split",
"children": [
{
"id": "c1a750abe73de0fd",
"id": "b05d5eb1f1c53401",
"type": "tabs",
"children": [
{
"id": "20f2ca150c0cd224",
"id": "94e05f6dd732b8d3",
"type": "leaf",
"state": {
"type": "markdown",
"state": {
"file": "chart-js/react-chartjs-2/datasetIdKey in react-chartjs-2.md",
"mode": "source",
"source": false
}
}
},
{
"id": "8a675673e0ddd445",
"type": "leaf",
"state": {
"type": "markdown",
"state": {
"file": "chart-js/react-chartjs-2/Tree-shaking with react-chartjs-2.md",
"mode": "source",
"source": false
}
"type": "empty",
"state": {}
}
}
]
Expand Down Expand Up @@ -114,7 +98,6 @@
"state": {
"type": "localgraph",
"state": {
"file": "chart-js/react-chartjs-2/datasetIdKey in react-chartjs-2.md",
"options": {
"collapse-filter": true,
"search": "",
Expand All @@ -137,7 +120,7 @@
"repelStrength": 10,
"linkStrength": 1,
"linkDistance": 250,
"scale": 0.713275462622443,
"scale": 0.5087618855792603,
"close": true
}
}
Expand All @@ -163,9 +146,7 @@
"type": "leaf",
"state": {
"type": "outline",
"state": {
"file": "chart-js/react-chartjs-2/datasetIdKey in react-chartjs-2.md"
}
"state": {}
}
},
{
Expand Down Expand Up @@ -202,9 +183,7 @@
"type": "leaf",
"state": {
"type": "file-properties",
"state": {
"file": "chart-js/react-chartjs-2/datasetIdKey in react-chartjs-2.md"
}
"state": {}
}
},
{
Expand All @@ -213,7 +192,6 @@
"state": {
"type": "backlink",
"state": {
"file": "chart-js/react-chartjs-2/datasetIdKey in react-chartjs-2.md",
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical",
Expand All @@ -230,7 +208,6 @@
"state": {
"type": "outgoing-link",
"state": {
"file": "chart-js/react-chartjs-2/datasetIdKey in react-chartjs-2.md",
"linksCollapsed": false,
"unlinkedCollapsed": false
}
Expand All @@ -256,15 +233,16 @@
"cmdr:Obsidian Git: Create backup": false
}
},
"active": "20f2ca150c0cd224",
"active": "94e05f6dd732b8d3",
"lastOpenFiles": [
"chart-js/react-chartjs-2/Tree-shaking in react-chartjs-2.md",
"chart-js/react-chartjs-2/react-chartjs-2.md",
"chart-js/react-chartjs-2/Tree-shaking with react-chartjs-2.md",
"chart-js/react-chartjs-2/datasetIdKey in react-chartjs-2.md",
"chart-js/react-chartjs-2/Datasets in react-chartjs-2.md",
"chart-js/Tree-shaking of Chart.js.md",
"react-chart-js-2.md",
"chart-js/Chart.js.md",
"chart-js/Getting Started with Chart.js.md",
"chart-js/Plugins of Chart.js.md",
"chart-js/Tree-shaking of Chart.js.md",
"chart-js/react-chartjs-2",
"chart-js/Untitled.md",
"attachments/react-chart-js-2.png",
Expand Down
61 changes: 61 additions & 0 deletions content/chart-js/react-chartjs-2/Datasets in react-chartjs-2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
title: Datasets in react-chartjs-2
draft: false
date: 2023-10-03 16:30
tags:
- learning
- chart-js
- react-chartjs-2
---

You may encounter a problem where certain events, such as hover tooltips, inadvertently trigger the merging of datasets in a chart. This issue occurs because the library requires a unique key to track changes in dataset series, and in the absence of such a key, it cannot distinguish between datasets during updates.

To address the problem, you have two possible solutions:
1. Add a `label` to each dataset. [[react-chartjs-2]] uses the `label` property as the default key to distinguish datasets.

```js title="Add label" {5,11}
const data: ChartData<"line"> = {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [
{
label: "Dataset 1",
data: [12, 19, 3, 5, 2, 3],
borderColor: "rgb(255, 99, 132)",
backgroundColor: "rgba(255, 99, 132, 0.5)",
},
{
label: "Dataset 2",
data: [2, 3, 20, 5, 1, 4],
borderColor: "rgb(54, 162, 235)",
backgroundColor: "rgba(54, 162, 235, 0.5)",
},
],
};
```

2. You can specify a different property to be used as a key by passing a `datasetIdKey` prop to your chart component.

```js title="Add datasetIdKey" {19}
const data: ChartData<"line"> = {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [
{
label: "",
data: [12, 19, 3, 5, 2, 3],
borderColor: "rgb(255, 99, 132)",
backgroundColor: "rgba(255, 99, 132, 0.5)",
},
{
label: "",
data: [2, 3, 20, 5, 1, 4],
borderColor: "rgb(54, 162, 235)",
backgroundColor: "rgba(54, 162, 235, 0.5)",
},
],
};

<Line data={data} options={options} datasetIdKey="borderColor" />
```

> [!info] References
> - [Working with datasets | react-chartjs-2](https://react-chartjs-2.js.org/docs/working-with-datasets)
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
---
title: Tree-shaking with react-chartjs-2
draft: false
date: 2023-10-03 16:29
tags:
- learning
- chart-js
- react-chartjs-2
---

There are three different ways to import dependencies before creating our chart:
1. The laziest way - import all dependencies from [[Chart.js]].
2. The tree-shakable way - import only what is needed for the specific chart.
3. The tree-shakable typed chart components - no need for a controller.

```tsx title="lazy way" {1}
import 'chart.js/auto';
import { Chart } from 'react-chartjs-2';

<Chart type='line' data={chartData} />
```

When you use `Chart` as your chart component, it's almost the same way to implement [[Tree-shaking of Chart.js|tree-shaking in native Chart.js]].

```tsx title="tree-shakable way" {10,12,22,25}
import {
CategoryScale,
LinearScale,
PointElement,
LineElement,
Title,
Legend,
Tooltip,
Chart as ChartJS,
LineController,
} from "chart.js";
import { Chart } from "react-chartjs-2";

ChartJS.register(
CategoryScale,
LinearScale,
PointElement,
LineElement,
Title,
Legend,
Tooltip,
LineController
);

<Chart type="line" data={data} options={options} />
```

When you use `Line` or any other chart component instead of `Chart`, you can simply omit the import of its controller. For example, there's no need to register `LineController` when you use `Line` instead of `Chart`.

```tsx title="typed chart components" {11,23}
import {
CategoryScale,
LinearScale,
PointElement,
LineElement,
Title,
Legend,
Tooltip,
Chart as ChartJS,
} from "chart.js";
import { Line } from "react-chartjs-2";

ChartJS.register(
CategoryScale,
LinearScale,
PointElement,
LineElement,
Title,
Legend,
Tooltip,
);

<Line data={data} options={options} />
```

> [!info] References
> - [Migration to v4 | react-chartjs-2](https://react-chartjs-2.js.org/docs/migration-to-v4#tree-shaking)

This file was deleted.

This file was deleted.

4 changes: 2 additions & 2 deletions content/chart-js/react-chartjs-2/react-chartjs-2.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ const App = () => (

You can achieve the following result. However, there are some issues and improvements to consider:

1. [[Tree-shaking with react-chartjs-2|Implementing tree-shaking with react-chartjs-2.]]
2. [[datasetIdKey in react-chartjs-2|Being cautious about the keys used in datasets.]]
1. [[Tree-shaking in react-chartjs-2|Implementing tree-shaking with react-chartjs-2.]]
2. [[Datasets in react-chartjs-2|Being cautious about the keys used in datasets.]]

![[react-chart-js-2.png]]

Expand Down
5 changes: 4 additions & 1 deletion quartz.layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ export const defaultContentPageLayout: PageLayout = {
Component.DesktopOnly(Component.Explorer()),
],
right: [
Component.Graph(),
Component.Graph({
globalGraph: { showTags: false },
localGraph: { showTags: false },
}),
Component.DesktopOnly(Component.TableOfContents()),
Component.Backlinks(),
],
Expand Down

0 comments on commit e895abd

Please sign in to comment.