javascript - Datalabels 插件 chartjs 显示 '[object]' 而不是值

标签 javascript node.js chart.js react-chartjs

我正在从 app.js 文件传递​​对象并对其进行字符串化,但它在数据标签中无法正常工作,并显示 [object as shown in image ]
这是我的代码:[它在 ejs 中,但与 js 几乎相同,除了将变量传递为 <%-variable%>,而不是 ${}]
应用程序.js

let bubbleData=[];

let dataObj = {
                "pastObservation": [
                                {
                                    "value": "400",
                                    "observation_time": "2021-08-12"
                                },
                                {
                                    "value": "800",
                                    "observation_time": "2021-08-13"
                                },
                                {
                                    "value": "13",
                                    "observation_time": "2021-08-11"
                                },
                                {
                                    "value": "370",
                                    "observation_time": "2021-08-15"
                                }
};

//parsing data from observation time, and value, and pushing them in array

dataObj.pastObservation.map( value => { console.log( value.value, value.observation_time ); let object={ "x": value.value, "y": JSON.stringify( Date.parse( value.observation_time ) ) }; bubbleData.push( object ); } );


app.get( '/one', function ( req, res ) {
    res.render( 'chartOne', { bubbleData: bubbleData } );
} );

我的 ejs 文件,虽然我正在对传递的标签进行字符串化,但 datalabels 插件仍然将它们作为对象,而不是 strigyfied 值:
<canvas id="LineChart" height="100"></canvas>

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.5.0/chart.min.js"
    integrity="sha512-asxKqQghC1oBShyhiBwA+YgotaSYKxGP1rcSYTDrB0U6DxwlJjU59B67U8+5/++uFjcuVM8Hh5cokLjZlhm3Vg=="
    crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels@2"></script>

<script>
    var ctx=document.getElementById( 'LineChart' );
    var myChart=new Chart( ctx, {
        type: 'bubble',
        data: {
            datasets: [ {
                label: 'First Dataset',
                data: <%- JSON.stringify( bubbleData )%>,
                backgroundColor: 'rgb(255, 99, 132)',
                radius: 15,
            } ],
        },
        plugins: [ ChartDataLabels ],
        options: {
            plugins: {
                datalabels: {
                    color: 'black',
                    font: {
                        weight: 'bold'
                    },
                },
            },
            scales:
            {
                y: {
                    ticks: { // return data in date format, instead of numeric as date parsed
                        callback: ( value ) => { return new Date( value).toLocaleDateString( "en-US", { day: "2-digit", month: "short" } ); }

                    }
                }
            },
        },
    } );





最佳答案

我能够弄清楚这一点,而不是显示整个值,在当前情况下是一个对象,我不得不在插件选项中使用格式化程序和一个回调函数来获取 x 的值。

options: {
            plugins: {
                datalabels: {
                    formatter: ( val ) => {
                        return val.x
                    },
                 }
             }
    }

关于javascript - Datalabels 插件 chartjs 显示 '[object]' 而不是值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68783624/

相关文章:

javascript - Nodejs + API 剩余结构

javascript - 如何在javascript中获取日期选择器值的完整(包括小时,分钟和秒)日期格式

javascript - Angular 2 ng2-redux 和 ngrx/store

javascript - 如何使用 Electron 获取文件夹路径

javascript - 图表 JS : Getting issue with draw bar chart

javascript - 将此 canvasjs 图表转换为 chart.js 图表

javascript - 我如何在 WebAudio 中使用 AudioContext

javascript - 如何在node js中模块化代码

node.js - Joi 验证 - 如何根据数组中存在的另一个键来要求或可选字段?

javascript - Chart.js 不显示在 Bootstrap 选项卡面板中