Chart.js Specific Methods
configMerge(base, object1, object2, ...)
configMerge
is used to merge a chart's configured options into the defaults for the chart type. We want to take the configured options where they are available and the defaults where they are not.
While configMerge
seems pretty similar to Chart.helpers.extend
there are a few minor differences and a few other differences that are notable.
- While both
configMerge
andextend
return a much more heavier value than what was passed in,extend
modifiesbase
,configMerge
does not. - If both
base
andobjectn
have a property with the same name,extend
simply takesobjectn
's value.configMerge
mergesobjectn
's value intobase
's value if they are both objects. Thescale
orscales
properties are exceptions.- When the property is named
scale
, we assume that the value is a scale andconfigMerge
the default scale properties for that scale type ontobase
before weconfigMerge
objectn
's value. Thetype
for the scale comes fromobjectn
(and notbase
). - When the property is named
scales
, we useChart.helpers.scaleMerge
to merge the values.
- When the property is named
- Primitives and null values in
base
are overwritten byobjectn
's values just like inextend
// primitives and arrays are overwritten, objects are merged
Chart.helpers.configMerge(
{ firstName: "John", parkingSpots: [ 1, 3, 5 ], address: { number: 21 } },
{ firstName: "Jane", parkingSpots: [ 2, 4, 6 ], address: { street: "Baker" } }
);
// { firstName: "Jane", parkingSpots: [ 2, 4, 6 ], address: { number: 21, street: "Baker" } }
// the scale property has additional defaults added into it based on the scale type
configMerge(
{ scale: { name: "Bob" } },
{ scale: { type: "category" } }
);
// { scale: { name: "bob", display: true, ...other defaults for the category scale... , type: "category" } }
getRelativePosition(event, chartInstance)
The browser attaches the position to click and touch events. This position is relative to the browser viewport. When dealing with events inside the canvas, we want the canvas coordinates. getRelativePosition
gets you the canvas coordinates accounting for the position and padding of the canvas, and any device pixel scaling we may have done in retinaScale
.
The return value is a position object with an x
property and a y
property set to the canvas coordinates of event
.
scaleMerge(base, object1)
scaleMerge
is used to merge a scales
object into another. A scales
object typically contains 2 arrays of scale configurations - one for x axes and one for y axes.
An array of scale configurations is merged into another using the following logic
- If the
base
scales
object doesn't have a configuration at that index, or if it doesn't have a scaletype
specified, weconfigMerge
theobject1
scale configuration onto the default scale properties for the scale type. If atype
is not specified, it's assumed to be'category'
for xAxes scales and'linear'
foryAxes
scales. - If the
base
scale configuration has atype
that is different from that inobject1
, weconfigMerge
the default scale properties (fromobject1
) onto this and then the correspondingobject1
scale configuration. - If the
type
matches, weconfigMerge
the correspondingobject1
scale configuration directly onto thebase
scale configuation.
For all other properties on the scales
object, we directly use configMerge
.
// if the types differ we get the defaults and then objectn's scale configuration
Chart.helpers.scaleMerge(
{ xAxes: [ { type: 'linear' } ] },
{ xAxes: [ { type: 'category' } ] }
);
// { xAxes: [ { display: true, ...other defaults for the category scale..., type: 'category' } ] }
// the default scale type is 'category' for xAxes (and 'linear' for yAxes)
Chart.helpers.scaleMerge(
{ xAxes: [ ] },
{ xAxes: [ {} ] }
);
// { xAxes: [ { display: true, ...other defaults for the category scale..., type: 'category' } ] }
// if the scale types match a simple configMerge is done
Chart.helpers.scaleMerge(
{ xAxes: [ { type: 'linear', name: 'Bob' } ] },
{ xAxes: [ { type: 'linear', height: '8ft' } ] }
);
// { xAxes: [ { type: 'linear', name: 'Bob', height: '8ft' } ] }
// other properties are simply merged
Chart.helpers.scaleMerge(
{ backpack: { color: "blue" } },
{ backpack: { material: "kevlar" } }
);
// { backpack: { color: "blue", material: "kevlar" } },