]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Destroy example charts on unmount (#8004)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Wed, 4 Nov 2020 19:58:53 +0000 (21:58 +0200)
committerGitHub <noreply@github.com>
Wed, 4 Nov 2020 19:58:53 +0000 (14:58 -0500)
* Destroy example charts on unmount
* Make scatter live
* Use spaces

13 files changed:
.editorconfig
docs/docs/axes/cartesian/index.mdx
docs/docs/charts/bar.mdx
docs/docs/charts/bubble.mdx
docs/docs/charts/doughnut.mdx
docs/docs/charts/line.mdx
docs/docs/charts/mixed.mdx
docs/docs/charts/polar.mdx
docs/docs/charts/radar.mdx
docs/docs/charts/scatter.md [deleted file]
docs/docs/charts/scatter.mdx [new file with mode: 0644]
docs/docs/configuration/animations.mdx
docs/docs/getting-started/index.mdx

index efa08f8015b3129cfd5d90547aa5a98ac2a3d329..a992ef3911cc045d652dcc490675b38eb2defb1a 100644 (file)
@@ -16,3 +16,7 @@ indent_size = 2
 [*.json]
 indent_style = space
 indent_size = 2
+
+[*.mdx]
+indent_style = space
+indent_size = 2
index b1d8d68f77bb24992ef21aa14d65a84506a36602..06b7245b8dcc6bed0876f1a3bf803e9fb8f1b5fc 100644 (file)
@@ -111,7 +111,8 @@ function exampleAlignment() {
         }
       }
     };
-    new Chart(document.getElementById('chartjs-1').getContext('2d'), cfg);
+    const chart = new Chart(document.getElementById('chartjs-1').getContext('2d'), cfg);
+    return () => chart.destroy();
   });
   return <div className="chartjs-wrapper"><canvas id="chartjs-1" className="chartjs"></canvas></div>;
 }
index 738931ab37695eba4bf62bd185ab941f36fbfd7e..77b6d87d9d08c4477aa47b04470320bbcade1285 100644 (file)
@@ -53,7 +53,8 @@ export const ExampleChart0 = () => {
         }
       }
     };
-    new Chart(document.getElementById('chartjs-0').getContext('2d'), cfg);
+    const chart = new Chart(document.getElementById('chartjs-0').getContext('2d'), cfg);
+    return () => chart.destroy();
   });
   return <div className="chartjs-wrapper"><canvas id="chartjs-0" className="chartjs"></canvas></div>;
 }
@@ -335,7 +336,8 @@ export const ExampleChart1 = () => {
         }
       }
     };
-    new Chart(document.getElementById('chartjs-1').getContext('2d'), cfg);
+    const chart = new Chart(document.getElementById('chartjs-1').getContext('2d'), cfg);
+    return () => chart.destroy();
   });
   return <div className="chartjs-wrapper"><canvas id="chartjs-1" className="chartjs"></canvas></div>;
 }
index 079172ad27340c6485e3849b02d9d5daebcc6522..7e8953fcab26e4a9613d5ef830e03cd6c31c6a20 100644 (file)
@@ -26,7 +26,8 @@ export const ExampleChart = () => {
         }]
       }
     };
-    new Chart(document.getElementById('chartjs-0').getContext('2d'), cfg);
+    const chart = new Chart(document.getElementById('chartjs-0').getContext('2d'), cfg);
+    return () => chart.destroy();
   });
   return <div className="chartjs-wrapper"><canvas id="chartjs-0" className="chartjs"></canvas></div>;
 }
index 920dc8a9593ba999aad1bc1241fea71b6bb26092..6248a47e037fe5b2f3c7ade19fb1f358d57662ff 100644 (file)
@@ -47,7 +47,8 @@ function example() {
         }]
       }
     };
-    new Chart(canvas.current.getContext('2d'), cfg);
+    const chart = new Chart(canvas.current.getContext('2d'), cfg);
+    return () => chart.destroy();
   });
   return <div className="chartjs-wrapper"><canvas ref={canvas} className="chartjs"></canvas></div>;
 }
@@ -81,7 +82,8 @@ function example() {
         }]
       }
     };
-    new Chart(canvas.current.getContext('2d'), cfg);
+    const chart = new Chart(canvas.current.getContext('2d'), cfg);
+    return () => chart.destroy();
   });
   return <div className="chartjs-wrapper"><canvas ref={canvas} className="chartjs"></canvas></div>;
 }
index 8ee843a8ab27cd1ce63cddb32056f49c5b525042..bd97cffdd3ac528952bd5b5bf1946433eef8c019 100644 (file)
@@ -31,7 +31,8 @@ export const ExampleChart = () => {
       options: {
       }
     };
-    new Chart(document.getElementById('chartjs-0').getContext('2d'), cfg);
+    const chart = new Chart(document.getElementById('chartjs-0').getContext('2d'), cfg);
+    return () => chart.destroy();
   });
   return <div className="chartjs-wrapper"><canvas id="chartjs-0" className="chartjs"></canvas></div>;
 }
