數據可視化【二】HTML+CSS+SVG+D3

HTML、CSS和SVG學習實現代碼:https://vizhub.com/Edward-Elric233/89185eb96bc64a9d81777873a0ccd0b9
index.html

<!DOCTYPE html>
<html><head><title>Shapes with SVG and CSS</title><link rel="stylesheet" href="./style.css">
</head><body><svg width="960" height="800"><g transform="scale(1.5)"><!--通過上面的函數放大--><circle cx="50" cy="50"r="50"></circle><rect x="100" y="25" width="50" height="50" fill="red"></rect><!--stoke 邊界顏色transform 設置g的位置lines 和 path 的區別:lines線段和線段連接的地方是斷開的path的連接是圓滑的 stroke-linejoin round設置線連接的地方是圓滑的--><g transform="translate(0,100)" fill="blue" stroke="black"><circle cx="50" cy="50"r="50" stroke-width="5"></circle><rect x="100" y="25" width="50" height="50" fill="red"></rect></g><g transform="translate(100,50)" class="lines"><line x1="100"y1="0" x2="100" y2="100"></line><path fill="none" d="M100 100 L200 100 L100 0"></path></g></g></svg>
</body></html>

index.js

body {margin: 0px;overflow: hidden;font-size: 2em;font-family: manosapce;
}.highlighted {color: red;
}.lines {stroke: black;stroke-width: 5;
}.lines path {stroke: red;stroke-linejoin: round;
}

在這里插入圖片描述

svg通過在html里面用組件進行繪畫得到各種各樣的圖形。為了方便繪制,可以將他們放在g標簽里面,再通過設置transform屬性進行移動。

D3學習實現代碼:https://vizhub.com/Edward-Elric233/a0996081ad68437aac3bf23612f6347c
index.html

<!DOCTYPE html>
<html><head><title>Let's make a face with D3.js</title><link rel="stylesheet" href="./styles.css"><script src="https://unpkg.com/d3@5.7.0/dist/d3.min.js"></script><!-- find D3 file on UNPKG d3.min.js-->
</head><body><svg width="960" height="500"></svg><script src="./index.js">console.log(d3); //test whether you have imported d3.js or not</script></body></html>

styles.css

body {margin: 0px;overflow: hidden;font-size: 2em;font-family: manosapce;
}.highlighted {color: red;
}.lines {stroke: black;stroke-width: 5;
}.lines path {stroke: red;stroke-linejoin: round;
}

index.js

const svg = d3.select('svg');
// svg.style('background-color', 'red'); testconst height = +svg.attr('height');
//+ equals paresFloat()
const width = +svg.attr('width');const g = svg.append('g').attr('transform', `translate(${width/2}, ${height/2})`);const circle = g.append('circle');circle.attr('r', height / 2).attr('fill', 'yellow').attr('stroke', 'black');const eyeSpacing = 100;
const eyeYOffset = -80;
const eyeRadius = 30;
const eyebrowWidth = 50;
const eyebrowHeight = 20;
const eyebrowYOffset = -60;
const mouthYOffset = -15;const eyesG = g.append('g').attr('transform', `translate(0, ${eyeYOffset})`);const leftEye = eyesG.append('circle').attr('r', eyeRadius).attr('cx', -eyeSpacing);const rightEye = eyesG.append('circle').attr('r', eyeRadius).attr('cx', eyeSpacing);const eyebrowG = eyesG.append('g').attr('transform', `translate(0, ${eyebrowYOffset})`);eyebrowG.transition().duration(2000).attr('transform', `translate(0, ${eyebrowYOffset-10})`).transition().duration(1000).attr('transform', `translate(0, ${eyebrowYOffset})`);const leftEyebrow = eyebrowG.append('rect').attr('width', eyebrowWidth).attr('height', eyebrowHeight).attr('x', -eyeSpacing - eyebrowWidth / 2);const rightEyebrow = eyebrowG.append('rect').attr('width', eyebrowWidth).attr('height', eyebrowHeight).attr('x', eyeSpacing - eyebrowWidth / 2);const mouth = g.append('path').attr('d', d3.arc()({innerRadius: 100,outerRadius: 120,startAngle: Math.PI / 2,endAngle: Math.PI * 3 / 2})).attr('transform', `translate(0, 50)`).transition().duration(2000).attr('d', d3.arc()({innerRadius: 120,outerRadius: 140,startAngle: Math.PI / 2,endAngle: Math.PI * 3 / 2})).transition().duration(1000).attr('d', d3.arc()({innerRadius: 100,outerRadius: 120,startAngle: Math.PI / 2,endAngle: Math.PI * 3 / 2}));

