iT邦幫忙

0

【已解決-D3js】畫兩條線時,要如何畫兩條線的點

  • 分享至 

  • xImage

大家好,
我想請問一下,當有兩組資料( d1 跟 d2 )時,
我這樣綁定

let getData = [
    {d1:0,d2:0,xAxis:0},
    {d1:10,d2:5,xAxis:1},
    {d1:25,d2:15,xAxis:2}
]

let myMap = new Map()
myMap.set('d1', getData)
myMap.set('d2', getData)

在畫 line 跟畫 area 時,沒有問題,如下
(怕版面太多,所以有刪了一些跟style有關的code,例如 .attr('fill','#fff') )

let line = svg.append('g')
    .attr("clip-path", "url(#clip)")
line.selectAll('.area')
    .data(myMap)
    .join('path')
    .attr('d', (d, i) => {
        let key = d[0]
        return d3.area()
            .defined((d, i) => d[key] || i == 0)
            .x(d => scaleX(d['xAxis']) )
            .y0( scaleY(0) )
            .y1(d => scaleY(d[key]) )
            (d[1])
    })
line.selectAll('.line')
    .data(myMap)
    .join('path')
    .attr('d', (d, i) => {
        let key = d[0]
        return d3.line()
            .defined((d, i) => d[key] || i == 0)
            .x(d => scaleX(d['xAxis']) )
            .y(d => scaleY(d[key]) )
            (d[1])
    })

但在畫點的時候,cx 跟 cy 那邊就出錯了

line.selectAll('.point')
    .data(myMap)
    .join('circle')
    .attr("cx", (d, i) => {
        // i是 new Map的 i,只會有 0 跟 1
        //最後return的會是陣列,所以出錯
        let key = d[0]
        return d[1].map((e,i)=>{
            if(e[key] || i == 0) return scaleX(e['xAxis'])
        }).filter(e=>e) //篩掉 y value == undefined 的值
    })
    .attr("cy", (d, i) =>{
        // i是 new Map的 i,只會有 0 跟 1
        //最後return的會是陣列,所以出錯
        let key = d[0]
        return d[1].map(e=>{
            if(e[key] || i == 0) return scaleY(e[key])
        }).filter(e=>e) //篩掉 y value == undefined 的值
    })
    .attr("r", 3)

目前想不到該怎麼處理 circle,請問該怎麼做呢?

-----已解決--------
只能重新整理資料,再帶入

myMap.forEach((value, key) => {
    let dataArray = value.map((e, i) => {
        if (e[key] || i == 0) {
            let obj = {
                data: e[key],
                xAxis: e['xAxis']
            }
            return obj
        }
    }).filter(e => e) //filter是確保,數值不是undefined
    
    line.selectAll('.point')
        .data(dataArray)
        .join('circle')
        .attr("fill", 'none')
        .attr("stroke", 'blue')
        .attr("stroke-width", 2)
        .attr("cx", (d, i) => scaleX(d['xAxis'])
        .attr("cy", (d, i) => scaleY(d['data']))
        .attr("r", 3)
});
看更多先前的討論...收起先前的討論...
froce iT邦大師 1 級 ‧ 2023-01-04 16:25:17 檢舉
看不太懂你circle的邏輯,cx和cy不是圓心嗎?怎麼會吐array?
greenriver iT邦研究生 5 級 ‧ 2023-01-04 16:37:49 檢舉
因為我綁的 data 是 myMap ,
myMap = { d1=> getData , d2=> getData }
所以 .attr( 'cx' , ( d , i ) => {
d = myMap
d[0] = key,d[1] = 整包 getData ,
主要是這個 i 是 myMap 的 index,不是 getData 的 index
所以吐不出 getData 裡的資料..
})
greenriver iT邦研究生 5 級 ‧ 2023-01-04 16:41:19 檢舉
還是說如果得到 getData 時,
想同時畫兩個線條(有線、有點、有線下面的區塊顏色)時,該怎麼綁資料呢?
greenriver iT邦研究生 5 級 ‧ 2023-01-04 16:44:09 檢舉
目前我只想到重新整理資料成這樣
d1=[ 0 , 10 , 25 ]
d2=[ 0 , 5 , 15 ]
xAxis=[ 0 , 1 , 2]
froce iT邦大師 1 級 ‧ 2023-01-04 17:09:39 檢舉
你要不要先弄個playground,我好測試看看。
另外你弄個示意圖表示一下你要的效果好了。
晚上有空我寫寫看。
froce iT邦大師 1 級 ‧ 2023-01-04 17:27:34 檢舉
https://d3-graph-gallery.com/graph/line_basic.html

你要畫像折線圖這類需要前後資料參考的不應該這樣搞。
froce iT邦大師 1 級 ‧ 2023-01-05 10:35:31 檢舉
https://d3-graph-gallery.com/graph/area_lineDot.html
我猜你是要這種圖啦...
但我從你的資料上看不懂意圖就是了
greenriver iT邦研究生 5 級 ‧ 2023-01-05 11:16:42 檢舉
謝謝 froce 大大的幫忙,我是想要畫
https://d3-graph-gallery.com/graph/area_lineDot.html
這種圖沒錯,不過是有好幾條線。
我正在嘗試中~
froce iT邦大師 1 級 ‧ 2023-01-05 12:40:18 檢舉
你要注意area和line都只是一個path去畫路徑出來而已,點才需要生出多個點,這會影響到你用datum還是data。
greenriver iT邦研究生 5 級 ‧ 2023-01-06 16:52:29 檢舉
謝謝 froce大大,我成功了。
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答