index c7913bede1cc6f898d8292bbdcf46ab6ce44329b..fe58df44c6205344309ad7b3134b46032e51bf8c 100644 (file)
@@ -60,7 +60,8 @@ export const ExampleChart = () => {
         }
       }
     };
-    new Chart(document.getElementById('chartjs-0').getContext('2d'), cfg);
+    const chart = new Chart(document.getElementById('chartjs-0').getContext('2d'), cfg);
+    return () => chart.destroy();
   });
   return <div className="chartjs-wrapper"><canvas id="chartjs-0" className="chartjs"></canvas></div>;
 }
index 680a620a3aee52bad1c45cbb0de5cb97bd598a1b..2589c6b7b733814e2f145b1b9dd6b05052a5b0f9 100644 (file)
@@ -33,7 +33,8 @@ export const ExampleChart = () => {
         }]
       }
     };
-    new Chart(document.getElementById('chartjs-0').getContext('2d'), cfg);
+    const chart = new Chart(document.getElementById('chartjs-0').getContext('2d'), cfg);
+    return () => chart.destroy();
   });
   return <div className="chartjs-wrapper"><canvas id="chartjs-0" className="chartjs"></canvas></div>;
 }
index 2c305a2fe8edcafad55255e8327cca65179c4662..6fec0ff041f9e8e30b8a3b737119a725b1ae07f3 100644 (file)
@@ -55,7 +55,8 @@ export const ExampleChart = () => {
         }
       }
     };
-    new Chart(document.getElementById('chartjs-0').getContext('2d'), cfg);
+    const chart = new Chart(document.getElementById('chartjs-0').getContext('2d'), cfg);
+    return () => chart.destroy();
   });
   return <div className="chartjs-wrapper"><canvas id="chartjs-0" className="chartjs"></canvas></div>;
 }
diff --git a/docs/docs/charts/scatter.md b/docs/docs/charts/scatter.md
deleted file mode 100644 (file)
index 3eb1e73..0000000
+++ /dev/null
@@ -1,56 +0,0 @@
----
-title: Scatter Chart
----
-
-Scatter charts are based on basic line charts with the x axis changed to a linear axis. To use a scatter chart, data must be passed as objects containing X and Y properties. The example below creates a scatter chart with 3 points.
-
-```javascript
-var scatterChart = new Chart(ctx, {
-    type: 'scatter',
-    data: {
-        datasets: [{
-            label: 'Scatter Dataset',
-            data: [{
-                x: -10,
-                y: 0
-            }, {
-                x: 0,
-                y: 10
-            }, {
-                x: 10,
-                y: 5
-            }]
-        }]
-    },
-    options: {
-        scales: {
-            x: {
-                type: 'linear',
-                position: 'bottom'
-            }
-        }
-    }
-});
-```
-
-## Dataset Properties
-
-The scatter chart supports all of the same properties as the [line chart](./charts/line.mdx#dataset-properties).
-
-## Data Structure
-
-Unlike the line chart where data can be supplied in two different formats, the scatter chart only accepts data in a point format.
-
-```javascript
-data: [{
-        x: 10,
-        y: 20
-    }, {
-        x: 15,
-        y: 10
-    }]
-```
-
-## Internal data format
-
-`{x, y}`
diff --git a/docs/docs/charts/scatter.mdx b/docs/docs/charts/scatter.mdx
new file mode 100644 (file)
index 0000000..7d091f7
--- /dev/null
@@ -0,0 +1,66 @@
+---
+title: Scatter Chart
+---
+
+Scatter charts are based on basic line charts with the x axis changed to a linear axis. To use a scatter chart, data must be passed as objects containing X and Y properties. The example below creates a scatter chart with 3 points.
+
+import { useEffect, useRef } from 'react';
+
+```jsx live
+function example() {
+  const canvas = useRef(null);
+  useEffect(() => {
+    const cfg = {
+        type: 'scatter',
+        data: {
+            datasets: [{
+                label: 'Scatter Dataset',
+                data: [{
+                    x: -10,
+                    y: 0
+                }, {
+                    x: 0,
+                    y: 10
+                }, {
+                    x: 10,
+                    y: 5
+                }]
+            }]
+        },
+        options: {
+            scales: {
+                x: {
+                    type: 'linear',
+                    position: 'bottom'
+                }
+            }
+        }
+    };
+    const chart = new Chart(canvas.current.getContext('2d'), cfg);
+    return () => chart.destroy();
+  });
+  return <div className="chartjs-wrapper"><canvas ref={canvas} className="chartjs"></canvas></div>;
+}
+```
+
+## Dataset Properties
+
+The scatter chart supports all of the same properties as the [line chart](./charts/line.mdx#dataset-properties).
+
+## Data Structure
+
+Unlike the line chart where data can be supplied in two different formats, the scatter chart only accepts data in a point format.
+
+```javascript
+data: [{
+        x: 10,
+        y: 20
+    }, {
+        x: 15,
+        y: 10
+    }]
+```
+
+## Internal data format
+
+`{x, y}`
index 5088f0f157497dc7050900d96beb45a7a0cd48f4..8ef12785a46d903002612752ab458f52bbd8330b 100644 (file)
@@ -50,7 +50,8 @@ function example() {
           }
       }
     };
