-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts
Plotly is a powerful JavaScript library used to create interactive, professional looking charts and data visualizations for the web. Unlike simple static charts, Plotly charts are interactive by default. Users can hover, zoom, pan, download images, and explore data directly inside the browser without writing extra code.
In this tutorial, you will learn what Plotly is, why it is widely used, how it works with JavaScript, how to include it in your project, and how to create different types of charts step by step. This chapter is written for beginners, but it also builds a strong base for advanced data visualization work.
Plotly is an open source charting library that allows developers to create interactive graphs using JavaScript. It is commonly used for data visualization, analytics dashboards, reports, and scientific charts.
Plotly supports many chart types, such as:
Line charts
Bar charts
Pie charts
Scatter plots
Area charts
Histograms
Box plots
Heatmaps
3D charts
Because Plotly uses SVG and WebGL internally, the charts are responsive, smooth, and visually appealing.
Plotly is popular because it solves many common visualization problems out of the box.
Some key benefits of Plotly are:
Built-in interactivity like hover tooltips and zoom
Clean and modern chart design
Easy integration with JavaScript
Support for large datasets
Responsive charts that adapt to screen size
Export charts as images
Works well with dashboards and analytics apps
You do not need to manually handle canvas drawing or SVG paths. Plotly manages everything internally.
Plotly works by defining data and layout objects in JavaScript. The data object describes what to draw, and the layout object controls how the chart looks.
At a basic level, you provide:
A container element
Data to plot
Optional layout settings
Plotly then renders the chart inside the container.
The easiest way to use Plotly is via CDN.
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
This single line gives you access to the full Plotly library.
You also need a container where the chart will appear.
<div id="myChart" style="width:100%;max-width:700px;height:400px;"></div>
Let us start with a simple line chart.
let xValues = [1, 2, 3, 4, 5];
let yValues = [10, 15, 13, 17, 20];
let data = [
{
x: xValues,
y: yValues,
type: "scatter"
}
];
Plotly.newPlot("myChart", data);
This code creates a basic interactive line chart. You can hover over points, zoom, and pan without writing any extra logic.
Each chart in Plotly is made of one or more traces. A trace represents a dataset.
Common properties in a trace include:
x and y for data values
type for chart type
mode for markers or lines
name for legend label
marker for styling points
Example:
{
x: [1, 2, 3],
y: [5, 10, 15],
type: "scatter",
mode: "lines+markers",
name: "Sales"
}
Layout controls titles, labels, colors, and spacing.
let layout = {
title: "Monthly Sales",
xaxis: {
title: "Month"
},
yaxis: {
title: "Revenue"
}
};
Plotly.newPlot("myChart", data, layout);
Using layout makes charts more readable and professional.
Bar charts are commonly used for comparisons.
let data = [
{
x: ["Jan", "Feb", "Mar", "Apr"],
y: [30, 20, 40, 35],
type: "bar"
}
];
Plotly.newPlot("myChart", data);
The bars are interactive by default.
Pie charts show proportions.
let data = [
{
labels: ["Chrome", "Firefox", "Edge", "Safari"],
values: [60, 20, 10, 10],
type: "pie"
}
];
Plotly.newPlot("myChart", data);
Hovering on a slice shows percentage and value.
Scatter plots are useful for data analysis.
let data = [
{
x: [5, 7, 9, 11],
y: [10, 14, 12, 18],
mode: "markers",
type: "scatter"
}
];
Plotly.newPlot("myChart", data);
You can also add lines, colors, and sizes.
Plotly allows multiple datasets in the same chart.
let data = [
{
x: [1, 2, 3],
y: [10, 15, 20],
type: "scatter",
name: "Product A"
},
{
x: [1, 2, 3],
y: [8, 12, 18],
type: "scatter",
name: "Product B"
}
];
Plotly.newPlot("myChart", data);
Each trace appears in the legend.
You can customize colors, fonts, and sizes.
let data = [
{
x: [1, 2, 3],
y: [10, 20, 15],
type: "bar",
marker: {
color: "teal"
}
}
];
Layout styling options include background color, font size, and margins.
Plotly charts are responsive by default. To ensure proper resizing:
let config = { responsive: true };
Plotly.newPlot("myChart", data, layout, config);
This is important for mobile friendly dashboards.
Plotly supports real time updates.
Plotly.update("myChart", {
y: [[12, 18, 25]]
});
This is useful for live dashboards and analytics tools.
Beginners often make these mistakes:
Forgetting to include the Plotly script
Using incorrect data structure
Mixing chart types incorrectly
Ignoring layout and labels
Overloading charts with too much data
Understanding the basics helps avoid these issues.
Keep charts simple and readable
Use meaningful titles and labels
Avoid too many colors
Choose the right chart type
Test charts on different screen sizes
Good visualization focuses on clarity.
Plotly is widely used in:
Business dashboards
Financial reports
Analytics platforms
Education tools
Scientific research
Admin panels
Many companies rely on Plotly for interactive reporting.
Compared to basic libraries, Plotly provides:
More interactivity
Better default styling
Advanced chart types
Built-in zoom and export
This makes it ideal for modern applications.
Plotly is a powerful JavaScript library for creating interactive and visually appealing charts. It allows developers to focus on data instead of low level drawing logic. With support for many chart types, responsive design, dynamic updates, and built-in interactivity, Plotly is an excellent choice for dashboards, analytics, and data visualization projects. By understanding how data, layout, and configuration work together, you can build professional charts that enhance user experience and make complex data easy to understand.
Create a basic line chart showing the sales of a product over 6 months. Include both lines and markers.
Create a bar chart comparing the quantities of 4 different fruits. Add custom colors for each bar.
Build a pie chart showing the distribution of website traffic sources (e.g., Direct, Organic, Referral).
Create a scatter plot with 10 data points and customize the marker size and color.
Create a 3D scatter plot with x, y, z values and enable rotation to view from different angles.
Update the y-values of an existing line chart dynamically using Plotly.update().
Add hover text to a bar chart that shows both the category and value.
Combine two traces in a single chart: one line chart and one scatter plot. Include a legend to differentiate them.
Create a stacked bar chart showing sales for two products across 5 months.
Implement a real-time chart where data points are added every second using setInterval() or requestAnimationFrame().
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts
