Search code examples
htmlgoogle-visualizationcarouselslideshow

Bootstrap Carousel: Captions not displaying


I would like to display captions(h3/p tags) on charts in BS carousel. It`s not getting displayed, I have referred w3 schools and made some changes, not working at all.

Below is what I have tried:

<div class="carousel-caption">
        <h3> Test caption1 </h3>
        <p> Test caption2 </p>
         </div>

Complete html:

<!DOCTYPE html>
<html lang="en">
<head>
<style>
  .carousel-indicators li {
    background-color: #BFC9CA !important;
  }
  .carousel-indicators .active {
    background-color: #BFC9CA !important;
  }
  h3
  {
  background-color: gold !important;
  }
  .carousel-inner {
    width: 100%;
    max-height: 500px !important;
}
</style>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css" />
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
   <script type='text/javascript' src='https://www.gstatic.com/charts/loader.js'></script>
    <script type='text/javascript' src='https://www.google.com/jsapi'></script>
    <script type='text/javascript'>google.charts.load('current', { packages: ['corechart'] });</script>
     <script language='JavaScript'>function drawChart(PassCount, FailCount, DivID) { var data = google.visualization.arrayToDataTable([['Status', 'Outcome', { role: 'style' }], ['Passed', PassCount, '#8BC34A'], ['Failed', FailCount, '#ff4c4c']]); var groupData = google.visualization.data.group(data, [{ column: 0, modifier: function () { return 'total' }, type: 'string' }], [{ column: 1, aggregation: google.visualization.data.sum, type: 'number' }]); var formatPercent = new google.visualization.NumberFormat({ pattern: '#,##0.0%' }); var formatShort = new google.visualization.NumberFormat({ pattern: 'short' }); var view = new google.visualization.DataView(data); view.setColumns([0, 1, 2, { calc: function (dt, row) { var amount = formatShort.formatValue(dt.getValue(row, 1)); var percent = formatPercent.formatValue(dt.getValue(row, 1) / groupData.getValue(0, 1)); return amount + ' (' + percent + ')'; }, type: 'string', role: 'annotation' }]); var options = { 'legend': 'none', tooltip: { trigger: 'none' }, 'width': 650, 'height': 400, animation: { duration: 1500, startup: true } }; var chart = new google.visualization.ColumnChart(document.getElementById(DivID)); chart.draw(view, options); } google.charts.setOnLoadCallback(function () { drawChart(70, 5, 'GoogleColChart_0'); }); google.charts.setOnLoadCallback(function () { drawChart(80, 10, 'GoogleColChart_1'); }); google.charts.setOnLoadCallback(function () { drawChart(90, 15, 'GoogleColChart_2'); });</script>
    <link rel='icon' type='Icon.ico' href='Icon.ico' />
</head>
<body>

<div class="container">
  <h2>Carousel Example</h2>
  <div id="myCarousel" class="carousel slide" data-ride="carousel">
    <!-- Indicators -->
    <ol class="carousel-indicators">
      <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
      <li data-target="#myCarousel" data-slide-to="1"></li>
      <li data-target="#myCarousel" data-slide-to="2"></li>
    </ol>

    <!-- Wrapper for slides -->
    <div class="carousel-inner">
      <div align="center" class="item active" id='GoogleColChart_0' style='width:800; height:500;border-style:groove;border-width: 1px;'>
        <script type='text/javascript'>drawChart(70, 5, 'GoogleColChart_0');</script>
        <div class="carousel-caption">
        <h3> Test caption1 </h3>
        <p> Test caption2 </p>
         </div>
      </div>
      <div align="center" class="item" id='GoogleColChart_1' style='width:800; height:500;border-style:groove;border-width: 1px;'>
        <script type='text/javascript'>drawChart(80, 10, 'GoogleColChart_1');</script>
        <div class="carousel-caption">
        <h3> Test caption1 </h3>
        <p> Test caption2 </p>
         </div>
      </div>    
      <div align="center" class="item" id='GoogleColChart_2' style='width:800; height:500;border-style:groove;border-width: 1px;'>
            <script type='text/javascript'>drawChart(90, 15, 'GoogleColChart_2');</script>
    <div class="carousel-caption">
        <h3> Test caption1 </h3>
        <p> Test caption2 </p>
    </div>
      </div>  
    </div>

    <!-- Left and right controls -->
    <a class="left carousel-control" href="#myCarousel" data-slide="prev">
      <span class="glyphicon glyphicon-chevron-left"></span>
      <span class="sr-only">Previous</span>
    </a>
    <a class="right carousel-control" href="#myCarousel" data-slide="next">
      <span class="glyphicon glyphicon-chevron-right"></span>
      <span class="sr-only">Next</span>
    </a>
  </div>
</div>

</body>
</html>

Captions should be displayed on positions mentioned.


Solution

  • The issue on your code is that you are targetting the elements with class class="item active" and this element is being overwritten by your drawChart function.

    A possible solution is to move the ids into an inner div, as shown in my solution:

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <style>
          .carousel-indicators li {
            background-color: #BFC9CA !important;
          }
          .carousel-indicators .active {
            background-color: #BFC9CA !important;
          }
          h3
          {
          background-color: gold !important;
          }
          .carousel-inner {
            width: 100%;
            max-height: 500px !important;
        }
        </style>
        <title>Bootstrap Example</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css" />
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
        <script type='text/javascript' src='https://www.gstatic.com/charts/loader.js'></script>
        <script type='text/javascript' src='https://www.google.com/jsapi'></script>
        <script type='text/javascript'>google.charts.load('current', { packages: ['corechart'] });</script>
        <script language='JavaScript'>function drawChart(PassCount, FailCount, DivID) { var data = google.visualization.arrayToDataTable([['Status', 'Outcome', { role: 'style' }], ['Passed', PassCount, '#8BC34A'], ['Failed', FailCount, '#ff4c4c']]); var groupData = google.visualization.data.group(data, [{ column: 0, modifier: function () { return 'total' }, type: 'string' }], [{ column: 1, aggregation: google.visualization.data.sum, type: 'number' }]); var formatPercent = new google.visualization.NumberFormat({ pattern: '#,##0.0%' }); var formatShort = new google.visualization.NumberFormat({ pattern: 'short' }); var view = new google.visualization.DataView(data); view.setColumns([0, 1, 2, { calc: function (dt, row) { var amount = formatShort.formatValue(dt.getValue(row, 1)); var percent = formatPercent.formatValue(dt.getValue(row, 1) / groupData.getValue(0, 1)); return amount + ' (' + percent + ')'; }, type: 'string', role: 'annotation' }]); var options = { 'legend': 'none', tooltip: { trigger: 'none' }, 'width': 650, 'height': 400, animation: { duration: 1500, startup: true } }; var chart = new google.visualization.ColumnChart(document.getElementById(DivID)); chart.draw(view, options); } google.charts.setOnLoadCallback(function () { drawChart(70, 5, 'GoogleColChart_0'); }); google.charts.setOnLoadCallback(function () { drawChart(80, 10, 'GoogleColChart_1'); }); google.charts.setOnLoadCallback(function () { drawChart(90, 15, 'GoogleColChart_2'); });</script>
        <link rel='icon' type='Icon.ico' href='Icon.ico' />
      </head>
      <body>
        <div class="container">
          <h2>Carousel Example</h2>
          <div id="myCarousel" class="carousel slide" data-ride="carousel">
            <!-- Indicators -->
            <ol class="carousel-indicators">
              <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
              <li data-target="#myCarousel" data-slide-to="1"></li>
              <li data-target="#myCarousel" data-slide-to="2"></li>
            </ol>
    
            <!-- Wrapper for slides -->
            <div class="carousel-inner">
              <div align="center" class="item active">
                <div id='GoogleColChart_0' style='width:800; height:500;border-style:groove;border-width: 1px;'>
                  <script type='text/javascript'>drawChart(70, 5, 'GoogleColChart_0');</script>
                </div>
                <div class="carousel-caption">
                  <h3> Test caption1 </h3>
                  <p> Test caption2 </p>
                </div>
              </div>
              <div align="center" class="item">
                <div id='GoogleColChart_1' style='width:800; height:500;border-style:groove;border-width: 1px;'>
                  <script type='text/javascript'>drawChart(80, 10, 'GoogleColChart_1');</script>
                </div>
                <div class="carousel-caption">
                  <h3> Test caption1 </h3>
                  <p> Test caption2 </p>
                </div>
              </div>
              <div align="center" class="item">
                <div id='GoogleColChart_2' style='width:800; height:500;border-style:groove;border-width: 1px;'>
                  <script type='text/javascript'>drawChart(90, 15, 'GoogleColChart_2');</script>
                </div>
                <div class="carousel-caption">
                  <h3> Test caption1 </h3>
                  <p> Test caption2 </p>
                </div>
              </div>
            </div>
    
            <!-- Left and right controls -->
            <a class="left carousel-control" href="#myCarousel" data-slide="prev">
              <span class="glyphicon glyphicon-chevron-left"></span>
              <span class="sr-only">Previous</span>
            </a>
            <a class="right carousel-control" href="#myCarousel" data-slide="next">
              <span class="glyphicon glyphicon-chevron-right"></span>
              <span class="sr-only">Next</span>
            </a>
          </div>
        </div>
      </body>
    </html>
    

    I would suggest to clean up a bit your code: - Move the calls to drawChart to a script that runs on document ready. - Remove the !important from the CSS.

    Hope it helps!