Skip to content

Custom Drawings

Use defineDrawing to add chart-scoped Canvas drawings such as trend lines, price segments, annotations, or other interactive overlays.

ts
const definition = defineDrawing<Config, CompleteConfig>({
  type: 'custom:price-segment',
  displayName: 'PriceSegment',
  parseConfig,
  draw,
  hitTest,
  onHover,
  onClick,
  onDrag,
});

const PriceSegment = definition.Component;

Register the definition on each chart that uses its component:

tsx
<Chart data={data} drawingDefinitions={[definition]}>
  <Panel>
    <Candlesticks />
    <PriceSegment
      startIndex={10}
      startValue={120}
      endIndex={30}
      endValue={135}
    />
  </Panel>
</Chart>

Definitions are local to a chart. A custom drawing type must be a non-empty string and must not collide with another custom drawing registered on the same chart.

defineDrawing

defineDrawing accepts:

PropertyTypeDescription
typestringStable drawing type used in JSX metadata and config objects.
displayNamestringOptional React component display name.
parseConfig(config, panelId, drawingIndex) => completeConfigApplies defaults and returns the runtime drawing config.
draw(renderContext) => voidDraws the custom drawing on Canvas.
hitTest`(hitTestContext) => DrawingHitTestResultnull`
onHover(hit, hitTestContext) => voidOptional drawing-level hover handler.
onClick(hit, hitTestContext) => voidOptional drawing-level click handler.
onDragStart(hit, hitTestContext) => voidOptional drag-start handler.
onDrag(dragContext) => voidOptional drag handler.
onDragEnd(hit, hitTestContext) => voidOptional drag-end handler.

Config Types

Extend DrawingConfig for JSX/user config and DrawingConfigComplete for the parsed runtime config:

ts
interface PriceSegmentConfig extends DrawingConfig {
  type: 'custom:price-segment';
  startIndex: number;
  startValue: number;
  endIndex: number;
  endValue: number;
  color?: string;
}

interface PriceSegmentConfigComplete extends DrawingConfigComplete {
  type: 'custom:price-segment';
  startIndex: number;
  startValue: number;
  endIndex: number;
  endValue: number;
  color: string;
}

The generated component accepts the config properties except type; the definition supplies that automatically.

Render Helpers

Drawing render and hit-test contexts provide helpers for mapping data coordinates into chart pixels:

HelperDescription
xForIndex(index)Returns the x-position for a bar index.
xForTimestamp(timestamp, nearest?)Returns the x-position for a timestamp.
valueToY(value, scaleKey?)Returns the y-position for a value.

scaleKey can be set on a drawing config when it should use a specific panel scale. If omitted, the chart uses the panel's available layer scale.

Chart Callbacks

Use chart-level callbacks when the parent component needs to react to drawing interactions:

tsx
<Chart
  data={data}
  drawingDefinitions={[priceSegment]}
  onDrawingHover={setHoveredDrawing}
  onDrawingClick={handleDrawingClick}
>
  ...
</Chart>

TypeScript

ts
interface DrawingConfig {
  id?: string;
  type: string;
  visible?: boolean;
  scaleKey?: string;
  [key: string]: unknown;
}

interface DrawingConfigComplete extends DrawingConfig {
  id: string;
  type: string;
  visible: boolean;
}

interface DrawingHit {
  drawingId: string;
  drawingType: string;
  panelId: string;
  target?: string;
  cursor?: string;
  data?: unknown;
}

React Candlesticks is available on npm and GitHub under the MIT License.