📄 09c04-1.php
字号:
<?php// Define the chart creation function.// Have parameters for the data itself and a maximum height for the chart.// You can also pass in 4 color parameters, for the border,// background, bars, and the grid - These are optional.function create_chart($data, $height, $bars = 'red', $bg = 'white', $border = 'black', $grid = '#DDD') { // First of all let's ensure that every time we call this we // generate a new 'number' for this chart, it's needed to distinguish // if we have multiple charts on one page: static $idx = 0; $idx++; // Consider height to be 2 pixels less to take into account the borders: $height -= 2; // Now let's calculate our scale factor, add in a little because for // visual purposes we don't want it to ever hit the top. $scale = $height / (max($data) * 1.05); // Calculate our width: $width = count($data); // Now we can create some basic CSS that will be used by this: echo "<style>#chartout{$idx} { position: relative; height: ", $height + 2, "px; width: ", $width + 2, "px; background-color: {$border};}#chartin{$idx} { position: absolute; top: 1px; left: 1px; height: {$height}px; width: {$width}px; background-color: {$bg};}.bar{$idx} { position: absolute; bottom: 0px; background-color: {$bars}; width: 1px; overflow: hidden;}.grid{$idx} { position: absolute; left: 0px; height: 1px; width: {$width}px; background-color: {$grid}; padding: 0px; margin: 0px; overflow: hidden;}</style>"; // Now output the basic parts of the chart: echo "<div id='chartout{$idx}'><div id='chartin{$idx}'>"; // Now create a grid pattern, divide the space into 5ths foreach(range(1,3) as $line) { $lh = round($line * ($height / 5)); echo "<div class='grid{$idx}' style='top: {$lh}px'></div>\n"; } // Now run through all elements of the array, making the actual bars foreach ($data as $pos => $val) { $barheight = round($val * $scale); echo "<div class='bar{$idx}' style='left:{$pos}px;height:{$barheight}px'></div>\n"; } // Finish off: echo "\n</div></div>\n";}// Let's generate 200 random points of data in two different arrays$chartdata = array();$chartdata2 = array();for ($i = 0; $i < 200; $i++) { $chartdata[$i] = rand(1,1000); $chartdata2[$i] = rand(1,1000);}// Now generate a chart based upon this, 100 pixels tall:create_chart($chartdata, 100);// Generate another one that is only 50 pixels tall, with different colorscreate_chart($chartdata2, 50, '#0C0', 'black', 'black', '#666');?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -