Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding threshold option to decimation plugin #9327

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -1978,6 +1978,7 @@ export const enum DecimationAlgorithm {
}
interface BaseDecimationOptions {
enabled: boolean;
threshold?: number;
}

interface LttbDecimationOptions extends BaseDecimationOptions {
Expand Down