step-step-gauge
Introduction
This is a gauge chart by D3 step by step.
Background
Many Javascript libraries support gauge charts in the web pages, most noticeably the well known C3.js library. Although highly configurable, I found it difficult to make the chart to look like the following by C3.js, so I decided to implement a gauge chart by myself.
For simplicity, I used the D3.js for the SVG manipulations. It is simple, but it is nice to keep a record on how to create the gauge chart step by step.
The attached is a Java Maven project. I have tested it on Tomcat 7. if you want run it, you do not have to use Tomcat, because all the files are simply static HTML files. But I would recommend you run the files through a web server to avoid browser security checks. You will also need the internet access to load the files, because the D3.js library is linked to a CDN.
step-0-start-from-a-half-circle.html
The following HTML content will be used to test the gauge. For simplicity, the SVG gauge will be appended directly to the ‘body’.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>step-0-start-from-a-half-circle.html</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.11/d3.min.js"></script>
</head>
<body>
<div><a href="index.html">Go back</a></div>
</body>
</body>
</html>
As the “0th” step, the only goal is to add an arc (half-circle) to the web page. Because we only need a half-circle, the size of the SVG is set to 200 x 100.

let d2r = function(d) {
return d * (Math.PI / 180);
};
let Gauge = function() {
let c = {
element: 'body',
width: 200,
height: 100,
thickness: 30,
gaugeColor: '#dde9f7',
backgroundColor: '#f5f5f5'
};
let svg = d3.select(c.element).append('svg')
.attr('width', c.width).attr('height', c.height);
// Add a rect as the background
svg.append('rect').attr('x', 0).attr('y', 0)
.attr('width', c.width).attr('height', c.height).attr('fill', c.backgroundColor);
let r = c.width/2;
let createArc = function(sa, ea) {
return d3.svg.arc()
.outerRadius(r)
.innerRadius(r - c.thickness)
.startAngle(d2r(sa))
.endAngle(d2r(ea));
};
let addArc = function(arc, color) {
return svg.append('path')
.attr('d', arc).attr('fill', color)
.attr('transform', 'translate('
+ r + ',' + r + '), scale(1, 1)');
};
// Create a 1/2 circle arc and put it at the center
addArc(createArc(-90, 90), c.gaugeColor);
};
window.addEventListener('load', function() {
// Test the createGauge function
let gauge = new Gauge();
});
- With the help of the D3.js, adding an arc to the SVG is a simple call to the “d3.svg.arc()” function;
- Besides adding the arc with “startAngle = -90 degree” and “endAngle = 90 degree”, a rectangle is also added to the SVG to serve as the background of the chart.
You should have noticed the JSON object “let c= {…}”. All the configurable parameters are defined in this object. In the later parts of this example, this object will also be used to take input configurable parameters.
step-1-add-margin.html
Although not always necessary, it should be nice to add some margins to the chart.
let c = {
element: 'body',
width: 200,
height: 100,
margin: 4,
thickness: 30,
gaugeColor: '#dde9f7',
backgroundColor: '#f5f5f5'
};
In order that the margin to be configurable, it is added to the configuration object. The outer radius of the arc is then calculated as the following to leave the room for the margins.
let r = (c.width - 2*c.margin)/2;
The center of the arc is also adjusted to include the margins into the consideration.
let addArc = function(arc, color) {
return svg.append('path')
.attr('d', arc).attr('fill', color)
.attr('transform', 'translate(' + (c.margin + r)
+ ',' + (c.margin + r) + '), scale(1, 1)');
};
You can see that the margins at the left, the top, and the right sides are added nicely. You should also see that there is no margin at the bottom. It is because I will chop and scale this arc and put labels at the bottom in the later steps.
step-2-scale-and-chop-the-arc.html
In order to achieve the desired look, I need to chop and scale the arc.
let c = {
element: 'body',
width: 200,
height: 100,
margin: 4,
yscale: 0.75,
chopAngle: 60,
thickness: 30,
gaugeColor: '#dde9f7',
backgroundColor: '#f5f5f5'
};
The “yscale” and “chopAngle” is added to the configuration object. In oder that the arc keeps the same left, top, and right margins, the radius of the arc needs to be calculated as the following to take the chop angle into consideration.
let ir = (c.width - 2*c.margin)/2;
let r = ir/Math.sin(d2r(c.chopAngle));
The center of the arc needs to take the scale into consideration, and the chop angle needs to be specified when drawing the arc.
let addArc = function(arc, color) {
return svg.append('path')
.attr('d', arc).attr('fill', color)
.attr('transform', 'translate(' + (c.margin + ir)
+ ',' + (c.margin + r*c.yscale) + '), scale(1, ' + c.yscale + ')');
};
addArc(createArc(-1*c.chopAngle, c.chopAngle), c.gaugeColor);
step-3-add-the-indicator.html
For the gauge chart to display a value, we need to add the value indicator.
let c = {
element: 'body',
width: 200,
height: 100,
margin: 4,
yscale: 0.75,
chopAngle: 60,
thickness: 30,
value: {
minValue: 0,
maxValue: 100,
initialvalue: 60
},
gaugeColor: '#dde9f7',
indicatorColor: '#4281a4',
backgroundColor: '#f5f5f5'
};
The configuration object now has the value object, which has the minimum, maximum, and the initial value of the gauge.
let getIndicatorData = function(value) {
let min = c.value.minValue;
let max = c.value.maxValue;
return {
sa: c.chopAngle - 2*c.chopAngle*value/(max - min),
ea: c.chopAngle
};
}
// Add the initial indicator
let data = getIndicatorData(c.value.initialvalue);
let indicator = addArc(createArc(data.sa, data.ea), c.indicatorColor);
In this gauge, the value represents “how much left/remaining”, so the indicator occupies the right side of the gauge. If you want the gauge value to represent “how much used/spent”, you can modify the “getIndicatorData()” function to achieve your desired result.
step-4-add-the-labels.html
Normally a gauge chart needs some labels to show the value in text.
let c = {
element: 'body',
width: 200,
height: 100,
margin: 4,
yscale: 0.75,
chopAngle: 60,
thickness: 30,
value: {
minValue: 0,
maxValue: 100,
initialvalue: 60
},
label: {
yoffset: 10,
unit: 'unit.',
labelText: {
text: 'REMAINING',
yoffset: 12
}
},
gaugeColor: '#dde9f7',
indicatorColor: '#4281a4',
backgroundColor: '#f5f5f5'
};
The “label” object in the configuration object has the information for us to create the labels for the chart.

