jmchart 1.1.81 → 1.1.91

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/example/bar.html CHANGED
@@ -1,181 +1,187 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <meta content="text/html; charset=UTF-8" http-equiv="content-type" />
5
- <meta name="viewport" content="width=device-width,initial-scale=1">
6
- <!--<script type="text/javascript" src="../src/jmchart.debug.js" id="jmgraph" ></script>-->
7
-
8
- <style>
9
- html,body {
10
- margin:0;
11
- padding:0;
12
- width:100%;
13
- height:100%;
14
- }
15
- #mycanvas canvas, #mycanvas div{
16
- background-color:#fff;
17
- margin:0 auto;
18
- }
19
- /*div.jm-tooltip {
20
- background-color: rgba(255,255,255,0.8);
21
- padding:4px;
22
- border:1px solid #000;
23
- box-shadow: 0 0 3px #000;
24
- border-radius: 6px;
25
- }*/
26
- </style>
27
- <script type="module">
28
- //window.devicePixelRatio = 1;
29
- import { jmChart } from '../dist/jmchart.esm.js';
30
-
31
- (function load() {
32
- var w = Math.max((window.innerWidth || window.document.body.clientWidth) * 0.9, 600);
33
- var h = Math.max((window.innerHeight || window.document.body.clientHeight) * 0.6, 200);
34
- inichart(w,h);
35
- })();
36
-
37
-
38
- function inichart(w, h) {
39
- var chart = new jmChart('mycanvas', {
40
- width: w,
41
- height: h,
42
- xField: 'x',
43
- baseY: 0, // Y轴基线的值 不填就会用最 小值
44
- // 最大和最小X值, 这里一般不用指定,除非硬是需要
45
- minXValue : 0,
46
- maxXValue : 10,
47
- minYValue: -200,
48
- maxYValue: 300,
49
- autoRefresh: true,
50
- style: {
51
- axis: {
52
- x: true,
53
- y: true
54
- }
55
- }
56
- });
57
- chart.legend.legendPosition = 'top';
58
- chart.legend.visible = true;
59
- chart.enableAnimate = false;
60
-
61
- // touch改变数据点事件
62
- chart.on('touchPointChange', function(args) {
63
- console.log(args);
64
- });
65
-
66
-
67
- var bar1 = chart.createSeries('bar', {
68
- field: 'y1',
69
- style: {
70
- // 渐变色
71
- color: 'linear-gradient(50% 0 50% 100%, #ccc 0, #000 0.5, #fff 1)',
72
- perWidth: 0.5, // 控制barWidth占比,优先级比barWidth低
73
- //barWidth: 80,// 优先级最高
74
- label: {
75
- show: true,
76
- offset: 5, // 离柱偏移量
77
- shadow: {
78
- x: 0,
79
- y: 0,
80
- blur: 0,
81
- color: 'transparent'
82
- },
83
- fill: function(point) {
84
- console.log(point);
85
- if(point.yValue < 0) return 'green';
86
- else return 'red';
87
- }
88
- }
89
- },
90
- itemLabelFormat(p) {
91
- return p.yValue.toFixed(2);
92
- },
93
- // 图例格式化,如果不给定,则采用legendLabel
94
- legendFormat: function(options) {
95
- return '图1';
96
- },
97
- yLabelFormat: (v, label) => {
98
- return v.toFixed(0);
99
- }
100
- });
101
-
102
- /*var bar2 = chart.createSeries('bar', {
103
- field: 'y8',
104
- style: {
105
- // 自定义单个bar的颜色
106
- color: function(p) {
107
- if(p && p.xValue == 3) {
108
- return 'green';
109
- }
110
- // 默认值
111
- return 'linear-gradient(50% 0 50% 100%, #ff0000 0, #00ff00 0.5, #0000ff 1)';
112
- },
113
- label: {
114
- show: false,
115
- offset: 5 // 离柱偏移量
116
- }
117
- },
118
- // 图例格式化,如果不给定,则采用legendLabel
119
- legendFormat: function(options) {
120
- return '图4';
121
- },
122
- yLabelFormat: (v, label) => {
123
- return v.toFixed(0);
124
- }
125
- });*/
126
-
127
- const datacount = 5;
128
- function animate() {
129
- if(true) {
130
- chart.data = [];
131
- for(var i = 0;i<datacount;i++) {
132
- var data = {
133
- x : i,
134
- y1 : Math.random() * 200 - 100,
135
- y2: Math.random() * 200 + 50,
136
- y3: Math.random() * 200 + 50,
137
- y4:Math.random() * 200 + 50,
138
- y5:Math.random() * 200 + 50,
139
- y6:Math.random() * 200 + 50,
140
- y7:i,
141
- y8:Math.random() * 200 - 50,
142
- y9:Math.random() * 200 - 50,
143
- y10:Math.random() * 200 - 50
144
- };
145
- if(i == 3) {
146
- data.y = null;
147
- }
148
- chart.data.push(data);
149
- }
150
- }
151
- else {
152
- for(var i=datacount-1;i>0;i--) {
153
- var x = chart.data[i].x;
154
- chart.data[i] = chart.utils.clone(chart.data[i - 1]);
155
- chart.data[i].x = x;
156
- }
157
-
158
- chart.source[0] = {
159
- x : chart.data[0].x,
160
- y : Math.random() * 200 - 100
161
- };
162
-
163
- }
164
-
165
- //setTimeout(animate,500);
166
- chart.refresh();
167
- console.log(chart.data);
168
- }
169
-
170
- animate();
171
- chart.utils.bindEvent(document.getElementById('btnRedraw'),'click',animate);
172
-
173
- }
174
- </script>
175
- </head>
176
- <body>
177
- <div id="mycanvas">
178
- </div>
179
- <button id="btnRedraw">重绘</button>
180
- </body>
1
+ <!doctype html>
2
+ <html>
3
+ <head>
4
+ <meta content="text/html; charset=UTF-8" http-equiv="content-type" />
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <!--<script type="text/javascript" src="../src/jmchart.debug.js" id="jmgraph" ></script>-->
7
+
8
+ <style>
9
+ html,body {
10
+ margin:0;
11
+ padding:0;
12
+ width:100%;
13
+ height:100%;
14
+ }
15
+ #mycanvas canvas, #mycanvas div{
16
+ background-color:#fff;
17
+ margin:0 auto;
18
+ }
19
+ /*div.jm-tooltip {
20
+ background-color: rgba(255,255,255,0.8);
21
+ padding:4px;
22
+ border:1px solid #000;
23
+ box-shadow: 0 0 3px #000;
24
+ border-radius: 6px;
25
+ }*/
26
+ </style>
27
+ <script type="module">
28
+ //window.devicePixelRatio = 1;
29
+ import { jmChart } from '../dist/jmchart.esm.js';
30
+
31
+ (function load() {
32
+ var w = Math.max((window.innerWidth || window.document.body.clientWidth) * 0.9, 600);
33
+ var h = Math.max((window.innerHeight || window.document.body.clientHeight) * 0.6, 200);
34
+ inichart(w,h);
35
+ })();
36
+
37
+
38
+ function inichart(w, h) {
39
+ var chart = new jmChart('mycanvas', {
40
+ width: w,
41
+ height: h,
42
+ xField: 'x',
43
+ baseY: 0, // Y轴基线的值 不填就会用最 小值
44
+ // 最大和最小X值, 这里一般不用指定,除非硬是需要
45
+ //minXValue : 0,
46
+ //maxXValue : 10,
47
+ //minYValue: -200,
48
+ //maxYValue: 300,
49
+ autoRefresh: true,
50
+ style: {
51
+ axis: {
52
+ x: true,
53
+ y: true
54
+ },
55
+ markLine: {
56
+ longtap: true
57
+ },
58
+ margin: {
59
+ bottom: 2
60
+ },
61
+ }
62
+ });
63
+ chart.legend.legendPosition = 'top';
64
+ chart.legend.visible = true;
65
+ chart.enableAnimate = false;
66
+
67
+ // touch改变数据点事件
68
+ chart.on('touchPointChange', function(args) {
69
+ console.log(args);
70
+ });
71
+
72
+
73
+ var bar1 = chart.createSeries('bar', {
74
+ field: 'y1',
75
+ style: {
76
+ // 渐变色
77
+ color: 'linear-gradient(50% 0 50% 100%, #ccc 0, #000 0.5, #fff 1)',
78
+ perWidth: 0.5, // 控制barWidth占比,优先级比barWidth低
79
+ //barWidth: 80,// 优先级最高
80
+ label: {
81
+ show: true,
82
+ offset: 10, // 离柱偏移量
83
+ shadow: {
84
+ x: 0,
85
+ y: 0,
86
+ blur: 0,
87
+ color: 'transparent'
88
+ },
89
+ fill: function(point) {
90
+ console.log(point);
91
+ if(point.yValue < 0) return 'green';
92
+ else return 'red';
93
+ }
94
+ }
95
+ },
96
+ itemLabelFormat(p) {
97
+ return p.yValue.toFixed(2);
98
+ },
99
+ // 图例格式化,如果不给定,则采用legendLabel
100
+ legendFormat: function(options) {
101
+ return '图1';
102
+ },
103
+ yLabelFormat: (v, label) => {
104
+ return v.toFixed(0);
105
+ }
106
+ });
107
+
108
+ /*var bar2 = chart.createSeries('bar', {
109
+ field: 'y8',
110
+ style: {
111
+ // 自定义单个bar的颜色
112
+ color: function(p) {
113
+ if(p && p.xValue == 3) {
114
+ return 'green';
115
+ }
116
+ // 默认值
117
+ return 'linear-gradient(50% 0 50% 100%, #ff0000 0, #00ff00 0.5, #0000ff 1)';
118
+ },
119
+ label: {
120
+ show: false,
121
+ offset: 5 // 离柱偏移量
122
+ }
123
+ },
124
+ // 图例格式化,如果不给定,则采用legendLabel
125
+ legendFormat: function(options) {
126
+ return '图4';
127
+ },
128
+ yLabelFormat: (v, label) => {
129
+ return v.toFixed(0);
130
+ }
131
+ });*/
132
+
133
+ const datacount = 5;
134
+ function animate() {
135
+ if(true) {
136
+ chart.data = [];
137
+ for(var i = 0;i<datacount;i++) {
138
+ var data = {
139
+ x : i,
140
+ y1 : Math.random() * 200 - 100,
141
+ y2: Math.random() * 200 + 50,
142
+ y3: Math.random() * 200 + 50,
143
+ y4:Math.random() * 200 + 50,
144
+ y5:Math.random() * 200 + 50,
145
+ y6:Math.random() * 200 + 50,
146
+ y7:i,
147
+ y8:Math.random() * 200 - 50,
148
+ y9:Math.random() * 200 - 50,
149
+ y10:Math.random() * 200 - 50
150
+ };
151
+ if(i == 3) {
152
+ data.y = null;
153
+ }
154
+ chart.data.push(data);
155
+ }
156
+ }
157
+ else {
158
+ for(var i=datacount-1;i>0;i--) {
159
+ var x = chart.data[i].x;
160
+ chart.data[i] = chart.utils.clone(chart.data[i - 1]);
161
+ chart.data[i].x = x;
162
+ }
163
+
164
+ chart.source[0] = {
165
+ x : chart.data[0].x,
166
+ y : Math.random() * 200 - 100
167
+ };
168
+
169
+ }
170
+
171
+ //setTimeout(animate,500);
172
+ chart.refresh();
173
+ console.log(chart.data);
174
+ }
175
+
176
+ animate();
177
+ chart.utils.bindEvent(document.getElementById('btnRedraw'),'click',animate);
178
+
179
+ }
180
+ </script>
181
+ </head>
182
+ <body>
183
+ <div id="mycanvas">
184
+ </div>
185
+ <button id="btnRedraw">重绘</button>
186
+ </body>
181
187
  </html>
