Member-only story

Adding Graphics to a Vue App with D3 — Axis and Arcs

John Au-Yeung
3 min readJan 31, 2021

--

Photo by Paul Carmona on Unsplash

D3 lets us add graphics to a front-end web app easily.

Vue is a popular front end web framework.

They work great together. In this article, we’ll look at how to add graphics to a Vue app with D3.

Axis API

We can use the axis API to add the axes for graphs.

For example, we can write:

<template>
<div id="app"></div>
</template>
<script>
import * as d3 from "d3";
import Vue from "vue";
export default {
name: "App",
mounted() {
Vue.nextTick(() => {
const width = 400,
height = 400;
const data = [100, 120, 140, 160, 180, 200];
const svg = d3
.select("body")
.append("svg")
.attr("width", width)
.attr("height", height);
const xscale = d3
.scaleLinear()
.domain([0, d3.max(data)])
.range([0, width - 100]);
const yscale = d3
.scaleLinear()
.domain([0, d3.max(data)])
.range([height / 2, 0]);
const xAxis = d3.axisBottom().scale(xscale);
const yAxis = d3.axisLeft().scale(yscale);
svg.append("g").attr("transform", "translate(50, 10)").call(yAxis);
const xAxisTranslate = height / 2 + 10;

--

--

No responses yet