D3.js 折线图

最近接了个第一财经的项目,大概是基于各种图表的数据呈现,但是要求是需要svg导出的功能。

echart

echart虽然支持svg渲染但是导出svg还有诸多bug,并且这个功能也没有写进文档。看来也是功能还没完善的原因

highChart

highChart svg导出功能完善 但是缺点是要钱。不考虑

d3.js

d3是一个数据驱动的可视化插件,简单理解就是操纵svg的jquery,优点是高度自定义,缺点是上手成本高。不过没办法,只能用这个了。

实践

经过简单的学习 从最简单的折线图开始

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>d3折线图</title>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script src="http://d3js.org/d3.v5.min.js" charset="utf-8"></script>
<style>

body {
font: 10px sans-serif;
margin: 50px;
}

.grid .tick {
stroke: lightgrey;
opacity: 0.7;
shape-rendering: crispEdges;
}

.grid path {
stroke-width: 0;
}

.axis path {
fill: none;
stroke: #bbb;
shape-rendering: crispEdges;
}

.axis text {
fill: #555;
}

.axis line {
stroke: #e7e7e7;
shape-rendering: crispEdges;
}

.axis .axis-label {
font-size: 14px;
}

.line {
fill:none;
stroke-width: 1.5px;
}

.dot {
/* consider the stroke-with the mouse detect radius? */
stroke: transparent;
stroke-width: 10px;
cursor: pointer;
}
.tips-border{
fill:rgba(0,0,0,0.6);
stroke: gray;
stroke-width: 2;

}
.dot:hover {
stroke: rgba(68, 127, 255, 0.3);
}
</style>
</head>
<body>
<div id="trendSvg"></div>
</body>
<script type="text/javascript">
var data = [
[{'x':1,'y':0},{'x':2,'y':5},{'x':3,'y':10},{'x':4,'y':0},{'x':5,'y':6},{'x':6,'y':11},{'x':7,'y':9},{'x':8,'y':4},{'x':9,'y':11},{'x':10,'y':2}],
[{'x':1,'y':1},{'x':2,'y':6},{'x':3,'y':11},{'x':4,'y':1},{'x':5,'y':7},{'x':6,'y':12},{'x':7,'y':8},{'x':8,'y':3},{'x':9,'y':13},{'x':10,'y':3}],
];
var colors = [
'steelblue',
'green',
'red',
'purple'
];

var margin = {top: 20, right: 30, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;

var x = d3.scaleLinear()
.domain([0, 12])
.range([0, width]);

var y = d3.scaleLinear()
.domain([-1, 16])
.range([height, 0]);

//x轴设置
var xAxis = d3.axisBottom(x)

//y轴设置
var yAxis = d3.axisLeft(y)



var svg = d3.select("#trendSvg").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");

svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);

svg.append("g")
.attr("class", "y axis")
.call(yAxis);

svg.append("g")
.attr("class", "y axis")
.append("text")
.attr("class", "axis-label")
.attr("x", -margin.left+30)
.attr("y", -margin.top+10)
.text('y轴');


var line = d3.line()
.x(function(d) { return x(d.x); })
.y(function(d) { return y(d.y); })
.curve(d3.curveMonotoneX)

svg.selectAll('.line')
.data(data)
.enter()
.append("path")
.attr("class", "line")
.attr("d", line)
.style("stroke-dasharray", function(d, i) {
var length = d3.select(this).node().getTotalLength()
return length+","+length
})
.attr('stroke', function(d,i){
return colors[i%colors.length];
})
.transition()
.duration(1500)
.styleTween("stroke-dashoffset", function(d,i) {
var length = d3.select(this).node().getTotalLength()
return d3.interpolateNumber(length, 0);
});



var tips = svg.append('g').attr('class', 'tips')
.attr('style', 'display:none')
.append('rect')
.attr('class', 'tips-border')
.attr('width', 200)
.attr('height', 50)
var wording1 = tips.append('text')
.attr('class', 'tips-text')
.attr('x', 10)
.attr('y', 20)
.text('');

var wording2 = tips.append('text')
.attr('class', 'tips-text')
.attr('x', 10)
.attr('y', 40)
.text('');
var points = svg.selectAll('.dots')
.data(data)
.enter()
.append("g")
.attr("class", "dots");


points.selectAll('.dot')
.data(function(d, index){
var a = [];
d.forEach(function(point,i){
a.push({'index': index, 'point': point});
});
return a;
})
.enter()
.append('circle')
.attr('class','dot')
.attr("r", 2)
.attr('fill', function(d,i){
return colors[d.index%colors.length];
})
.attr("transform", function(d) {
return "translate(" + x(d.point.x) + "," + y(d.point.y) + ")"; }
)
.on('mousemove', function(d) {
d3.select('.tips')
.attr('transform', "translate(" + (x(d.point.x)-100) + "," + (y(d.point.y)-60) + ")")
.style('display', 'block')
.append('text')
.attr('fill','white')
.attr('class', 'tips-text')
.attr('x', 10)
.attr('y', 20)
.text('哈哈哈哈');
d3.select('.tips').append('text')
.attr('fill','white')
.attr('class', 'tips-text')
.attr('x', 10)
.attr('y', 40)
.text('嘻嘻嘻嘻')
})
.on('mouseout', function() {
d3.select('.tips').style('display', 'none');
});
</script>
</html>

效果图:

0%