let createLabel = function(config) {
let c = {
x: config.width/2,
y: config.height/2 + config.label.yoffset,
unit: config.label.unit,
label: {
text: config.label.labelText.text,
yoffset: config.label.labelText.yoffset
},
value: config.value.initialvalue
};
let text = svg.append('text')
.attr("x", c.x)
.attr("y", c.y)
.attr("text-anchor", "middle")
.attr("alignment-baseline", "middle")
.attr("font-family", "sans-serif")
.attr("font-size", "25px");
let label = text.append('tspan')
.attr("font-size", "25px")
.text(c.value);
text.append('tspan')
.attr('font-size', '10px').text(c.unit)
.attr('fill', '#333333');
text = svg.append('text')
.attr("x", c.x)
.attr("y", c.y + c.label.yoffset)
.attr("text-anchor", "middle")
.attr("alignment-baseline", "central")
.attr("font-family", "sans-serif")
.attr('fill', '#a8a8a8')
.attr("font-size", "12px");
text.text(c.label.text);
return label;
};
// Add the label
let valueText = createLabel(c);
step-5-external-configuration.html
In order to allow the “Gauge()” function to take external configuration parameters, I created the following method to merge the external configuration object with the default configuration object.
var jmerge = function(t, s) {
if (t === null || typeof t === 'undefined'
|| s === null || typeof s === 'undefined') { return s;}
if ((typeof t !== 'object') || (typeof s !== 'object')) { return s; }
if ((t instanceof Array) || (s instanceof Array)) { return s; }
if ((t instanceof String) || (s instanceof String)) { return s; }
for (var key in s) {
if (s.hasOwnProperty(key)) {
if (!t.hasOwnProperty(key)) { t[key] = s[key]; }
else { t[key] = jmerge(t[key], s[key]); }
}
}
return t;
}
The “jmerge” function recursively merges the “s” object into the “t” object and returns the merged result. It is not a perfect Javascript merge function, but it should be good enough to serve the purpose of this example. With the help of the “jmerge” function, the “Gauge” function now takes an external configuration object.

