diff --git a/src/controllers/controller.histogram.js b/src/controllers/controller.histogram.js new file mode 100644 index 00000000000..8540a1ebcfb --- /dev/null +++ b/src/controllers/controller.histogram.js @@ -0,0 +1,55 @@ +import BarController from './controller.bar.js'; + +/** + * @typedef { import('../types.js').ChartMeta } ChartMeta + * @typedef { import('../types.js').Point } Point + */ + +export default class HistogramController extends BarController { + + static id = 'histogram'; + + /** + * @type {any} + */ + static defaults = { + datasetElementType: false, + dataElementType: 'bar', + + // Bars fill the entire category width with no gaps between them, + // which is the expected appearance for a histogram. + barPercentage: 1, + categoryPercentage: 1, + + grouped: false, + + animations: { + numbers: { + type: 'number', + properties: ['x', 'y', 'base', 'width', 'height'] + } + } + }; + + /** + * @type {any} + */ + static overrides = { + scales: { + // A linear index axis is required so bars are positioned and sized + // according to their numeric x values rather than by category index. + _index_: { + type: 'linear', + offset: false, + grid: { + offset: false + } + }, + _value_: { + type: 'linear', + beginAtZero: true, + } + } + }; + +} diff --git a/src/controllers/index.js b/src/controllers/index.js index 9d265dc9dc6..91fbc3122d2 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -1,5 +1,6 @@ export {default as BarController} from './controller.bar.js'; export {default as BubbleController} from './controller.bubble.js'; +export {default as HistogramController} from './controller.histogram.js'; export {default as DoughnutController} from './controller.doughnut.js'; export {default as LineController} from './controller.line.js'; export {default as PolarAreaController} from './controller.polarArea.js'; diff --git a/src/types.ts b/src/types.ts index ff16b8be546..5d27ab09d50 100644 --- a/src/types.ts +++ b/src/types.ts @@ -11,6 +11,7 @@ export { BarController, BubbleController, DoughnutController, + HistogramController, LineController, PieController, PolarAreaController, diff --git a/src/types/index.d.ts b/src/types/index.d.ts index 911b4cb2bc8..fba48416bec 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -159,6 +159,15 @@ export declare const BarController: ChartComponent & { new (chart: Chart, datasetIndex: number): BarController; }; +export type HistogramControllerDatasetOptions = BarControllerDatasetOptions; +export type HistogramControllerChartOptions = BarControllerChartOptions; + +export type HistogramController = BarController +export declare const HistogramController: ChartComponent & { + prototype: HistogramController; + new (chart: Chart, datasetIndex: number): HistogramController; +}; + export interface BubbleControllerDatasetOptions extends ControllerDatasetOptions, ScriptableAndArrayOptions>, @@ -3716,6 +3725,14 @@ export interface ChartTypeRegistry { parsedDataType: BarParsedData, scales: keyof CartesianScaleTypeRegistry; }; + histogram: { + chartOptions: HistogramControllerChartOptions; + datasetOptions: HistogramControllerDatasetOptions; + defaultDataPoint: ScatterDataPoint | number | null; + metaExtensions: {}; + parsedDataType: BarParsedData; + scales: keyof CartesianScaleTypeRegistry; + }; line: { chartOptions: LineControllerChartOptions; datasetOptions: LineControllerDatasetOptions & FillerControllerDatasetOptions;