在這里插入圖片描述

使用d3其實是通過d3的函數繪制svg圖形,不過因為d3提供了方便的接口,使得繪制更加方便。

想要繪制什么圖形可以到D3官網查詢API。

而且d3繪制的圖形通過使用transition函數還能變形。以上面的笑臉為例可以讓笑臉進行變化 。雖然程度有限。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/383617.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/383617.shtml
英文地址,請注明出處:http://en.pswp.cn/news/383617.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

數據可視化【三】基本概念

Visualization is suitable when there is a need to augment human capabilities rather than replace people with computational decision-making methods. 當可以信賴的智能化的解決方案存在的時候&#xff0c;可視化是不必要的。 當不知道需要分析的問題是什么的時候&…

數據可視化【四】Bar Chart

Make a Bar Chart Representing a data table in JavaScriptCreating rectangles for each rowUsing linear and band scalesThe margin conventionAdding axes 以下學習內容參考博客&#xff1a;傳送門 select()選擇所有指定元素的第一個 selectAll()選擇指定元素的全部 上…

數據庫原理及應用【三】DBMS+SQL

DBMS Query LanguagesInterface and maintaining tools(GUI)APIsClass Library QL 不是圖靈完備的&#xff0c;不是一種編程語言。 QL SQL是一種非過程化的查詢語言。 DDL數據定義語言&#xff1a;表&#xff0c;視圖QL 查詢語言DML 數據操縱語言DCL 數據控制語言 Base t…

數據可視化【五】 Scatter Plot

Scatter Plot vizhub上實現的代碼&#xff1a; https://vizhub.com/Edward-Elric233/53807a1b35d94329b3689081cd2ea945 https://vizhub.com/Edward-Elric233/b9647d50899a4a0e8e917f913cd0a53a https://vizhub.com/Edward-Elric233/8c6b50cd81a04f048f490f48e4fe6264 由前…

數據可視化【六】Line Chart Area Chart

Line Chart vizhub代碼&#xff1a; https://vizhub.com/Edward-Elric233/094396fc7a164c828a4a8c2e13045308 實現效果&#xff1a; 這里先使用d3.line()設置每個點的x坐標和y坐標&#xff0c;然后再用這個東西設置path的d屬性&#xff0c;就可以得到曲線。 const lineGen…

數據可視化【七】 更新模式

Enter 以下面這個簡單的代碼進行分析 const svg d3.select(svg); // svg.style(background-color, red); testconst height svg.attr(height); // equals paresFloat() const width svg.attr(width);const makeFruit type >( {type} ); //這種寫法好像能夠直接得到一個…

數據可視化【八】根據數據類型選擇可視化方式

Marks:Rows PointsLinesAreas Channels:Columns PositionColorShape

數據可視化【九】單向數據流交互

我們使用一下上上篇博客的代碼。 例如我們想要當鼠標點擊水果的時候會出現黑色的框&#xff0c;再點擊一下黑色的框就會消失。 首先&#xff0c;我們應該給組件添加點擊事件&#xff1a; fruitBowl.js gruopAll.on(click, d > onClick(d.id));這個on函數第一個參數是事件…

數據庫原理及應用【四】數據庫管理系統

查詢優化 數據庫管理系統中非常重要的一部分。 代數優化 按照一定的規則將語句變化成關系代數以后進行優化 操作優化 對代數優化后的查詢樹使用比較好的方法進行查詢。 主要是對連接運算進行優化 嵌套循環歸并掃描索引優化哈希連接 恢復機制 備份&#xff08;完整備份差…

數據庫原理及應用【五】安全性和完整性約束

