Home > Web Front-end > JS Tutorial > Data Visualization: How to Create Styled Cryptocurrency Candlesticks with Highcharts

Data Visualization: How to Create Styled Cryptocurrency Candlesticks with Highcharts

Linda Hamilton
Release: 2024-12-31 01:49:09
Original
215 people have browsed it

What is Data Visualization?

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:

  • Highcharts: This is the basic Highcharts module that is required in all charts. It can be used to create simple line, bar and pie charts.
  • Highcharts Stock: This is used for creating general stock and timeline charts for your applications. Some examples include simple Stock charts, Candlesticks & Heikin-Ashi, OHLC. You can also make use of the Stock Tools module which provides a GUI that permits interaction with charts.
  • Highcharts Maps: Highcharts also provides an option to generate schematic maps which allow developers add interactive, customizable maps to their web application. It offers options to either use map collection provided by Highcharts or create custom SVG maps to suit your purpose.
  • Highcharts Gantt: This is a special type of bar chart used for illustrating project schedules.

We will use the Highcharts Stock to create styled candlesticks with oscillators and technical indicators provided by the Stock Tools module.

Creating the Candlestick

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?!

Getting Started

To begin using Highcharts, we must first download Highcharts. Highcharts provides several options to introduce Highcharts into your project. You can choose to either:

  • Download the entire Highcharts library. Depending on your use case, you can also download the Highcharts Stock, Maps, or Gantt libraries.
  • Install Highcharts via NPM and import as modules. These are best for Single Page Applications like React and Vue.
  • Use the Highcharts CDN to access files directly.

We will be using the Highcharts CDN for this article.

The HTML

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>
Copy after login
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>

Copy after login
  • The first parameter, renderTo, is the DOM element or the id of the DOM element to which the chart should render.
  • The second parameter, options, are the options that structure the chart.
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,  
      }
    ]
  });
});
Copy after login

The above code gives us a simple candlestick with basic styling provided by Highcharts.
Data Visualization: How to Create Styled Cryptocurrency Candlesticks with Highcharts

Stock Tools

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
      },
    ],
Copy after login

Here is what we have now:
Data Visualization: How to Create Styled Cryptocurrency Candlesticks with Highcharts

Styling the Chart

Before we can begin styling the chart, we need to understand first the different parts that make up the chart.
Data Visualization: How to Create Styled Cryptocurrency Candlesticks with Highcharts
Highcharts provides two ways of styling charts:

  • Highcharts.CSSObject: This is the default method of styling charts. It builds upon the options object within the stockChart class provided by Highcharts to define the visual appearance of individual SVG elements and HTML elements within the chart.
  • styledMode: boolean: This defaults to false. However, when in styled mode, no presentational attributes are applied to the SVG via the options object. Hence, CSS rules are required to style the chart.

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>
Copy after login
Copy after login

This is what our final chart becomes:
Data Visualization: How to Create Styled Cryptocurrency Candlesticks with Highcharts

Conclusion

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!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template