Jump to content

Recommended Posts

Hi all,

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

http://www.maani.us/charts/index.php?menu=Tutorial&submenu=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.recruitmentnl.info/training/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

Link to comment
https://forums.phpfreaks.com/topic/14498-php-line-charts-phpswf-add-in/
Share on other sites

I think you need something like this
[code]<?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);
}
?>[/code]

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]
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.recruitmentnl.info/training/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
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.