Chartjs线图超越了面板

时间:2015-05-29 23:55:11

标签: javascript html html5 twitter-bootstrap chart.js

我目前正在使用chartjs图表,我希望能够在引导程序面板中显示这些图表。我已经设法得到图表,但图表似乎稍微超过了面板主体。

<div class="panel panel-default" draggable="true">
        <div class="panel-heading">
            <h3 class="panel-title">
                Panel title
                <span class="glyphicon glyphicon-pencil panel-icons"></span>
                <span class="glyphicon glyphicon-zoom-in panel-icons"></span>
                <span class="glyphicon glyphicon-trash panel-icons"></span>
            </h3>
        </div>
        <div class="panel-body">
            <canvas id="chartjs"></canvas>
        </div>
    </div>

    var data = {
    labels: ["January", "February", "March", "April", "May", "June", "July"],
    datasets: [
        {
            label: "My First dataset",
            fillColor: "rgba(220,220,220,0.2)",
            strokeColor: "rgba(220,220,220,1)",
            pointColor: "rgba(220,220,220,1)",
            pointStrokeColor: "#fff",
            pointHighlightFill: "#fff",
            pointHighlightStroke: "rgba(220,220,220,1)",
            data: [65, 59, 80, 81, 56, 55, 40]
        },
        {
            label: "My Second dataset",
            fillColor: "rgba(151,187,205,0.2)",
            strokeColor: "rgba(151,187,205,1)",
            pointColor: "rgba(151,187,205,1)",
            pointStrokeColor: "#fff",
            pointHighlightFill: "#fff",
            pointHighlightStroke: "rgba(151,187,205,1)",
            data: [28, 48, 40, 19, 86, 27, 90]
        }
    ]
};


function loadChart() {
    var context = $('#chartjs').get(0).getContext("2d");
    window.myLine = new Chart(context).Line(data, {
        responsive: true,
        legendTemplate: "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].strokeColor%>\"></span><%if(datasets[i].label){%><%=datasets[i].label%><%}%></li><%}%></ul>"
    });
}

window.onload = loadChart();

这就是它目前的样子 Chart Image.

我不确定如何在面板范围内获取图表。如果需要更多信息,我很乐意提供更多信息。

1 个答案:

答案 0 :(得分:2)

只需在canvas元素中添加一些padding-right即可。这应该给你足够的空间来使画布适合panel-body

var data = {
  labels: ["January", "February", "March", "April", "May", "June", "July"],
  datasets: [
    {
      label: "My First dataset",
      fillColor: "rgba(220,220,220,0.2)",
      strokeColor: "rgba(220,220,220,1)",
      pointColor: "rgba(220,220,220,1)",
      pointStrokeColor: "#fff",
      pointHighlightFill: "#fff",
      pointHighlightStroke: "rgba(220,220,220,1)",
      data: [65, 59, 80, 81, 56, 55, 40]
    },
    {
      label: "My Second dataset",
      fillColor: "rgba(151,187,205,0.2)",
      strokeColor: "rgba(151,187,205,1)",
      pointColor: "rgba(151,187,205,1)",
      pointStrokeColor: "#fff",
      pointHighlightFill: "#fff",
      pointHighlightStroke: "rgba(151,187,205,1)",
      data: [28, 48, 40, 19, 86, 27, 90]
    }
  ]
};


function loadChart() {
  var context = $('#chartjs').get(0).getContext("2d");
  window.myLine = new Chart(context).Line(data, {
    responsive: true,
    legendTemplate: "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].strokeColor%>\"></span><%if(datasets[i].label){%><%=datasets[i].label%><%}%></li><%}%></ul>"
  });
}

window.onload = loadChart();
canvas {
  padding: 0 30px 0 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js"></script>

<div class="col-sm-6">
  <div class="panel panel-default" draggable="true">
    <div class="panel-heading">
      <h3 class="panel-title">
        Panel title
        <span class="glyphicon glyphicon-pencil panel-icons"></span>
        <span class="glyphicon glyphicon-zoom-in panel-icons"></span>
        <span class="glyphicon glyphicon-trash panel-icons"></span>
      </h3>
    </div>
    <div class="panel-body">
      <canvas id="chartjs"></canvas>
    </div>
  </div>
</div>