Create Your First Chart
This guide will walk you through creating a working line chart with amCharts 5. By the end, you’ll have a fully interactive chart with animations.Set up your HTML
Create an HTML file with a container div for your chart:
index.html
The
chartdiv element will be the container for your chart. You can customize its size with CSS.Import amCharts 5 modules
In your JavaScript/TypeScript file, import the required modules:
index.js
We’re importing three modules: the core library, XY chart components, and the Animated theme for smooth transitions.
Create the Root element
Every amCharts 5 visualization starts with a Root element:The Root manages the chart’s rendering, themes, and lifecycle. It connects to the DOM element with ID
chartdiv.Create an XY Chart
Create the chart instance and add it to the root container:This creates an XY chart with mouse wheel zoom enabled on the X axis.
Add axes
Create X and Y axes for your data:We’re using a DateAxis for time-based data on the X axis and a ValueAxis for numeric data on the Y axis.
Create a series
Add a line series to visualize your data:The series defines how data is displayed. Here,
valueYField and valueXField map to properties in your data objects.Add data
Generate sample data and set it on the series:
Replace this with your own data. Each object should have properties matching the
valueXField and valueYField configured in the series.Complete Code Example
Here’s the complete working code:Understanding the Code
Let’s break down the key concepts:Root Element
Root Element
The
Root is the foundation of every amCharts 5 chart. It:- Connects to a DOM element
- Manages themes and rendering
- Handles chart lifecycle and disposal
- Provides shared resources like formatters and colors
Chart Instance
Chart Instance
The chart container organizes axes, series, and other visual elements:
Axes
Axes
Axes define the coordinate system and scale for your data:
- DateAxis: For time-based data
- ValueAxis: For numeric data
- CategoryAxis: For categorical data
Series
Series
Series visualize your data. Each series type renders data differently:
- LineSeries: Connects data points with lines
- ColumnSeries: Displays vertical bars
- PieSeries: Shows proportional slices
Data Format
Data Format
Data is an array of objects with properties matching your field configurations:
Next Steps
Core Concepts
Learn about Root, Sprites, Containers, and more
XY Charts
Explore different series types and axes
Customization
Customize colors, themes, and appearance
Data Loading
Load data from JSON, CSV, and APIs