JavaScript data visualization library for creating interactive charts, graphs, and scientific visualizations
npx @tessl/cli install tessl/npm-plotly-js-dist@3.1.0Plotly.js is a comprehensive JavaScript data visualization library that enables developers to create interactive charts, graphs, and scientific visualizations directly in web browsers. It supports dozens of chart types including statistical charts, 3D graphs, scientific charts, SVG and tile maps, financial charts, and geographic visualizations.
npm install plotly.js-distimport Plotly from 'plotly.js-dist';For specific bundles:
import Plotly from 'plotly.js-dist/lib/core';
import Plotly from 'plotly.js-dist/lib/index-basic';
import Plotly from 'plotly.js-dist/lib/index-cartesian';For CommonJS:
const Plotly = require('plotly.js-dist');import Plotly from 'plotly.js-dist';
// Create a basic scatter plot
const trace = {
x: [1, 2, 3, 4],
y: [10, 11, 12, 13],
type: 'scatter'
};
const data = [trace];
const layout = {
title: 'My Plot'
};
// Create the plot
Plotly.newPlot('myDiv', data, layout);
// Update data
Plotly.restyle('myDiv', {'y': [[15, 16, 17, 18]]}, [0]);
// Update layout
Plotly.relayout('myDiv', {'title': 'Updated Plot'});Plotly.js is built around several key components:
Plotly.register()Primary functions for creating and manipulating plots. These are the main entry points for all plotting operations.
// Create a new plot from scratch
function newPlot(graphDiv, data, layout, config): Promise<HTMLElement>;
// Smart update with automatic diffing
function react(graphDiv, data, layout, config): Promise<HTMLElement>;
// Force complete redraw
function redraw(graphDiv): Promise<HTMLElement>;Functions for updating trace data and layout properties of existing plots without recreating them.
// Update trace properties
function restyle(graphDiv, update, traces): Promise<HTMLElement>;
// Update layout properties
function relayout(graphDiv, update): Promise<HTMLElement>;
// Combined trace and layout update
function update(graphDiv, traceUpdate, layoutUpdate, traces): Promise<HTMLElement>;Functions for adding, removing, and reordering traces in existing plots.
// Add new traces
function addTraces(graphDiv, traces, newIndices): Promise<HTMLElement>;
// Remove traces
function deleteTraces(graphDiv, indices): Promise<HTMLElement>;
// Reorder traces
function moveTraces(graphDiv, currentIndices, newIndices): Promise<HTMLElement>;Functions for efficiently appending or prepending data to existing traces, ideal for real-time data visualization.
// Append data to traces
function extendTraces(graphDiv, update, indices, maxPoints): Promise<HTMLElement>;
// Prepend data to traces
function prependTraces(graphDiv, update, indices, maxPoints): Promise<HTMLElement>;Functions for creating smooth animated transitions between plot states and managing animation frames.
// Animate between states
function animate(graphDiv, frames, animationOpts): Promise<void>;
// Add animation frames
function addFrames(graphDiv, frameList, indices): Promise<HTMLElement>;
// Remove animation frames
function deleteFrames(graphDiv, frameList): Promise<HTMLElement>;48+ trace types covering statistical, scientific, financial, geographic, and 3D visualizations.
// Basic trace types
interface ScatterTrace {
type: 'scatter';
x?: number[];
y?: number[];
mode?: 'lines' | 'markers' | 'text' | 'lines+markers' | 'lines+text' | 'markers+text' | 'lines+markers+text' | 'none';
}
interface BarTrace {
type: 'bar';
x?: string[] | number[];
y?: number[];
orientation?: 'v' | 'h';
}
interface PieTrace {
type: 'pie';
labels?: string[];
values?: number[];
}Comprehensive layout controls for plot appearance, axes, legends, annotations, and interactive elements.
interface Layout {
title?: string | TitleConfig;
xaxis?: AxisConfig;
yaxis?: AxisConfig;
legend?: LegendConfig;
annotations?: AnnotationConfig[];
shapes?: ShapeConfig[];
images?: ImageConfig[];
width?: number;
height?: number;
margin?: MarginConfig;
}Functions for exporting plots as images, validating data, and managing plot lifecycle.
// Export plot as image
function toImage(figure, options): Promise<string>;
// Download plot as image file
function downloadImage(graphDiv, options): Promise<void>;
// Validate plot data and layout
function validate(data, layout): ValidationError[];
// Clean up plot resources
function purge(graphDiv): HTMLElement;Built-in UI components for legends, colorbars, toolbars, and interactive controls.
interface LegendConfig {
bgcolor?: string;
bordercolor?: string;
borderwidth?: number;
font?: FontConfig;
orientation?: 'v' | 'h';
x?: number;
y?: number;
}
interface ColorbarConfig {
title?: string;
titleside?: 'right' | 'top' | 'bottom';
thickness?: number;
len?: number;
}Choose the appropriate bundle for your use case:
interface Config {
displayModeBar?: boolean | 'hover';
modeBarButtons?: string[][];
displaylogo?: boolean;
responsive?: boolean;
editable?: boolean;
scrollZoom?: boolean;
doubleClick?: 'reset' | 'autosize' | 'reset+autosize' | false;
locale?: string;
}// Core data structure for plots
type PlotData = Array<Partial<ScatterTrace | BarTrace | PieTrace | HeatmapTrace | /* ... all other trace types */>>;
// Event data structures
interface PlotlyEvent {
points: PlotlyEventPoint[];
event: MouseEvent;
}
interface PlotlyEventPoint {
data: any;
fullData: any;
curveNumber: number;
pointNumber: number;
x: any;
y: any;
}
// Error types
interface ValidationError {
code: string;
message: string;
path: string;
value: any;
}