d3 實(shí)現(xiàn)簡(jiǎn)單熱力圖

熱力圖效果圖如下油额,支持縮放


<div id="data_div" style="background-color: rgb(190, 228, 155); width:'500px'; height: '500px'; ">

</div>

setChart() {

? ? const margin = {top: 80, right: 25, bottom: 30, left: 40},

? ? width = 450 - margin.left - margin.right,

? ? height = 450 - margin.top - margin.bottom;

? ? // append the svg object to the body of the page

? ? const svg = d3.select("#data_div")

? ? ? .append("svg")

? ? ? ? .attr("width", width + margin.left + margin.right)

? ? ? ? .attr("height", height + margin.top + margin.bottom)

? ? ? ? .call(d3.zoom().on("zoom", function (event) { ?// 控制縮放

? ? ? ? ? svg.attr("transform", event.transform)

? ? ? ? }))

? ? ? .append("g")

? ? ? ? .attr("transform", `translate(${margin.left}, ${margin.top})`);

? ? //Read the data

? ? const data = [

? ? ? {group:'A', variable:'V1', value:'30'},

? ? ? {group:'A', variable:'V2', value:'60'},

? ? ? {group:'A', variable:'V4', value:'40'},

? ? ? {group:'B', variable:'V1', value:'50'},

? ? ? {group:'B', variable:'V2', value:'80'},

? ? ? {group:'C', variable:'V3', value:'70'},

? ? ? {group:'C', variable:'V1', value:'40'},

? ? ? {group:'D', variable:'V3', value:'90'},

? ? ? {group:'D', variable:'V4', value:'80'},

? ? ? {group:'E', variable:'V4', value:'50'},

? ? ? {group:'E', variable:'V5', value:'80'},

? ? ? {group:'F', variable:'V5', value:'60'},

? ? ? {group:'F', variable:'V6', value:'70'},

? ? ];

? ? // Labels of row and columns -> unique identifier of the column called 'group' and 'variable'

? ? const myGroups = Array.from(new Set(data.map(d => d.group)))

? ? const myVars = Array.from(new Set(data.map(d => d.variable)))

? ? // Build X scales and axis,Y scales and axis:

? ? const x = d3.scaleBand()

? ? ? .range([ 0, width ])

? ? ? .domain(myGroups)

? ? ? .padding(0.05);

? ? svg.append("g")

? ? ? .style("font-size", 15)

? ? ? .attr("transform", `translate(0, ${height})`)

? ? ? .call(d3.axisBottom(x).tickSize(0))???// tickSize(0) 去掉坐標(biāo)點(diǎn)的指示線

? ? ? .select(".domain").remove()??// 去掉X軸的坐標(biāo)線

? ? const y = d3.scaleBand()

? ? ? .range([ height, 0 ])

? ? ? .domain(myVars)

? ? ? .padding(0.05);

? ? svg.append("g")

? ? ? .style("font-size", 15)

? ? ? .call(d3.axisLeft(y).tickSize(0))

? ? ? .select(".domain").remove()

? ? // Build color scale

? ? const myColor = d3.scaleSequential()

? ? ? .interpolator(d3.interpolateInferno)

? ? ? .domain([1,100])

? ? // create a tooltip

? ? const tooltip = d3.select("#data_div")

? ? ? .append("div")

? ? ? .style("opacity", 0)

? ? ? .attr("class", "tooltip")

? ? ? .style("background-color", "cyan")

? ? ? .style("border", "solid")

? ? ? .style("border-width", "1px")

? ? ? .style("border-radius", "5px")

? ? ? .style("padding", "5px")

? ? // Three function that change the tooltip when user hover / move / leave a cell

? ? const mouseover = function(event,d) {

? ? ? tooltip

? ? ? ? .style("opacity", 1)

? ? ? d3.select(this)

? ? ? ? .style("stroke", "black")

? ? ? ? .style("opacity", 1)

? ? }

? ? const mousemove = function(event,d) {

? ? ? tooltip

? ? ? ? .html("The exact value of<br>this cell is: " + d.value)

? ? ? ? .style("left", "120px")

? ? ? ? .style("top", ((event.x)/2 + 11) + "px")

? ? }

? ? const mouseleave = function(event,d) {

? ? ? tooltip

? ? ? ? .style("opacity", 0)

? ? ? d3.select(this)

? ? ? ? .style("stroke", "none")

? ? ? ? .style("opacity", 0.8)

? ? }

? ? // add the squares

? ? svg.selectAll()

? ? ? .data(data, function(d) {return d.group+':'+d.variable;})

? ? ? .join("rect")

? ? ? ? .attr("x", function(d) { return x(d.group) })

? ? ? ? .attr("y", function(d) { return y(d.variable) })

? ? ? ? .attr("rx", 4)

? ? ? ? .attr("ry", 4)

? ? ? ? .attr("width", x.bandwidth() )

? ? ? ? .attr("height", y.bandwidth() )

? ? ? ? .style("fill", function(d) { return myColor(d.value)} )

? ? ? ? .style("stroke-width", 4)

? ? ? ? .style("stroke", "none")

? ? ? ? .style("opacity", 0.8)

? ? ? .on("mouseover", mouseover)

? ? ? .on("mousemove", mousemove)

? ? ? .on("mouseleave", mouseleave)

? ? // Add title to graph

? ? svg.append("text")

? ? ? .attr("x", 0)

? ? ? .attr("y", -50)

? ? ? .attr("text-anchor", "left")

? ? ? .style("font-size", "22px")

? ? ? .text("A d3.js heatmap");

? ? // Add subtitle to graph

? ? svg.append("text")

? ? ? .attr("x", 0)

? ? ? .attr("y", -20)

? ? ? .attr("text-anchor", "left")

? ? ? .style("font-size", "14px")

? ? ? .style("fill", "grey")

? ? ? .style("max-width", 400)

? ? ? .text("A short description of the take-away message of this chart.");

? }

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市掂僵,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌锰蓬,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,252評(píng)論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件麻顶,死亡現(xiàn)場(chǎng)離奇詭異舱卡,居然都是意外死亡辅肾,警方通過(guò)查閱死者的電腦和手機(jī)灼狰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,886評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)份汗,“玉大人,你說(shuō)我怎么就攤上這事杯活“敬剩” “怎么了旁钧?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,814評(píng)論 0 361
  • 文/不壞的土叔 我叫張陵歪今,是天一觀的道長(zhǎng)嚎幸。 經(jīng)常有香客問(wèn)我寄猩,道長(zhǎng),這世上最難降的妖魔是什么田篇? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,869評(píng)論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮椎镣,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘状答。我一直安慰自己刀崖,他們只是感情好剪况,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,888評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布蒲跨。 她就那樣靜靜地躺著授翻,像睡著了一般。 火紅的嫁衣襯著肌膚如雪堪唐。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 52,475評(píng)論 1 312
  • 那天男公,我揣著相機(jī)與錄音,去河邊找鬼枢赔。 笑死拥知,一個(gè)胖子當(dāng)著我的面吹牛踏拜,可吹牛的內(nèi)容都是我干的低剔。 我是一名探鬼主播肮塞,決...
    沈念sama閱讀 41,010評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼姻锁,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了位隶?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,924評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤钓试,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后恋谭,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,469評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡疚颊,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,552評(píng)論 3 342
  • 正文 我和宋清朗相戀三年信认,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片嫁赏。...
    茶點(diǎn)故事閱讀 40,680評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖款熬,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情贤牛,我是刑警寧澤,帶...
    沈念sama閱讀 36,362評(píng)論 5 351
  • 正文 年R本政府宣布殉簸,位于F島的核電站沽讹,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏妥泉。R本人自食惡果不足惜椭微,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,037評(píng)論 3 335
  • 文/蒙蒙 一盲链、第九天 我趴在偏房一處隱蔽的房頂上張望蝇率。 院中可真熱鬧,春花似錦本慕、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,519評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)浪腐。三九已至,卻和暖如春议街,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背特漩。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,621評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工骨杂, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留涂身,地道東北人搓蚪。 一個(gè)月前我還...
    沈念sama閱讀 49,099評(píng)論 3 378
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像妒潭,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子杜耙,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,691評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容