NodeJS
简单的说 Node.js 就是运行在服务端的 JavaScript。
下载网址:http://nodejs.cn/download/
查看版本:node --version
写文件server.js,试运行:
var http = require('http');
http.createServer(function (request, response){
// 发送HTTP头部
// HTTP状态值:200:OK
// 内容类型:text/plain
response.writeHead(200, {'Content-Type':'text/plain'});
// 发送响应数据 "Hello World"
response.end('Hello World\n');
}).listen(8888);
// 终端打印如下信息
console.log('Server running at http://127.0.0.1:8888/');
运行命令及反馈:
命令行Ctrl+C退出
Vue
安装:npm install vue
Vite
安装:npm install vite
创建工程:npm init vite-app hellovue
运行当前工程:npm run dev
VSCode
下载网址:https://code.visualstudio.com/
GitHub
下载网址:https://git-scm.com/
显示在线的知识图谱实体内容
<template>
<h1>{{ msg }}</h1>
<button @click="count++">count is: {{ count }}</button>
<p>Edit <code>components/HelloWorld.vue</code> to test hot module replacement.</p>
<p v-for="info in ldh">{{ info }}</p>
</template>
<script>
import axios from 'axios'
export default {
name: 'HelloWorld',
props: {
msg: String
},
data() {
return{
ldh:""
}
},
mounted(){
axios.get("https://api.ownthink.com/kg/knowledge?entity=刘德华").then((response) => {
this.ldh=response['data']['data']['avp'];
//console.log(this.ldh);
})
}
}
</script>
直方图
<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
<template>
<img alt="Vue logo" src="./assets/logo.png" />
<!-- <HelloWorld msg="Hello Vue 3.0 + Vite" /> -->
<BarChart />
</template>
<script>
// import HelloWorld from './components/HelloWorld.vue';
// import TestAxios from './components/TestAxios.vue';
import BarChart from './components/BarChart.vue';
export default {
name: 'App',
components: {
// HelloWorld
// TestAxios
BarChart
}
}
</script>