Chartjs.FFI
- Package
- purescript-chartjs
- Repository
- philippedev101/purescript-chartjs
Low-level FFI bindings to Chart.js for creating, updating, and destroying chart instances.
#ChartInstance Source
data ChartInstanceOpaque handle to a Chart.js instance.
#createChart Source
createChart :: HTMLElement -> Json -> Effect ChartInstanceCreate a new Chart.js instance on a canvas element with a JSON config.
#updateChart Source
updateChart :: ChartInstance -> Json -> Maybe String -> Effect UnitUpdate an existing chart with a new JSON config. The mode parameter controls animation: Nothing for default animation, Just "none" to skip animation, or any other Chart.js update mode.
#destroyChart Source
destroyChart :: ChartInstance -> Effect UnitDestroy a chart instance and release its resources.
#createChartWithCallbacks Source
createChartWithCallbacks :: HTMLElement -> Json -> Foreign -> Effect ChartInstanceCreate a chart with a JSON config and a callbacks/overlays object.
#updateChartWithCallbacks Source
updateChartWithCallbacks :: ChartInstance -> Json -> Foreign -> Maybe String -> Effect UnitUpdate a chart with a new JSON config and a callbacks/overlays object. The mode parameter controls animation: Nothing for default animation, Just "none" to skip animation, or any other Chart.js update mode.
#stopChart Source
stopChart :: ChartInstance -> Effect ChartInstanceStop the current animation. Returns the chart instance for chaining.
#toBase64Image Source
toBase64Image :: ChartInstance -> Maybe String -> Maybe Number -> Effect StringExport the chart as a base64-encoded image data URL. The type parameter is a MIME type (e.g. "image/png", "image/jpeg"). The quality parameter is 0-1 for lossy formats like JPEG.
#setDatasetVisibility Source
setDatasetVisibility :: ChartInstance -> Int -> Boolean -> Effect UnitSet the visibility of a dataset. Call updateChart afterward to render.
#isDatasetVisible Source
isDatasetVisible :: ChartInstance -> Int -> Effect BooleanCheck whether a dataset is currently visible.
#hideDataset Source
hideDataset :: ChartInstance -> Int -> Maybe Int -> Effect UnitHide a dataset or a specific data element with animation. Pass Nothing for dataIndex to hide the entire dataset.
#showDataset Source
showDataset :: ChartInstance -> Int -> Maybe Int -> Effect UnitShow a dataset or a specific data element with animation. Pass Nothing for dataIndex to show the entire dataset.
#renderChart Source
renderChart :: ChartInstance -> Effect UnitTrigger a redraw of existing chart elements. Does NOT update elements
for new data — use updateChart when data changes.
#resetChart Source
resetChart :: ChartInstance -> Effect UnitRestore the chart to its initial state before animations began. A
subsequent updateChart call will trigger new animations.
#resizeChart Source
resizeChart :: ChartInstance -> Maybe Number -> Maybe Number -> Effect UnitAdjust canvas dimensions. Pass Nothing for both width and height to
resize to match the container. Normally called automatically when the
container resizes; call this explicitly if you changed the container
size imperatively.
#clearChart Source
clearChart :: ChartInstance -> Effect ChartInstanceClear the chart canvas. Used internally between animation frames; rarely needed directly. Returns the chart instance for chaining.
#getDatasetMeta Source
getDatasetMeta :: ChartInstance -> Int -> Effect ForeignLook up the internal metadata object for a dataset by index. The
returned Foreign is Chart.js's ChartMeta — a mutable bag of
controller/scale/data references. Consumers who need specific fields
(e.g. data, visible, hidden) coerce via Foreign operations.
#getElementsAtEventForMode Source
getElementsAtEventForMode :: ChartInstance -> Event -> InteractionMode -> InteractionOptions -> Boolean -> Effect (Array InteractionItem)Find chart elements matching an event for a given interaction mode.
Useful for click handlers that need to know which point/bar/arc was
hit. The useFinalPosition flag controls whether to use the
animation's final position (true) or current position (false).
#interactionOptionsToForeign Source
interactionOptionsToForeign :: InteractionOptions -> ForeignConvert InteractionOptions to a plain JS object suitable for passing
to Chart.js's getElementsAtEventForMode. Nothing fields are dropped
so Chart.js uses its defaults for them. Exported for testing — callers
normally use getElementsAtEventForMode directly.
#toggleDataVisibility Source
toggleDataVisibility :: ChartInstance -> Int -> Effect UnitToggle the visibility of a single data element by its index within
the dataset. Inverts the current visibility flag on the matching
meta entry. Call updateChart afterwards to render the change.
#getDataVisibility Source
getDataVisibility :: ChartInstance -> Int -> Effect BooleanCheck whether a single data element is currently visible.
#getActiveElements Source
getActiveElements :: ChartInstance -> Effect (Array Foreign)Get the array of currently active (hovered or otherwise highlighted)
chart elements. Each entry is an opaque Foreign wrapping Chart.js's
ActiveElement (which has datasetIndex, index, and element).
#setActiveElements Source
setActiveElements :: ChartInstance -> Array { datasetIndex :: Int, index :: Int } -> Effect UnitProgrammatically set the active elements (e.g. to highlight a data
point from outside a hover handler). Each entry must specify a
datasetIndex and index. Call updateChart to apply.
#isPluginEnabled Source
isPluginEnabled :: ChartInstance -> String -> Effect BooleanCheck whether a Chart.js plugin is currently enabled by its plugin id
(the string returned by the plugin's id field). Useful for
conditional behavior in custom plugins.
#getSortedVisibleDatasetMetas Source
getSortedVisibleDatasetMetas :: ChartInstance -> Effect (Array Foreign)Get the sorted list of visible dataset metadata objects. Each entry
is the same opaque Foreign shape returned by getDatasetMeta,
filtered to only the visible datasets and sorted in draw order.
#getVisibleDatasetCount Source
getVisibleDatasetCount :: ChartInstance -> Effect IntReturn the number of currently visible datasets.
#chartRegister Source
chartRegister :: Array Foreign -> Effect UnitRegister one or more Chart.js components (controllers, plugins,
scales, or elements) globally. Each item must be a JS object the
Chart.js registry recognizes — typically a controller class with an
id field. After registration, the component is available to all
subsequent chart instances in this process. Pair with CustomType
from Chartjs.Types (#27) to use a registered controller as a
chart type.
The PS argument is Array Foreign because Chart.js components are
arbitrary JS objects (and registering plugins / controllers / scales
/ elements works the same way at the API level). Build the
components in JS or via unsafeToForeign from PS.
#chartUnregister Source
chartUnregister :: Array Foreign -> Effect UnitReverse chartRegister. Removes the given components from the
global registry. Mostly useful for tests; production code rarely
needs to unregister.
#getChartByElement Source
getChartByElement :: HTMLElement -> Effect (Maybe ChartInstance)Look up an existing chart instance by its canvas element. Returns
Nothing if no chart is currently bound to the canvas. Useful for
accessing a chart from outside the code that created it (e.g. from
a separate event handler that has only the DOM reference).
#getChartByCanvasId Source
getChartByCanvasId :: String -> Effect (Maybe ChartInstance)Look up an existing chart instance by canvas element id (the id
attribute of the underlying <canvas>). Returns Nothing if no
matching canvas exists or no chart is bound to it.
#chartRegistry Source
chartRegistry :: Effect ForeignGet the global Chart.js registry as an opaque Foreign value.
The registry exposes typed sub-registries (controllers, plugins,
scales, elements) plus generic add / remove methods. Use
this for advanced registration scenarios — registering a custom
interaction mode (via Chart.helpers.registry) or a custom tooltip
positioner, both of which the typed PureScript API does not yet
model. Consumers using this should reference Chart.js's registry
API docs directly.
#isPointInArea Source
isPointInArea :: ChartInstance -> { x :: Number, y :: Number } -> Effect BooleanCheck whether a pixel coordinate falls inside the chart area (the plotting rectangle, excluding axes/legends/padding). Useful for custom event handlers that need to distinguish clicks inside the chart from clicks on surrounding chrome.
#getChartContext Source
getChartContext :: ChartInstance -> Effect ForeignReturn the chart's context object — { chart, type }. The
chart field is the Chart.js instance itself; type is a string
like "chart". Primarily used inside custom plugins that need to
pass context down. Returned as Foreign because the shape is
self-referential.
#notifyPlugins Source
notifyPlugins :: ChartInstance -> String -> Foreign -> Effect BooleanNotify all registered plugins of a lifecycle hook. hook is the
event name (e.g. "beforeDraw", "afterUpdate"). args is an
arbitrary JS object passed to each plugin's handler — build via
unsafeToForeign. Returns false if any plugin returned false
to cancel the hook, true otherwise. Rarely needed outside
custom plugin authoring.