亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

散點圖的 Chart.js 點格式數據

散點圖的 Chart.js 點格式數據

守著一只汪 2022-10-08 17:07:32
Chart.js 散點圖只接受點格式 (x, y) 的數據。我試圖用名為 meds.json 的文件中的藥物信息填充數據點更具體地說,x 將是藥物最后一次填充日期的月份,y 將是劑量。如何從 meds.json 文件中獲取所有這些數據并將其插入數據中以創建散點圖的點?如果我嘗試獲取所有日期并將它們存儲在一個數組中,并將所有劑量值存儲在另一個數組中,我該如何使用這些數組填充點數據?這是使用 Chart.js 制作散點圖的方法,我正在嘗試填充數據“數據”下的 x、y 點:// charts.jsvar scatterChart = new Chart(ctx, {        type: 'scatter',        data: {            datasets: [{                label: 'Scatter Dataset',                data: [{                    // x = month, y = dose                    // fill these in for all meds in the json file                    x: -10,                    y: 0                }, {                    x: 0,                    y: 10                }, {                    x: 10,                    y: 5                }]            }]        },        options: {            scales: {                xAxes: [{                    type: 'linear',                    position: 'top'                }]            }        }    });}meds.json[  {    "name": "Simvastatin",    "dose": 10,    "dose unit": "mg",    "freq": "qd",    "route": "PO",    "last fill date": "2/15/2020",    "coverage": "100%",    "anticipated remaining fills": 2  },  {    "name": "Lisinopril",    "dose": 5,    "dose unit": "mg",    "freq": "qd",    "route": "PO",    "last fill date": "2/15/2020",    "coverage": "100%",    "anticipated remaining fills": 2  }      ...... The list goes on]
查看完整描述

1 回答

?
HUH函數

TA貢獻1836條經驗 獲得超4個贊

您實際上應該為此使用折線圖,這會更合適,并準確顯示月份與劑量之間的趨勢和關系。


但既然你要求散點圖......你可以這樣做:


const data = [{

    "name": "Simvastatin",

    "dose": 10,

    "dose unit": "mg",

    "freq": "qd",

    "route": "PO",

    "last fill date": "2/15/2020",

    "coverage": "100%",

    "anticipated remaining fills": 2

  },

  {

    "name": "Lisinopril",

    "dose": 5,

    "dose unit": "mg",

    "freq": "qd",

    "route": "PO",

    "last fill date": "2/15/2020",

    "coverage": "100%",

    "anticipated remaining fills": 2

  }

]


const transformedData = data.map(obj=>{

  return {

    x:new Date(obj["last fill date"]).getMonth() + 1,

    y:obj.dose,

  }

})


console.log(transformedData)


然后用作


var scatterChart = new Chart(ctx, {

        type: 'scatter',

        data: {

            datasets: [{

                label: 'Scatter Dataset',

                data: transformedData

            }]

        },

        options: {

            scales: {

                xAxes: [{

                    type: 'linear',

                    position: 'top'

                }]

            }

        }

    });

}

希望這可以幫助 !


查看完整回答
反對 回復 2022-10-08
  • 1 回答
  • 0 關注
  • 189 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號