dpf7891 2016-02-26 13:35
浏览 47
已采纳

两个向量的高亮图:分割向量?

I want to plot data points with the attributes 'value' and 'date'. Problem is, the format is data: [[...], [...]] where the first bracket is, say, 'value' and the second 'date'. I'd like to know how to split the two vectors, that is, take one value from each vector and put it into that format to create a data point. Perhaps there's some other way to do this?

'values' are stored as $data[] = $row['value']; 'date' are stored as $data2[] = $row['date'];

Fully working code with only one working vector 'value' down below:

<!DOCTYPE html>
<html>
<head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>My first column chart by Highcharts</title>
        <!-- 1. Add JQuery and Highcharts in the head of your page -->
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
        <script src="http://code.highcharts.com/highcharts.js"></script>

        <!-- 2. You can add print and export feature by adding this line -->
        <script src="http://code.highcharts.com/modules/exporting.js"></script>


        <!-- 3. Add the JavaScript with the Highchart options to initialize the chart -->
        <script type="text/javascript">


<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>


<?php
$con=mysql_connect('localhost','root','');
mysql_select_db("sensordb", $con);
$result=mysql_query('select value, date from data');
while($row = mysql_fetch_array($result)) {

    $data[] = $row['value'];
    $data2[] = $row['date'];

}
?>


<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

    <script type="text/javascript">

$(function () {
    $('#container').highcharts({
        title: {
            text: 'Monthly Average Temperature',
            x: -20 //center
        },
        subtitle: {
            text: 'Source: WorldClimate.com',
            x: -20
        },
        xAxis: {

        },
        yAxis: {
            title: {
                text: 'Temperature (°C)'
            },
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }]
        },
        tooltip: {
            valueSuffix: '°C'
        },
        legend: {
            layout: 'vertical',
            align: 'right',
            verticalAlign: 'middle',
            borderWidth: 0
        },
        series: [{
            name: 'Tokyo',
            data: [<?php echo join($data, ',') ?>]
        }]
    });
});


</script>


    </head>
    <body>

        <!-- 3. Add the container -->
        <div id="container" style="width: 600px; height: 400px; margin: 0 auto"></div>      

    </body>
</html>
  • 写回答

1条回答 默认 最新

  • dongren5293 2016-02-26 14:10
    关注

    So based on your comments, you need your data in the standard format of

    data: [[date,value],[date,value]...]
    

    Which is a very different thing from splitting the data into two arrays. :)

    Take this:

    $data[] = $row['value'];
    $data2[] = $row['date'];
    

    And change it to this:

    $data[] = array($row['date'],$row['value']);
    

    Keep in mind that your dates will need to be in epoch format, as milliseconds, and you will need to json_encode the array.

    You'll also need to make sure your values are numeric, by casting them to either integers or floats, depending on which they are expected to be.

    You can get the date and value in the right format like this:

    $data[] = array(
       strtotime($row['date']) * 1000, //convert to epoch time, multiply for milliseconds
       (float)$row['value']  //cast value as a float; use (int) to cast as integer
    );
    

    When you add the data to the chart, try it this way:

    series:[{
      data: <?echo json_encode($data); ?>
    }]
    

    (note that I do not include the brackets - the json_encode() does that already)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料