D3 数据可视化006——坐标轴 Axis

在D3中,坐标轴是在svg中绘制的,对于svg常见的属性和标签要有基本了解。D3中提供了四个朝向的坐标轴,如下图:

下面跟着我一起学习D3中坐标绘制的过程

1、创建一个Svg元素,用来包裹其它图像元素

let height = 300, 
    width = 300, 
    margin = 30, 
    offset = 40,
    axisWidth = width - 2 * margin,
    svg;
  •  声明Svg的宽高
  • 避免绘图在边缘的不可见性,设定间距和偏移
  • 宽度或者高度减去两边的间距就是坐标轴的长度
  • 最后创建Svg并添加宽高等属性
const createSvg = (container) => { // 后面调用时传入容器参数
    svg = d3.select(container).append('svg')
            .attr('width', width)
            .attr('height', height)
}






HTML和CSS
<template>
    <div id="d3Container" class="container">
        <div class="axisTopBox axisBox" style="background-color: rgb(166 206 227)"></div>
        <div class="axisBottomBox axisBox" style="background-color: rgb(251 154 153)"></div>
        <div class="axisLeftBox axisBox" style="background-color: rgb(178 223 138)"></div>
        <div class="axisRightBox axisBox" style="background-color: rgb(202 178 214)"></div>
    </div>
</template>

<style lang="scss">
.container {
    font-size: 20px;
    width: 50vw;
    height: 650px;
    margin:auto;
    background-color: #fbe9d5;
    display: flex;
    flex-wrap: wrap;

    .axisBox {
        width: 50%;
        height: 50%;
        background-color: #ff9000;
    }
}
</style>

 2、 坐标轴方向,创建坐标轴

  • 标题在坐标轴之上  d3.axisTop
  • 标题在坐标轴之下  d3.axisBottom
  • 标题在坐标轴左边  d3.axisLeft
  • 标题在坐标轴右边  d3.axisRight

   其中左右就是竖直的坐标轴,上下是水平坐标轴 

   下面使用 d3.axisBottom() 创建坐标轴:

const renderAxis = () => {
    let axis = d3.axisBottom()
                .scale(  // 坐标轴的scale()给这个坐标轴提供尺度
                    d3.scaleLinear()
                        .domain([0, 1000])
                        .range([0, axisWidth])
                ).ticks(5) // 告诉D3需要渲染几个刻度

    svg.append('g')
        .attr('transform', () => { // 通过变形偏移保证在svg中的可见性
                return 'translate(' + margin + ',' + offset + ')'
        }).call(axis) // 在当前图形集合上调用call绑定坐标轴
}

 其中d3.axisBottom().scale().ticks()  表示标题朝下的坐标轴,scale()为这个坐标轴提供尺度,本例中是一个线性尺度,大家可以使用任何之前学习的尺度。ticks()表示刻度个数,不过D3在大部分情况下会自动计算,除非你给的值比较可靠。

3、调用以上方法,并传入容器,坐标轴绘制完成 

const renderAll = (container) => {
    createSvg(container)
    renderAxis()
}

renderAll('.axisTopBox')


HTML
<div class="axisTopBox axisBox" style="background-color: rgb(166 206 227)"></div>
CSS
.container {
    font-size: 20px;
    width: 50vw;
    height: 650px;
    margin:auto;
    background-color: #fbe9d5;
    display: flex;
    flex-wrap: wrap;

    .axisBox {
        width: 50%;
        height: 50%;
        background-color: #ff9000;
    }
}

  

最后给出本节的完整代码

 

// 效果就是开头的图片
<script setup>
import { onMounted } from "vue";
import * as d3 from "d3";

let height = 300, width = 300, margin = 30, offset = 40,
    axisWidth = width - 2 * margin,
    svg;

const createSvg = (container) => {
    svg = d3.select(container).append('svg')
            .attr('width', width)
            .attr('height', height)
}

const renderAxis = (fn, scale, i) => {
    let axis = fn()
                .scale(scale).ticks(5)
    svg.append('g')
        .attr('transform', () => {
            if([d3.axisTop, d3.axisBottom].indexOf(fn) >= 0) {
                return 'translate(' + margin + ',' +i*offset + ')'
            }else {
                return 'translate(' + i * offset + ',' + margin + ')'
            }
        }).call(axis)
}


const renderAll = (container, fn) => {
    createSvg(container)
    renderAxis(fn, d3.scaleLinear().domain([0, 1000]).range([0, axisWidth]), 1)
}



onMounted(() => {
   renderAll('.axisTopBox', d3.axisBottom)
   renderAll('.axisBottomBox', d3.axisTop)
   renderAll('.axisLeftBox', d3.axisLeft)
   renderAll('.axisRightBox', d3.axisRight)
})

</script>

<template>
    <div id="d3Container" class="container">
        <div class="axisTopBox axisBox" style="background-color: rgb(166 206 227)"></div>
        <div class="axisBottomBox axisBox" style="background-color: rgb(251 154 153)"></div>
        <div class="axisLeftBox axisBox" style="background-color: rgb(178 223 138)"></div>
        <div class="axisRightBox axisBox" style="background-color: rgb(202 178 214)"></div>
    </div>
</template>

<style lang="scss">
.container {
    font-size: 20px;
    width: 50vw;
    height: 650px;
    margin:auto;
    background-color: #fbe9d5;
    display: flex;
    flex-wrap: wrap;

    .axisBox {
        width: 50%;
        height: 50%;
        background-color: #ff9000;
    }
}
</style>

  • 17
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
数据可视化是一种将数据转换为图表、图形和图像等可视化形式的技术,在数据分析和数据展示中非常常见。下面是数据可视化的实验步骤: 1. 准备数据。首先需要准备要展示的数据,并确定需要展示哪些方面的信息。数据可以来自不同的数据源,如数据库、文件或API等。 2. 选择可视化工具。根据要展示的数据和展示需求,选择适合的可视化工具。常见的可视化工具包括Tableau、Power BI、matplotlib、D3.js等。 3. 设计可视化方案。根据要展示的数据和展示需求,设计合适的可视化方案。这包括选择合适的图表类型、颜色和字体等,以及确定展示的细节和交互方式。 4. 编写代码。根据设计的可视化方案,使用合适的编程语言和可视化工具编写代码。在编写代码时,需要注意代码的可读性和可维护性,以便后续修改和优化。 5. 运行测试。使用准备好的数据,运行可视化代码进行测试。测试时需要注意记录每个测试的输入数据和输出结果,以便后续分析和比较。 6. 优化可视化效果。根据测试结果和用户反馈,优化可视化效果和交互体验,以便用户更好地理解和分析数据。 下面是一个使用Python和matplotlib实现简单折线图的phony代码示例: ```python import matplotlib.pyplot as plt # 准备数据 x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] # 绘制折线图 plt.plot(x, y) # 添加标题和标签 plt.title("Sample Line Chart") plt.xlabel("X-axis") plt.ylabel("Y-axis") # 显示图表 plt.show() ``` 这段代码使用了matplotlib库绘制了一个简单的折线图,其中x和y分别表示横坐标和纵坐标的数据。通过调用plot()函数和添加标题、标签等信息,最后使用show()函数显示图表。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值