Skip to content

Commit

Permalink
Make Chart.defaults/Ticks/Interaction importable (chartjs#4512)
Browse files Browse the repository at this point in the history
Default options can now be accessed by importing `core/core.defaults`. The returned object acts as a singleton and is populated when importing classes that expose their own default values (meaning that importing only `code.defaults` results in an empty object). Also make `Chart.Ticks` and `Chart.Interaction` importable since existing defaults rely on these values.

Add the `defaults._set` method that make easier declaring new defaults by merging given values with existing ones for a specific scope (`global`, `scale`, `bar`, etc).
  • Loading branch information
simonbrunel authored and yofreke committed Dec 30, 2017
1 parent 56a63f4 commit b17c452
Show file tree
Hide file tree
Showing 32 changed files with 1,126 additions and 1,069 deletions.
6 changes: 4 additions & 2 deletions src/chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ Chart.helpers = require('./helpers/index');
// @todo dispatch these helpers into appropriated helpers/helpers.* file and write unit tests!
require('./core/core.helpers')(Chart);

Chart.defaults = require('./core/core.defaults');
Chart.Interaction = require('./core/core.interaction');
Chart.platform = require('./platforms/platform');
Chart.Ticks = require('./core/core.ticks');

require('./core/core.element')(Chart);
require('./core/core.plugin')(Chart);
Expand All @@ -17,9 +20,7 @@ require('./core/core.controller')(Chart);
require('./core/core.datasetController')(Chart);
require('./core/core.layoutService')(Chart);
require('./core/core.scaleService')(Chart);
require('./core/core.ticks')(Chart);
require('./core/core.scale')(Chart);
require('./core/core.interaction')(Chart);
require('./core/core.tooltip')(Chart);

require('./elements/element.arc')(Chart);
Expand All @@ -42,6 +43,7 @@ require('./controllers/controller.doughnut')(Chart);
require('./controllers/controller.line')(Chart);
require('./controllers/controller.polarArea')(Chart);
require('./controllers/controller.radar')(Chart);
require('./controllers/controller.scatter')(Chart);

require('./charts/Chart.Bar')(Chart);
require('./charts/Chart.Bubble')(Chart);
Expand Down
40 changes: 0 additions & 40 deletions src/charts/Chart.Scatter.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,8 @@
'use strict';

module.exports = function(Chart) {

var defaultConfig = {
hover: {
mode: 'single'
},

scales: {
xAxes: [{
type: 'linear', // scatter should not use a category axis
position: 'bottom',
id: 'x-axis-1' // need an ID so datasets can reference the scale
}],
yAxes: [{
type: 'linear',
position: 'left',
id: 'y-axis-1'
}]
},
showLines: false,

tooltips: {
callbacks: {
title: function() {
// Title doesn't make sense for scatter since we format the data as a point
return '';
},
label: function(tooltipItem) {
return '(' + tooltipItem.xLabel + ', ' + tooltipItem.yLabel + ')';
}
}
}
};

// Register the default config for this type
Chart.defaults.scatter = defaultConfig;

// Scatter charts use line controllers
Chart.controllers.scatter = Chart.controllers.line;

Chart.Scatter = function(context, config) {
config.type = 'scatter';
return new Chart(context, config);
};

};
153 changes: 78 additions & 75 deletions src/controllers/controller.bar.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,91 @@
'use strict';

var defaults = require('../core/core.defaults');
var helpers = require('../helpers/index');

module.exports = function(Chart) {
defaults._set('bar', {
hover: {
mode: 'label'
},

Chart.defaults.bar = {
hover: {
mode: 'label'
},
scales: {
xAxes: [{
type: 'category',

scales: {
xAxes: [{
type: 'category',
// Specific to Bar Controller
categoryPercentage: 0.8,
barPercentage: 0.9,

// Specific to Bar Controller
categoryPercentage: 0.8,
barPercentage: 0.9,
// grid line settings
gridLines: {
offsetGridLines: true
}
}],

yAxes: [{
type: 'linear'
}]
}
});

defaults._set('horizontalBar', {
hover: {
mode: 'label'
},

scales: {
xAxes: [{
type: 'linear',
position: 'bottom'
}],

yAxes: [{
position: 'left',
type: 'category',

// Specific to Horizontal Bar Controller
categoryPercentage: 0.8,
barPercentage: 0.9,

// grid line settings
gridLines: {
offsetGridLines: true
}
}]
},

// grid line settings
gridLines: {
offsetGridLines: true
elements: {
rectangle: {
borderSkipped: 'left'
}
},

tooltips: {
callbacks: {
title: function(item, data) {
// Pick first xLabel for now
var title = '';

if (item.length > 0) {
if (item[0].yLabel) {
title = item[0].yLabel;
} else if (data.labels.length > 0 && item[0].index < data.labels.length) {
title = data.labels[item[0].index];
}
}
}],
yAxes: [{
type: 'linear'
}]

return title;
},

label: function(item, data) {
var datasetLabel = data.datasets[item.datasetIndex].label || '';
return datasetLabel + ': ' + item.xLabel;
}
}
};
}
});

module.exports = function(Chart) {

Chart.controllers.bar = Chart.DatasetController.extend({

Expand Down Expand Up @@ -309,62 +368,6 @@ module.exports = function(Chart) {
}
});


// including horizontalBar in the bar file, instead of a file of its own
// it extends bar (like pie extends doughnut)
Chart.defaults.horizontalBar = {
hover: {
mode: 'label'
},

scales: {
xAxes: [{
type: 'linear',
position: 'bottom'
}],
yAxes: [{
position: 'left',
type: 'category',

// Specific to Horizontal Bar Controller
categoryPercentage: 0.8,
barPercentage: 0.9,

// grid line settings
gridLines: {
offsetGridLines: true
}
}]
},
elements: {
rectangle: {
borderSkipped: 'left'
}
},
tooltips: {
callbacks: {
title: function(tooltipItems, data) {
// Pick first xLabel for now
var title = '';

if (tooltipItems.length > 0) {
if (tooltipItems[0].yLabel) {
title = tooltipItems[0].yLabel;
} else if (data.labels.length > 0 && tooltipItems[0].index < data.labels.length) {
title = data.labels[tooltipItems[0].index];
}
}

return title;
},
label: function(tooltipItem, data) {
var datasetLabel = data.datasets[tooltipItem.datasetIndex].label || '';
return datasetLabel + ': ' + tooltipItem.xLabel;
}
}
}
};

Chart.controllers.horizontalBar = Chart.controllers.bar.extend({
/**
* @private
Expand Down
66 changes: 34 additions & 32 deletions src/controllers/controller.bubble.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
'use strict';

var defaults = require('../core/core.defaults');
var helpers = require('../helpers/index');

module.exports = function(Chart) {

Chart.defaults.bubble = {
hover: {
mode: 'single'
},

scales: {
xAxes: [{
type: 'linear', // bubble should probably use a linear scale by default
position: 'bottom',
id: 'x-axis-0' // need an ID so datasets can reference the scale
}],
yAxes: [{
type: 'linear',
position: 'left',
id: 'y-axis-0'
}]
},

tooltips: {
callbacks: {
title: function() {
// Title doesn't make sense for scatter since we format the data as a point
return '';
},
label: function(tooltipItem, data) {
var datasetLabel = data.datasets[tooltipItem.datasetIndex].label || '';
var dataPoint = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
return datasetLabel + ': (' + tooltipItem.xLabel + ', ' + tooltipItem.yLabel + ', ' + dataPoint.r + ')';
}
defaults._set('bubble', {
hover: {
mode: 'single'
},

scales: {
xAxes: [{
type: 'linear', // bubble should probably use a linear scale by default
position: 'bottom',
id: 'x-axis-0' // need an ID so datasets can reference the scale
}],
yAxes: [{
type: 'linear',
position: 'left',
id: 'y-axis-0'
}]
},

tooltips: {
callbacks: {
title: function() {
// Title doesn't make sense for scatter since we format the data as a point
return '';
},
label: function(item, data) {
var datasetLabel = data.datasets[item.datasetIndex].label || '';
var dataPoint = data.datasets[item.datasetIndex].data[item.index];
return datasetLabel + ': (' + item.xLabel + ', ' + item.yLabel + ', ' + dataPoint.r + ')';
}
}
};
}
});


module.exports = function(Chart) {

Chart.controllers.bubble = Chart.DatasetController.extend({

Expand Down
Loading

0 comments on commit b17c452

Please sign in to comment.