如何将数据放在一列中

时间:2016-11-01 20:27:26

标签: javascript jquery highcharts

我使用Highcharts.js插件及其所有功能,但我要展示的是例如这些数据:

  

1,5,10,20

这里的问题是它在一列中显示的每个值以及我想要的是在一列中显示所有值,所以我的问题是如何才能像我想要的那样显示我的图表?这是我目前的代码:



$(document).ready(function() {
  $('#xd').click(function() {
    draw();
  });
});

function draw() {
  var myArray = [];
  myArray = [1, 5, 10, 11, 8];

  $('#grafic').highcharts({
    chart: {
      type: 'column'
    },
    title: {
      text: 'Sampled Parts'
    },
    xAxis: {
      categories: ['Data'],
      labels: {
        rotation: -33,
      }
    },
    yAxis: {
      allowDecimals: true,
      min: 0,
      title: {
        text: 'Resultados'
      }
    },
    tooltip: {
      formatter: function() {
        return '<b>' + this.x + '</b><br/>' +
          this.series.name + ': ' + this.y + '<br/>' +
          'Total: ' + this.point.stackTotal;
      }
    },

    legend: {
      reversed: true
    },
    plotOptions: {
      column: {
        stacking: 'normal'
      }
    },
    series: [{
      name: 'My Data',
      data: myArray
    }]
  });
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>






<div id="grafic">
</div>

<button id="xd">Click</button>
&#13;
&#13;
&#13;

如果你看到我的代码,我有5列,我想要的是只有一列中的所有值,但说实话,我不知道我能做什么。

Codepen code

1 个答案:

答案 0 :(得分:1)

如果你想堆叠值,那么你需要创建多个系列(每个系列中只有一个值,我猜)。改变它:

var myArray = [];
myArray = [1, 5, 10, 11, 8];

到此:

series: [{
      name: 'mydata',
      data: [1]
    }, {
      name: 'mydata2',
      data: [5]
    }, {
      name: 'mydata3',
      data: [10]
    }, {
      name: 'mydata4',
      data: [11]
    }, {
      name: 'mydata5',
      data: [8]
    }]

请参阅正在运行的示例

$(document).ready(function() {
  $('#xd').click(function() {
    draw();
  });
});

function draw() {
  $('#grafic').highcharts({
    chart: {
      type: 'column'
    },
    title: {
      text: 'Sampled Parts'
    },
    xAxis: {
      categories: ['Data'],
      labels: {
        rotation: -33,
      }
    },
    yAxis: {
      allowDecimals: true,
      min: 0,
      title: {
        text: 'Resultados'
      }
    },
    tooltip: {
      formatter: function() {
        return '<b>' + this.x + '</b><br/>' +
          this.series.name + ': ' + this.y + '<br/>' +
          'Total: ' + this.point.stackTotal;
      }
    },

    legend: {
      reversed: true
    },
    plotOptions: {
      column: {
        stacking: 'normal'
      }
    },
    //  var myArray = [];
    //  myArray = [1, 5, 10, 11, 8];

    series: [{
      name: 'mydata',
      data: [1]
    }, {
      name: 'mydata2',
      data: [5]
    }, {
      name: 'mydata3',
      data: [10]
    }, {
      name: 'mydata4',
      data: [11]
    }, {
      name: 'mydata5',
      data: [8]
    }]
  });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>

<div id="grafic">
</div>

<button id="xd">Click</button>