@@ -0,0 +1,288 @@
1
+ <!doctype html>
2
+ <html>
3
+ <head>
4
+ <meta content="text/html; charset=UTF-8" http-equiv="content-type" />
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <!--<script type="text/javascript" src="../src/jmchart.debug.js" id="jmgraph" ></script>-->
7
+ <script src="js/vconsole.js"></script>
8
+
9
+ <style>
10
+ html,body {
11
+ margin:0;
12
+ padding:0;
13
+ width:100%;
14
+ height:100%;
15
+ }
16
+ </style>
17
+ <script type="module">
18
+ import jmChart from '../dist/jmchart.esm.js';
19
+ (function load() {
20
+ var w = window.innerWidth || window.document.body.clientWidth;
21
+ var h = Math.min((window.innerHeight || window.document.body.clientHeight) * 0.5, 500);
22
+
23
+ var vConsole = new window.VConsole();
24
+
25
+ inichart(w,h);
26
+ })();
27
+
28
+
29
+ function inichart(w, h) {
30
+ const option = {
31
+ width: w,
32
+ height: h,
33
+ xField: 'day',
34
+ enableAnimate: false,
35
+ legendPosition: 'top',
36
+ legendVisible: false, // 不显示图例
37
+ touchGraph: true, // 是否依赖生成操作图层,当点比较多时,性能低下可以把它设为true
38
+ style: {
39
+ layout: 'inside', // inside 二边不对齐Y轴,内缩一个刻度 | normal
40
+ // 跟随标线
41
+ markLine: {
42
+ x: true, // 显示X标线
43
+ y: true, // 显示Y标线
44
+ stroke: 'red',
45
+ fill: '#fff',
46
+ lineWidth: 1,
47
+ longtap: true // 长按启用
48
+ },
49
+ // x轴 Y轴隐藏
50
+ axis: {
51
+ stroke: 'transparent',
52
+ align: 'center', // label居中对齐
53
+ grid: {
54
+ x: false, // 不显示X轴网格
55
+ y: false // 显示Y轴网格
56
+ }
57
+ },
58
+ margin: {
59
+ top: 30,
60
+ left: 50,
61
+ right: 10,
62
+ bottom: 30
63
+ }
64
+ }
65
+ };
66
+ const chart1 = new jmChart('mycanvas1', option);
67
+ // touch改变数据点事件
68
+ chart1.on('touchPointChange', function(args) {
69
+ console.log(args);
70
+ document.getElementById('mydatalog').innerHTML = JSON.stringify(args.points[0].data);
71
+ });
72
+
73
+
74
+ const kline = chart1.createSeries('candlestick', {
75
+ // 如果字段名跟下面的一至,可以不指定
76
+ fields: [
77
+ 'start', // 开盘
78
+ 'end', // 收盘
79
+ 'max', // 最大值
80
+ 'min' // 最小值
81
+ ],
82
+ style: {
83
+ // 阴线颜色
84
+ negativeColor: 'green',
85
+ // 阳线颜色
86
+ masculineColor: 'red',
87
+ lineWidth: 2,
88
+ //barWidth: 15
89
+ }
90
+ });
91
+
92
+ const chart2 = new jmChart('mycanvas2', {
93
+ ...option,
94
+ height: 200,
95
+ style: {
96
+ ...option.style,
97
+ // 跟随标线
98
+ markLine: {
99
+ x: false, // 显示X标线
100
+ y: true, // 显示Y标线
101
+ stroke: 'red',
102
+ fill: '#fff',
103
+ lineWidth: 1,
104
+ longtap: true // 长按启用
105
+ }
106
+ }
107
+ });
108
+
109
+
110
+ const bar1 = chart2.createSeries('bar', {
111
+ field: 'max',
112
+ style: {
113
+ label: {
114
+ show: false
115
+ },
116
+ // 跟随标线
117
+ markLine: {
118
+ x: false, // 显示X标线
119
+ y: true, // 显示Y标线
120
+ stroke: 'red',
121
+ fill: '#fff',
122
+ lineWidth: 1,
123
+ longtap: true // 长按启用
124
+ },
125
+ }
126
+ });
127
+
128
+ // 图1的标线移动 图2跟随
129
+ chart1.on('marklinestartmove', (args)=>{
130
+ args.cancel = true;// 终止事件继续执行,以免二个chart死循环
131
+ chart2.markLine.startMove(args, 'y'); //只联动y
132
+ });
133
+ chart1.on('marklinemove', (args)=>{
134
+ args.cancel = true;// 终止事件继续执行,以免二个chart死循环
135
+ chart2.markLine.move(args);
136
+ });
137
+ chart1.on('marklineendmove', (args)=>{
138
+ args.cancel = true;// 终止事件继续执行,以免二个chart死循环
139
+ chart2.markLine.endMove(args);
140
+ });
141
+
142
+ // 图2的标线移动 图1跟随
143
+ chart2.on('marklinestartmove', (args)=>{
144
+ args.cancel = true;// 终止事件继续执行,以免二个chart死循环
145
+ chart1.markLine.startMove(args, 'y');//只联动y
146
+ });
147
+ chart2.on('marklinemove', (args)=>{
148
+ args.cancel = true;// 终止事件继续执行,以免二个chart死循环
149
+ chart1.markLine.move(args);
150
+ });
151
+ chart2.on('marklineendmove', (args)=>{
152
+ args.cancel = true;// 终止事件继续执行,以免二个chart死循环
153
+ chart1.markLine.endMove(args);
154
+ });
155
+
156
+
157
+ const data = createData(); // 生成测试数据
158
+
159
+
160
+ let dataCount = 20; // 展示数据个数
161
+ let dataIndex = data.length - dataCount; // 展示数据起始索引
162
+
163
+ function refresh() {
164
+ chart2.data = chart1.data = data.filter((d, i) => {
165
+ if(i >= dataIndex && i < dataCount + dataIndex) return true;
166
+ return false;
167
+ });
168
+ chart1.refresh();
169
+ chart2.refresh();
170
+ }
171
+
172
+ refresh(0, 20);
173
+
174
+ // 根据滑动来绘制数据
175
+ let dataTouchPosition = {
176
+ x: 0,
177
+ touching: false, // 是否在滑动中
178
+ scaleTouching : false,
179
+ scaleTouchOffset: 0
180
+ };
181
+ chart1.on('touchstart', (args) => {
182
+ // 双指滑动, 缩放
183
+ if(args.event.touches.length === 2) {
184
+ dataTouchPosition.scaleTouching = true;
185
+ const ox = args.event.touches[0].pageX - args.event.touches[1].pageX ;
186
+ const oy = args.event.touches[0].pageY - args.event.touches[1].pageY;
187
+ // 数据偏移距离
188
+ dataTouchPosition.scaleTouchOffset = Math.sqrt(ox * ox + oy * oy);
189
+ }
190
+ else {
191
+ dataTouchPosition.touching = true;
192
+ }
193
+ dataTouchPosition.x = args.position.x;
194
+ });
195
+
196
+ // 长按了
197
+ chart1.on('marklinelongtapstart', (args) => {
198
+ dataTouchPosition.touching = false;// 长按取消数据滑动
199
+
200
+ document.getElementById('mylog').innerHTML = 'marklinelongtapstart:';
201
+ console.log('marklinelongtapstart', args);
202
+ });
203
+ chart1.on('touchmove', (args) => {
204
+ if(!args.target || !args.target.chartGraph) return;
205
+ console.log('touchmove', args);
206
+
207
+ // 双指缩放
208
+ if(dataTouchPosition.scaleTouching && args.event.touches.length === 2) {
209
+ const ox = args.event.touches[0].pageX - args.event.touches[1].pageX ;
210
+ const oy = args.event.touches[0].pageY - args.event.touches[1].pageY;
211
+ // 数据偏移距离
212
+ const scaleOffset = Math.sqrt(ox * ox + oy * oy);
213
+ const dataOffset = Math.round((dataTouchPosition.scaleTouchOffset - scaleOffset) / args.target.bounds.width * dataCount);
214
+
215
+ if(Math.abs(dataOffset) > 0) {
216
+ let index = Math.max(dataIndex - dataOffset, 0);
217
+ if(index >= 0) dataIndex = index;
218
+ dataCount += dataOffset * 2;
219
+ // 展示数据个数不能越过数组上限
220
+ if(dataCount > data.length) dataCount = data.length;
221
+ if(dataCount > data.length - dataIndex) dataCount = data.length - dataIndex;
222
+
223
+ refresh();// 刷新数据
224
+
225
+ dataTouchPosition.scaleTouchOffset = scaleOffset;
226
+
227
+ document.getElementById('mylog').innerHTML = 'scaleTouching:' + dataOffset + ' index:' + dataIndex + ' dataCount:' + dataCount;
228
+ }
229
+ }
230
+ // 在滑动标线,则不处理
231
+ //else if(args.target.chartGraph.yMarkLine && args.target.chartGraph.yMarkLine.visible) {
232
+ // dataTouchPosition.touching = false;
233
+ //}
234
+ else if(dataTouchPosition.touching) {
235
+ const ox = dataTouchPosition.x - args.position.x;
236
+ // 数据偏移量
237
+ const offDataCount = Math.round(ox / args.target.bounds.width * dataCount);
238
+ if(Math.abs(offDataCount) > 0) {
239
+ let index = dataIndex + offDataCount;
240
+ if(index < 0) index = 0
241
+ else if(index > data.length - dataCount) index = data.length - dataCount;
242
+
243
+ // 刷新数据
244
+ dataIndex = index;
245
+ refresh();
246
+
247
+ dataTouchPosition.x = args.position.x;
248
+ }
249
+ }
250
+ });
251
+ chart1.on('touchend touchcancel touchleave', (args) => {
252
+ dataTouchPosition.touching = false;
253
+ dataTouchPosition.scaleTouching = false;
254
+ console.log('touchend');
255
+ });
256
+ }
257
+
258
+ function createData() {
259
+ const data = [];
260
+ const startDate = new Date('2021/01/01 00:00:00');
261
+ for(var i = 0;i<365;i++) {
262
+ startDate.setDate(startDate.getDate() + 1);
263
+ const d = {
264
+ day : startDate.getFullYear() + '-' + ('00'+(startDate.getMonth()+1)).substr(-2) + '-' + ('00'+startDate.getDate()).substr(-2),
265
+ start : Math.random() * 5 + 15,
266
+ end: Math.random() * 5 + 15
267
+ };
268
+ d.max = Math.max(d.start, d.end) + Math.random() * 5;
269
+ d.min = Math.min(d.start, d.end) - Math.random() * 5;
270
+ data.push(d);
271
+ }
272
+ return data;
273
+ }
274
+ </script>
275
+ </head>
276
+ <body>
277
+ <div id="mycanvas1">
278
+ </div>
279
+ <hr />
280
+ <div id="mydatalog" style="word-wrap:break-word">
281
+ </div>
282
+ <hr />
283
+ <div id="mylog" style="word-wrap:break-word">
284
+ </div>
285
+ <div id="mycanvas2">
286
+ </div>
287
+ </body>
288
+ </html>