question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bootstrap + C3; Grid layout doesn't work on load

See original GitHub issue
  • C3 version: 0.6.7
  • D3 version: 5
  • Browser: Firefox ESR
  • OS: Debian 9

I want to make a web page that has four rows of three donut charts each. I’m using Bootstrap with C3.js.

My code looks like this.

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">

	<title>U.S. presidential vote, 1972-2016, by race</title>

	<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
	<link href="css/c3.min.css" rel="stylesheet">
	<style type="text/css">
		.c3-chart-arcs-title {
			font-size: 1.5rem;
		}
	</style>


	<script src="https://d3js.org/d3.v5.min.js"></script>
	<script src="js/c3.min.js"></script>

	<script type="text/javascript">
		var dataDir = "vote-by-race";
		d3.csv(dataDir+"/white.csv").then(function(data){
			var row;
			data.forEach(function(d,i){
				if (i%3===0) {
					row = document.createElement("div");
					row.className = "row";
				}

				var col = document.createElement("div");
				col.className = "col-sm";

				var chartDiv = document.createElement("div");
				chartDiv.id = "chart"+i;

				col.appendChild(chartDiv);
				row.appendChild(col);
				document.getElementById("charts").appendChild(row);

				var chartDataObj = Object.assign({},d);
				delete chartDataObj["Year"];
				var chart = c3.generate({
					data: {
						json: chartDataObj,
						type: "donut",
						colors: {
							Democrat: "#2166ac",
							Republican: "#b2182b",
							Other: "#bbb"
						}
					},
					tooltip: {
						show: false
					},
					legend: {
						show: false
					},
					donut: {
						title: d["Year"],
						label: {
							format: function(value,ratio,id){
								return d3.format('.0%')(value/100);
							}
						}
					},
					bindto: '#'+chartDiv.id
				});
			});
		});
	</script>
</head>
<body>
	<div id="charts" class="container"></div>
</body>
</html>

My CSV…

Year,Democrat,Republican,Other
1972,32,66,2
1976,47,52,1
1980,35,56,9
1984,35,64,1
1988,40,59,1
1992,39,40,21
1996,43,46,11
2000,42,54,4
2004,41,58,1
2008,43,55,2
2012,39,59,2
2016,37,58,5

When I load the page in my browser, I see 12 donut charts in a single vertical column. But when I resize the browser window to its thinnest, then expand it again, I see four rows of three donut charts each.

How do I make the browser show four rows of three charts each when the charts are first loaded?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kt3kcommented, Sep 11, 2018

I think the point is the timing between creating container elements and drawing charts. You need to create all rows and columns before drawing any charts. How about the following?

	<script type="text/javascript">
		var dataDir = "vote-by-race";
		d3.csv(dataDir+"/white.csv").then(function(data){
			var row;
			// Creates container rows and columns
			data.forEach(function(d,i){
				if (i%3===0) {
					row = document.createElement("div");
					row.className = "row";
				}

				var col = document.createElement("div");
				col.className = "col-sm";

				var chartDiv = document.createElement("div");
				chartDiv.id = "chart"+i;

				col.appendChild(chartDiv);
				row.appendChild(col);
				document.getElementById("charts").appendChild(row);
			})

			// Draws charts
			data.forEach(function(d,i){
				var chartDataObj = Object.assign({},d);
				delete chartDataObj["Year"];
				var chart = c3.generate({
				    ...
				});
			});
		});
	</script>

	<div id="charts" class="container"></div>
0reactions
kt3kcommented, Sep 19, 2018

Closing the issue because the rest of the problem doesn’t seem belonging to c3.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bootstrap + C3; Grid layout doesn't work on load
I want to make a web page that has four rows of three donut charts each. I'm using Bootstrap with C3.js. My code...
Read more >
Grid system - Bootstrap
Use our powerful mobile-first flexbox grid to build layouts of all shapes and sizes thanks to a twelve column system, five default responsive...
Read more >
[Best solution]-Bootstrap grid layout is not working?
I don't know where I am missing something, but grid system is not working at all. I am using firefox. Is there any...
Read more >
Bootstrap grid system not working for me. Columns displayed ...
You've used the non-existent CSS class form-group-row on your rows. It should be two separate classes: form-group row . You also don't need ......
Read more >
Bootstrap Grid System - W3Schools
Bootstrap's grid system is responsive, and the columns will re-arrange depending on the screen size: On a big screen it might look better...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found