Home > Web Front-end > JS Tutorial > body text

How to create a column chart using Highcharts

王林
Release: 2023-12-18 13:27:38
Original
932 people have browsed it

How to create a column chart using Highcharts

How to use Highcharts to create a column chart

The column chart is a commonly used form in data visualization, which can clearly show the comparison of data in different categories or time periods. Highcharts is a powerful and easy-to-use data visualization library that provides rich chart types and flexible configuration options. This article will introduce in detail how to use Highcharts to create a column chart and provide specific code examples.

Step 1: Introduce the Highcharts library
First, we need to introduce the Highcharts library into the HTML page. You can download the file and introduce the local path, or use CDN to introduce the Highcharts library. The following is a sample code introduced using CDN:

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

Step 2: Create a chart container
Create a container element in the HTML page to display the column chart. You can use a div element and specify an ID as the unique identifier of the container. The following is a sample code:

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

Step 3: Prepare data
Before creating a column chart, we need to prepare the data to be displayed. Data can be static or dynamically obtained. Here we use a simple static data as an example. The following is sample data:

const data = [
  {name: '类别1', value: 10},
  {name: '类别2', value: 20},
  {name: '类别3', value: 30},
  {name: '类别4', value: 40},
  {name: '类别5', value: 50}
];
Copy after login

Step 4: Configure chart options
When using Highcharts to create a column chart, you can personalize it through configuration options. The following is a sample code:

const options = {
  chart: {
    type: 'column'
  },
  title: {
    text: '柱状图表'
  },
  xAxis: {
    categories: data.map(item => item.name)
  },
  yAxis: {
    title: {
      text: '数值'
    }
  },
  series: [{
    name: '数值',
    data: data.map(item => item.value)
  }]
};
Copy after login

In the above code, we set the type of chart to column chart (column) through configuration options, and specified the title of the chart, the titles of the x-axis and y-axis, and the data series . Among them, the categories configuration item of xAxis is used to set the category of the x-axis coordinate, the title configuration item of yAxis is used to set the y-axis title, and the data configuration item of series is used to set the data of the histogram.

Step 5: Create a chart instance
In the HTML page, we can use JavaScript code to create a Highcharts chart instance and bind it to the specified container. The following is a sample code:

Highcharts.chart('chart-container', options);
Copy after login

In the above code, 'chart-container' is the ID of the container element created previously, and options are the chart options configured previously.

Step 6: View the results
After completing the above steps, refresh the HTML page and you will see the column chart created using Highcharts. The chart will display the corresponding data and settings, including chart title, axis, bar chart, etc.

Summary:
This article details how to use Highcharts to create a column chart and provides specific code examples. By following the above steps, we can easily use the Highcharts library to create and display bar charts. I hope this article is helpful to you and has reference value for further understanding and using the Highcharts library.

The above is the detailed content of How to create a column chart using Highcharts. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!