Skip to content

Commit

Permalink
Allow time scale to offset using skipped ticks (#10278)
Browse files Browse the repository at this point in the history
  • Loading branch information
etimberg committed Apr 3, 2022
1 parent 43889f2 commit a39f694
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/axes/cartesian/time.md
Expand Up @@ -30,6 +30,7 @@ Namespace: `options.scales[scaleId]`
| `suggestedMax` | `number`\|`string` | | The maximum item to display if there is no datapoint behind it. [more...](../index.md#axis-range-settings)
| `adapters.date` | `object` | `{}` | Options for adapter for external date library if that adapter needs or supports options
| `bounds` | `string` | `'data'` | Determines the scale bounds. [more...](./index.md#scale-bounds)
| `offsetAfterAutoskip` | `boolean` | `false` | If true, bar chart offsets are computed with auto skipped ticks.
| `ticks.source` | `string` | `'auto'` | How ticks are generated. [more...](#ticks-source)
| `time.displayFormats` | `object` | | Sets how different time units are displayed. [more...](#display-formats)
| `time.isoWeekday` | `boolean`\|`number` | `false` | If `boolean` and true and the unit is set to 'week', then the first day of the week will be Monday. Otherwise, it will be Sunday. If `number`, the index of the first day of the week (0 - Sunday, 6 - Saturday)
Expand Down
2 changes: 2 additions & 0 deletions src/core/core.scale.js
Expand Up @@ -453,6 +453,7 @@ export default class Scale extends Element {
if (tickOpts.display && (tickOpts.autoSkip || tickOpts.source === 'auto')) {
this.ticks = autoSkip(this, this.ticks);
this._labelSizes = null;
this.afterAutoSkip();
}

if (samplingEnabled) {
Expand Down Expand Up @@ -619,6 +620,7 @@ export default class Scale extends Element {
afterCalculateLabelRotation() {
call(this.options.afterCalculateLabelRotation, [this]);
}
afterAutoSkip() {}

//

Expand Down
8 changes: 8 additions & 0 deletions src/scales/scale.time.js
Expand Up @@ -351,6 +351,14 @@ export default class TimeScale extends Scale {
return ticksFromTimestamps(this, ticks, this._majorUnit);
}

afterAutoSkip() {
// Offsets for bar charts need to be handled with the auto skipped
// ticks. Once ticks have been skipped, we re-compute the offsets.
if (this.options.offsetAfterAutoskip) {
this.initOffsets(this.ticks.map(tick => +tick.value));
}
}

/**
* Returns the start and end offsets from edges in the form of {start, end}
* where each value is a relative width to the scale and ranges between 0 and 1.
Expand Down
52 changes: 52 additions & 0 deletions test/fixtures/scale.time/offset-auto-skip-ticks.js
@@ -0,0 +1,52 @@
const data = {
labels: [],
datasets: [{
label: 'Dataset',
borderColor: '#2f54eb',
data: [{
y: 3,
x: 1646345700000
}, {
y: 7,
x: 1646346600000
}, {
y: 9,
x: 1646347500000
}, {
y: 5,
x: 1646348400000
}, {
y: 5,
x: 1646349300000
}],
}]
};

module.exports = {
description: 'https://github.com/chartjs/Chart.js/issues/10215',
config: {
type: 'bar',
data,
options: {
maintainAspectRatio: false,
scales: {
x: {
type: 'time',
offset: true,
offsetAfterAutoskip: true,
axis: 'x',
grid: {
offset: true
},
},
y: {
display: false,
}
}
}
},
options: {
spriteText: true,
canvas: {width: 600, height: 400}
}
};
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions types/index.esm.d.ts
Expand Up @@ -3202,6 +3202,13 @@ export type TimeScaleOptions = Omit<CartesianScaleOptions, 'min' | 'max'> & {
*/
bounds: 'ticks' | 'data';

/**
* If true, bar chart offsets are computed with skipped tick sizes
* @since 3.8.0
* @default false
*/
offsetAfterAutoskip: boolean;

/**
* options for creating a new adapter instance
*/
Expand Down

0 comments on commit a39f694

Please sign in to comment.