0

First of all, I have a stores table as follows :

Table Name : stores

id |name
1  | M1
2  | M2

And Each store is considered as a table which have its own items, selling price and qty as follows

Table Name : M1

id | item | qty | sell_price
1  | z1   | 12  | 5.00
2  | z2   | 15  | 6.00

Table Name : M2

id | item | qty | sell_price
1  | z1   | 12  | 5.00
2  | z5   | 25  | 12.00
3  | z6   | 5   | 7.00

Now I want to create a bar graph by which I need to see Each store its Total selling price ( SUM(sell_price) ) , as follows :

Y-axis ( Total Sell_price )
 30|          _____
 25|  ____   |     |
 20| |    |  |     |
 15| |    |  |     |
  0|_|____|__|_____|________ X - axis (Store Name )
       M1      M2

I've created the PHP code as follows :

$query1 = "SELECT name from stores";
$result1 = mysql_query($query1);
if(!mysql_num_rows($result1)>0){
    echo "<center><font color=red>No Stores Found !</font></center>";   
}
else{
while ($row1 = mysql_fetch_assoc($result1)){
$query = "SELECT *,SUM(sell_price) FROM {$row1['name']}";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);

$store[]= $row1['name'];
$sale[] = $row['SUM(sell_price)'];

}
}

Bar Graph JavaScript - Please Note var s1 and var ticks are the ones needed to be changed and collects array from the above PHP loop

<script class="code" type="text/javascript">$(document).ready(function(){
        $.jqplot.config.enablePlugins = true;
         var s1 = [200, 888, 645, 1044];
         var ticks = ['M1','M7','M9','M10'];


        plot1 = $.jqplot('chart1', [s1], {
            // Only animate if we're not using excanvas (not in IE 7 or IE 8)..
            animate: !$.jqplot.use_excanvas,
            seriesDefaults:{
                renderer:$.jqplot.BarRenderer,
                pointLabels: { show: true }
            },
            axes: {
                xaxis: {
                    renderer: $.jqplot.CategoryAxisRenderer,
                    ticks: ticks
                }
            },
            highlighter: { show: false }
        });

        $('#chart1').bind('jqplotDataClick', 
            function (ev, seriesIndex, pointIndex, data) {
                $('#info1').html('series: '+seriesIndex+', point: '+pointIndex+', data: '+data);
            }
        );
    });</script>

Now as you can see, I've created an array $store and $sale - Not sure if its a correct array because i'm not too familiar with arrays. However I need to find how can I assign the array values to javascript vars (s1 and ticks) accordingly.

1 Answer 1

1
<script class="code" type="text/javascript">$(document).ready(function(){
        $.jqplot.config.enablePlugins = true;
         var s1 = ['<?php echo trim( implode( "', '", $sale ), ',' ); ?>'];
         var ticks = [<?php echo implode( ', ', $store ); ?>];
         [...]
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your reply, I've tried your answer but the graph did not show up at all.
Btw, s1 should be for sale, and ticks for store name. I've tried only s1 and the total selling prices shows up but it seems numbers on y-axis are duplicated on top of each other. Maybe data in the array itself is not correct ?
I've inverted my code to reflect your comment. If you look at the web page's source code, are the values printed correctly by PHP?
Yes values are shown correctly, Now I've tried again using the above codes, the graph shows up but its seems that M1 and M2 bars are on top of each other. when I tried to remove the javascript code from the while loop it will only shows M2 store only with its correct total selling price. Any idea how to fix this ?... Thanks for your time mate
So the chart displays properly if you use manual data but if the same data is printed by PHP it doesn't work? If you can't make the chart work with manual data then you should post a different question for help with the chart specifically.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.