1-安装NodeJS(自带包管理器NPM),测试安装成功在控制台运行:node --version
2-写一个server.js文件,运行它:node server.js(类似HelloWorld)
写server.js文件
var http=require('http');
http.createServer(function (request, response){
response.writeHead(200,{'Content-Type':'text/plain'});
response.end('Hello World\n');
}).listen(8888);
console.log('Server running at http://127.0.0.1:8888/');
运行server.js
3.安装vue,控制台:npm install vue
4.安装vite,控制台:npm install vite
5.创建一个vue空工程:npm init vite-app hellovue
6.运行这个Web工程:npm run dev
7.安装VScode
8.创建工程yarn create @vitejs/app my-vue-app --template vue,在VSCode里打开运行
3.Vue+D3可视化
安装d3 yarn add axios
安装axios yarn add axios
在public文件夹下导入数据文件alphabet.json
接着在source文件夹中的components文件夹下建立BarChart.vue文件,文件代码如下
<template>
<h2>📊 咱是直方图</h2>
<div id="bar-chart-container"></div>
</template>
<script>
import { defineComponent } from "vue";
import axios from "axios";
import * as d3 from "d3";
export default defineComponent({
data() {
return {
color: "steelblue",
margin: { top: 30, right: 0, bottom: 30, left: 40 },
};
},
/**
* 在挂载后即开始执行
*/
mounted() {
axios.get("./alphabet.json").then((res) => {
const barChartData = Object.assign(this.formatData(res.data), {
format: "%",
y: "↑ Frequency",
});
this.drawBarChart(barChartData);
});
},
methods: {
/**
* 格式化数据
*/
formatData(data) {
return data
.map(({ letter, frequency }) => {
return { name: letter, value: frequency };
})
.sort((a, b) => d3.descending(a.value, b.value));
},
/**
* 绘制直方图
*/
drawBarChart(data) {
const margin = this.margin;
const width = 800;
const height = 500;
// 初始化 SVG 元素
const svg = d3
.select("#bar-chart-container")
.append("svg")
.attr("class", "bar-chart")
.attr("viewBox", `0 0 ${width} ${height}`)
.attr("width", width)
.attr("height", height)
.append("g");
// https://observablehq.com/@d3/d3-scaleband
// x 轴的缩放比例尺
const x = d3
.scaleBand()
.domain(d3.range(data.length))
.range([margin.left, width - margin.right])
.padding(0.1);
// y 轴的缩放比例尺
const y = d3
.scaleLinear()
.domain([0, d3.max(data, (d) => d.value)])
.nice()
.range([height - margin.bottom, margin.top]);
// x 坐标轴
// tickSizeOuter(0) 移除 0 处初始的标记
// tickFormat https://github.com/d3/d3-scale/blob/master/README.md#tickFormat
const xAxis = (g) =>
g.attr("transform", `translate(0,${height - margin.bottom})`).call(
d3
.axisBottom(x)
.tickFormat((i) => data[i].name)
.tickSizeOuter(0)
);
// y 坐标轴
const yAxis = (g) =>
g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).ticks(null, data.format))
// 移除区域间的竖线
.call((g) => g.select(".domain").remove())
.call((g) =>
g
.append("text")
.attr("x", -margin.left)
.attr("y", 10)
.attr("fill", "currentColor")
.attr("text-anchor", "start")
.text(data.y)
);
svg
.append("g")
.attr("fill", this.color)
.selectAll("rect")
.data(data)
.join("rect")
.attr("x", (d, i) => x(i))
.attr("y", (d) => y(d.value))
.attr("height", (d) => y(0) - y(d.value))
.attr("width", x.bandwidth());
// 绘制到 SVG
svg.append("g").call(xAxis);
svg.append("g").call(yAxis);
},
},
});
</script>
以上是根据首字母出现频率进行的统计。之后只需要在App.vue文件中使用<BarChart />语句加载BarChart组件,就可以看到直方图效果。App.vue代码如下
<template>
<HelloWorld msg="" />
<div>
<!-- <p v-for="(char, i) in alphabet" :key="i">
<span>{{ char.letter }}</span>
<span>{{ char.frequency }}</span>
</p> -->
<BarChart />
</div>
</template>
<script setup>
import BarChart from "./components/BarChart.vue"
import HelloWorld from "./components/HelloWorld.vue";
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
最终做出来直方图如下