Skip to content

Commit

Permalink
Use index instead of base for consistency, chart level option
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle committed Feb 23, 2020
1 parent 693f81e commit 5c4be2b
Show file tree
Hide file tree
Showing 21 changed files with 55 additions and 82 deletions.
4 changes: 2 additions & 2 deletions docs/charts/bar.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ the color of the bars is generally set this way.
| Name | Type | [Scriptable](../general/options.md#scriptable-options) | [Indexable](../general/options.md#indexable-options) | Default
| ---- | ---- | :----: | :----: | ----
| [`backgroundColor`](#styling) | [`Color`](../general/colors.md) | Yes | Yes | `'rgba(0, 0, 0, 0.1)'`
| [`baseAxis`](#general) | `string` | `'x'` | The base axis for the dataset. Use `'y'` for horizontal bar.
| [`borderColor`](#styling) | [`Color`](../general/colors.md) | Yes | Yes | `'rgba(0, 0, 0, 0.1)'`
| [`borderSkipped`](#borderskipped) | `string` | Yes | Yes | `'start'`
| [`borderWidth`](#borderwidth) | <code>number&#124;object</code> | Yes | Yes | `0`
Expand All @@ -78,6 +77,7 @@ the color of the bars is generally set this way.
| [`hoverBackgroundColor`](#interactions) | [`Color`](../general/colors.md) | - | Yes | `undefined`
| [`hoverBorderColor`](#interactions) | [`Color`](../general/colors.md) | - | Yes | `undefined`
| [`hoverBorderWidth`](#interactions) | `number` | - | Yes | `1`
| [`indexAxis`](#general) | `string` | `'x'` | The base axis for the dataset. Use `'y'` for horizontal bar.
| [`label`](#general) | `string` | - | - | `''`
| [`order`](#general) | `number` | - | - | `0`
| [`xAxisID`](#general) | `string` | - | - | first x axis
Expand All @@ -87,8 +87,8 @@ the color of the bars is generally set this way.

| Name | Description
| ---- | ----
| `baseAxis` | The base axis of the dataset. `'x'` for vertical bars and `'y'` for horizontal bars.
| `clip` | How to clip relative to chartArea. Positive value allows overflow, negative value clips that many pixels inside chartArea. `0` = clip at chartArea. Clipping can also be configured per side: `clip: {left: 5, top: false, right: -2, bottom: 0}`
| `indexAxis` | The base axis of the dataset. `'x'` for vertical bars and `'y'` for horizontal bars.
| `label` | The label for the dataset which appears in the legend and tooltips.
| `order` | The drawing order of dataset. Also affects order for stacking, tooltip and legend.
| `xAxisID` | The ID of the x axis to plot this dataset on.
Expand Down
2 changes: 1 addition & 1 deletion docs/getting-started/v3-migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Chart.js 3.0 introduces a number of breaking changes. Chart.js 2.0 was released

## Chart types

* `horizontalBar` chart type was removed. Horizontal bar charts can be configured using the new [`baseAxis`](../charts/bar.md#general) option
* `horizontalBar` chart type was removed. Horizontal bar charts can be configured using the new [`indexAxis`](../charts/bar.md#general) option

### Ticks

Expand Down
6 changes: 1 addition & 5 deletions samples/charts/bar/horizontal.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,7 @@
type: 'bar',
data: horizontalBarChartData,
options: {
bar: {
datasets: {
baseAxis: 'y'
}
},
indexAxis: 'y',
// Elements options apply to all of the options unless overridden in a dataset
// In this case, we are setting the border of each horizontal bar to be 2px wide
elements: {
Expand Down
5 changes: 2 additions & 3 deletions src/controllers/controller.bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ defaults.set('bar', {
},

datasets: {
baseAxis: 'x',
categoryPercentage: 0.8,
barPercentage: 0.9,
animation: {
Expand All @@ -23,14 +22,14 @@ defaults.set('bar', {
},

scales: {
i: {
_index_: {
type: 'category',
offset: true,
gridLines: {
offsetGridLines: true
}
},
v: {
_value_: {
type: 'linear',
beginAtZero: true,
}
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/controller.line.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ defaults.set('line', {
},

scales: {
x: {
_index_: {
type: 'category',
},
y: {
_value_: {
type: 'linear',
},
}
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/controller.polarArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defaults.set('polarArea', {
animateScale: true
},
datasets: {
baseAxis: 'r'
indexAxis: 'r'
},
scales: {
r: {
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/controller.radar.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defaults.set('radar', {
}
},
datasets: {
baseAxis: 'r'
indexAxis: 'r'
},
elements: {
line: {
Expand Down
34 changes: 20 additions & 14 deletions src/core/core.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,43 @@ import {version} from '../../package.json';

const valueOrDefault = helpers.valueOrDefault;

function getBaseAxis(type, options) {
function getIndexAxis(type, options) {
const typeDefaults = defaults[type] || {};
const typeDatasetDefaults = typeDefaults.datasets || {};
const datasetDefaults = typeDefaults.datasets || {};
const typeOptions = options[type] || {};
const datasetOptions = typeOptions.datasets || {};
return datasetOptions.baseAxis || typeDatasetDefaults.baseAxis || 'x';
return datasetOptions.indexAxis || options.indexAxis || datasetDefaults.indexAxis || 'x';
}

function getAxisFromDefaultScaleID(id, baseAxis) {
let ret = id;
if (id === 'i') {
ret = baseAxis;
} else if (id === 'v') {
ret = baseAxis === 'x' ? 'y' : 'x';
function getAxisFromDefaultScaleID(id, indexAxis) {
let axis = id;
if (id === '_index_') {
axis = indexAxis;
} else if (id === '_value_') {
axis = indexAxis === 'x' ? 'y' : 'x';
}
return ret;
return axis;
}

function getDefaultScaleIDFromAxis(axis, indexAxis) {
return axis === indexAxis ? '_index_' : '_value_';
}

function mergeScaleConfig(config, options) {
options = options || {};
const chartDefaults = defaults[config.type] || {scales: {}};
const configScales = options.scales || {};
const chartIndexAxis = getIndexAxis(config.type, options);
const firstIDs = {};
const scales = {};

// First figure out first scale id's per axis.
Object.keys(configScales).forEach(id => {
const scaleConf = configScales[id];
const axis = scaleConf.axis || id[0];
const defaultId = getDefaultScaleIDFromAxis(axis, chartIndexAxis);
firstIDs[axis] = firstIDs[axis] || id;
scales[id] = helpers.mergeIf({axis}, [scaleConf, chartDefaults.scales[axis]]);
scales[id] = helpers.mergeIf({axis}, [scaleConf, chartDefaults.scales[axis], chartDefaults.scales[defaultId]]);
});

// Backward compatibility
Expand All @@ -59,11 +65,11 @@ function mergeScaleConfig(config, options) {
// Then merge dataset defaults to scale configs
config.data.datasets.forEach(dataset => {
const type = dataset.type || config.type;
const baseAxis = dataset.baseAxis || getBaseAxis(type, options);
const indexAxis = dataset.indexAxis || getIndexAxis(type, options);
const datasetDefaults = defaults[type] || {};
const defaultScaleOptions = datasetDefaults.scales || {};
Object.keys(defaultScaleOptions).forEach(defaultID => {
const axis = getAxisFromDefaultScaleID(defaultID, baseAxis);
const axis = getAxisFromDefaultScaleID(defaultID, indexAxis);
const id = dataset[axis + 'AxisID'] || firstIDs[axis] || axis;
scales[id] = scales[id] || {};
helpers.mergeIf(scales[id], [{axis}, configScales[id], defaultScaleOptions[defaultID]]);
Expand Down Expand Up @@ -515,7 +521,7 @@ export default class Chart {
meta = me.getDatasetMeta(i);
}
meta.type = type;
meta.baseAxis = dataset.baseAxis || getBaseAxis(type, me.options);
meta.indexAxis = dataset.indexAxis || getIndexAxis(type, me.options);
meta.order = dataset.order || 0;
me._updateMetasetIndex(meta, i);
meta.label = '' + dataset.label;
Expand Down
6 changes: 3 additions & 3 deletions src/core/core.datasetController.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,9 @@ export default class DatasetController {
const xid = meta.xAxisID = dataset.xAxisID || getFirstScaleId(chart, 'x');
const yid = meta.yAxisID = dataset.yAxisID || getFirstScaleId(chart, 'y');
const rid = meta.rAxisID = dataset.rAxisID || getFirstScaleId(chart, 'r');
const baseAxis = meta.baseAxis;
const iid = meta.iAxisID = baseAxis === 'x' ? xid : baseAxis === 'r' ? rid : yid;
const vid = meta.vAxisID = baseAxis === 'x' ? yid : baseAxis === 'r' ? rid : xid;
const indexAxis = meta.indexAxis;
const iid = meta.iAxisID = indexAxis === 'x' ? xid : indexAxis === 'r' ? rid : yid;
const vid = meta.vAxisID = indexAxis === 'x' ? yid : indexAxis === 'r' ? rid : xid;
meta.xScale = me.getScaleForId(xid);
meta.yScale = me.getScaleForId(yid);
meta.rScale = me.getScaleForId(rid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ module.exports = {
options: {
legend: false,
title: false,
bar: {
datasets: {
baseAxis: 'y'
}
},
indexAxis: 'y',
scales: {
x: {display: false, min: 0},
y: {display: false, stacked: true}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"legend": false,
"bar": {
"datasets": {
"baseAxis": "y"
"indexAxis": "y"
}
},
"scales": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"legend": false,
"bar": {
"datasets": {
"baseAxis": "y"
"indexAxis": "y"
}
},
"scales": {
Expand Down
6 changes: 1 addition & 5 deletions test/fixtures/controller.bar/horizontal-borders.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ module.exports = {
options: {
legend: false,
title: false,
bar: {
datasets: {
baseAxis: 'y'
}
},
indexAxis: 'y',
elements: {
rectangle: {
backgroundColor: '#AAAAAA80',
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/core.scale/label-offset-vertical-axes.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"title": false,
"bar": {
"datasets": {
"baseAxis": "y"
"indexAxis": "y"
}
},
"scales": {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/core.scale/tick-drawing.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"title": false,
"bar": {
"datasets": {
"baseAxis": "y"
"indexAxis": "y"
}
},
"scales": {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/scale.category/ticks-from-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ module.exports = {
type: 'bar',
data: {
datasets: [{
baseAxis: 'y',
data: [10, 5, 0, 25, 78]
}],
labels: ['tick1', 'tick2', 'tick3', 'tick4', 'tick5']
},
options: {
indexAxis: 'y',
legend: false,
title: false,
elements: {
Expand Down
14 changes: 3 additions & 11 deletions test/specs/controller.bar.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1388,11 +1388,7 @@ describe('Chart.controllers.bar', function() {
type: 'bar',
data: this.data,
options: {
bar: {
datasets: {
baseAxis: 'y'
}
},
indexAxis: 'y',
scales: {
y: {
min: 'March',
Expand All @@ -1408,11 +1404,7 @@ describe('Chart.controllers.bar', function() {
type: 'bar',
data: this.data,
options: {
bar: {
datasets: {
baseAxis: 'y'
}
},
indexAxis: 'y',
scales: {
x: {
stacked: true
Expand Down Expand Up @@ -1527,7 +1519,7 @@ describe('Chart.controllers.bar', function() {
type: 'bar',
data: {
datasets: [{
baseAxis: 'y',
indexAxis: 'y',
minBarLength: minBarLength,
data: [0.05, -0.05, 10, 15, 20, 25, 30, 35]
}]
Expand Down
12 changes: 2 additions & 10 deletions test/specs/core.interaction.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,7 @@ describe('Core.Interaction', function() {
type: 'bar',
data: data,
options: {
bar: {
datasets: {
baseAxis: 'y'
}
}
indexAxis: 'y',
}
});

Expand Down Expand Up @@ -281,11 +277,7 @@ describe('Core.Interaction', function() {
type: 'bar',
data: data,
options: {
bar: {
datasets: {
baseAxis: 'y'
}
}
indexAxis: 'y',
}
});

Expand Down
2 changes: 1 addition & 1 deletion test/specs/scale.category.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,6 @@ describe('Category scale tests', function() {
type: 'bar',
data: {
datasets: [{
baseAxis: 'y',
data: [
{x: 10, y: 0},
{x: 5, y: 1},
Expand All @@ -481,6 +480,7 @@ describe('Category scale tests', function() {
labels: [0, 1, 2, 3]
},
options: {
indexAxis: 'y',
scales: {
x: {
type: 'linear',
Expand Down
Loading

0 comments on commit 5c4be2b

Please sign in to comment.