如何在每行的表中显示stackbarchart?

问题描述 投票:0回答:1

我有表和5列,其中一列具有StackBarChart。基本上我想做的就是这个。enter image description here有什么方法可以循环显示图表并为每一行显示一个?我指的是此link,但仍然无法正常工作

下面是我的代码

<table class="table">
<tr>
            <th>Name</th>
            <th>Total MD</th>
            <th>Total LR</th>
            <th>Total LB</th>
            <th> Graph</th>
</tr>
@foreach($detail_laka as $laka)
  </tr>
     <td>{{$laka->name}}</td>
     <td>{{$laka->total_md}}</td>
     <td>{{$laka->total_lb}}</td>
     <td>{{$laka->total_lr}}</td>
     <td></td> <!--StackBarChart-->
  </tr>
@endforeach

感谢您的帮助

javascript jquery laravel charts highcharts
1个回答
2
投票

[JSFiddle我想我大部分时间都在工作,您可能需要稍作调整,但是图表应该可以工作。

.bar-chart-bar {
  background-color: #e8e8e8;
  display: block;
  position: relative;
  width: 100%;
  height: 20px;
}

.bar {
  float: left;
  height: 100%;
}

.bar1 {
  background-color: green;
}

.bar2 {
  background-color: yellow;
}

.bar3 {
  background-color: red;
}

.squareRed {
  display: inline-block;
  height: 10px;
  width: 10px;
  background-color: red;
}

.squareGreen {
  display: inline-block;

  height: 10px;
  width: 10px;
  background-color: green;
}

.squareYellow {
  display: inline-block;

  height: 10px;
  width: 10px;
  background-color: yellow;
}

.ib {
  display: inline-block;
}
function getInt(n) {
  var parsed = parseInt(n);
  return isNaN(parsed) ? 0 : parsed;
}

$(function() {
  $("#dTable").dataTable({
    "columns": [{
        "title": "Name"
      },
      {
        "title": "Total MD"
      },
      {
        "title": "Total LR"
      },
      {
        "title": "Total LB"
      },
      {
        "title": "Graph",
        "sortable": false,
        "render": function(data, type, row, meta) {
          return $("<div></div>", {
            "class": "bar-chart-bar"
          }).append(function() {
            var bars = [];
            var total = row.reduce((a, c) => getInt(a) + getInt(c));
            for (var i = 1; i < row.length; i++) {
              bars.push($("<div></div>", {
                "class": "bar " + "bar" + i
              }).css({
                "width": (row[i] / total) * 100 + "%"
              }))
            }
            return bars;
          }).prop("outerHTML")
        }
      }
    ]
  });
});


<table id="dTable" class="table table-striped table-bordered" cellspacing="0" width="100%">
    <tbody>
        @foreach($detail_laka as $laka)
        </tr>
        <td>{{$laka->name}}</td>
        <td>{{$laka->total_md}}</td>
        <td>{{$laka->total_lb}}</td>
        <td>{{$laka->total_lr}}</td>
        <td></td> <!--StackBarChart-->
        </tr>
        @endforeach
    </tbody>
      <tfoot>
    <tr>
      <th colspan="5" style="text-align:right">
        <div class="ib">MD: </div>
        <span class="squareRed"></span>
        <div class="ib">LR: </div>
        <span class="squareGreen"></span>
        <div class="ib">LB: </div>
        <span class="squareYellow"></span>
      </th>
    </tr>
  </tfoot>
</table>
© www.soinside.com 2019 - 2024. All rights reserved.