0

I need to draw line chart for the data present in the database, my database looks like

CREATE TABLE `details` (
`id` varchar(2) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
`value` varchar(5) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


INSERT INTO `details` (`id`, `value`) VALUES
('1', '62.32'),
('2', '52.45'),
('3', '52.20'),
('4', '51.28'),
('5', '52.20'),
('6', '53.20'),
('7', '51.70'),
('8', '52.61'),
('9', '56.48'),
('10', '56.48'),
('11', '58.08'),
('12', '58.52'),
('13', '57.75'),
('14', '58.32'),
('15', '59.95'),
('16', '59.45'),
('17', '59.45'),
('18', '60.04'),
('19', '55.37'),
('20', '55.13'),
('21', '55.99'),
('22', '55.99'),
('23', '57.08'),
('24', '57.55'),
('25', '57.55'),
('26', '54.05'),
('27', '56.45'),
('28', '56.85'),
('29', '57.45'),
('30', '57.45');

I should generate linechart in php between id and the value. My php code is

<?php

$db_host = 'localhost';
$db_database = 'hello';
$db_user = 'root';
$db_password = 'test123';

include('../../inc/db.php');
$db = mysql_connect($db_host, $db_user, $db_password);
mysql_select_db($db_database);
/**
* Used to produce a Google Chart for the weekly average prices of widgets
*/
function generateGoogleChart()
{
//Get the data from the database
$sqlQuery = "SELECT id,value FROM details";
$sqlResult = mysql_query($sqlQuery);
while ($row = mysql_fetch_assoc($sqlResult)) 
{
$valueResults[] = $row;
}
//Set-up the values for the Axis on the chart
$valueWeeks = count($valueResults);
//Set chd parameter to no value
$chd = '';
//Limit in my example represents weeks of the year
$limit = 30;
//Start to compile the prices data

//Use the $scaleValues array to define my Y Axis 'buffer'
//$YScaleMax = round(max($scaleValues)) + 5;
//$YScaleMin = round(min($scaleValues)) - 5;
//Generate the number of weeks of the year for A Axis labels
$graphSequence = generateSequence(1, 30, "|");
//Set the Google Image Chart API parameters
$cht = 'lc';//Set the chart type
$chxl = '1:|' . $graphSequence . '2:|value|3:||4:|id';//custom axis labels
$chxp = '2,50|4,50';//Axis Label Positions
$chxr = '0,' . $YScaleMin . ',' . $YScaleMax . '|1,1,30|3,1,12|5,' . $YScaleMin . ',' . $YScaleMax . '';//Axis Range
$chxs = '0,252525,10,1,l,676767|1,252525,10,0,l,676767|2,03619d,13|4,03619d,13|5,252525,10,1,l,676767';//Axis Label Styles
$chxtc = '0,5|1,5|5,5';//Axis Tick Mark Styles
$chxt = 'y,x,y,x,x,r';//Visible Axes
$chs = '918x300';//Chart Size in px
$chco = 'FF0000';//Series Colours
$chds = '' . $YScaleMin . ',' . $YScaleMax . '';//Custom Scaling
$chg = '-1,-1,1,5';//Grid lines
$chls = '1';//line styles
$chm = 'o,252525,0,-1,3';//Shape Markers
//Build the URL
$googleUrl = 'http://chart.apis.google.com/chart?';
$rawUrl = $googleUrl . 'cht=' . $cht . '&chxl=' . $chxl . '&chxp=' . $chxp . '&chxr=' . $chxr . '&chxs=' . $chxs . '&chxtc=' . $chxtc . '&chxt=' . $chxt . '&chs=' . $chs . '&chco=' . $chco . '&chd=t:' . $chd . '&chds=' . $chds . '&chg=' . $chg . '&chls=' . $chls . '&chm=' . $chm;
$output = $rawUrl;
return $output;
}
/**
* A nicer way to test arrays
*/
function displayArray($val)
{
echo "<pre>";
print_r($val);
echo "</pre>";
return;
}
/**
* a simple numeric sequence generator. requires a min and max for the sequence, and can take an optional seperator
*/
function generateSequence($min, $max, $seperator = "")
{
$output = '';
for ($i = $min; $i <= $max; $i++)
{
$output .= $i . $seperator;
}
return $output;
}
$chart = generateGoogleChart();
$html = '<div id="chart">';
$html .= '<img src="' . $chart . '" alt="Performance in schools" />';
$html .= '</div>';
echo $html;

When i run the code the graph is not getting generated. Please help me.

2
Contributors
2
Replies
31
Views
2 Years
Discussion Span
Last Post by sheelakumari
0

No it is from google chart. I don't know how to generate line chart in php.I modified from google chart.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.