如何将Donut Chart JS融入Bootstrap专栏?

时间:2018-01-11 22:09:03

标签: html css twitter-bootstrap chart.js chart.js2

我希望Chart.js中的Donut图形适合我的bootstrap列。我也希望这个列具有特定的大小(实际上,我更关心高度)。问题是canvas div的大小实际上比我的列div大,并且所有图表都相互重叠。理想情况下,我希望所有4个图表都与行的右侧对齐,并且右侧有一点空间(例如,10px的某个边距)。

我试图在列div和画布上使用边距/填充设置。无法弄清楚......

以下是jsfiddle示例

我的HTML:

<div class="row">
  <div class="col-sm-3">
    Some content
  </div>
  <div class="col-sm-3">
    Some other content
  </div>

  <div class="col-sm-2"></div>

  <div class="col-sm-1">
    <div class="chart-container" style="position: relative; width: 200px; height: 100px;">
      <canvas id="chart1" width="200" height="100"></canvas>
    </div>
  </div>
  <div class="col-sm-1">
    <div class="chart-container" style="position: relative; width: 200px; height: 100px;">
      <canvas id="chart2" width="200" height="100"></canvas>
    </div>
  </div>
  <div class="col-sm-1">
    <div class="chart-container" style="position: relative; width: 200px; height: 100px;">
      <canvas id="chart3" width="200" height="100"></canvas>
    </div>
  </div>
  <div class="col-sm-1">
    <div class="chart-container" style="position: relative; width: 200px; height: 100px;">
      <canvas id="chart4" width="200" height="100"></canvas>
    </div>
  </div>
</div>

我的JS:

var myNewChart;
var data = [{
  value: 30,
  color: "#F7464A"
}, {
  value: 50,
  color: "#E2EAE9"
}, {
  value: 100,
  color: "#D4CCC5"
}, {
  value: 40,
  color: "#949FB1"
}, {
  value: 100,
  color: "#4D5360"
}];

var options = {
  animation: true,
  animationEasing: 'easeInOutQuart',
  animationSteps: 80
};

//Get the context of the canvas element we want to select
var ctx1 = document.getElementById("chart1")
  .getContext("2d");
var ctx2 = document.getElementById("chart2")
  .getContext("2d");
var ctx3 = document.getElementById("chart3")
  .getContext("2d");
var ctx4 = document.getElementById("chart4")
  .getContext("2d");

/*******************************************************/
myNewChart1 = new Chart(ctx1).Doughnut(data, options);
myNewChart2 = new Chart(ctx2).Doughnut(data, options);
myNewChart3 = new Chart(ctx3).Doughnut(data, options);
myNewChart4 = new Chart(ctx4).Doughnut(data, options);

1 个答案:

答案 0 :(得分:3)

您可以添加一些CSS来正确设置Chart.js canvasdiv容器的样式:

canvas {
  width: 100%;
  height: 100%;
  display: block;
}

div.chart-container {
  padding: 1px;
}

检查此小提琴已更新:http://jsfiddle.net/beaver71/ht7uevay/