第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

PHP GD 從 MYSQL 表創(chuàng)建圖形

PHP GD 從 MYSQL 表創(chuàng)建圖形

PHP
牛魔王的故事 2022-11-12 10:02:44
我目前正在使用 GD 為 PHP 創(chuàng)建一個(gè)條形圖(inb4 使用 Java 腳本,我不能。這是一個(gè)要求)就目前而言,我在 GD 中有一個(gè)正常工作的條形圖,我可以顯示正常數(shù)字,但現(xiàn)在我是試圖從 MYSQL 表中檢索數(shù)據(jù)并將其顯示在圖表中。require "connect.php";$sql = "SELECT title, searchcountFROM moviesORDER BYsearchcount DESC LIMIT 10";$result = $conn->query($sql);if (mysqli_num_rows($result) > 0) {    while($row = mysqli_fetch_array($result))    {        $data = [            $row['title'] => substr_count($row['searchcount'], "1"),        ];    }}上述代碼目前可用于檢索所需的 10 個(gè)結(jié)果之一。我想不通的是如何找回其他 9 個(gè),我們將不勝感激。https://imgur.com/m8sU8QG 這是圖表當(dāng)前狀態(tài)的鏈接圖形是如何繪制的。/* * Chart settings and create image */// Image dimensions$imageWidth = 700;$imageHeight = 400;// Grid dimensions and placement within image$gridTop = 40;$gridLeft = 50;$gridBottom = 340;$gridRight = 650;$gridHeight = $gridBottom - $gridTop;$gridWidth = $gridRight - $gridLeft;// Bar and line width$lineWidth = 1;$barWidth = 20;// Font settings$font = 'OpenSans-Regular.ttf';$fontSize = 10;// Margin between label and axis$labelMargin = 8;// Max value on y-axis$yMaxValue = 25;// Distance between grid lines on y-axis$yLabelSpan = 5;// Init image$chart = imagecreate($imageWidth, $imageHeight);// Setup colors$backgroundColor = imagecolorallocate($chart, 255, 255, 255);$axisColor = imagecolorallocate($chart, 85, 85, 85);$labelColor = $axisColor;$gridColor = imagecolorallocate($chart, 212, 212, 212);$barColor = imagecolorallocate($chart, 47, 133, 217);imagefill($chart, 0, 0, $backgroundColor);imagesetthickness($chart, $lineWidth);/* * Print grid lines bottom up */}
查看完整描述

1 回答

?
忽然笑

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超5個(gè)贊

好的,我測(cè)試了你的代碼并且它有效,所以我的猜測(cè)是格式不正確或者你沒(méi)有從數(shù)據(jù)庫(kù)獲得正確的輸出。嘗試更改此行:


$data = [

            $row['title'] => substr_count($row['searchcount'], "1"),

        ];

對(duì)此:


$data[$row['title']] = substr_count($row['searchcount'], "1");

正如你期望得到一$key => $value對(duì),所以$value不是array(). 這是test我使用的數(shù)據(jù)和結(jié)果:


<?php


$data = array(

    'test1' => 10,

    'test2' => 4,

    'test3' => 10,

    'test4' => 4,

    'test5' => 10,

    'test6' => 4,

    'test7' => 10,

    'test8' => 4,

    'test9' => 10,

    'test10' => 4

);



/*

 * Chart settings and create image

 */


// Image dimensions

$imageWidth = 700;

$imageHeight = 400;


// Grid dimensions and placement within image

$gridTop = 40;

$gridLeft = 50;

$gridBottom = 340;

$gridRight = 650;

$gridHeight = $gridBottom - $gridTop;

$gridWidth = $gridRight - $gridLeft;


// Bar and line width

$lineWidth = 1;

$barWidth = 20;


// Font settings

$font = getcwd() . '/arial.ttf'; // I switched to use Arial font 

$fontSize = 10;


// Margin between label and axis

$labelMargin = 8;


// Max value on y-axis

$yMaxValue = 25;


// Distance between grid lines on y-axis

$yLabelSpan = 5;


// Init image

$chart = imagecreate($imageWidth, $imageHeight);


// Setup colors

$backgroundColor = imagecolorallocate($chart, 255, 255, 255);

$axisColor = imagecolorallocate($chart, 85, 85, 85);

$labelColor = $axisColor;

$gridColor = imagecolorallocate($chart, 212, 212, 212);

$barColor = imagecolorallocate($chart, 47, 133, 217);


imagefill($chart, 0, 0, $backgroundColor);


imagesetthickness($chart, $lineWidth);


/*

 * Print grid lines bottom up

 */


for($i = 0; $i <= $yMaxValue; $i += $yLabelSpan) {

    $y = $gridBottom - $i * $gridHeight / $yMaxValue;


    // draw the line

    imageline($chart, $gridLeft, $y, $gridRight, $y, $gridColor);


    // draw right aligned label

    $labelBox = imagettfbbox($fontSize, 0, $font, strval($i));

    $labelWidth = $labelBox[4] - $labelBox[0];


    $labelX = $gridLeft - $labelWidth - $labelMargin;

    $labelY = $y + $fontSize / 2;


    imagettftext($chart, $fontSize, 0, $labelX, $labelY, $labelColor, $font, strval($i));

}


/*

 * Draw x- and y-axis

 */


imageline($chart, $gridLeft, $gridTop, $gridLeft, $gridBottom, $axisColor);

imageline($chart, $gridLeft, $gridBottom, $gridRight, $gridBottom, $axisColor);


/*

 * Draw the bars with labels

 */


$barSpacing = $gridWidth / count($data);

$itemX = $gridLeft + $barSpacing / 2;


foreach($data as $key => $value) {

    // Draw the bar

    $x1 = $itemX - $barWidth / 2;

    $y1 = $gridBottom - $value / $yMaxValue * $gridHeight;

    $x2 = $itemX + $barWidth / 2;

    $y2 = $gridBottom - 1;


    imagefilledrectangle($chart, $x1, $y1, $x2, $y2, $barColor);


    // Draw the label

    $labelBox = imagettfbbox($fontSize, 0, $font, $key);

    $labelWidth = $labelBox[4] - $labelBox[0];


    $labelX = $itemX - $labelWidth / 2;

    $labelY = $gridBottom + $labelMargin + $fontSize;


    imagettftext($chart, $fontSize, 0, $labelX, $labelY, $labelColor, $font, $key);


    $itemX += $barSpacing;

}


/*

 * Output image to browser

 */


imagepng($chart, 'chart.png');



?>

輸出

http://img1.sycdn.imooc.com//636efefa0001174006500363.jpg

查看完整回答
反對(duì) 回復(fù) 2022-11-12
  • 1 回答
  • 0 關(guān)注
  • 121 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)