Home  >  Article  >  Web Front-end  >  How to use area chart to display data in Highcharts

How to use area chart to display data in Highcharts

WBOY
WBOYOriginal
2023-12-16 17:39:38458browse

How to use area chart to display data in Highcharts

How to use area charts to display data in Highcharts requires specific code examples

Area charts are a commonly used data display form that can visually present data. Trends and changes. In Highcharts, we can create and customize area charts with simple code. The following will introduce how to use the Highcharts library to create an area chart and provide specific code examples.

First, we need to introduce the code of the Highcharts library into the web page. You can download the corresponding library file from the Highcharts official website, and then insert the following code into the head tag of the HTML file:

<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>

Next, we need to insert a container into the web page as the display area of ​​the area chart. Insert the following code in the HTML file:

<div id="areaChartContainer"></div>

We can then use JavaScript code to define the data and create the area chart. The following is a simple sample code:

// 定义数据
var data = [
  [1596230400000, 100],
  [1596316800000, 120],
  [1596403200000, 90],
  [1596489600000, 110],
  [1596576000000, 130],
];

// 创建面积图
Highcharts.chart('areaChartContainer', {
  chart: {
    type: 'area'
  },
  title: {
    text: '数据趋势图'
  },
  xAxis: {
    type: 'datetime'
  },
  yAxis: {
    title: {
      text: '数值'
    }
  },
  series: [{
    name: '数据',
    data: data
  }]
});

In the above code, we first define a data array. Each element in the array is an array containing timestamps and values. Then, in the Highcharts configuration object, we specified the chart type as an area chart, set the relevant parameters of the title, x-axis and y-axis, and passed the data to the data field in the series attribute.

Finally, call the Highcharts.chart function to render the area chart into the previously defined container (the div with the id of areaChartContainer).

With the above code, we can display a simple area chart on the web page, which shows the trends and changes in the data. If you need further customization and beautification, you can refer to the methods and properties provided by Highcharts official documentation to personalize the chart.

To summarize, creating an area chart using the Highcharts library is very simple. With just a few lines of code, we can display clear and intuitive data trend charts on web pages. I hope the code examples in this article can help readers better use Highcharts to present data.

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

Statement:
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