在click chart.js上获取值

yruzcnhs  于 2023-10-18  发布在  Chart.js
关注(0)|答案(1)|浏览(184)

早上好,
使用下面的代码,我想知道如何在alert中检索日期值(例如202301,.......):alert(label +“:“+ value);
现在我的警报给了我一个结果,例如,1月:153.我希望1月:153 202301或甚至只是202301

<div class="row" id="sx"
        <?php
        $month = array(202301, 202302, 202303, 202304, 202305, 202306); 
        $nmonth = array('JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE' );
        ?>
            
        <div class="chartBox1">
            <canvas id="myChart" ></canvas>
        </div>
        
        <?php
        $values = array(153, 152, 170, 40, 80, 112);
        ?>
                
                <script>
                var ctx = document.getElementById("myChart").getContext('2d');
                var myChart = new Chart(ctx, {
                    type: 'bar',
                    data: {
                        labels: <?php echo json_encode($nmonth); ?>,
                        datasets: [{
                                label: '# Months Values',
                                data: <?php echo json_encode($values); ?>,
                                backgroundColor: [
                                    'rgba(255, 99, 132, 0.2)',
                                    'rgba(54, 162, 235, 0.2)',
                                    'rgba(255, 206, 86, 0.2)',
                                    'rgba(75, 192, 192, 0.2)',
                                    'rgba(153, 102, 255, 0.2)',
                                    'rgba(255, 159, 64, 0.2)'
                                ],
                                borderColor: [
                                    'rgba(255,99,132,1)',
                                    'rgba(54, 162, 235, 1)',
                                    'rgba(255, 206, 86, 1)',
                                    'rgba(75, 192, 192, 1)',
                                    'rgba(153, 102, 255, 1)',
                                    'rgba(255, 159, 64, 1)'
                                ],
                                borderWidth: 1
                            }
                        ]
                    },
                    options: {
                        scales: {
                            yAxes: [{
                                    ticks: {
                                        beginAtZero:true
                                    }
                                }]
                        }
                    },
                    
                    onClick:function(e) {
                    /*var activePoints = myChart.getElementsAtEvent(e);
                    var selectedIndex = activePoints[0]._index; */
                    /* alert(this.data.datasets[0].data[selectedIndex]);
                    console.log(this.data.datasets[0].data[selectedIndex]);
                    */
                    }
                });

                    /* https://github.com/chartjs/Chart.js/issues/2292 */
                document.getElementById("myChart").onclick = function (evt) {
                    var activePoints = myChart.getElementsAtEventForMode(evt, 'point', myChart.options);
                    var firstPoint = activePoints[0];
                    var label = myChart.data.labels[firstPoint._index];
                    var value = myChart.data.datasets[firstPoint._datasetIndex].data[firstPoint._index];
                    alert(label + ": " + value);
                    getNewAlert(value);
                };
                </script>
    </div>

你有办法解决我的问题吗

de90aj5v

de90aj5v1#

您正在从values数组中获取当前值。

1-首先获取对应的key/index

$month = array(202301, 202302, 202303, 202304, 202305, 202306); 
$values = array(153, 152, 170, 40, 80, 112);

$valueIndex =  array_search(153, $values); 
echo $valueIndex; // 0

2 -获取您的valueIndex对应月份

echo $month[$valueIndex]; // 202301

相关问题