let Gauge = function(config) {
let c = {
element: 'body',
width: 200,
height: 100,
margin: 4,
yscale: 0.75,
chopAngle: 60,
thickness: 30,
value: {
minValue: 0,
maxValue: 100,
initialvalue: 60
},
label: {
yoffset: 10,
unit: 'unit.',
labelText: {
text: 'REMAINING',
yoffset: 12
}
},
gaugeColor: '#dde9f7',
indicatorColor: '#4281a4',
backgroundColor: '#f5f5f5'
};
// Merge the external config to the default config
c = config? jmerge(c, config): c;
// Rest of the code ...
};
When displaying the chart, we can try to give it some external configuration parameters to override the default configuration.
let gauge = new Gauge({
margin: 10,
label: {
unit: 'Hours',
labelText: {
text: 'GOING-ON'
}
}
});
step-6-update-the-value.html
In order that we can update the gauge display with the real-time data, I added the “update” method to the “Gauge” construction function.
let self = this;
self.update = function(value) {
// Update the indicator
let data = getIndicatorData(value);
indicator.attr('d', createArc(data.sa, data.ea))
// Update the label
valueText.text(value);
};
We can then make a call to the “update” method to change the value of the gauge.
let gauge = new Gauge({
margin: 10,
label: {
unit: 'Hours',
labelText: {
text: 'GOING-ON'
}
}
});
// Test the update function
gauge.update(40);
g rsacwgxy
http://t.co/4kPhN7Ek0L
https://twittbot.net/
Betsy Abrams
cbd hemp oil
where to buy viagra online in india
french doors with security screens
Tractor Workshop Manuals
cbd for sale san fernando valley ca.
cbd treats for dogs for sale
John Deere Diagnostic and Test Manuals
science lab cbd soft chews for dogs
cbd oil bad for dog
cbd oil for dog south carolina
cbd oil benefits herpes
review
take a look at the site here
Google
Google
situs judi online banyak promo
judi bola banyak bonus
pokerpelangi
vipmandiriqq
daftar dingdong togel
dingdong
aplikasi dingdongtogel
poker pulsa
judi deposit pulsa
permainan microgaming slot
dingdong togel daftar
dingdongtogel
wap togel
pulsa joker123
dingdong 42d
togel wap hk
dingdong casino togel
Darwin Coit
139.180.185.166
Download PUBG Mobile 2020
66.42.58.199
Sbobet777 Indonesia
Judi online
totobet sgp
totobet hk
styrofoam
keluaran togel
link alternatif totobet
bali car charter with driver
ISO SMK3
SMK3 Perusahaan
Reza Lukmana
one punch man manga online
sbobet777 login
sbobet777 login
kaldu bubuk
lo de online uy tin tai 188loto
trang soi cau 188 loto
lo de online the thao bet
lo de online uy tin 188xoso
du doan xsmb hom nay tai thethaobet
soi cau lo de mien bac k8loto
link alternatif joker123
daftar joker123
Situs Slot Pulsa Online
situs judi slot online terpercaya
poker 88 idn
poker 88
idn poker 88
joker123 download
aplikasi idn poker
daftar joker123
joker123 login
joker apk
Situs Judi Poker Online
Agen BandarQQ Online
site link
kalori defisit
download joker123
daftar poker88
cara membuat blog
paragonpoker
Judi Slot
idn poker 88
idn poker apk
Aplikasi kesehatan Android
cara import barang china
rumus lightroom
cara membuat blog di blogger
jagad poker
emeraldnarrowsburg.com
Kayak Storage Ideas
dingdong togel hari ini
dingdongtogel
daftar dingdong togel
dingdong 24d
dingdong 34d
situs judi online
Katalog Harga Promo
KelasJudi
rs primaya
situs poker
bwinbet365
s128 apk
https://www.bwinkasino.com/
s12888
download s128
https://www.bwinkasino.com/
s128 live
kings128
sabung ayam s1288
kings128
s128
Judi Domino99
Casino Online
Capsa Susun
daftar pokerqq
Asia88
Sbobet999 Slot
sbobet777 Slot
s128
s128
s128 net
Bet88 Mobile
situs bet88
sabung ayam online
jagos128
s128 sabung ayam online
hop over to here
s128
s128
s128
s128h.com
Situs Bandarq
Judi Online
Themeforia
Bandarq Terpercaya
Judi Aduqq
Agen Casino Online
Situs Judi Qiuqiu
Agen Slot Online
situs judi bola
BandarQQ
Prediksi Togel Terlengkap
Prediksi Togel SGP
best web hosting 2020
http://99bandarqiuqiu.com/
http://senangdominopkv.com/
http://senangdomino.in/
http://69.16.212.126
s1288
sabung ayam s128
s1288
s128.apk
download s128
s128 login
slot online
link king4d
cara daftar sv388
login totobet terbaru
slot deposit pulsa
login totobet
agen slot deposit pulsa
daftar totobet online
cara daftar totobet
Situs Download Pkv
agen dominoqq
situs dominobet online
poker online
Judi Dominoqq
www.doktercash.com
Megabandar
88csn
Ulatmovie.com
asikdewa
qbesar
dominobetqq
http://www.kamera4d.org/
sosbobet.cc
elevenconsignment.com
helpful site
rezgamer
togel online indonesia
totobet login
m11hkb.com
ding dong togel
dindongtogel
togel dingdong
kamera4d.org
sabung ayam online
s1281
cheap flights with jet2.com
link alternatif pokerace99
resep masakanku
mystargarden.com
QQ77Bet
Agen Judi Joinpoker.com
dingdongtogel
joker123.net
vivo slot
joker 888
joker 388
joker 188
flight tickets cheap
site web
sbobet88.com
slotonline
judionline.com
poker idn
judionline
idn poker 88
osg777
s128
Kertas puyer press
original site
joker 123 slot
jokerslot
joker123
joker123
joker123 net
joker123 slot online
joker 123
slot online joker123
joker slot
slot joker123
BaliQQ
WongQQ
Samsung Printer
printer reviews
khazanahpromosi.com
Furniture Jepara
baca selengkapnya
https://ponseloka.com
https://www.aplikasidewasa.com
Mandiriqq
https://sahabatqq.mobi/
Pokerpelangi
qbesar
levidio.musafirdigital.com
https://thompson2009.com/
https://charlesforboston.com/
dominoqqmu.com
agen joker388 online
joker 888
like it
sbobet mobile
download joker123 apk
cara daftar sbobet
sbobet bola
daftar sbobet88 online
Google
http://103.194.171.205/
https://hermes21.net/
www.masterhoki.com
go to my site
http://103.194.171.205/
http://107.152.32.187/
안전카지노
check this out
Kolten Casey
Google
Trik ubah kuota
tugas mahasiswa
Sentapoker
free ssh
sejarah perang
Esiabet
Printable Worksheets
judi online
Hidroponik bagi Pemula
daftar slot
Pkv QQ
s128 daftar
daftar sabung ayam s128
s128 sabung ayam live
sabung ayam s1288
sabung ayam online
download joker123
judi sabung ayam online
daftar akun joker123
Aufaproject46.com
homes for rent by owners near me
houses to rent by private landlords
wshh mobile
media islam indonesia
houses for rent near me private owner
http://202.182.119.182/
http://45.76.157.60/
waheedbaly.com
java388
http://66.42.50.255/
sistem pemerintahan orde lama dan orde baru
Jualfollowersmurah
Pulauparidino.com
judi sakong
situs togel terpercaya
DamianPSawka
judi slot online
situs poker online
situs poker online
PULSA PAYPAL OTOMATIS
service lcd iphone
daftar agen pulsa resmi
harga webbing sling
service xiaomi malang
harga webbing sling
electric chain hoist
daftar agen pulsa
flat illustration website template
daftar agen pulsa resmi
situs poker
judi online
bandarq
situs dominoqq
pkv games
login langitqq
situs judi online
daftar joker123
situs bandarq
judi online terpercaya
poker online
cara memilih merpati balap
langitqq login
slot deposit pulsa
judi qq
pkv games
dominoqq
situs judi
poker online
Wedesini
situs judi online
Slot Online
rfp consultant
Judi Slot Online
motorcycle accident law firm
langitqq
doatogel
Paten99.cc
qbesar
wiki4d
doacasino
langitqq
qbesar
qbesar
qkecil
qkecil
qbesar
Paket Aqiqah Murah Bekasi
judi pulsa
judi pulsa online
deposit poker pulsa
joker123
judi deposit pulsa
microgaming slot
situs judi casino online
judi slot online terpercaya
sv388
joker123 slot
download aplikasi sv388
Desain Rumah Minimalis
pinjaman online terpercaya
idn poker 88
idn poker
download sv388
sv388 login
s128 online
sv388 online
s128 online
daftar sv388
download aplikasi s128
download apk s128
s128 online
agen s128
tekno alvindayu
s128
joker slot
daftar s128
Judi Bandarqq
situs bola
taruhan bola online
situs bola
slot online
Situs Judi Bandarqq
informasi teknologi
Situs Judi Bandarqq
umimarfa
situs judi online
harga suzuki ertiga
Situs Judi Bandarqq
Situs Judi Bandarqq
slot joker123
slot deposit pulsa
cara login idn poker
idn poker 88
daftar slot deposit pulsa
sabung ayam online
sbobet bola
agen sabung ayam online
sbobet
sv388 login
judi slot pulsa
daftar slot online
slot online
slot online terbaru
deposit slot pulsa
situs slot terbaru
agen poker idn poker online
agen slot online terbaik
idn poker terbaru
idn poker
poker online terbaru
login idn poker
judi online24 jam terpercaya 2020
BukuMasakan.com
judi online24 jam terpercaya 2020
main login idnpoker
agen Judi
zadulqq
copy trade terbaik
TukangKataKata.com
rentalmobil.info
poker383
Login terimaqq
situs poker
Terimaqq
Maxbet Falmouthheritagewalks
Login terimaqq
Judi Bola online terpercaya
judi online24 jam terpercaya 2020
simpatiqq
Bandar Bola Online
bigosport
download film
http://situsjudipoker.site/
situs togel online
best parenting websites
judi online terpercaya
domino poker online
100 cbd oil
cbd cream for sale
judi poker online
bunda.co
judi domino online
agen dominoqq terpercaya
MakmurQQ
domino poker online
daftarcasinoind
best parenting websites
Bunda
katalog promo terbaru 2020
kumpulan syair togel
https://jambitotomobile.asia
klinik spesialis
klinik spesialis
macauslot188
anaksenja.com
game online terpercaya
situs poker pulsa
pkv
casino online terpercaya
judi casino online
situs judi terpercaya
judi poker
situs casino online
dominoqq bandarq online
situs judi online terpercaya
agen sbobet
sbobet88
agen bola
pkv games
situs poker online
sbobet.com
sv388.net
joker 888
joker388
sv388
joker388.net
idn poker88
Agen Judi Online
poker idn
sbobet com
Judi Poker Online
idnplay
agen sbobet resmi
link alternatif joker123
pkv poker88
poker88
download joker123 apk
cbd oil olive oil dogs
Bali backpacking
agen sbobet bola
deposit slot pulsa
sbobet mobile
login sbobet
slot pulsa
win money casino online
cara membuat tabel di word
fafa slot
idn poker
Bandarqq
Bandarqq
sbobet
Judi Bandarqq
cbd oil for pain relief
idnplay download
poker 88 online
ind play
apk poker88
download idn poker apk
idn poker apk
idnplay login
situs sv388
sv388 daftar
vivo slot
download apk joker123
sv 288
sv 388
slotonline
konveksi seragam kerja
slot depo via pulsa tanpa potongan
jual rumah sukodono sidoarjo
harga tanah surabaya
deposit judi pulsa
slot pulsa
judi depsoit pulsa
deposit pulsa slot tanpa potongan
jual rumah sidoarjo
slot pulsa
jual gudang margomulyo surabaya
Situs Judi Bola Online
slot online uang asli
Judi Bola online terpercaya
judi bola
situs qq online
babapoker
babapoker
mp3 download
situs judi qq online
Cek resi
poker idn
download vivoslot
download vivoslot apk
daftar vivo slot
link altenatif vivo slot
vivoslot
apk vivoslot
vivo slot
agen slot vivoslot
mpo slot terpercaya
daftar idn poker
daftar slot deposit pulsa
cara daftar idn poker
daftar joker123 deposit pulsa
Jual Bumbu Mie Majelismie
daftar joker123 deposit pulsa
Rumah Syariah
joker123 net
joker 123
joker123 net
win money now online
slot joker123
poker88
joker slot
idn poker
joker123
situs slot online indonesia
idn poker 88
idnplay
joker slot
slot online joker123
idn play poker
joker 123
joker123.net
slot joker123
bola sbobet888
vivo slot online
vivoslot.net
sbobet 88
vivo slot
slot vivoslot
vivo slot
slot online
slot vivoslot
vivo slot
sabung ayam sv388
idn poker 88
sv388 com
sv388.com
sabung ayam sv388
idnpoker
idn pulsa 5000
deposit slot pulsa 5000
sv388 casino
judi deposit pulsa 10000
sabung ayam sv388
Matthewobliz
sbobet88
download apk vivoslot
agen vivoslot
download vivoslot
vivoslot apk
link altenatif vivo slot
daftar vivo slot
slot vivo
daftar pkv poker
pkv poker
joker388
poker88
login pkv poker
joker388 slot
idn poker
live chat idn poker online
download idnpoker
vivoslot
poker idn play
idn poker online