Skip to content

Commit

Permalink
Adding threshold option to decimation plugin (#9327)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nico-DF committed Jul 4, 2021
1 parent dac7101 commit b4dee55
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/configuration/decimation.md
Expand Up @@ -11,6 +11,7 @@ Namespace: `options.plugins.decimation`, the global options for the plugin are d
| `enabled` | `boolean` | `false` | Is decimation enabled?
| `algorithm` | `string` | `'min-max'` | Decimation algorithm to use. See the [more...](#decimation-algorithms)
| `samples` | `number` | | If the `'lttb'` algorithm is used, this is the number of samples in the output dataset. Defaults to the canvas width to pick 1 sample per pixel.
| `threshold` | `number` | | If the number of samples in the current axis range is above this value, the decimation will be triggered. Defaults to 4 times the canvas width.<br />The number of point after decimation can be higher than the `threshold` value.

## Decimation Algorithms

Expand Down
3 changes: 2 additions & 1 deletion src/plugins/plugin.decimation.js
Expand Up @@ -234,7 +234,8 @@ export default {
}

let {start, count} = getStartAndCountOfVisiblePointsSimplified(meta, data);
if (count <= 4 * availableWidth) {
const threshold = options.threshold || 4 * availableWidth;
if (count <= threshold) {
// No decimation is required until we are above this threshold
cleanDecimatedDataset(dataset);
return;
Expand Down
43 changes: 41 additions & 2 deletions test/specs/plugin.decimation.tests.js
Expand Up @@ -15,7 +15,7 @@ describe('Plugin.decimation', function() {
{x: 8, y: 8},
{x: 9, y: 9}];

it('should draw all element if sample is greater than data', function() {
it('should draw all element if sample is greater than data based on canvas width', function() {
var chart = window.acquireChart({
type: 'line',
data: {
Expand Down Expand Up @@ -54,7 +54,7 @@ describe('Plugin.decimation', function() {
expect(chart.data.datasets[0].data.length).toBe(10);
});

it('should draw the specified number of elements', function() {
it('should draw the specified number of elements based on canvas width', function() {
var chart = window.acquireChart({
type: 'line',
data: {
Expand Down Expand Up @@ -94,6 +94,45 @@ describe('Plugin.decimation', function() {
expect(chart.data.datasets[0].data.length).toBe(7);
});

it('should draw the specified number of elements based on threshold', function() {
var chart = window.acquireChart({
type: 'line',
data: {
datasets: [{
data: originalData,
label: 'dataset1'
}]
},
options: {
parsing: false,
scales: {
x: {
type: 'linear'
}
},
plugins: {
decimation: {
enabled: true,
algorithm: 'lttb',
samples: 5,
threshold: 7
}
}
}
}, {
canvas: {
height: 100,
width: 100
},
wrapper: {
height: 100,
width: 100
}
});

expect(chart.data.datasets[0].data.length).toBe(5);
});

it('should draw all element only in range', function() {
var chart = window.acquireChart({
type: 'line',
Expand Down
1 change: 1 addition & 0 deletions types/index.esm.d.ts
Expand Up @@ -1991,6 +1991,7 @@ export const enum DecimationAlgorithm {
}
interface BaseDecimationOptions {
enabled: boolean;
threshold?: number;
}

interface LttbDecimationOptions extends BaseDecimationOptions {
Expand Down

0 comments on commit b4dee55

Please sign in to comment.