Cal-Heatmap Documentation
repository·master·Indexed 25 days ago
https://github.com/wa0x6e/cal-heatmapA JavaScript library for creating highly customizable time-series calendar heatmaps to visualize data density over time, similar to GitHub's contribution graph. Version 4.3.0-beta.4 supports animated date navigation, locale and timezone support, a plugin system, and various temporal granularities from minutes to years.
What's inside cal-heatmap
- Cal-Heatmap is a JavaScript charting library used to create time-series calendar heatmaps, similar to the GitHub contribution calendar. It supports features such as animated date navigation, customizable time intervals, full layout/UI control, locale and timezone support, a plugin system, and right-to-left (RTL) support.
Configure Jest E2E testing environment
masterThe project uses a custom Jest configuration for end-to-end (E2E) testing. Key settings include using
jsdomas the test environment, ats-jestpreset for ESM support, and specific patterns for detecting E2E test files located in thee2e/directory.export default { clearMocks: true, coverageProvider: 'v8', globalSetup: './test/e2e/utils/setup.js', globalTeardown: './test/e2e/utils/teardown.js', preset: 'ts-jest/presets/default-esm', roots: [ "./test" ], testEnvironment: 'jsdom', testRegex: ['e2e/(.*).test.ts'], // ... other settings };Configure Cal-Heatmap using OptionsType
masterThe
OptionsTypeobject is the primary configuration interface for Cal-Heatmap. You can initialize or update settings using theinit()method of theOptionsclass.Key configuration groups include:
itemSelector: A CSS selector or DOM element where the heatmap will be appended.domain: Settings for the main time units (e.g., weeks, months), includingtype,gutter,padding, andlabeloptions.subDomain: Settings for the individual cells within a domain, includingtype,width,height,gutter,radius, andlabel.date: Controls the time range, includingstart,min,max,highlight(array of Dates),locale, andtimezone.data: Configures the data source (source,type), mapping keys (x,y), and aggregation (groupY).scale: Configures color schemes and opacity.theme: Set to'light'or'dark'.verticalOrientation: Boolean to switch between horizontal and vertical layouts.
Default configuration options for Cal-Heatmap
masterWhen no options are provided, Cal-Heatmap uses the following default values. These constants define the initial behavior for domains, subdomains, animation, and selection:
- Domain Type:
'hour' - Subdomain Type:
'minute' - Subdomain Dimensions: Width
10, Height10, Gutter2, Radius0 - Animation Duration:
200ms - Range:
12 - Item Selector:
'#cal-heatmap' - Theme:
'light' - Locale:
'en'
- Domain Type:
Default color scale settings
masterThe default color scale configuration used for heatmap intensity is:
- Base Color:
'red' - Color Scheme:
'YlOrBr' - Color Type:
'quantize' - Color Domain:
[0, 100]
- Base Color:
Configure module mapping and transformations in Jest
masterTo support ESM and style imports in tests, the Jest configuration usesmoduleNameMapperto resolve.jsextensions in imports and mocks SCSS files. It also usests-jestwithuseESM: trueto transform TypeScript files.Import CalHeatmap and VERSION
masterThe library provides a default exportCalHeatmapwhich is the main class used to instantiate heatmaps, and a named exportVERSIONcontaining the current library version string.Initialize or update options with init()
masterThe
init(opts?: DeepPartial<OptionsType>)method is used to apply a configuration object to theOptionsinstance. It merges the providedoptswith the existing defaults. Note that for arrays, the providedsrcValuewill overwrite the default rather than merging elements.When calling
init(), the class also automatically runs pre-processors and calculates internal dimensions for labels and scales.Update a single option with set()
masterThe
set(key: string, value: any): booleanmethod allows you to update a specific configuration property.- It only updates the value if the new value is different from the current one (using
isEqual). - It returns
trueif the value was changed, andfalseif it remained the same or the key does not exist. - If a pre-processor exists for the specified
key, the value is passed through the pre-processor before being set.
- It only updates the value if the new value is different from the current one (using
Use built-in Cal-Heatmap templates
masterCal-Heatmap provides a collection of built-in templates for different time granularities. These templates can be used to define the layout of the heatmap. The available templates are:
minuteTemplate: For minute-level granularity.hourTemplate: For hour-level granularity.dayTemplate: For day-level granularity.xDayTemplate: For multi-day granularity.ghDayTemplate: GitHub-style day granularity.weekTemplate: For week-level granularity.monthTemplate: For month-level granularity.yearTemplate: For year-level granularity.
Define Custom Templates with Template type
masterYou can define custom rendering logic by creating a
Templatefunction. This function receives theDateHelperandOptionsTypeand returns aTemplateResult.export type Template = { (dateHelper: DateHelper, options: OptionsType): TemplateResult; }; export type TemplateResult = { name: string; parent?: string; allowedDomainType: DomainType[]; rowsCount: (ts: Timestamp) => number; columnsCount: (ts: Timestamp) => number; mapping: (startTimestamp: Timestamp, endTimestamp: Timestamp) => SubDomain[]; extractUnit: (ts: Timestamp) => Timestamp; };SubDomainobjects returned by the mapping function define the coordinates and values for cells:t:Timestampx:numbery:numberv:number | string | null(optional value)
CalHeatmap Class API Reference
masterThe
CalHeatmapclass is the primary entry point for the library. You can instantiate it usingnew CalHeatmap()and use its methods to render, navigate, and manipulate the heatmap.Core Methods
paint(options?: DeepPartial<OptionsType>, plugins?: IPlugin[]): Promise<unknown>: Renders the heatmap with the provided options and optional plugins.next(n?: number): Promise<unknown>: Moves the calendar to the next period.previous(n?: number): Promise<unknown>: Moves the calendar to the previous period.jumpTo(date: Date, reset?: boolean): Promise<unknown>: Jumps the calendar to a specific date.fill(dataSource?: OptionsType['data']['source']): Promise<unknown>: Fills the heatmap with new data.destroy(): Promise<unknown>: Destroys the heatmap instance.addTemplates(templates: Template | Template[]): void: Registers new templates for rendering.on(name: string, fn: () => any): void: Registers an event listener via the internaleventEmitter.dimensions(): Dimensions: Returns the current width and height of the heatmap.