Skip to content

Commit

Permalink
Add normalizedData option
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Nov 19, 2019
1 parent e5b03a0 commit 49732b4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/axes/cartesian/time.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ The following options are provided by the time scale. You may also set options p
| `adapters.date` | `object` | `{}` | Options for adapter for external date library if that adapter needs or supports options
| `distribution` | `string` | `'linear'` | How data is plotted. [more...](#scale-distribution)
| `bounds` | `string` | `'data'` | Determines the scale bounds. [more...](#scale-bounds)
| `normalizedData` | `boolean` | `false` | If true, the data is assumed to be unique, sorted, and consistent (i.e. each dataset / labels contain same index values).
| `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` | `false` | If true and the unit is set to 'week', then the first day of the week will be Monday. Otherwise, it will be Sunday.
Expand Down
4 changes: 4 additions & 0 deletions docs/general/performance.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ There are many approaches to data decimation and selection of an algorithm will

Line charts are able to do [automatic data decimation during draw](#automatic-data-decimation-during-draw), when certain conditions are met. You should still consider decimating data yourself before passing it in for maximum performance since the automatic decimation occurs late in the chart life cycle.

## Time Scale

Provide data that is unique, sorted, and consistent (i.e. each dataset / labels contain same index values) and set `normalizedData: true`

## Line Charts

### Disable Bezier Curves
Expand Down
20 changes: 17 additions & 3 deletions src/scales/scale.time.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ function ticksFromTimestamps(scale, values, majorUnit) {
}

function getDataTimestamps(scale) {
var options = scale.options;
var timestamps = scale._cache.data || [];
var i, ilen, metas;

Expand All @@ -367,6 +368,10 @@ function getDataTimestamps(scale) {
}

metas = scale._getMatchingVisibleMetas();
if (options.normalizedData && metas.length) {
return (scale._cache.data = metas[0].controller._getAllParsedValues(scale));
}

for (i = 0, ilen = metas.length; i < ilen; ++i) {
timestamps = timestamps.concat(metas[i].controller._getAllParsedValues(scale));
}
Expand All @@ -377,6 +382,7 @@ function getDataTimestamps(scale) {
}

function getLabelTimestamps(scale) {
var options = scale.options;
var timestamps = scale._cache.labels || [];
var i, ilen, labels;

Expand All @@ -390,10 +396,11 @@ function getLabelTimestamps(scale) {
}

// We could assume labels are in order and unique - but let's not
return (scale._cache.labels = arrayUnique(timestamps.sort(sorter)));
return (scale._cache.labels = options.normalizedData ? timestamps : arrayUnique(timestamps.sort(sorter)));
}

function getAllTimestamps(scale) {
var options = scale.options;
var timestamps = scale._cache.all || [];
var label, data;

Expand All @@ -403,12 +410,14 @@ function getAllTimestamps(scale) {

data = getDataTimestamps(scale);
label = getLabelTimestamps(scale);
if (data.length && label.length) {
if (label.length && (!data.length || options.normalizedData)) {
timestamps = label;
} else if (data.length && label.length) {
// If combining labels and data (data might not contain all labels),
// we need to recheck uniqueness and sort
timestamps = arrayUnique(data.concat(label).sort(sorter));
} else {
timestamps = data.length ? data : label;
timestamps = data;
}
timestamps = scale._cache.all = timestamps;

Expand Down Expand Up @@ -498,6 +507,11 @@ const defaultConfig = {
*/
bounds: 'data',

/**
* If true, the data is assumed to be unique, sorted, and consistent (i.e. each dataset / labels contain same index values)
*/
normalizedData: false,

adapters: {},
time: {
parser: false, // false == a pattern string from https://momentjs.com/docs/#/parsing/string-format/ or a custom callback that converts its argument to a moment
Expand Down

0 comments on commit 49732b4

Please sign in to comment.