HighCharts - Environment Setup
Highcharts - Environment Setup
Section titled “Highcharts - Environment Setup”Setting up Highcharts in your web project is straightforward. Highcharts is a standalone JavaScript library, meaning it no longer requires jQuery or any other external framework to function.
You can include Highcharts in your project using several methods:
Method 1: Using the Official CDN
Section titled “Method 1: Using the Official CDN”The Content Delivery Network (CDN) is the quickest way to get started. Highcharts hosts its files on servers distributed globally, ensuring fast loading times for your users.
1. Add the Highcharts Core Library:
Section titled “1. Add the Highcharts Core Library:”Include the main Highcharts script in the <head> or before the closing </body> tag of your HTML file:
<!-- Always check code.highcharts.com for the latest version --><script src="https://code.highcharts.com/highcharts.js"></script>2. Add Optional Modules:
Section titled “2. Add Optional Modules:”Many features like exporting, accessibility enhancements, or specific chart types (e.g., heatmap, treemap) are available as separate modules. Include them after the core library:
<!-- Example: Adding Exporting and Accessibility modules --><script src="https://code.highcharts.com/modules/exporting.js"></script><script src="https://code.highcharts.com/modules/accessibility.js"></script>
<!-- Example: Adding a specific chart type module --><script src="https://code.highcharts.com/modules/heatmap.js"></script>Basic HTML Structure (CDN Method):
Section titled “Basic HTML Structure (CDN Method):”<!DOCTYPE html><html><head> <meta charset="utf-8"> <title>Highcharts CDN Example</title> <script src="https://code.highcharts.com/highcharts.js"></script> <script src="https://code.highcharts.com/modules/exporting.js"></script> <script src="https://code.highcharts.com/modules/accessibility.js"></script></head><body>
<div id="container" style="width:100%; height:400px;"></div>
<script> document.addEventListener('DOMContentLoaded', function () { const chart = Highcharts.chart('container', { // Your Highcharts configuration object goes here chart: { type: 'bar' // Example chart type }, title: { text: 'My First Highchart' }, series: [{ name: 'Example Series', data: [1, 3, 2, 4] }] }); }); </script>
</body></html>Method 2: Using NPM or Yarn (for Module Bundlers)
Section titled “Method 2: Using NPM or Yarn (for Module Bundlers)”If you are using a build process with tools like Webpack, Rollup, or Parcel, installing Highcharts via a package manager is the recommended approach.
1. Install Highcharts:
Section titled “1. Install Highcharts:”Open your terminal in your project directory and run:
# Using npmnpm install highcharts
# Using yarnyarn add highcharts2. Import Highcharts and Modules:
Section titled “2. Import Highcharts and Modules:”In your JavaScript or TypeScript file, import the Highcharts library and initialize any necessary modules:
// Import Highcharts core libraryimport Highcharts from 'highcharts';
// Import optional modulesimport Exporting from 'highcharts/modules/exporting';import Accessibility from 'highcharts/modules/accessibility';import Heatmap from 'highcharts/modules/heatmap'; // Example chart type module
// Initialize the modulesExporting(Highcharts);Accessibility(Highcharts);Heatmap(Highcharts); // Initialize chart type modules too
// Find your container element (assuming you have <div id="container"></div> in your HTML)const container = document.getElementById('container');
// Check if the container exists before creating the chartif (container) { // Create the chart const chart = Highcharts.chart(container, { // Your Highcharts configuration object goes here chart: { type: 'heatmap' // Example chart type }, title: { text: 'Highcharts NPM Example' }, // ... other configuration options series: [ /* ... series data ... */ ] });} else { console.error('Chart container #container not found.');}Framework Wrappers:
Section titled “Framework Wrappers:”Highcharts also provides official wrappers for popular frameworks like React, Angular, and Vue, simplifying integration. Check the official Highcharts documentation for specific instructions for your framework.
Choose the method that best fits your project structure and development workflow.