【问题标题】:Wrong date in highstock Charthighstock 图表中的错误日期
【发布时间】:2017-09-22 00:38:18
【问题描述】:

如何将正确的日期合并到我的高位图表中?我的滑块中一直显示一月到二月的月份(正确的范围应该是七月到八月),这是不正确的。在我的 PHP 中,我将数据库中的日期/时间转换为 JavaScript 时间戳,但仍然不起作用。任何帮助都会很棒,谢谢!

输出数组:data.txt

当前高位图:

PHP & JS 和 HTML:

<?php
$stmt = mysqli_prepare($con, "SELECT date, IFNULL(AT,'null') FROM test");
	$result = array('date' => array(), 'AT' => array());
	if ($stmt) {
		mysqli_stmt_execute($stmt);
		mysqli_stmt_bind_result($stmt, $date, $at);
	    while (mysqli_stmt_fetch($stmt)) {
	        $result['date'][] = $date;
	        $result['AT'][] = (int)$at;
            
           $stamp = strtotime($date); // get unix timestamp
           $time = $stamp*1000;       

	    }
	    mysqli_stmt_close($stmt);
       
    }
 
?>
<!DOCTYPE html> 
<html > 
    <!--<![endif]-->     
    <head> 
        <meta charset="utf-8"> 
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/highcharts-more.js"></script>
<script src="https://code.highcharts.com/modules/data.js"></script>      

<script>

$(function(data) {
  
     
       
	

     $('#chart2').highcharts('StockChart', {          //Stock chart - might need zones to match gauges
		

		    rangeSelector : {
                buttons : [{
                    type : 'hour',
                    count : 3,
                    text : '3h'
                }, {
                    type : 'day',
                    count : 2,
                    text : '2D'
                }, {
                    type : 'week',
                    count : 1,
                    text : '1W'
                }, {
                    type : 'month',
                    count : 1,
                    text : '1M'
                },  {
                    type : 'all',
                    count : 1,
                    text : 'All'
                }],
                selected : 2,
                
            },

		    title: {
		        text: 'Power'
		    },
         

			
		    yAxis: [{
		        title: {
		            text: 'Bat V'
		        },
		        height: 400,
		        lineWidth: 2,
				oposite: true
		    }, {
		        title: {										// yAxis 1 ie secondary y-axis
		            text: 'Solar V'
		        },
		        //top: 200,
		        height: 400,
		        offset: 25,
		        lineWidth: 2,
				oposite: false
		    }],
		    
			xAxis:{
					type: <?php echo json_encode($time) ?>,
     
				  },  
				  
		    series: [{
                 pointInterval:  24 * 3600 * 1000,
		        type: 'line',
		        data:  <?php echo json_encode($result['AT']) ?>
		    },],
		});										//end of stockchart graphic

      									// end of get function
		
  });											// end of graphing function

</script>         
</head>     
    <body>     
        <?php echo $time; ?>
		  <br><br /><br /><br />
		  <div id="chart2" style="width:100%; height:600px;"></div>
    </body>     
</html>

【问题讨论】:

  • 嗨 hokletrain,您在 Highcharts 论坛上发布了非常相似的问题。我向您发送了您的主题请求。请看一下。

标签: javascript highcharts highstock


【解决方案1】:

根据您的输出数组:data.txt 是 [["2017-07-25 16:44",12],["2017-07-25 17:00",12],...] 它应该是 [[1500981240000,12],[1500982200000,12],...] 所以修复是 $result['date'][] = strtotime($date)*1000 以毫秒为单位获取 unix 时间戳

<?php
$stmt = mysqli_prepare($con, "SELECT date, IFNULL(AT,'null') FROM test");
    $result = array('date' => array(), 'AT' => array());
    if ($stmt) {
        mysqli_stmt_execute($stmt);
        mysqli_stmt_bind_result($stmt, $date, $at);
        while (mysqli_stmt_fetch($stmt)) {
            $result['date'][] = strtotime($date)*1000; // get unix timestamp in milliseconds
            $result['AT'][] = (int)$at;

           $stamp = strtotime($date); // get unix timestamp
           $time = $stamp*1000;       

        }
        mysqli_stmt_close($stmt);

    }

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-06
    • 1970-01-01
    相关资源
    最近更新 更多