How to create dynamic bar chart in php or javascript with mysql

A shortcut method to quickly creating dynamic Chart.js bar charts using PHP and data from a MySQL database. This blog post is for a single Y-axis bar chart.

Gist link.

The idea is to set PHP variables at the top of the page for configuration and customization to then easily mass-produce bar charts with minimal editing.

This bar chart was made with the code below

How to create dynamic bar chart in php or javascript with mysql
chart.js bar chart made with PHP and MySQL data

Unseen is the MySQL database but it is self-explanatory with the data being fetched for the id = 202331 and season type = 2 as this means regular season data.

Function yAxisValues() will Y-axis data with parameters being passed in as to what data (column) to fetch and return.



    <?php echo $page_title; ?>
    
    
    
    

 PDO::ERRMODE_EXCEPTION);
    return new PDO($db, $db_user, $db_password, $options);
}

function xAxisValues(int $value)
{//X axis is horizontal
    $db = db_connect();
    $select = $db->prepare("SELECT season FROM player_base_yby WHERE `pid` = ? GROUP BY season;");
    $select->execute([$value]);
    $data = array();
    while ($row = $select->fetch(PDO::FETCH_ASSOC)) {
        $data[] = $row['season'];
    }
    echo json_encode($data);
}

function yAxisValues(string $value_type, int $value)
{//Y axis is verticals
    $db = db_connect();
    if ($value_type === 'FGM') {
        $select = $db->prepare("SELECT fgm as d FROM player_base_yby WHERE `pid` = ? GROUP BY season;");
    } elseif ($value_type === 'FGA') {
        $select = $db->prepare("SELECT fga as d FROM player_base_yby WHERE `pid` = ? GROUP BY season;");
    } elseif ($value_type === 'FGP') {
        $select = $db->prepare("SELECT (fg_pct * 100) as d FROM player_base_yby WHERE `pid` = ? GROUP BY season;");
    }
    $select->execute([$value]);
    $data = array();
    while ($row = $select->fetch(PDO::FETCH_ASSOC)) {
        $data[] = $row['d'];
    }
    echo json_encode($data);
}

?>

How do I create a dynamic pie chart in PHP or JavaScript with MySQL?

Here are the main steps for creating a pie chart in PHP MySQL :.
First, we need to include the FusionCharts. js JavaScript class and FusionCharts. php. ... .
Next, include DBConn. php to connect to the database..
Generate XML document and store it in a strXML variable..
Render the Pie 3D Chart with data from $strXML..

How do you draw a bar graph using data from MySQL table and PHP?

ChartJS | How to draw Bar graph using data from MySQL table and PHP.
Requirement. We will need the following items to complete this tutorial. ... .
MySQL table. ... .
Create a New Project. ... .
Moving the JS files in the js folder. ... .
Create data. ... .
Output of data.php file. ... .
Create bargraph. ... .
Write some javascript in app..

How do I create a dynamic bar chart in HTML?

Here are the steps for creating Dynamic Chart..
var xVal = dps. length + 1;.
var yVal = 100;.
var updateInterval = 1000;.
var updateChart = function () {.
yVal = yVal + Math. round(5 + Math. random() *(-5-5));.
dps. push({x: xVal,y: yVal,});.
xVal++;.
chart. render();.

What is a dynamic bar chart?

A dynamic chart is a special chart in Excel which updates itself when the range of the chart is updated. In static charts, the chart does not change itself when the range is updated. To create a dynamic chart in Excel, the range or the source of data needs to be dynamic in nature.