如何仅在具有Highcharts的列图中的xAxis上显示特定年份

时间:2015-02-16 08:31:02

标签: highcharts axis-labels column-chart

无法在我的柱形图中仅显示两个选定年份。它总是增加其他年份,图表看起来很糟糕。它应显示“2007”和“2010”,但显示“2006”,“2008”和“2010”。我想改变是件小事,但我无法弄清楚会是什么。

Here is a fiddle以及代码:

感谢任何提示。

    $(function () {
        var chart = new Highcharts.Chart({
            "title": {
                "text": "<span style='font-family: Helvetica'>Emissions of CO2 - from Fossil Fuels - Total (CDIAC)<\/span>",
                "useHTML": true
            },
            "yAxis": {
                "min": 0,
                "title": {
                    "text": "Gigagrams of CO2",
                    "align": "high",
                    "rotation": 0,
                    "y": -20,
                    "offset": -50
                }
            },
            "xAxis": {
                "labels": {
                    "step": 1
                },
                "tickWidth": 0,
                "showFirstLabel": true,
                "showLastLabel": true
            },
            "plotOptions": {
                "series": {
                    "connectNulls": true,
                    "shadow": false,
                    "lineWidth": 2,
                    "marker": {
                        "enabled": false
                    }
                }
            },
            "chart": {
                "renderTo": "container",
                "type": "column",
                "zoomType": "xy"
            },
            "series": [{
                "name": "Bangladesh",
                "data": [
                    [2007, 48506],
                    [2010, 56199]
                ]
            }, {
                "name": "Sri Lanka",
                "data": [
                    [2007, 12467],
                    [2010, 12720]
                ]
            }]
        });

    });

1 个答案:

答案 0 :(得分:2)

我认为你应该使用分类的xAxis。然后你只需要两个小的改动:

  • 设置xAxis.type = 'category'
  • 将年份更改为字符串:2008 - &gt; "2008"

演示:http://fiddle.jshell.net/3bcb8sxw/4/