简体   繁体   English

两个Div彼此相邻,其中ID包含图像

[英]Two Div next to each other where IDs contain an image

Normally, float works for me, where I can just float left or right. 通常,浮动对我有用,在这里我可以向左或向右浮动。 However, this time I am working with Google Charts and this code uses getElementByID. 但是,这次我正在使用Google Charts,并且此代码使用getElementByID。 Then each div ID contains an image of a chart. 然后,每个div ID都包含一个图表图像。 My goal is to place two charts next to each other, but what ends up happening is that the chart that is supposed to be on the left side just disappears. 我的目标是将两个图表彼此相邻放置,但最终发生的事情是应该位于左侧的图表消失了。

So far I have tried using float in style and class for div. 到目前为止,我已经尝试在div的样式和类中使用float。 Neither seems to be working. 似乎都没有工作。 I am not sure if I am doing it correctly, however I have gotten float to work before in other documents, so I am going to assume I need to use a different approach for this situation. 我不确定我是否做得正确,但是在其他文档中,我之前已经浮空工作了,因此我将假定我需要针对这种情况使用其他方法。

I can paste code if needed. 如果需要,我可以粘贴代码。 What is the solution for placing two divs next to each other when both IDs contain an Google Chart? 两个ID都包含Google图表时,两个div相邻放置的解决方案是什么?

EDIT : Here is a paste of my code http://pastebin.com/frhhEDYt 编辑:这是我的代码的粘贴http://pastebin.com/frhhEDYt

Try putting a parent container with the two charts in it, specify the width and height of each charts both in JS and CSS , set each charts as block (to ensure cross browser as well), float them, clear the next rows. 尝试在其中放置包含两个图表的父容器,在JSCSS中指定每个图表的widthheight ,将每个图表设置为block (以确保也可以跨浏览器),将其float ,清除下一行。 Don't forget to set a specified width for the container enough to encase the charts. 不要忘记为容器设置足够的width来装入图表。 If its 100% , it will follow the window's / parent width, or if the value is lower than the width of the charts in total, one chart will fall below. 如果其100% ,它将遵循窗口的/父级宽度,或者如果该值小于图表的总宽度,则一张图表将低于该宽度。

Here's my jsfiddle for it: http://jsfiddle.net/JSwth/ 这是我的jsfiddle: http : //jsfiddle.net/JSwth/

Hope it helps. 希望能帮助到你。

Perhaps lay them out with a table, to start, if floats aren't working? 如果花车不起作用,也许用一张桌子摆放它们,然后开始吧? That would at least make sure that it's the style causing the second chart to disappear, not Google Charts API. 至少可以确保它是导致第二张图表消失的样式,而不是Google Charts API。

This thread may help, too (this addresses Google Charts usage with two charts directly): How to add two Google charts on the one page? 此线程也可能有帮助(这直接解决了两个图表在Google Charts中的使用): 如何在一页上添加两个Google图表?

#chart_div{
  width:250px;
  float:left;
}  
#chart_div2{
  width:250px;
  float:left;
} 

<script type="text/javascript" src="https://www.google.com/jsapi">
</script>

<div id="chart_div">
</div>
<div id="chart_div2">
</div>
<div id="chart_div3">
</div>

// Load the Visualization API and the piechart package.
google.load('visualization', '1.0', {
    'packages': ['corechart']
});

// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);

// Callback that creates and populates a data table,
// instantiates the pie chart, passes in the data and
// draws it.
function drawChart() {

    // Create the data table.
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Topping');
    data.addColumn('number', 'Slices');
    data.addRows([
        ['Mushrooms', 3],
        ['Onions', 1],
        ['Olives', 1],
        ['Zucchini', 1],
        ['Pepperoni', 2]
    ]);
    // Create the data table.
    var data2 = new google.visualization.DataTable();
    data2.addColumn('string', 'Topping');
    data2.addColumn('number', 'Slices');
    data2.addRows([
        ['Mushrooms', 3],
        ['Onions', 1],
        ['Olives', 15],
        ['Zucchini', 1],
        ['Pepperoni', 2]
    ]);

    var data3 = new google.visualization.DataTable();
    data3.addColumn('string', 'Year');
    data3.addColumn('number', 'Sales');
    data3.addColumn('number', 'Expenses');
    data3.addRows([
        ['2004', 1000, 400],
        ['2005', 1170, 460],
        ['2006', 860, 580],
        ['2007', 1030, 540]
    ]);

    // Set chart options
    var options = {
        'title': 'How Much Pizza I Ate Last Night',
        'width': 250,
        'height': 200
    };
    // Set chart options
    var options2 = {
        'title': 'How Much Pizza You Ate Last Night',
        'width': 250,
        'height': 200
    };
    // Set chart options
    var options3 = {
        'title': 'Line chart',
        'width': 250,
        'height': 200
    };

    // Instantiate and draw our chart, passing in some options.
    var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
    chart.draw(data, options);
    var chart2 = new google.visualization.PieChart(document.getElementById('chart_div2'));
    chart2.draw(data2, options2);
    var chart3 = new google.visualization.LineChart(document.getElementById('chart_div3'));
    chart3.draw(data3, options3);

}

I have created a bin for you please check using this link http://codebins.com/codes/home/4ldqpc5 我为您创建了一个回收站,请使用此链接进行检查http://codebins.com/codes/home/4ldqpc5

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

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