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)

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

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?