【问题标题】:Generating highcharts issues in dual axis column chart在双轴柱形图中生成 highcharts 问题
【发布时间】:2014-04-26 15:50:59
【问题描述】:

我正在使用 json 和 mysql 查询从 Highcharts 创建一个双轴、折线和柱形图。通过 firebug 进行调试,我确保数据是通过 json 生成的,但图形不会显示在浏览器中。 我需要在样条图上显示“产品成本”,并在来自数据库的柱形图上显示“产品规格率”。 请求你请参考下面[链接]http://jsfiddle.net/mjena/vjdLu/4/ 我也在尝试所有在 stackoverflow 上找到但没有成功的建议。

我的图形 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);

请求你帮助我。

【问题讨论】:

  • 您的代码看起来不错,在您的$.getJSONjson 最终等于什么?控制台中是否有任何 javascript 错误?
  • @Mark 在控制台中没有错误。但是 json 输出出现但图形不显示。请检查 [link]jsfiddle.net/mjena/Yu4g4/2

标签: json highcharts


【解决方案1】:

1.) 你的 JSON 引用错误,通过 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 不喜欢 "VIDEOCON'"IBM' 上不匹配的引号

2.) setData 方法只接受数据片段而不是整个系列配置选项。你想要:

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

3.) 你真的应该把你的 json 调用放在图表的load 事件中。这样,当绘制图表/调用 json 时,您将不会遇到时间问题。

Here's an updated fiddle implementing all these ideas.

【讨论】:

  • 谢谢。我很高兴收到您的回复。现在它工作正常。我需要在列 /100 顶部添加一件事,例如 [link] highcharts.com/demo/column-drilldown 在此链接中,它显示 53.6% 和 18.7% 像这样。但在我的情况下我需要 45/100、55/100 像这样在列的顶部。所以请给我一些想法如何显示这样的内容。
  • @Marks 感谢您的回复。现在一切正常。
猜你喜欢
  • 2014-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-25
  • 1970-01-01
相关资源
最近更新 更多