Jump to content


Photo

PHP Line Charts PHP/SWF Add In


  • Please log in to reply
3 replies to this topic

#1 notonurnelly

notonurnelly
  • Members
  • PipPip
  • Member
  • 28 posts

Posted 13 July 2006 - 03:59 PM

Hi all,

I am trying to generate a simple line chart using the PHP/SWF

http://www.maani.us/...menu=Chart_Data

I am trying to read the values from a mysql database this is not a problem

the problem comes from trying to generate an array from the values and plot them as a graph as shown in the tutorial

My table is a simpletable structured as below

  Question  Score 
      Q1      14
      Q2      7
      Q5      12
      Q3      2
      Q4      8

The code I am using to generate the graphs is shown below

<?php

include "charts.php";
$chart ['chart_type'] = "line";

$chart ['chart_data'][0][0]="";
$chart ['chart_data'][1][0]="Q1";
$chart ['chart_data'][2][0]="Q2";
$chart ['chart_data'][3][0]="Q3";
$chart ['chart_data'][4][0]="Q4";
$chart ['chart_data'][5][0]="Q5";
mysql_connect ("localhost","x04recru_jamie","tatungjj");
mysql_select_db ("x04recru_main");

$result = mysql_query("SELECT MIN(Score) as MinScore from tblChart");
$MinScore = mysql_result ($result,0,"MinScore");


$result = mysql_query ("SELECT * FROM tblChart");

for ($i=0; $i < mysql_num_rows($result); $i++){

switch (mysql_result ($result,$i,"Question")){
case "Q1":
$row = 1;
break;

case "Q2":
$row = 2;
break;

case "Q3":
$row = 3;
break;

case "Q4":
$row = 4;
break;

case "Q5":
$row = 5;
break;
}

$col = mysql_result ($result,$i,"Score")-$MinScore + 1;


$chart ['chart_data'][0][$col]=mysql_result($result,$i,"Question");
$chart ['chart_data'][$row][$col]=mysql_result($result,$i,"Score");
}


//echo $MinScore;
SendChartData ($chart);
?>


The vlaues seem to be falling out of the array okay and plotting on the graph as shown below

http://www.recruitme.../chart_test.php


the trouble is I cannot configure the array correctly to move the line graph from the first column (ie far left) across to the right columns to represent the values.

Has anyone any experience of this add in and could possibly help me with this.

Many Thanks
Jamie



#2 notonurnelly

notonurnelly
  • Members
  • PipPip
  • Member
  • 28 posts

Posted 13 July 2006 - 04:00 PM

Bye the way the  username and password for the page is

jamie
jamie

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 13 July 2006 - 06:54 PM

I think you need something like this
<?php
$res = mysql_query("SELECT question, score FROM tblChart
		ORDER BY question");
$chart['chartdata'][0] = array('', '');
while (list($q, $s) = mysql_fetch_row($res)) {
	$chart['chartdata'][] = array($q, $s);
}
?>

As an aside, a line graph is good for showing changes over time, but for comparisons like this a bar chart is a better choice. EG

[pre]
15 |   
    |  +++
    |  +++
    |  +++                +++
    |  +++                +++
10 |  +++                +++
    |  +++                +++
    |  +++            +++  +++
    |  +++  +++      +++  +++
    |  +++  +++      +++  +++
  5 |  +++  +++      +++  +++
    |  +++  +++      +++  +++
    |  +++  +++      +++  +++
    |  +++  +++  +++  +++  +++
    |  +++  +++  +++  +++  +++
    ------------------------------
      Q1  Q2  Q3  Q4  Q5
[/pre]
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 notonurnelly

notonurnelly
  • Members
  • PipPip
  • Member
  • 28 posts

Posted 14 July 2006 - 07:48 AM

Hi Thanks for your last reply,

that did not seems to work for some reason

the chart displayed just seeme dto be the standard line graph.

http://www.recruitme.../chart_test.php

I have removed all password now.

The code i am using now is

<?php

include "charts.php";
$chart ['chart_type'] = "line";

mysql_connect ("localhost","x04recru_jamie","tatungjj");
mysql_select_db ("x04recru_main");

$res = mysql_query("SELECT Question, Score FROM tblChart
ORDER BY Question");
$chart['chartdata'][0] = array('', '');
while (list($q, $s) = mysql_fetch_row($res)) {
$chart['chartdata'][] = array($q, $s);
}


SendChartData ($chart);

Hope someone can help
Im not sure whats going on here really, Im just following the tutorial on the PHP/SWF web site.

are there any other charting options available to me?

many Thanks
Jamie




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users