The building blocks under the charts: data normalization, scales, math and small utilities. Plugins and custom layers may use them; charts made with the chart functions don’t need them.
const axisUnits: { readonly x: { readonly pos: 'x'; readonly len: 'width'; readonly dir: 'horizontal'; readonly rectStart: 'x1'; readonly rectEnd: 'x2'; readonly rectOffset: 'y2' }; readonly y: { readonly pos: 'y'; readonly len: 'height'; readonly dir: 'vertical'; readonly rectStart: 'y2'; readonly rectEnd: 'y1'; readonly rectOffset: 'x1' } }The names an axis uses per dimension: its position, length and direction, and its edges in the ChartRect.
const namespaces: Record<string, string>This object contains all namespaces used within Chartist.
const precision: 8Precision level used internally in Chartist for rounding. If you require more decimal places you can increase this number.
const escapingMap: Record<string, string>A map with characters to escape for strings to be safely used as attribute values.
function normalizePadding(padding: number | Partial<ChartPadding> | undefined): { top: number; right: number; bottom: number; left: number }Turns a padding option into a full ChartPadding: a number pads every side, an object keeps its numeric sides and sets the missing ones to 0, and undefined gives 0 on every side.
function createChartRect(svg: Svg, options: Options): { x1: number; x2: number; y1: number; y2: number; padding: { top: number; right: number; bottom: number; left: number }; width: () => number; height: () => number }Initialize chart drawing rectangle (area where chart is drawn) x1,y1 = bottom left / x2,y2 = top right
function getBounds(axisLength: number, highLow: { high: number; low: number }, scaleMinSpace: number, onlyInteger?: boolean): BoundsCalculate and retrieve all the bounds for the chart and return them in one array
function getMetaData(seriesData: FlatSeriesValue | Series | SeriesObject<SeriesPrimitiveValue>, index: number): anyGet meta data of a specific value in a series.
function isDataHoleValue(value: unknown): value is null | undefinedChecks if a value is considered a hole in the data series.
function isArrayOfSeries(value: unknown): value is (Series | SeriesObject<SeriesPrimitiveValue>)[]Checks if value is array of series objects.
function isMultiValue(value: unknown): value is MultiChecks if provided value object is multi value (contains x or y properties)
function getMultiValue(value: unknown, dimension?: AxisName): number | undefinedGets a value from a dimension value.x or value.y while returning value directly if it's a valid numeric value. If the value is not numeric and it's falsey this function will return defaultValue.
function getHighLow(data: NormalizedSeries[], options: Options, dimension?: AxisName, extent?: { readonly low: number; readonly high: number }): { high: number; low: number }Get highest and lowest value of data array. This Array contains the data that will be visualized in the chart.
function normalizeData(data: Data<FlatSeries>, reverse?: boolean, multi?: false): NormalizedData<NormalizedFlatSeries>function normalizeData(data: Data<(Series | SeriesObject<SeriesPrimitiveValue>)[]>, reverse: boolean | undefined, multi: true | AxisName): NormalizedData<NormalizedSeries[]>function normalizeData(data: Data<(Series | SeriesObject<SeriesPrimitiveValue>)[] | FlatSeries>, reverse: boolean | undefined, multi: boolean | AxisName, distributed: true): NormalizedData<NormalizedSeries[]>function normalizeData(data: Data<(Series | SeriesObject<SeriesPrimitiveValue>)[] | FlatSeries>, reverse?: boolean, multi?: boolean | AxisName): NormalizedData<NormalizedSeries[] | NormalizedFlatSeries>Ensures that the data object passed as second argument to the charts is present and correctly initialized.
function splitIntoSegments(pathCoordinates: number[], valueData: SegmentData[], options?: { increasingX?: boolean; fillHoles?: boolean }): Segment[]Splits a list of coordinates and associated values into segments. Each returned segment contains a pathCoordinates valueData property describing the segment.
With the default options, segments consist of contiguous sets of points that do not have an undefined value. Any points with undefined values are discarded.
**Options** The following options are used to determine how segments are formed javascript
var options = {
// If fillHoles is true, undefined values are simply discarded without creating a new segment. Assuming other options are default, this returns single segment.
fillHoles: false,
// If increasingX is true, the coordinates in all segments have strictly increasing x-values.
increasingX: false
};
function serialize(data: string | number | object): stringfunction serialize(data: unknown): string | null | undefinedThis function serializes arbitrary data to a string. In case of data that can't be easily converted to a string, this function will create a wrapper object and serialize the data using JSON.stringify. The outcoming string will always be escaped using Chartist.escapingMap. If called with null or undefined the function will return immediately with null or undefined.
function deserialize<T extends string | number | object = object>(data: string): Tfunction deserialize<T extends string | number | object = object>(data: string | null | undefined): T | null | undefinedThis function de-serializes a string previously serialized with Chartist.serialize. The string will always be unescaped using Chartist.escapingMap before it's returned. Based on the input value the return type can be Number, String or Object. JSON.parse is used with try / catch to see if the unescaped string can be parsed into an Object and this Object will be returned on success.
function ensureUnit<T>(value: T, unit: string): string | TConverts a number to a string with a unit. If a string is passed then this will be returned unmodified.
function quantity<T>(input: T): { value: number; unit: string | undefined } | { unit?: undefined; value: number }Converts a number or string to a quantity object.
function alphaNumerate(n: number): stringGenerates a-z from a number 0 to 26
const EPSILON: 2.221e-16A tiny number, about the gap between 1 and the next float: the tolerance of float comparisons.
function orderOfMagnitude(value: number): numberCalculate the order of magnitude for the chart scale
function projectLength(axisLength: number, length: number, bounds: Bounds): numberProject a data length into screen coordinates (pixels)
function roundWithPrecision(value: number, digits?: number): numberRounds a value to a number of digits after the decimal point, to remove float errors such as 0.30000000000000004. 0 digits round to whole numbers.
function rho(num: number): numberPollard Rho Algorithm to find smallest factor of an integer value. There are more efficient algorithms for factorization, but this one is quite efficient and not so complex.
function polarToCartesian(centerX: number, centerY: number, radius: number, angleInDegrees: number): { x: number; y: number }Calculate cartesian coordinates of polar coordinates
function extend<T>(target: T): Tfunction extend<T, A>(target: T, a: A): T & Afunction extend<T, A, B>(target: T, a: A, b: B): T & A & BMerges the sources into target, deeply, and returns target. Plain objects merge key by key into an object already there, and replace any other value (an object over a number gives a copy of the object); arrays, from any realm, and other values replace what's there. Keys of the target's prototype are skipped, against prototype pollution.
function noop<T>(n: T): THelps to simplify functional style code
function times(length: number): undefined[]function times<T = unknown>(length: number, filler: (index: number) => T): T[]Functional style helper to produce array with given length initialized with undefined values
function sum(previous: number, current: number): numberSum helper to be used in reduce functions
function serialMap<T, K>(array: T[][], callback: (...args: T[]) => K): K[]Map for multi dimensional arrays where their nested arrays will be mapped in serial. The output array will have the length of the largest nested array. The callback function is called with variable arguments where each argument is the nested array value (or undefined if there are no more values).
For example:
function safeHasProperty<T, K extends string>(target: T, property: K): target is FilterByKey<T, K>This function safely checks if an objects has an owned property.
function isNumeric(value: number): truefunction isNumeric(value: unknown): booleanChecks if a value can be safely coerced to a number. This includes all values except null which result in finite numbers when coerced. This excludes NaN, since it's not finite.
function isFalseyButZero(value: unknown): value is false | '' | null | undefinedReturns true on all falsey values except the numeric value 0.
function getNumberOrUndefined(value: number): numberfunction getNumberOrUndefined(value: unknown): number | undefinedReturns a number if the passed parameter is a valid number or the function will return undefined. On all other values than a valid number, this function will return undefined.
function isArrayOfArrays(data: unknown): data is unknown[][]Checks if value is array of arrays or not.
function each<T>(list: T[], callback: (item: T, index: number, itemIndex: number) => void, reverse?: boolean): voidLoop over array.
type XAxisUnits = typeof axisUnits.xThe units of the X axis: axisUnits.x.
type YAxisUnits = typeof axisUnits.yThe units of the Y axis: axisUnits.y.
type AxisUnits = XAxisUnits | YAxisUnitsThe units of either axis.
interface ChartRectThe drawing area of a 1.x chart in px: x1, y1 is its bottom left corner and x2, y2 its top right (SVG y grows downward, so y1 is the larger one).
interface ViewBoxThe size of an SVG viewBox.
interface BoundsThe scale getBounds computes for an axis: its range and steps.
interface SegmentA run of points without holes, as splitIntoSegments returns it.
interface SegmentDataA point's value, its index in the series and its meta.
type AxisName = 'x' | 'y'An axis dimension: 'x' or 'y'.
type FilterByKey<T, K extends string> = T extends Record<K, unknown> ? T : T extends Partial<Record<K, unknown>> ? T & { [key in K]: T[K] } : neverNarrows a union T to the members that have the key K (an optional K becomes required).
type RequiredKeys<T, K extends keyof T, V extends keyof T = never> = T & Required<Pick<T, K | V>> & { [key in V]: Required<T[V]> }T with the keys K required, and the keys V required down one level.