數據庫一致性被破壞&#xff1a; 系統故障許多用戶的并發訪問人為破壞事務本身不正確 保護數據庫一致性的方法&#xff1a; 視圖/查詢修改訪問控制 普通用戶擁有資源特權的用戶DBA 數據庫的安全問題 身份驗證 口令物理設備 GRANT CONNECT TO John IDENTIFIED BY 123456…

遞歸式復雜度求解

代換法 猜測復雜度驗證是否滿足遞歸式&#xff08;使用歸納法&#xff09;找到常數應該滿足的條件針對基本情況&#xff0c;常數足夠大時總是成立的 需要注意的是&#xff0c;我們猜測的復雜度有可能不滿足遞歸式&#xff0c;這個時候就要通過減去一些低階項來使得歸納成立。…

斐波那契數列計算

定義 斐波那契數列&#xff1a; F[n]{0,n01,n1F[n?1]F[n?2],elseF[n] \begin{cases} 0,n0 \\ 1,n1\\ F[n-1]F[n-2],else \end{cases} F[n]??????0,n01,n1F[n?1]F[n?2],else? 樸素計算法 根據遞歸式F[n]F[n?1]F[n?2]F[n]F[n-1]F[n-2]F[n]F[n?1]F[n?2]進行計算…

P、NP、NP完全問題、NP難問題

可以在多項式時間內求解的問題稱為易解的&#xff0c;而不能在多項式時間內求解的問題稱為難解的。 P類問題&#xff1a;多項式類型&#xff0c;是一類能夠用&#xff08;確定性的&#xff09;算法在多項式的時間內求解的判定問題。 只有判定問題才屬于P 不可判定問題&#…

數據可視化【十】繪制地圖

Loading and parsing TOPOJSON 導入Topojson d3文件 地址&#xff1a;https://unpkg.com/topojson3.0.2/dist/topojson.min.js 想要找d3文件的話去unpkg.com好像大部分都能找到的樣子 Rendering geographic features 尋找合適的地圖數據&#xff1a;谷歌搜索world-atlas npm…

數據可視化【十一】樹狀圖

Constructing a node-link tree visualization 首先將節點之間的連線畫出來。 使用json函數讀取文件以后&#xff0c;使用hierarchy等函數得到連線的數組&#xff0c;然后綁定這個數組&#xff0c;給每個元素添加一個path&#xff0c;繪畫使用的是一個函數linkHorizontal&…

數據可視化【十二】 顏色圖例和尺寸圖例

有了前面的知識&#xff0c;制作一個圖例應該不是很難&#xff0c;關鍵是我們想要制作一個可以在其他地方進行使用的圖例&#xff0c;這樣就需要能夠動態地設置圖例的大小&#xff0c;位置&#xff0c;等等。 這里直接上代碼&#xff1a; colorLegend.js export const color…

數據可視化【十三】地區分布圖

在前面的博客中已經介紹了如何繪制地圖&#xff0c;這一節學習如何繪制地區分布圖。如果對繪制地圖還不熟悉的話可以了解一下之前我寫的博客&#xff1a;數據可視化【十】繪制地圖 Intergrating(整合) TopoJSON with tabular data(列表數據) 在前面的博客中沒有使用到tsv文件…

3.01【python正則表達式以及re模塊】

python正則表達式以及re模塊 元字符 正則表達式的語法就由表格中的元字符組成&#xff0c;一般用于搜索、替換、提取文本數據 元字符含義.匹配除換行符以外的任何單個字符*匹配前面的模式0次或1次匹配前面的模式1次或多次?匹配前面的模式0次或1次[]用于定義字符集&#xff…

Linux配置編程環境+云服務器上傳文件

Java環境配置 Ubuntu https://www.cnblogs.com/lfri/p/10437266.html Centos https://blog.csdn.net/qq_21077715/article/details/85536399 Tomcat配置 Centos https://blog.csdn.net/qq_21077715/article/details/85541685 https://www.cnblogs.com/newwind/p/9904561…

gbd + cgbd

gbd&#xff1a;傳送門 cgbd&#xff1a;傳送門 | 傳送門