简体   繁体   English

使用CSS与CanvasJS图表并排设置多个div

[英]Set multiple div side by side with CanvasJS charts using CSS

Im currerntly using Canvas Js to show different charts. 我立即使用Canvas Js显示不同的图表。 In one of the requirement i would like to show multiple charts side by side. 在一项要求中,我想并排显示多个图表。 These are mostly doughnut charts. 这些大多是甜甜圈图。 However im unable to achieve it. 但是即时通讯无法实现。

JS Fiddle : https://jsfiddle.net/Lqw6a2dq/ JS小提琴: https : //jsfiddle.net/Lqw6a2dq/

Working Fiddle : https://jsfiddle.net/SujitJ/zm0gbp80/8/ 工作小提琴: https : //jsfiddle.net/SujitJ/zm0gbp80/8/

CSS: CSS:

.container{
display: flex;
flex-flow: row wrap;    
justify-content: space-between;
}

Currently in the fiddle i have just included 2 div for charts but the no. 目前在小提琴中,我刚刚将2 div包括在图表中,但没有。 of div would be dynamic hence would need a dynamic solution for the placement of div 的div是动态的,因此需要动态解决div的位置

You can use .appendchild() in a while loop to render the div that you want in a dynamic way. 您可以在while循环中使用.appendchild()以动态方式呈现所需的div。

You first create a variable, where you will tell in which element ( we take the id of the element ) you want to append the divs. 首先创建一个变量,在其中您将要在哪个元素(我们采用元素的id)中告诉您要附加div的变量。

var div = document.getElementById('container');

Then we do a simple while loop. 然后我们做一个简单的while循环。

{
 if(!document.getElementById('chartContainer'+i))
  {
    var ele = document.createElement("div");//we say what element we should create, a div in our case
    ele.setAttribute("id","chartContainer"+i);//add the id to our element
    div.appendChild(ele);     
  }
  i++;
}

https://jsfiddle.net/Lqw6a2dq/1/ https://jsfiddle.net/Lqw6a2dq/1/

Use inspect element to see the two divs. 使用inspect元素查看两个div。 If you want to add more divs, just change the number in while(i<=2) to whatever you need. 如果要添加更多div,只需将while(i<=2)的数字更改为所需的数字即可。

You can use class = "container" to set your classes in row with equal dimensions. 您可以使用class =“ container”在尺寸相等的行中设置类。

 <div class= "container"> <div class="row"> <div class="col-md-4">Chart 1</div> <div class="col-md-4">Chart 2</div> <div class="col-md-4">Chart 3</div> </div> </div> 

If number of charts are different or dynamic, you can choose class col-md-4 at run time using javascript DOM. 如果图表数量不同或动态,则可以在运行时使用JavaScript DOM选择类col-md-4。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM