duangejian6657 2019-01-18 13:51
浏览 225

通过ajax加载的Highcharts,动态创建yAxis的问题

I have a page that dynamically updates a HighCharts graph from a multiselect dropdown.

<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <script src="https://code.highcharts.com/highcharts.js"></script>
    <script src="https://code.highcharts.com/modules/series-label.js"></script>
    <script src="https://code.highcharts.com/modules/exporting.js"></script>
    <script src="https://code.highcharts.com/modules/export-data.js"></script>

  </head>
  <body>
    <!-- FIELDS -->
    <div id="fields">
        <form method="post" action="">
            <select id="f" name="f[]" multiple>
                <option value="rss1" select>1RSS(dB)</option>
                <option value="rss2">2RSS(dB)</option>
                <option value="rqly">RQly(%)</option>
                <option value="rsnr">RSNR(dB)</option>
            </select>
        </form>
    </div>

    <!-- GRAPH -->
    <div id="graph">No selection</div>

    <script>    
        var updateChart = function(json) {
            // console.log(json)
            var options = {
                chart: {
                    renderTo: 'graph',
                    type: 'line',
                    zoomType: 'x'
                },
                title: { text: null },
                plotOptions: {
                    series: {
                        label: {
                            connectorAllowed: false
                        },
                        pointStart: 0
                    }
                },
                series: []
            }

            options.series = json;
            var chart = new Highcharts.Chart(options);

            // update yaxis
            for (i=0; i<json.length; i++) {
                if(i==0) {
                    // it seems that a yAxis already exist when graph is initialized. Did not find a smarter way to destroy it...
                    chart.yAxis[0].update({ title: { text: json[i].name }});
                } else {
                    chart.addAxis({ title: { text: json[i].name } });
                }
            }
        }

        $(document).ready(function() {
            $('#f').change(function() {
                var requestParams = { f: $('#f').val() };
                $.post('analysisajax.php', requestParams, updateChart);             
            });
        });    

    </script>

  </body>
</html>

Now, my analysisajax.php file looks like this:

<?php
header("Content-type: text/json");
// Initialize the session
session_start();

// Include config file
require_once "config.php";

$jsonObject = array();
$yaxis = 0;

foreach($_POST["f"] as $v) {

    $data = array();
    $sql = "SELECT $v FROM log WHERE id_user = " . $_SESSION["id_user"];
    $result = $link->query($sql);

    while($row = $result->fetch_row()) {
        $data[] = $row[0];
    }

    $jsonObject[] = array(
            "name" => $v,
            "data" => $data,
            "yAxis"=>$yaxis
        );
    $yaxis++;
}

$myJSON = json_encode($jsonObject, JSON_NUMERIC_CHECK);
echo $myJSON;

// Close connection
mysqli_close($link);
?>

When I'm selecting 1 value from the form, the graph loads without problem, but as soon as more than 1 value is selected from the dropdown, the graph fails with the following trace:

highcharts.src.js:498 Uncaught Error: Highcharts error #18: www.highcharts.com/errors/18
    at Object.a.error (highcharts.src.js:498)
    at highcharts.src.js:32669
    at Array.forEach (<anonymous>)
    at r.<anonymous> (highcharts.src.js:32621)
    at a.fireEvent (highcharts.src.js:2635)
    at r.bindAxes (highcharts.src.js:32618)
    at r.init (highcharts.src.js:32482)
    at a.Chart.initSeries (highcharts.src.js:26913)
    at highcharts.src.js:28765
    at Array.forEach (<anonymous>)

I feel that the issue is coming from my dynamic construction of yAxis but can't find a way to make it work. Any idea? Thanks a lot.

  • 写回答

1条回答 默认 最新

  • duanmi8349 2019-01-19 09:38
    关注

    I eventually made it work with the following solution:

    In the analysisajax.php script, I no longer generate the yaxis. I only send name and data.

    The code to generate the graph now looks like this:

        var updateChart = function(json) {
            //console.log(json)
            var options = {
                chart: {
                    renderTo: 'graph',
                    type: 'line',
                    zoomType: 'x'
                },
                title: { text: null },
                yAxis:[],
                legend: {
                    layout: 'vertical',
                    align: 'right',
                    verticalAlign: 'bottom'
                },
                plotOptions: {
                    series: {
                        label: {
                            connectorAllowed: false
                        },
                        pointStart: 0
                    }
                },
                series: [],
                tooltip: { shared: true }
            }
    
            var chart = new Highcharts.Chart(options);
    
            // update axis and series
            for (i=0; i<json.length; i++) {
                // add axis
                chart.addAxis({ title: { text: json[i].name } });
    
                // add serie
                var a = chart.series.length;
                var seriesOptions = {
                    name: json[i].name,
                    data: json[i].data,
                    yAxis: a
                }
                chart.addSeries(seriesOptions,true);
                chart.options.series.push(seriesOptions);       
            }
        }
    
        $(document).ready(function() {
            $('#f').change(function() {
                var requestParams = { f: $('#f').val() };
                $.post('analysisajax.php', requestParams, updateChart);             
                //return false;
            });
        });    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看