-    new Chart(ctx, cfg);
+    const chart = new Chart(ctx, cfg);
+    return () => chart.destroy();
   });
   return <div className="chartjs-wrapper"><canvas id="chartjs-a0" className="chartjs"></canvas></div>;
 }
@@ -96,7 +97,8 @@ function example() {
           }
       }
     };
-    new Chart(ctx, cfg);
+    const chart = new Chart(ctx, cfg);
+    return () => chart.destroy();
   });
   return <div className="chartjs-wrapper"><canvas id="chartjs-a1" className="chartjs"></canvas></div>;
 }
@@ -117,7 +119,7 @@ The default configuration is defined here: <a href="https://github.com/chartjs/C
 | `debug` | `boolean` | `undefined` | Running animation count + FPS display in upper left corner of the chart.
 | `delay` | `number` | `undefined` | Delay before starting the animations.
 | `loop` | `boolean` | `undefined` | If set to `true`, the animations loop endlessly.
-| [[mode]](#animation-mode-configuration) | `object` | [defaults...](#default-modes) | Option overrides for update mode. Core modes: `'active'`, `'hide'`, `'reset'`, `'resize'`, `'show'`. See **Hide and show [mode]** example above. 
+| [[mode]](#animation-mode-configuration) | `object` | [defaults...](#default-modes) | Option overrides for update mode. Core modes: `'active'`, `'hide'`, `'reset'`, `'resize'`, `'show'`. See **Hide and show [mode]** example above.
 | [[property]](#animation-property-configuration) | `object` | `undefined` | Option overrides for a single element `[property]`. These have precedence over `[collection]`. See **Looping tension [property]** example above.
 | [[collection]](#animation-properties-collection-configuration) | `object` | [defaults...](#default-collections) | Option overrides for multiple properties, identified by `properties` array.
 
@@ -125,8 +127,8 @@ These defaults can be overridden in `options.animation` or `dataset.animation` a
 
 ## Animation mode configuration
 
-Mode option configures how an update mode animates the chart. 
-The cores modes are `'active'`, `'hide'`, `'reset'`, `'resize'`, `'show'`. 
+Mode option configures how an update mode animates the chart.
+The cores modes are `'active'`, `'hide'`, `'reset'`, `'resize'`, `'show'`.
 A custom mode can be used by passing a custom `mode` to [update](../developers/api.md#updatemode).
 A mode option is defined by the same options of the main [animation configuration](#animation-configuration).
 
@@ -181,13 +183,13 @@ These default collections are overridden by most dataset controllers.
 
 ## Disabling animation
 
-To disable an animation configuration, the animation node must be set to `false`, with the exception for animation modes which can be disable setting the `duration` to `0`. 
+To disable an animation configuration, the animation node must be set to `false`, with the exception for animation modes which can be disable setting the `duration` to `0`.
 
 ```javascript
 chart.options.animation = false; // disables the whole animation
 chart.options.animation.active.duration = 0; // disables the animation for 'active' mode
 chart.options.animation.colors = false; // disables animation defined by the collection of 'colors' properties
-chart.options.animation.x = false; // disables animation defined by the 'x' property 
+chart.options.animation.x = false; // disables animation defined by the 'x' property
 ```
 
 ## Easing
index d9830a5486c64e823c5bff9db9cdf2d2b7ca98b2..c487b3dd071bcc2eb2a6661ac82f2f8cfe953a1d 100644 (file)
@@ -25,25 +25,26 @@ import { useEffect } from 'react';
 ```jsx live
 function example() {
   useEffect(() => {
-       var ctx = document.getElementById('myChart').getContext('2d');
-       var chart = new Chart(ctx, {
-               // The type of chart we want to create
-               type: 'line',
-
-               // The data for our dataset
-               data: {
-                       labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
-                       datasets: [{
-                               label: 'My First dataset',
-                               backgroundColor: 'rgb(255, 99, 132)',
-                               borderColor: 'rgb(255, 99, 132)',
-                               data: [0, 10, 5, 2, 20, 30, 45]
-                       }]
-               },
-
-               // Configuration options go here
-               options: {}
-       });
+    var ctx = document.getElementById('myChart').getContext('2d');
+    var chart = new Chart(ctx, {
+      // The type of chart we want to create
+      type: 'line',
+
+      // The data for our dataset
+      data: {
+        labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
+        datasets: [{
+          label: 'My First dataset',
+          backgroundColor: 'rgb(255, 99, 132)',
+          borderColor: 'rgb(255, 99, 132)',
+          data: [0, 10, 5, 2, 20, 30, 45]
+        }]
+      },
+
+      // Configuration options go here
+      options: {}
+    });
+    return () => chart.destroy();
   });
   return <div className="chartjs-wrapper"><canvas id="myChart" className="chartjs"></canvas></div>;
 }