Home Web Front-end JS Tutorial How to use candlestick charts to display data in Highcharts

How to use candlestick charts to display data in Highcharts

Dec 18, 2023 pm 04:42 PM
highcharts display data candlestick chart

How to use candlestick charts to display data in Highcharts

Highcharts is a very popular JavaScript chart library that can display various forms of data. Candlestick Chart is a type of chart specially used to display financial data such as stocks. It can intuitively display information such as opening price, closing price, highest price, lowest price, etc. This article will introduce how to use candlestick charts to display data in Highcharts, and give specific code examples.

1. Preparation

Before using Highcharts, we need to introduce the JavaScript file of Highcharts. It can be introduced through CDN or downloading local files. Here is the CDN method as an example:

<script src="https://cdn.jsdelivr.net/npm/highcharts@9.2.2/highcharts.js"></script>
Copy after login

In addition, in order to facilitate the display of data, an open source JavaScript library Faker.js is used to generate random data. It can also be introduced through CDN:

<script src="https://cdn.jsdelivr.net/npm/Faker.js"></script>
Copy after login

2. Create a container

To display Highcharts charts, you first need to create a container element, usually a div tag, and specify an ID, for example:

<div id="chart-container"></div>
Copy after login

Here we set the ID of the chart container to "chart-container".

3. Set up data

Here, we need to generate some fake data to display the candlestick chart. We can use the Faker.js library to generate random data and then format it into the data format required by Highcharts. The following is an example of generating 100 data points:

let data = [];
for (let i = 0; i < 100; i++) {
  let open = parseFloat(Faker.Finance.amount(1000, 5000));
  let high = parseFloat(Faker.Finance.amount(open, open * 1.1));
  let low = parseFloat(Faker.Finance.amount(open * 0.9, open));
  let close = parseFloat(Faker.Finance.amount(low, high));
  data.push([i, open, high, low, close]);
}
Copy after login

The above code will generate an array containing 100 data points. Each data point is an array containing five values, respectively. Index, opening price, high price, low price and closing price.

4. Create a candlestick chart

After we have the data, we can create a basic candlestick chart. The following is a simple sample code:

Highcharts.chart('chart-container', {
  chart: {
    type: 'candlestick'
  },
  title: {
    text: '股票数据'
  },
  series: [{
    data: data
  }]
});
Copy after login

The above code will create a candlestick chart in the "chart-container" container, and the data uses previously generated random data. Among them:

  • type: 'candlestick' specifies the chart type as candlestick chart.
  • title: { text: 'Stock Data' } Set the chart title to "Stock Data".
  • series: [{ data: data }]Specifies the data series and sets the previously generated random data as the data series.

5. Customized style

The default candlestick chart style may not meet our needs, and we need to customize the style. Here is a slightly more complex sample code that allows for richer styling effects:

Highcharts.chart('chart-container', {
  chart: {
    type: 'candlestick'
  },
  title: {
    text: '股票数据'
  },
  xAxis: {
    labels: {
      formatter: function () {
        return data[this.value][0];
      }
    }
  },
  yAxis: {
    opposite: false,
    labels: {
      formatter: function () {
        return '$' + this.value;
      }
    }
  },
  tooltip: {
    pointFormat: '<span style="font-weight:bold">{series.name}</span><br>' +
        '开盘价: <b>${point.open}</b><br/>' +
        '最高价: <b>${point.high}</b><br/>' +
        '最低价: <b>${point.low}</b><br/>' +
        '收盘价: <b>${point.close}</b><br/>'
  },
  plotOptions: {
    candlestick: {
      color: '#0f0',
      upColor: '#f00',
      lineColor: '#000',
      upLineColor: '#000',
      lineWidth: 1
    }
  },
  series: [{
    name: '股票价格',
    data: data
  }]
});
Copy after login

In the above code, we can see that the following has been added:

  • xAxis.labels.formatterProperty sets the label of the X-axis to the data index.
  • yAxis.labels.formatterThe property sets the Y-axis label to a dollar sign, which can also be modified according to actual needs.
  • tooltip.pointFormatThe property adjusts the format of the prompt box, including information such as the opening price, the highest price, the lowest price, and the closing price.
  • plotOptions.candlestickThe property is used to set the style of the candlestick chart. Here we specify the rising and falling colors and border colors, and set the line width to 1.

6. Summary

Using Highcharts to display candlestick charts is not very complicated. First we need to prepare the data, then create a container element to specify the ID, then create a Highcharts instance and pass in the container ID, and finally set the data, style, title and other attributes. Of course, the specific style settings need to be adjusted according to the actual situation. The following is the complete sample code:




  
  
  <script src="https://cdn.jsdelivr.net/npm/highcharts@9.2.2/highcharts.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/Faker.js"></script>
  Highcharts展示烛台图示例


  <div id="chart-container"></div>

  <script>
    let data = [];
    for (let i = 0; i < 100; i++) {
      let open = parseFloat(Faker.Finance.amount(1000, 5000));
      let high = parseFloat(Faker.Finance.amount(open, open * 1.1));
      let low = parseFloat(Faker.Finance.amount(open * 0.9, open));
      let close = parseFloat(Faker.Finance.amount(low, high));
      data.push([i, open, high, low, close]);
    }

    Highcharts.chart('chart-container', {
      chart: {
        type: 'candlestick'
      },
      title: {
        text: '股票数据'
      },
      xAxis: {
        labels: {
          formatter: function () {
            return data[this.value][0];
          }
        }
      },
      yAxis: {
        opposite: false,
        labels: {
          formatter: function () {
            return '$' + this.value;
          }
        }
      },
      tooltip: {
        pointFormat: '<span style="font-weight:bold">{series.name}</span><br>' +
            '开盘价: <b>${point.open}</b><br/>' +
            '最高价: <b>${point.high}</b><br/>' +
            '最低价: <b>${point.low}</b><br/>' +
            '收盘价: <b>${point.close}</b><br/>'
      },
      plotOptions: {
        candlestick: {
          color: '#0f0',
          upColor: '#f00',
          lineColor: '#000',
          upLineColor: '#000',
          lineWidth: 1
        }
      },
      series: [{
        name: '股票价格',
        data: data
      }]
    });
  </script>

