javascriptjsonhighcharts

Generating highcharts issues in dual axis column chart


I am creating one Dual axes,line and column charts from Highcharts using json and mysql query.Debug through firebug and i am ensure data is generating through json but graph not display in browser. I need to display 'product cost' on spline chart and 'Products specs Rates' display on column chart which comes from database. request you please refer bellow [link] http://jsfiddle.net/mjena/vjdLu/4/ I am also trying all suggestions which find on stackoverflow but not successfully work.

My code for graph product.php

     $(function () {
     $('#container').highcharts({
    chart: {
        zoomType: 'xy'
    },
    title: {
        text: 'Product Specification Rating'
    },
    subtitle: {
        text: 'product raja'
    },
    xAxis: [{
        categories: []
    }],
    yAxis: [{ // Primary yAxis
        labels: {
          //  format: '{value} Rs.',
            style: {
                color: Highcharts.getOptions().colors[1]
            }
        },
        title: {
            text: 'Product Cost',
            style: {
                color: Highcharts.getOptions().colors[1]
            }
        }
    }, { // Secondary yAxis
        title: {
            text: 'Product Specification rating',
            style: {
                color: Highcharts.getOptions().colors[0]
            }
        },
        labels: {
            //format: '{value} out of 100',
            style: {
                color: Highcharts.getOptions().colors[0]
            }
        },
        opposite: true
    }],
    tooltip: {
        shared: true
    },
    legend: {
        layout: 'vertical',
        align: 'left',
        x: 120,
        verticalAlign: 'top',
        y: 100,
        floating: true,
        backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
    },
    series: [{
        name: 'Product Rate',
        type: 'column',
        yAxis: 1,
        data: [],
        tooltip: {
            valueSuffix: ' out of 100'
        }

    }, {
        name: 'Product Cost',
        type: 'spline',
        data: [],
        tooltip: {
            valueSuffix: 'Rs.'
        }
      }]
      });

      $.getJSON("data.php", function(json) {

        var theChart = $('#container').highcharts();
     theChart.xAxis[0].setCategories(json[0]['data']); 
    theChart.series[0].setData(json[1]);
    theChart.series[1].setData(json[2]);
       // chart = new Highcharts.Chart(options);

    });
     });

data.php

         $sql1 = mysql_query("SELECT products.product_name,product_rate.pro_rating 
         FROM products LEFT OUTER JOIN product_rate 
          ON products.product_id= product_rate.product_id ");

     $category = array();
     $category['name'] = 'Product Name';
    while($r1 = mysql_fetch_array($sql1)) {
    $category['data'][] = $r1['product_name'];

  }

    $sql2 = mysql_query("SELECT products.product_name,product_rate.pro_rating,prod_cost.product_cost 
    FROM products LEFT OUTER JOIN product_rate 
    ON products.product_id= product_rate.product_id
    LEFT OUTER JOIN prod_cost ON product_rate.product_id=prod_cost.product_id  ");

     $series1 = array();
     $series1['name'] = 'Product Rate';
     $series2 = array();
      $series2['name'] = 'Product Cost';

    while($r2 = mysql_fetch_array($sql2)) {
    $series1['data'][] = $r2['pro_rating'];
    $series2['data'][] = $r2['product_cost'];
   }
    $result = array();
     array_push($result,$category);
     array_push($result,$series1);
      array_push($result,$series2);
    print json_encode($result, JSON_NUMERIC_CHECK);

Requesting you please help me.


Solution

  • 1.) You JSON is misquoted, running it through JSONLint:

    [{"name":"Product Name","data":["HP", "DELL", "APPLE", "HCL", "LENOVE", "SONY","TOSHIBA", "SAMSUNG", "ACER", "VIDEOCON', "IBM', "MAC"]},{"name":"Product Rate","data":[49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]},{"name":"Product Cost","data":[7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]}]
    
    Parse error on line 14:
    ...       "VIDEOCON', "IBM',            "M
    -----------------------^
    Expecting '}', ':', ',', ']'
    

    JSONLint dislikes the mismatched quotes on "VIDEOCON' and "IBM'

    2.) The setData method only takes the data piece not the entire series config options. You want:

    theChart.series[0].setData(json[1]["data"]);
    

    3.) You should really place your json call in the load event of the chart. This way you'll have no timing issues with when the chart is drawn/json call is made.

    Here's an updated fiddle implementing all these ideas.