如何将甜甜圈图表 JS 放入引导列中



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

我尝试在列div 和画布上玩弄边距/填充设置。想不通...

这是jsfiddle示例

我的网页:

<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);

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

canvas {
  width: 100%;
  height: 100%;
  display: block;
}
div.chart-container {
  padding: 1px;
}

检查这个小提琴更新:http://jsfiddle.net/beaver71/ht7uevay/

相关内容

  • 没有找到相关文章

最新更新