Copy after login

The above is the entire process of using Highcharts to display candlestick charts. Through the above sample code we can learn several major areas:

  • Import Highcharts JavaScript document.
  • Import the Faker.js JavaScript file.
  • Create a container element with id.
  • Use Faker.js to generate random data.
  • Create a new Highcharts instance and pass in the container element ID.
  • Define the type of candlestick chart you want to display in the Highcharts instance.
  • Pass the generated data into the Highcharts instance as the Data property.
  • Format data for X-axis labels and Y-axis labels: for example, setting dollar signs, etc.
  • Set the style-related properties of the candlestick chart in the Highcharts instance: such as color, line width, etc.
  • Set the tooltip format in the Highcharts instance and set the prompt content in super detail.
  • Set a title to describe the contents of the chart.

The above code should be easy to understand if you understand the basics of Highcharts and the basic syntax of JavaScript. For beginners, they can initially master the related skills of Highcharts. For senior technical engineers, reading this article will also benefit a lot. The technical necessity of front-end chart visualization is still rising. I believe this article can also help you. Provide a solid foundation for moving forward.

The above is the detailed content of How to use candlestick charts to display data in Highcharts. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use Sankey chart to display data in Highcharts How to use Sankey chart to display data in Highcharts Dec 17, 2023 pm 04:41 PM

How to use Sankey diagram to display data in Highcharts Sankey diagram (SankeyDiagram) is a chart type used to visualize complex processes such as flow, energy, and funds. It can clearly display the relationship and flow between various nodes, and can help us better understand and analyze data. In this article, we will introduce how to use Highcharts to create and customize a Sankey chart, with specific code examples. First, we need to load the Highcharts library and Sank

How to use dynamic data in Highcharts to display real-time data How to use dynamic data in Highcharts to display real-time data Dec 17, 2023 pm 06:57 PM

How to use dynamic data in Highcharts to display real-time data. With the advent of the big data era, the display of real-time data has become more and more important. Highcharts, as a popular charting library, provides rich functions and customizability, allowing us to flexibly display real-time data. This article will introduce how to use dynamic data in Highcharts to display real-time data, and give specific code examples. First, we need to prepare a data source that can provide real-time data. In this article, I

How to create a Gantt chart using Highcharts How to create a Gantt chart using Highcharts Dec 17, 2023 pm 07:23 PM

How to use Highcharts to create a Gantt chart requires specific code examples. Introduction: The Gantt chart is a chart form commonly used to display project progress and time management. It can visually display the start time, end time and progress of the task. Highcharts is a powerful JavaScript chart library that provides rich chart types and flexible configuration options. This article will introduce how to use Highcharts to create a Gantt chart and give specific code examples. 1. Highchart

How to use stacked charts to display data in Highcharts How to use stacked charts to display data in Highcharts Dec 18, 2023 pm 05:56 PM

How to use stacked charts to display data in Highcharts Stacked charts are a common way of visualizing data, which can display the sum of multiple data series at the same time and display the contribution of each data series in the form of a bar chart. Highcharts is a powerful JavaScript library that provides a rich variety of charts and flexible configuration options to meet various data visualization needs. In this article, we will introduce how to use Highcharts to create a stacked chart and provide

How to create a map heat map using Highcharts How to create a map heat map using Highcharts Dec 17, 2023 pm 04:06 PM

How to use Highcharts to create a map heat map requires specific code examples. A heat map is a visual data display method that can represent the data distribution in each area through different color shades. In the field of data visualization, Highcharts is a very popular JavaScript library that provides rich chart types and interactive functions. This article will introduce how to use Highcharts to create a map heat map and provide specific code examples. First, we need to prepare some data

How to use scatter plots to display data in Highcharts How to use scatter plots to display data in Highcharts Dec 17, 2023 pm 10:30 PM

How to use scatter plots to display data in Highcharts Preface Highcharts is an open source JavaScript chart library that provides a variety of chart types and powerful customization functions. Among them, scatter plot is a commonly used data visualization method that can show the relationship between two variables and the distribution of variables. This article will introduce how to use scatter plots to display data in Highcharts and provide specific code examples. Step 1: Introduce the Highcharts library

How to create custom charts with Highcharts How to create custom charts with Highcharts Dec 17, 2023 pm 10:39 PM

How to create custom charts with Highcharts Highcharts is a powerful and easy-to-use JavaScript chart library that helps developers create various types of interactive and customizable charts. In order to create custom charts using Highcharts, we need to master some basic concepts and techniques. This article walks through some important steps and provides specific code examples. Step 1: Introduce the Highcharts library First, we need to

How to use maps to display data in Highcharts How to use maps to display data in Highcharts Dec 18, 2023 pm 04:06 PM

How to use maps to display data in Highcharts Introduction: In the field of data visualization, using maps to display data is a common and intuitive way. Highcharts is a powerful JavaScript charting library that provides rich functionality and flexible configuration options. This article will introduce how to use maps to display data in Highcharts and provide specific code examples. Introducing map data: When using a map, you first need to prepare map data. High

See all articles