Cal-Heatmap Documentation

repository·master·Indexed 25 days ago

https://github.com/wa0x6e/cal-heatmap

A 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.

Tokens
2.6K
Snippets
5
Records
21
Agent score
83%

What's inside cal-heatmap

  1. Overview of Cal-Heatmap

    master
    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.
  2. Configure Jest E2E testing environment

    master

    The project uses a custom Jest configuration for end-to-end (E2E) testing. Key settings include using jsdom as the test environment, a ts-jest preset for ESM support, and specific patterns for detecting E2E test files located in the e2e/ 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
    };
  3. Configure Cal-Heatmap using OptionsType

    master

    The OptionsType object is the primary configuration interface for Cal-Heatmap. You can initialize or update settings using the init() method of the Options class.

    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), including type, gutter, padding, and label options.
    • subDomain: Settings for the individual cells within a domain, including type, width, height, gutter, radius, and label.
    • date: Controls the time range, including start, min, max, highlight (array of Dates), locale, and timezone.
    • 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.
  4. Default configuration options for Cal-Heatmap

    master

    When 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, Height 10, Gutter 2, Radius 0
    • Animation Duration: 200ms
    • Range: 12
    • Item Selector: '#cal-heatmap'
    • Theme: 'light'
    • Locale: 'en'
  5. Initialize or update options with init()

    master

    The init(opts?: DeepPartial<OptionsType>) method is used to apply a configuration object to the Options instance. It merges the provided opts with the existing defaults. Note that for arrays, the provided srcValue will 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.

  6. Update a single option with set()

    master

    The set(key: string, value: any): boolean method 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 true if the value was changed, and false if 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.
  7. Use built-in Cal-Heatmap templates

    master

    Cal-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.
  8. Define Custom Templates with Template type

    master

    You can define custom rendering logic by creating a Template function. This function receives the DateHelper and OptionsType and returns a TemplateResult.

    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;
    };

    SubDomain objects returned by the mapping function define the coordinates and values for cells:

    • t: Timestamp
    • x: number
    • y: number
    • v: number | string | null (optional value)
  9. CalHeatmap Class API Reference

    master

    The CalHeatmap class is the primary entry point for the library. You can instantiate it using new 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 internal eventEmitter.
    • dimensions(): Dimensions: Returns the current width and height of the heatmap.