如何在每个单元格中动态显示图表?

时间:2016-08-09 07:05:07

标签: javascript html highcharts

每次动态需要生成id。 我做了,但我把硬代码数据。 我一次又一次地使用不同的id调用此代码,我想使用循环,所以任何建议?

你能建议我怎么做或任何其他方法吗?

JS:

$(function() {
  $('#container').highcharts({
    chart: {
      type: 'bar'
    },
    title: {
      text: ''
    },
    xAxis: {
      //categories: ['Work Center 1']
    },
    yAxis: {
      min: 0,
      title: {
        text: ''
      }
    },
    legend: {
      reversed: true
    },
    plotOptions: {
      series: {
        stacking: 'percent'
      }
    },
    series: [{
      name: 'Booked Order',
      data: [5]
    }, {
      name: 'Urgent Order',
      data: [2]
    }, {
      name: 'Available',
      data: [3]
    }]
  });
});

HTML:

<tbody>
  <tr>
    <td>Month1</td>
    <td>
      <div id="container" style="width: 250px; height: 200px; margin: 0 auto"></div>
    </td>
    <td>
      <div id="container1" style="width: 250px; height: 200px; margin: 0 auto; "></div>
    </td>
    <td>
      <div id="container2" style="width: 250px; height: 200px; margin: 0 auto; "></div>
    </td>
    <td>
      <div id="container3" style="width: 250px; height: 200px; margin: 0 auto; "></div>
    </td>
    <td>
      <div id="container4" style="width: 250px; height: 200px; margin: 0 auto; "></div>
    </td>
  </tr>
</tbody>

1 个答案:

答案 0 :(得分:1)

这样的东西?

$(function() {
  var chartData = {
    chart: {
      type: 'bar'
    },
    title: {
      text: ''
    },
    xAxis: {
      //categories: ['Work Center 1']
    },
    yAxis: {
      min: 0,
      title: {
        text: ''
      }
    },
    legend: {
      reversed: true
    },
    plotOptions: {
      series: {
        stacking: 'percent'
      }
    },
    series: [{
      name: 'Booked Order',
      data: [5]
    }, {
      name: 'Urgent Order',
      data: [2]
    }, {
      name: 'Available',
      data: [3]
    }]
  };

  $('div[id^=container]').each(function() {
    $(this).highcharts(chartData);
  });;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highcharts/4.2.6/highcharts.js"></script>
<tbody>
  <tr>
    <td>Month1</td>
    <td>
      <div id="container" style="width: 250px; height: 200px; margin: 0 auto"></div>
    </td>
    <td>
      <div id="container1" style="width: 250px; height: 200px; margin: 0 auto; "></div>
    </td>
    <td>
      <div id="container2" style="width: 250px; height: 200px; margin: 0 auto; "></div>
    </td>
    <td>
      <div id="container3" style="width: 250px; height: 200px; margin: 0 auto; "></div>
    </td>
    <td>
      <div id="container4" style="width: 250px; height: 200px; margin: 0 auto; "></div>
    </td>
  </tr>
</tbody>