From ae7e0cfa5c148df81d202a951628b0e1fa562cab Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Mon, 28 Oct 2019 14:43:33 -0700 Subject: [PATCH] Combine performance docs --- docs/SUMMARY.md | 1 + docs/charts/line.md | 72 ------------------------------ docs/general/performance.md | 87 ++++++++++++++++++++++++++++++++++--- 3 files changed, 82 insertions(+), 78 deletions(-) diff --git a/docs/SUMMARY.md b/docs/SUMMARY.md index c8fbe87903e..72760b12127 100644 --- a/docs/SUMMARY.md +++ b/docs/SUMMARY.md @@ -16,6 +16,7 @@ * [Options](general/options.md) * [Colors](general/colors.md) * [Fonts](general/fonts.md) + * [Performance](general/performance.md) * [Configuration](configuration/README.md) * [Animations](configuration/animations.md) * [Layout](configuration/layout.md) diff --git a/docs/charts/line.md b/docs/charts/line.md index cca8bb65543..beec61ed50f 100644 --- a/docs/charts/line.md +++ b/docs/charts/line.md @@ -216,75 +216,3 @@ var stackedLine = new Chart(ctx, { } }); ``` - -## High Performance Line Charts - -When charting a lot of data, the chart render time may start to get quite large. In that case, the following strategies can be used to improve performance. - -### Data Decimation - -Decimating your data will achieve the best results. When there is a lot of data to display on the graph, it doesn't make sense to show tens of thousands of data points on a graph that is only a few hundred pixels wide. - -There are many approaches to data decimation and selection of an algorithm will depend on your data and the results you want to achieve. For instance, [min/max](https://digital.ni.com/public.nsf/allkb/F694FFEEA0ACF282862576020075F784) decimation will preserve peaks in your data but could require up to 4 points for each pixel. This type of decimation would work well for a very noisy signal where you need to see data peaks. - -### Disable Bezier Curves - -If you are drawing lines on your chart, disabling bezier curves will improve render times since drawing a straight line is more performant than a bezier curve. - -To disable bezier curves for an entire chart: - -```javascript -new Chart(ctx, { - type: 'line', - data: data, - options: { - elements: { - line: { - tension: 0 // disables bezier curves - } - } - } -}); -``` - -### Disable Line Drawing - -If you have a lot of data points, it can be more performant to disable rendering of the line for a dataset and only draw points. Doing this means that there is less to draw on the canvas which will improve render performance. - -To disable lines: - -```javascript -new Chart(ctx, { - type: 'line', - data: { - datasets: [{ - showLine: false // disable for a single dataset - }] - }, - options: { - showLines: false // disable for all datasets - } -}); -``` - -### Disable Animations - -If your charts have long render times, it is a good idea to disable animations. Doing so will mean that the chart needs to only be rendered once during an update instead of multiple times. This will have the effect of reducing CPU usage and improving general page performance. - -To disable animations - -```javascript -new Chart(ctx, { - type: 'line', - data: data, - options: { - animation: { - duration: 0 // general animation time - }, - hover: { - animationDuration: 0 // duration of animations when hovering an item - }, - responsiveAnimationDuration: 0 // animation duration after a resize - } -}); -``` diff --git a/docs/general/performance.md b/docs/general/performance.md index 96e0b5e1ba8..f6e17d0086f 100644 --- a/docs/general/performance.md +++ b/docs/general/performance.md @@ -1,9 +1,84 @@ # Performance -Chart.js charts are rendered on `canvas` elements, which makes rendering quite fast. For large datasets or performance sensitive applications, you may wish to consider the tips below: +Chart.js charts are rendered on `canvas` elements, which makes rendering quite fast. For large datasets or performance sensitive applications, you may wish to consider the tips below. -* Set `animation: { duration: 0 }` to disable [animations](../configuration/animations.md). -* [Specify a rotation value](https://www.chartjs.org/docs/latest/axes/cartesian/#tick-configuration) by setting `minRotation` and `maxRotation` to the same value -* For large datasets: - * You may wish to sample your data before providing it to Chart.js. E.g. if you have a data point for each day, you may find it more performant to pass in a data point for each week instead - * Set the [`ticks.sampleSize`](../axes/cartesian/README.md#tick-configuration) option in order to render axes more quickly +## Tick Calculation + +### Rotation + +[Specify a rotation value](https://www.chartjs.org/docs/latest/axes/cartesian/#tick-configuration) by setting `minRotation` and `maxRotation` to the same value, which avoids the chart from having to automatically determine a value to use. + +### Sampling + +Set the [`ticks.sampleSize`](../axes/cartesian/README.md#tick-configuration) option. This will determine how large your labels are by looking at only a subset of them in order to render axes more quickly. This works best if there is not a large variance in the size of your labels. + +## Disable Animations + +If your charts have long render times, it is a good idea to disable animations. Doing so will mean that the chart needs to only be rendered once during an update instead of multiple times. This will have the effect of reducing CPU usage and improving general page performance. + +To disable animations + +```javascript +new Chart(ctx, { + type: 'line', + data: data, + options: { + animation: { + duration: 0 // general animation time + }, + hover: { + animationDuration: 0 // duration of animations when hovering an item + }, + responsiveAnimationDuration: 0 // animation duration after a resize + } +}); +``` + +## Data Decimation + +Decimating your data will achieve the best results. When there is a lot of data to display on the graph, it doesn't make sense to show tens of thousands of data points on a graph that is only a few hundred pixels wide. + +There are many approaches to data decimation and selection of an algorithm will depend on your data and the results you want to achieve. For instance, [min/max](https://digital.ni.com/public.nsf/allkb/F694FFEEA0ACF282862576020075F784) decimation will preserve peaks in your data but could require up to 4 points for each pixel. This type of decimation would work well for a very noisy signal where you need to see data peaks. + + +## Line Charts + +### Disable Bezier Curves + +If you are drawing lines on your chart, disabling bezier curves will improve render times since drawing a straight line is more performant than a bezier curve. + +To disable bezier curves for an entire chart: + +```javascript +new Chart(ctx, { + type: 'line', + data: data, + options: { + elements: { + line: { + tension: 0 // disables bezier curves + } + } + } +}); +``` + +### Disable Line Drawing + +If you have a lot of data points, it can be more performant to disable rendering of the line for a dataset and only draw points. Doing this means that there is less to draw on the canvas which will improve render performance. + +To disable lines: + +```javascript +new Chart(ctx, { + type: 'line', + data: { + datasets: [{ + showLine: false // disable for a single dataset + }] + }, + options: { + showLines: false // disable for all datasets + } +}); +```