Data visualization is the practice of representing data/information in pictorial or graphical formats. It is a means by which large data sets or metrics are converted into visual elements like maps, graphs, and charts, which are much more appealing to an end-user.
The JavaScript ecosystem currently has several reliable, first-rate libraries for data visualization. Some of which include D3.js, Highcharts, Charts.js, Rechart, etc. However, in this article, we will be using Highcharts to create our charts.
Highcharts is a JavaScript library for creating SVG-based, responsive, and interactive charts for web and mobile. It provides deep customization of charts via JavaScript or CSS. Highcharts offers four product categories for creating charts. These include:
We will use the Highcharts Stock to create styled candlesticks with oscillators and technical indicators provided by the Stock Tools module.
A candlestick chart( or Japanese candlestick chart) is a style of financial chart used by traders to determine possible price movements of a stock, security, or currency based on previous patterns. It makes use of key price points/ OHLC(open, high, low, close) values taken at regular intervals for a specified period of time.
Not to be confused with the typical candlestick chart is the Heikin-Ashi('average bar') chart. Although identical to the candlestick chart, it is mostly used in conjunction with the candlestick as it helps make candlestick chart trends easier to analyze. Hence, making it more readable.
The Highcharts API provides options for creating both candlestick charts and Heikin-Ashi charts. This article focuses on candlestick charts; however, I will point out the tradeoffs required for creating an Heikin-Ashi chart along the way. Let's get our hands dirty, shall we?!
To begin using Highcharts, we must first download Highcharts. Highcharts provides several options to introduce Highcharts into your project. You can choose to either:
We will be using the Highcharts CDN for this article.
The bulk of the HTML contains script tags used to load the Highcharts CDN. The first three are required modules for all stock charts created with Highcharts.
<script src="https://code.highcharts.com/stock/highstock.js"></script> <script src="https://code.highcharts.com/stock/modules/data.js"></script> <script src="https://code.highcharts.com/stock/modules/exporting.js"></script>
Unlike candlestick charts, if you need to create a Heikin-Ashi chart, you will need to bring in the module separately as below:
<script src="https://code.highcharts.com/stock/modules/heikinashi.js"></script>
The final CDN we need to bring into our application is the Stock Tools module. This enables us to make use of technical indicators. The Stock Tools module must be loaded last so it can pick up all available modules from above.
<script src="https://code.highcharts.com/stock/indicators/indicators-all.js"></script>
Rather than loading all technical indicators from the Stock Tools module, you can also load specific indicators depending on your needs:
<script src="https://code.highcharts.com/indicators/indicators.js"></script> <script src="https://code.highcharts.com/indicators/rsi.js"></script> <script src="https://code.highcharts.com/indicators/ema.js"></script> <script src="https://code.highcharts.com/indicators/macd.js"></script>
Lastly, we need to create an HTML element to hold our chart that we can reference from the JavaScript:
<div> <h3> The JavaScript </h3> <p><strong>Bringing in our Data</strong><br> The first item on our itinerary is to bring in the data we will be plotting. Highcharts provides a .getJSON method similar to that of jQuery, which is used for making HTTP requests. It also provides a stockChart class for creating the chart. The stockChart class takes in two parameters:</p>
Highcharts.getJSON('https://api.coingecko.com/api/v3/coins/ethereum/ohlc?vs_currency=usd&days=365', function (candlestick) { // create the chart Highcharts.stockChart('container', { title: { text: 'Untitled Masterpiece' }, series: [ { type: "candlestick", //heikinashi for Heikin-Ashi chart name: "Ethereum", //chart name id: "eth", // chart id, useful when adding indicators and oscillators data: candlestick, //data gotten from the API call above }, ], yAxis: [ { height: "100%", // height of the candlestick chart visible: true, } ] }); });
The above code gives us a simple candlestick with basic styling provided by Highcharts.
The Highcharts Stock Tools is an optional feature in Highcharts. You can either enable the entire Stock Tools Graphical User Interface(GUI), which allows users to add indicators and oscillators based on their needs, or add specific Stock Tools to your web app via Javascript.
We will add an indicator(Acceleration bands) and an oscillator(awesome oscillator) to our chart. To do this, we need to edit both the series and yAxis objects above:
series: [ { type: "candlestick", name: "Ethereum", id: "eth", // chart id, useful when adding indicators and oscillators data: data, }, { type: "abands", //acceleration bands indicator id: "overlay", // overlays use the same scale and are plotted on the same axes as the main series. linkedTo: "eth", //targets the id of the data series that it points to yAxis: 0, // the index of yAxis the particular series connects to }, { type: "ao", // awesome oscillator id: "oscillator", // oscillators requires additional yAxis be created due to different data extremes. linkedTo: "eth", //targets the id of the data series that it points to yAxis: 1, // the index of yAxis the particular series connects to }, ], yAxis: [ { //index 0 height: "80%", //height of main series 80% resize: { enabled: true, // allow resize of chart heights }, }, { //index 1 top: "80%", // oscillator series to begin at 80% height: "20%", //height of oscillator series }, ],
Here is what we have now:
Before we can begin styling the chart, we need to understand first the different parts that make up the chart.
Highcharts provides two ways of styling charts:
We will be making use of the Highcharts default styling for this article. Therefore, within the options object:
<script src="https://code.highcharts.com/stock/highstock.js"></script> <script src="https://code.highcharts.com/stock/modules/data.js"></script> <script src="https://code.highcharts.com/stock/modules/exporting.js"></script>
This is what our final chart becomes:
Creating styled cryptocurrency candlesticks with Highcharts allows you to transform raw data into visually compelling and actionable insights. By leveraging Highcharts’ flexibility, you can customize candlestick charts to align with your branding, enhance user experience, and effectively communicate market trends. Whether you're building a financial dashboard or enhancing a trading platform, the ability to design and implement tailored visualizations is a critical skill in today’s data-driven landscape.
With the steps outlined in this guide, you now have a foundation for working with Highcharts to create dynamic candlestick charts. Explore additional customizations and experiment with Highcharts’ extensive API to bring your cryptocurrency visualizations to the next level.
The above is the detailed content of Data Visualization: How to Create Styled Cryptocurrency Candlesticks with Highcharts. For more information, please follow other related articles on the PHP Chinese website!