echart.js显示图表的代码

admin 4月前 98

<?php
$previousDataFile = 'logs/159509.txt';
$data = file_get_contents($previousDataFile);
$data = "[".$data."]";
// 解码 JSON 数据
$json_data = json_decode($data, true);
// 初始化数组来存储时间戳和hs值
$timestamps = array();
$hs_values = array();
// 提取时间戳和hs值
foreach ($json_data as $entry) {
    $timestamps[] = $entry['logtime']; // 将时间转换为时间戳
  //   $timestamps[] = strtotime($entry['logtime']); // 将时间转换为时间戳
    $hs_values[] = $entry['msg']['yjl'];
}
// 构建包含时间戳和hs值的新数组
$chart_data = array();
for ($i = 0; $i < count($timestamps); $i++) {
    $chart_data[] = array("x" => $timestamps[$i], "y" => $hs_values[$i]);
}
// 输出为 JSON 格式
//echo json_encode($chart_data);
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Line Chart Example</title>
    <!-- 引入 Chart.js 库 -->
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script >
</head>
<body>
    <canvas id="myChart" width="400" height="200"></canvas>
    <script>
        // 数据
        const labels = <?php echo json_encode($timestamps); ?>;
        const data = <?php echo json_encode($hs_values); ?>;
        // 获取 canvas 元素
        var ctx = document.getElementById('myChart').getContext('2d');
        // 创建图表
        var myChart = new Chart(ctx, {
            type: 'line',
            data: {
                labels: labels,
                datasets: [{
                    label: '溢价率%',
                    data: data,
                    fill: false,
                    borderColor: 'rgb(75, 192, 192)',
                    tension: 0.1
                }]
            },
            options: {
                scales: {
                    y: {
                        beginAtZero: false
                    }
                }
            }
        });
    </script>
</body>
</html>


