我正在用Microsoft WebMatrix创建一个基本网站。我有一个PHP中的浮点值数组。我想用这些值在CSS中创建一个条形图(例如,50.0的值创建一个高度为300px 50%的条形图)。这是我正在使用的全部代码,因为没有那么多。当我在浏览器(Google Chrome或Internet Explorer)中运行它时,它会写第一个echo语句,但不会产生任何条。如何编辑代码以便绘制条形图?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Bar chart</title>
<style>
.bar
{
color: #00ff21;
width: 30px; //This defines the colour and width of the bars
}
</style>
</head>
<body>
<?php
echo("<p>This is my bar chart!</p>");
$values = array(50.0, 20.0, 35.0, 70.0); //This is the array of float values
for ($i = 0; $i < count($values); $i++) //This loop should create a bar for each element in the array
{
$currentHeight = 300 * $values[$i] / 100.0; ?>
<div class="bar" style="height: <?php echo($currentHeight); ?> "></div>
<?php
}
?>
</body>
所以我对css不是很熟悉,但您的div元素中缺少一些参数,使其能够工作。
<div class="bar" style="height: <?php echo $currentHeight . "px;"; ?> border: solid; display: inline-block;"></div>
你在没有添加像素识别的情况下呼应了高度,也没有给它一个坚实的边界,所以它无论如何都不会在网络浏览器中显示出来。
你也可以在样式标签中添加边框和显示值,但你也需要有空的高度选择器,我不完全确定为什么会这样,但代码看起来是这样的:
.bar
{
color: #00ff21;
width: 30px; //This defines the colour and width of the bars
height: ;
display: inline-block;
border: solid;
}
<div class="bar" style="height: <?php echo $currentHeight . "px;"; ?>"></div>
我不确定你想要的外观,但这里有三个问题:
-
div需要一个边框或背景色才能看到它们:
background-color: #00ff21;
或border: solid;
-
您需要指定高度单位,如px:
style="height: <?php echo($currentHeight); ?>px"
-
div相互覆盖,因此定位它们或浮动:
style="float: left; height: <?php echo($currentHeight); ?>px"