<?php
/*
 * @Author: 杨水淼 yangshuimiao@jsjd.cc
 * @Date: 2025-07-16 14:34:25
 * @LastEditors: 杨水淼 yangshuimiao@jsjd.cc
 * @LastEditTime: 2025-07-17 10:45:36
 * @FilePath: \echarts\stockep.php
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
$_REQUEST['ts_code']=$_REQUEST['ts_code']?$_REQUEST['ts_code']:'000001';
$_REQUEST['t_start']=$_REQUEST['t_start']?$_REQUEST['t_start']:'2022-01-01';
$_REQUEST['t_end']=$_REQUEST['t_end']?$_REQUEST['t_end']:date('Y-m-d');
$title="股价VS EP趋势:".$ts_name.'('.$_REQUEST['ts_code'].')';
include_once "html/head.php";
?>
<script src="js/renderCharts.js"></script>
<form name="main" id="main" method="get"> <div style="width:100%;text-align:center">
股票代码: <input type='text' name='ts_code' id='ts_code' width="30px" value='<?=$_REQUEST['ts_code']?>' >
开始时间: <input type='date' name='t_start' id='t_start' width='30px' value='<?=$_REQUEST['t_start']?>' >
结束时间: <input type='date' name='t_end' id='t_end' width='30px' value='<?=$_REQUEST['t_end']?>' >
 <button id="submitBtn" class="btn-submit"><span>查询数据</span></button>
<div >&nbsp;</div>

</div> </form>
<div style="margin:0 auto;clear:both;left:100px;width:1000px;height:30px;text-align:left"><span style="color:grey">EP=100*TTM每股收益/股价。如：EP=5表示每股收益率5%</span></div>
 <!-- 图表容器 -->
  <div id="container" style="width: 1000px;height:400px; margin: 0 auto 20px;">
        <div class="loading-overlay" id="loadingOverlay">
            <div class="spinner"></div>
            <p style="font-size:18px; color:#2c3e50;">正在加载数据，请稍候...</p>
        </div>

    </div>
</div>


<script>
    // 页面加载时初始化图表
    $(document).ready(function() {
  
        // 加载初始数据
        loadChartData();
        
        // 绑定按钮点击事件
        $('#submitBtn').click(function(e) {
            //e.preventDefault(); // 阻止表单提交刷新页面
            $('#loadingOverlay').addClass('active');
            loadChartData();
            $('#loadingOverlay').removeClass('active');
        });
    });

    // 加载图表数据的函数
    function loadChartData() {
        // 显示加载动画
        $('#loadingOverlay').addClass('active');

        let t_start = $('#t_start').val();
        let t_end = $('#t_end').val();
        let ts_code = $("#ts_code").val();

        url =   "https://api.doorcome.cn/api/stockep/?tscode="+ts_code;
        url +=  "&start_date="+t_start+"&end_date="+t_end;
        url2 = "https://api.doorcome.cn/api/stockinfo/?tscode="+ts_code;
        Promise.all([
            fetch(url),
            fetch(url2)
            ])
            .then(responses => Promise.all(responses.map(r => r.json())))
            .then(([parsedData,stockInfo]) => {
                // 图表配置:pe_ttm
                let codeName = stockInfo[0].name;
                var params = {};
                params.chartid='container';
                params.legend = [codeName,'EP'];
                params.text = params.legend[0]+' V.S '+params.legend[1];
                params.data1 = pickData(parsedData, 'trade_date', 'close');
                params.data2 = pickData(parsedData, 'trade_date', 'basic_ep_ttm');
                dataCal = calculateStats(params.data2);
                params.sub_text = "TTM EP Max:"+dataCal['max'];
                params.sub_text += ",  Min:"+dataCal['min'];
                params.sub_text += ",  Average:"+dataCal['avg'];
                params.sub_text += ",  Recent:"+dataCal['last'];
                doubleLineChart(params);
               
            })
            .catch(error => console.error('数据加载失败:', error))
            .finally(() => {
                // 隐藏加载动画
                $('#loadingOverlay').removeClass('active');
            });
    }
</script>

</div>
<?php include_once "html/footer.php"; ?>

