New Axes

Axes in Chart.js can be individually extended. Axes should always derive from Chart.Scale but this is not a mandatory requirement.

  1. let MyScale = Chart.Scale.extend({
  2. /* extensions ... */
  3. });
  4. // MyScale is now derived from Chart.Scale

Once you have created your scale class, you need to register it with the global chart object so that it can be used. A default config for the scale may be provided when registering the constructor. The first parameter to the register function is a string key that is used later to identify which scale type to use for a chart.

  1. Chart.scaleService.registerScaleType('myScale', MyScale, defaultConfigObject);

To use the new scale, simply pass in the string key to the config when creating a chart.

  1. var lineChart = new Chart(ctx, {
  2. data: data,
  3. type: 'line',
  4. options: {
  5. scales: {
  6. yAxes: [{
  7. type: 'myScale' // this is the same key that was passed to the registerScaleType function
  8. }]
  9. }
  10. }
  11. });

Scale Properties

Scale instances are given the following properties during the fitting process.

  1. {
  2. left: number, // left edge of the scale bounding box
  3. right: number, // right edge of the bounding box
  4. top: number,
  5. bottom: number,
  6. width: number, // the same as right - left
  7. height: number, // the same as bottom - top
  8. // Margin on each side. Like css, this is outside the bounding box.
  9. margins: {
  10. left: number,
  11. right: number,
  12. top: number,
  13. bottom: number
  14. },
  15. // Amount of padding on the inside of the bounding box (like CSS)
  16. paddingLeft: number,
  17. paddingRight: number,
  18. paddingTop: number,
  19. paddingBottom: number
  20. }

Scale Interface

To work with Chart.js, custom scale types must implement the following interface.

  1. {
  2. // Determines the data limits. Should set this.min and this.max to be the data max/min
  3. determineDataLimits: function() {},
  4. // Generate tick marks. this.chart is the chart instance. The data object can be accessed as this.chart.data
  5. // buildTicks() should create a ticks array on the axis instance, if you intend to use any of the implementations from the base class
  6. buildTicks: function() {},
  7. // Get the value to show for the data at the given index of the the given dataset, ie this.chart.data.datasets[datasetIndex].data[index]
  8. getLabelForIndex: function(index, datasetIndex) {},
  9. // Get the pixel (x coordinate for horizontal axis, y coordinate for vertical axis) for a given value
  10. // @param index: index into the ticks array
  11. getPixelForTick: function(index) {},
  12. // Get the pixel (x coordinate for horizontal axis, y coordinate for vertical axis) for a given value
  13. // @param value : the value to get the pixel for
  14. // @param index : index into the data array of the value
  15. // @param datasetIndex : index of the dataset the value comes from
  16. getPixelForValue: function(value, index, datasetIndex) {},
  17. // Get the value for a given pixel (x coordinate for horizontal axis, y coordinate for vertical axis)
  18. // @param pixel : pixel value
  19. getValueForPixel: function(pixel) {}
  20. }

Optionally, the following methods may also be overwritten, but an implementation is already provided by the Chart.Scale base class.

  1. {
  2. // Transform the ticks array of the scale instance into strings. The default implementation simply calls this.options.ticks.callback(numericalTick, index, ticks);
  3. convertTicksToLabels: function() {},
  4. // Determine how much the labels will rotate by. The default implementation will only rotate labels if the scale is horizontal.
  5. calculateTickRotation: function() {},
  6. // Fits the scale into the canvas.
  7. // this.maxWidth and this.maxHeight will tell you the maximum dimensions the scale instance can be. Scales should endeavour to be as efficient as possible with canvas space.
  8. // this.margins is the amount of space you have on either side of your scale that you may expand in to. This is used already for calculating the best label rotation
  9. // You must set this.minSize to be the size of your scale. It must be an object containing 2 properties: width and height.
  10. // You must set this.width to be the width and this.height to be the height of the scale
  11. fit: function() {},
  12. // Draws the scale onto the canvas. this.(left|right|top|bottom) will have been populated to tell you the area on the canvas to draw in
  13. // @param chartArea : an object containing four properties: left, right, top, bottom. This is the rectangle that lines, bars, etc will be drawn in. It may be used, for example, to draw grid lines.
  14. draw: function(chartArea) {}
  15. }

The Core.Scale base class also has some utility functions that you may find useful.

  1. {
  2. // Returns true if the scale instance is horizontal
  3. isHorizontal: function() {},
  4. // Get the correct value from the value from this.chart.data.datasets[x].data[]
  5. // If dataValue is an object, returns .x or .y depending on the return of isHorizontal()
  6. // If the value is undefined, returns NaN
  7. // Otherwise returns the value.
  8. // Note that in all cases, the returned value is not guaranteed to be a number
  9. getRightValue: function(dataValue) {},
  10. // Returns the scale tick objects ({label, major})
  11. getTicks: function() {}
  12. }