数据可视化【十二】 颜色图例和尺寸图例
生活随笔
收集整理的這篇文章主要介紹了
数据可视化【十二】 颜色图例和尺寸图例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
有了前面的知識,制作一個圖例應該不是很難,關鍵是我們想要制作一個可以在其他地方進行使用的圖例,這樣就需要能夠動態地設置圖例的大小,位置,等等。
這里直接上代碼:
colorLegend.js
export const colorLegend = (selection, props) => {const {colorScale, height, circleRadius, spacing,textOffset} = props;const groups = selection.selectAll('g').data(colorScale.domain());const groupEnter = groups.enter().append('g').attr('class', 'tick').attr('transform', (d,i) => `translate(0, ${i*spacing+circleRadius})`);;groups.exit().remove();groupEnter.append('circle').attr('r', 0).merge(groups.select('circle')) //both enter section and update section.attr('fill', colorScale).transition().duration(1000).attr('r', circleRadius);const text = groups.select('text');groupEnter.append('text').attr('x', textOffset).attr('dy', '0.32em').merge(text) //both enter section and update section.text(d => d)}sizeLegend.js
export const sizeLegend = (selection, props) => {const {sizeScale, height, spacing,textOffset, numTicks, circleFill} = props;const ticks = sizeScale.ticks(numTicks).filter(d => d !== 0);const groups = selection.selectAll('g').data(ticks);const groupEnter = groups.enter().append('g').attr('class', 'tick').attr('transform', (d,i) => `translate(0, ${i*spacing})`);;groups.exit().remove();groupEnter.append('circle').attr('r', 0).merge(groups.select('circle')) //both enter section and update section.attr('fill', circleFill).transition().duration(1000).attr('r', d => sizeScale(d) - 10);const text = groups.select('text');groupEnter.append('text').attr('x', d => sizeScale(d) + textOffset).attr('dy', '0.32em').merge(text) //both enter section and update section.text(d => d)}上面的代碼就可以直接在其他地方使用,例如:
index.js
效果圖:
代碼地址:https://vizhub.com/Edward-Elric233/bc54edb3b722482590f498f3a1047a62
總結
以上是生活随笔為你收集整理的数据可视化【十二】 颜色图例和尺寸图例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 明日之后适者生还任务有什么用
- 下一篇: 数据可视化【十三】地区分布图