欢迎联系本站长QQ:3216572
最新回复 (2)
  • admin 4月前
    2
    <?php
    date_default_timezone_set('Asia/Shanghai'); 
    // 获取当前时间戳
    $current_time = time();
    function zz($preg,$con,$num=1){
    	  preg_match($preg,$con,$arr);
    	   return $arr[$num];
      }
    // 读取上次写入时间
    $previous_time_file = 'last_time.txt';
    if (file_exists($previous_time_file)) {
        $previous_time = intval(file_get_contents($previous_time_file));
    } else {
        // 如果不存在,则初始化为当前时间,以便第一次检查总是可以通过
        $previous_time = $current_time;
    }
    // 计算时间差是否超过1小时
    $one_hour = 60; // 秒数
    $time_difference = $current_time - $previous_time;
    echo $time_difference;
    echo "<br>";
    $currentDateTime = new DateTime();
    $tradingStartTime = new DateTime('today 09:30:00');
    $tradingEndTime   = new DateTime('today 15:00:00');
    // 判断是否在交易时间内
    if ($currentDateTime >= $tradingStartTime && $currentDateTime <= $tradingEndTime) {
    	if ($time_difference >= $one_hour) {
    		// 在时间内执行的操作
    		executeTradingFunction();
    		file_put_contents($previous_time_file, $current_time);
    	}
    } else {
        // 不在时间内,不执行任何操作或者执行其他非交易时段的任务
        echo "当前时间不在时间内,无法执行相关操作。";
    }
    function executeTradingFunction() {
        // 这里是你的交易相关的代码
    		/////////////////////////////////////////////////////////提取数据//////////////////////////////////////
    		$cookie_file = dirname(__FILE__).'/cookie.txt';
    		$url = "https://xueqiu.com/";
    		$ch = curl_init($url); //初始化
    		curl_setopt($ch, CURLOPT_HEADER, 0); //不返回header部分
    		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //返回字符串,而非直接输出
    		curl_setopt($ch, CURLOPT_COOKIEJAR,  $cookie_file); //存储cookies
    		curl_exec($ch);
    		curl_close($ch);
    		$url="https://xueqiu.com/S/SZ159509";
    		$ch = curl_init($url);
    		curl_setopt($ch, CURLOPT_HEADER, 0);
    		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    		curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); //使用上面获取的cookies
    		$response = curl_exec($ch);
    		curl_close($ch);
    		//$content=json_decode($response, true);
    		$con= zz('#window.STOCK_PAGE(.*)currentUser#iUs',$response);
    		//echo $con;
    		$shijia= zz('#市价:<span>(.*)</span>#iUs',$con);
    		$jingzhi= zz('#单位净值:<span>(.*)</span>#iUs',$con);
    		$yijialv= zz('#溢价率:<span>(.*)%</span>#iUs',$con);
    		
    		$chengjiaoe= zz('#"amount":(.*),"#iUs',$con); //成交额
    		$fene= zz('#"total_shares":(.*),"#iUs',$con);  //总份额
    		$shizhi= zz('#"market_capital":(.*),"#iUs',$con);  //总市值
    		
    		
    		
    		$hs=round(($chengjiaoe*100/$shizhi),2);
    		$statur= zz('#"status":"(.*)",#iUs',$con);
    		echo $statur;
    	//if($statur!="已收盘" || $statur!="休市" || $statur!="未开盘"){  
    	
    		$newData = $fene.";".$yijialv.";".$jingzhi.";".$shijia.";".$hs.";".$chengjiaoe;
    		// 将新数据写入临时txt文件(用于比较)
    		$tempFile = 'temp_data.txt';
    		file_put_contents($tempFile, $newData);
    		// 读取暂存的txt文件内容
    		$previousDataFile = 'stored_data.txt';
    		$previousData = file_get_contents($previousDataFile);
    		if ($newData !== $previousData) {
    				require_once('Log.class.php');
    				$filename = "logs/159509.txt";
    				$msg = array(
    					
    					'sj' =>  $shijia,
    					'jz' =>  $jingzhi,
    					'yjl' =>  $yijialv,
    					'hs' =>  $hs,
    					'fe' =>  $fene,
    					'cje' =>  $chengjiaoe
    				);
    				$Log = new Log();
    				$Log->writeLog($filename,$msg);
    				echo "ok";
    			// 更新上次缓存内容
    			copy($tempFile, $previousDataFile);
    		 }
    // 更新上次执行时间
     
    }//操作大函数的结尾
    ?>

    获取数据信息

  • admin 4月前
    3
    <?php 
    /*
     * 日志记录
     * 每天生成一个日志文件,当文件超过指定大小则备份日志文件并重新生成新的日志文件
     *
    */
    class Log {
    	private $maxsize = 1024000000000; //最大文件大小1M
    	
    	//写入日志
    	public function writeLog($filename,$msg){
    		$res = array();
    		$res['msg'] = $msg;
    		//$res['logtime'] = date("Y-m-d h:m:s",time());
    $res['logtime']  = date('Y-m-d H:i:s');
    		//如果日志文件超过了指定大小则备份日志文件
    		if(file_exists($filename) && (abs(filesize($filename)) > $this->maxsize)){
    			$newfilename = dirname($filename).'/'.time().'-'.basename($filename);
    			rename($filename, $newfilename);
    		}
    		//如果是新建的日志文件,去掉内容中的第一个字符逗号
    		if(file_exists($filename) && abs(filesize($filename))>0){
    			$content = ",".json_encode($res);
    		}else{
    			$content = json_encode($res);
    		}
    		//往日志文件内容后面追加日志内容
    	    file_put_contents($filename, $content, FILE_APPEND);
    	}
    	//读取日志
    	public function readLog($filename){
    		if(file_exists($filename)){
    			$content = file_get_contents($filename);
    			$json = json_decode('['.$content.']',true);
    		}else{
    			$json = '{"msg":"The file does not exist."}';
    		}
    		return $json;
    	}
    }
     ?>

    Log.class.php 文件

返回