UNPKG

2.88 kBJavaScriptView Raw
1 (function () {
2 'use strict'
3
4 //Append h1 to div
5 d3.select('#divHello')
6 .append('h1')
7 .text('Hello')
8 .style('text-align', 'center')
9 .style('line-height', '100px')
10 .style('font-size', '100px')
11 .style('transform', 'rotate(-180deg) scale(0.001, 0.001)')
12 .transition()
13 .duration(1500)
14 .style('transform', null)
15
16 //Create Bullet Chart
17
18 var data = [{
19 'title': 'Profit',
20 'subtitle': '%',
21 'ranges': [20, 25, 30],
22 'measures': [21, 23],
23 'markers': [26]
24 }, {
25 'title': 'Order Size',
26 'subtitle': 'US$, average',
27 'ranges': [350, 500, 600],
28 'measures': [100, 320],
29 'markers': [550]
30 }]
31
32 var margin = {
33 top: 5,
34 right: 40,
35 bottom: 20,
36 left: 120
37 },
38 width = 960 - margin.left - margin.right,
39 height = 50 - margin.top - margin.bottom
40
41 var chart = d3.bullet().width(width).height(height)
42
43 var svg = d3.select('#bulletchart')
44 .selectAll('svg')
45 .data(data).enter()
46 .append('svg')
47 .attr('class', 'bullet')
48 .attr('width', width + margin.left + margin.right)
49 .attr('height', height + margin.top + margin.bottom).append('g')
50 .attr('transform', 'translate(' + margin.left + ',' + margin.top + ')')
51
52 svg.call(chart)
53
54
55 var title = svg.append('g')
56 .style('text-anchor', 'end')
57 .attr('transform', 'translate(-6,' + height / 2 + ')')
58
59 title.append('text')
60 .attr('class', 'title')
61 .text(function (d) {
62 return d.title
63 })
64
65 title.append('text')
66 .attr('class', 'subtitle')
67 .attr('dy', '1em')
68 .text(function (d) {
69 return d.subtitle
70 })
71
72 d3.selectAll('button').on('click', function () {
73 svg.datum(randomize).call(chart.duration(1000)) // TODO automatic transition
74 })
75
76
77 function randomize(d) {
78 if (!d.randomizer) d.randomizer = randomizer(d)
79 d.ranges = d.ranges.map(d.randomizer)
80 d.markers = d.markers.map(d.randomizer)
81 d.measures = d.measures.map(d.randomizer)
82 return d
83 }
84
85 function randomizer(d) {
86 var k = d3.max(d.ranges) * .2
87 return function (d) {
88 return Math.max(0, d + k * (Math.random() - .5))
89 }
90 }
91
92 //Create a SVg and add color to it by using SVG function
93
94 var svg = d3.svg('#svgEle').style('background-color', 'red')
95 svg.attr('height', 200)
96
97 })()
\No newline at end of file