如何将 Donut Chart JS 放入 Bootstrap 列?
How to fit Doughnut Chart JS into Bootstrap column?
我希望 Chart.js 中的甜甜圈图片适合我的 bootstrap 专栏。我还希望此列具有特定大小(实际上,我更关心高度)。问题是 canvas div 的大小实际上比我的专栏 div 大,所有图表都相互重叠。理想情况下,我希望所有 4 个图表都与行的右侧对齐,并且从右侧有一点 space(例如 10px 的边距)。
我试过在 div 和 canvas 列上使用 margin/padding 设置。想不通...
这里是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);
您可以添加一些 CSS 以正确设置 Chart.js canvas
和 div
容器的样式:
canvas {
width: 100%;
height: 100%;
display: block;
}
div.chart-container {
padding: 1px;
}
检查此 fiddle 更新:http://jsfiddle.net/beaver71/ht7uevay/
我希望 Chart.js 中的甜甜圈图片适合我的 bootstrap 专栏。我还希望此列具有特定大小(实际上,我更关心高度)。问题是 canvas div 的大小实际上比我的专栏 div 大,所有图表都相互重叠。理想情况下,我希望所有 4 个图表都与行的右侧对齐,并且从右侧有一点 space(例如 10px 的边距)。
我试过在 div 和 canvas 列上使用 margin/padding 设置。想不通...
这里是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);
您可以添加一些 CSS 以正确设置 Chart.js canvas
和 div
容器的样式:
canvas {
width: 100%;
height: 100%;
display: block;
}
div.chart-container {
padding: 1px;
}
检查此 fiddle 更新:http://jsfiddle.net/beaver71/ht7uevay/