javascript 具有独立于年份的日期轴的图- JS

tgabmvqs  于 2023-03-21  发布在  Java
关注(0)|答案(1)|浏览(86)

应该由chart.js绘制的数据可以在postgres表中以如下结构获得:

date        season  cum_sum
2021-12-23  2022    11
2022-01-01  2022    19
2022-01-04  2022    20
2022-01-05  2022    40 
2022-03-01  2022    43
2022-12-01  2023    3
2022-12-02  2023    7
2022-12-10  2023    11
2022-12-23  2023    17
2023-01-01  2023    19
2023-01-05  2023    30

我的想法是提供这个数据为JSONPHP.我如何绘制(线或点图)这个数据与x轴从12月到3月,没有一年,所以1月1日的值是在相同的x位置?对于每个季节,我需要一个自己的线或点的颜色.
这里我尝试:get_data.php

$sql = "...";
$rows = queryDatabase($sql);
$chartData = array();

foreach ($rows as $row) {
    // extract the month and day from the date string
    $date = strtotime($row['date']);
    $month = date('m', $date);
    $day = date('d', $date);

    // add the data to the chartData array
    $chartData[$row['season']][] = array($month . '/' . $day, $row['cum_sum']);
}

// encode the chartData array as JSON and output it
echo json_encode($chartData);

创建如下数据:

{"2022":[["01\/03","6.5"],["01\/03","6.5"],["01\/04","11.0"],["05\/12","887.7"]],"2023":[["12\/05","2.5"],["12\/05","2.5"],["12\/06","10.0"],["12\/06","10.0"],...

make_chart.php

<script>
        $( document ).ready(function() {
            // retrieve data from PHP as JSON object using fetch API
            fetch('./inc/get_data.php')
            .then(response => response.json())
            .then(data  => {
                // create chart
                var ctx = document.getElementById('myChart').getContext('2d');
                var chart = new Chart(ctx, {
                type: 'line',
                data: {
                    datasets: [{
                        label: 'Season 2022',
                        data: data.filter(d => d['2022'] ),
                        borderColor: 'blue',
                        fill: false
                        }, {
                        label: 'Season 2023',
                        data: data.filter(d => d['2023'] ),
                        borderColor: 'red',
                        fill: false
                    }]
                },
                options: {
                    scales: {
                    xAxes: [{
                        type: 'category',
                        ticks: {
                        callback: function(value, index, values) {
                            return value.replace(/-\d{2}/, '');
                        }
                        }
                    }],
                    yAxes: [{
                        ticks: {
                        beginAtZero: true
                        }
                    }]
                    }
                }
                });
            })
            .catch(error => console.error(error));
        });

</script>

获取TypeError: data.filter is not a function(编辑:在#1中解决)
编辑:x轴不是按时间顺序排列的。2023赛季的天数,不属于2022赛季的一部分,被绘制在x轴的末尾。例如:data_2022 = [“01-03”,5],[“01-10”,15] data_2023 = [“01-03”,10],[“01-05”,20] --〉01/05绘制在x轴末端

blmhpbnm

blmhpbnm1#

data不是一个数组,而是一个对象,因此它没有filter函数。您可以创建单个datasetsdata如下:

const baseData = {"2022":[["01\/02","6.5"],["01\/03","6.5"],["01\/04","11.0"],["05\/12","887.7"]],"2023":[["12\/05","2.5"],["12\/05","2.5"],["12\/06","10.0"],["12\/06","10.0"]]};

const data = baseData['2022'].map(arr => ({ x: arr[0], y: parseFloat(arr[1])}));
console.log(data );

您也可以动态创建所有datasets,类似于本答案中提出的内容:https://stackoverflow.com/a/75635274/2358409 .

相关问题