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. class MyScale extends Chart.Scale {
  2. /* extensions ... */
  3. }
  4. MyScale.id = 'myScale';
  5. MyScale.defaults = defaultConfigObject;
  6. // 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.

  1. Chart.register(MyScale);
  2. // If the new scale is not extending Chart.Scale, the prototype can not be used to detect what
  3. // you are trying to register - so you need to be explicit:
  4. // Chart.registry.addScales(MyScale);

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. y: {
  7. type: 'myScale' // this is the same id that was set on the scale
  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 label to show for the given value
  8. getLabelForValue: function(value) {},
  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. // Adds labels to objects in the ticks array. The default implementation simply calls this.options.ticks.callback(numericalTick, index, ticks);
  3. generateTickLabels: function() {},
  4. // Determine how much the labels will rotate by. The default implementation will only rotate labels if the scale is horizontal.
  5. calculateLabelRotation: 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. // Returns the scale tick objects ({label, major})
  5. getTicks: function() {}
  6. }