官术网_书友最值得收藏!

Introducing bar charts

Bar charts are similar to column charts, except that they are aligned vertically.

In the following example, we will create a basic bar chart to show the book consumption per capita for the year 2014. Let's start with the basic HTML template we have been using so far:

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Highcharts Essentials</title>
</head>
<body>
  
  <div id="book_consumption" style="width: 600px; height: 450px;"></div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="js/highcharts.js"></script>
</body>
</html>

The code for the chart is as follows:

(function() {
  $( '#book_consumption' ).highcharts({
    chart: {
      type: 'bar'
    },
    title: {
      text: 'Average Book Consumption Per Capita'
    },
    subtitle: {
      text: 'Source: Harris Interactive'
    },
    xAxis: {
      categories: ['0', '1-2', '3-5', '6-10', '11-20', '21+']
    },
    yAxis: {
      min:0,
      max: 25,
      tickInterval: 5
    },
    tooltip: {
      enabled: false
    },
    series: [{
      name: '2014',
      data: [16, 17, 18, 13,15, 21]
    }]
  });
})();

Notice the use of min and max in yAxis to define the minimum and maximum values. We have also set an interval of 5 using the tickInterval property.

Since the population is measured in percentage in this particular example, it's appropriate to append a % symbol to the labels on the yAxis component using the formatter method, as shown in the following code:

yAxis: {
  min:0,
  max: 25,
  tickInterval: 5,
  labels: {
        enabled: true,
        formatter: function() {
            return this.value + "%";
        }
    }
}

This will result in the following chart:

We can also enable dataLabels in the plotOptions component to show data point values next to the bars:

plotOptions: {
  series: {
    dataLabels: {
      enabled: true,
      formatter: function() {
        return this.y + '%';
      }
    }
  }
}

We used the formatter method to append a % symbol next to the values. Also, note that we referenced the data point values using this.y inside the formatter.

The data labels will now appear next to the bars, as shown in the following screenshot:

Note

You might also be interested in other properties that are available in the formatter() method. You can find more about them at http://api.highcharts.com/highcharts#tooltip.formatter.

主站蜘蛛池模板: 巴林左旗| 翁牛特旗| 全南县| 密山市| 溧水县| 怀宁县| 新竹市| 六枝特区| 泉州市| 凌源市| 望城县| 昌平区| 通化县| 仙居县| 上高县| 京山县| 贵港市| 保靖县| 娱乐| 梁山县| 丹巴县| 万州区| 江阴市| 宁强县| 宁明县| 禹城市| 连山| 福贡县| 苏州市| 道孚县| 蓬莱市| 海林市| 鄢陵县| 兴安盟| 绥阳县| 邢台县| 阿克苏市| 红桥区| 九龙县| 丹凤县| 星子县|