jmchart 1.2.11 → 1.2.13

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.
@@ -0,0 +1,187 @@
1
+ import jmSeries from './series.js';
2
+
3
+ const ANIMATION_DATA_THRESHOLD = 100;
4
+ const DEFAULT_ANIMATION_COUNT = 10;
5
+
6
+ /**
7
+ * 热力图
8
+ *
9
+ * @class jmHeatmapSeries
10
+ * @module jmChart
11
+ * @param {jmChart} chart 当前图表
12
+ * @param {array} mappings 图形字段映射
13
+ * @param {style} style 样式
14
+ */
15
+
16
+ export default class jmHeatmapSeries extends jmSeries {
17
+ constructor(options) {
18
+ options.style = options.style || options.graph.style.heatmap;
19
+ super(options);
20
+ }
21
+
22
+ /**
23
+ * 初始化热力图
24
+ *
25
+ * @method init
26
+ * @for jmHeatmapSeries
27
+ */
28
+ init() {
29
+ const {
30
+ points,
31
+ dataChanged
32
+ } = this.initDataPoint();
33
+
34
+ const len = points.length;
35
+ if(!len) return;
36
+
37
+ // 计算数据的最大值和最小值,用于颜色映射
38
+ let minValue = Infinity;
39
+ let maxValue = -Infinity;
40
+
41
+ for(let i = 0; i < len; i++) {
42
+ const p = points[i];
43
+ if(typeof p.value === 'number') {
44
+ minValue = Math.min(minValue, p.value);
45
+ maxValue = Math.max(maxValue, p.value);
46
+ }
47
+ }
48
+
49
+ this.minValue = minValue;
50
+ this.maxValue = maxValue;
51
+
52
+ const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD;
53
+
54
+ const aniCount = (this.style.aniCount || DEFAULT_ANIMATION_COUNT);
55
+ const aniStep = Math.floor(len / aniCount) || 1;
56
+
57
+ for(let i = 0; i < len; i++) {
58
+ const p = points[i];
59
+
60
+ if(typeof p.y === 'undefined' || p.y === null) {
61
+ continue;
62
+ }
63
+
64
+ if(isRunningAni && i > this.___animateCounter) {
65
+ break;
66
+ }
67
+
68
+ this.createHeatmapCell(p, minValue, maxValue);
69
+ this.createItemLabel(p);
70
+
71
+ this.emit('onPointCreated', p);
72
+ }
73
+
74
+ if(this.___animateCounter >= len - 1) {
75
+ this.___animateCounter = 0;
76
+ } else if(isRunningAni) {
77
+ this.___animateCounter += aniStep;
78
+ this.graph.utils.requestAnimationFrame(() => {
79
+ this.needUpdate = true;
80
+ });
81
+ }
82
+ }
83
+
84
+ /**
85
+ * 生成热力图单元格
86
+ *
87
+ * @method createHeatmapCell
88
+ * @for jmHeatmapSeries
89
+ * @param {object} p 数据点
90
+ * @param {number} minValue 最小值
91
+ * @param {number} maxValue 最大值
92
+ */
93
+ createHeatmapCell(p, minValue, maxValue) {
94
+ // 计算单元格大小
95
+ const cellWidth = this.style.cellWidth || 20;
96
+ const cellHeight = this.style.cellHeight || 20;
97
+
98
+ // 计算颜色
99
+ const color = this.getHeatmapColor(p.value, minValue, maxValue);
100
+
101
+ // 创建矩形单元格
102
+ const cellStyle = this.graph.utils.clone(this.style.item, {
103
+ stroke: color,
104
+ fill: color
105
+ });
106
+
107
+ const cellShape = this.graph.createShape('rect', {
108
+ style: cellStyle,
109
+ position: {
110
+ x: p.x - cellWidth / 2,
111
+ y: p.y - cellHeight / 2
112
+ },
113
+ width: cellWidth,
114
+ height: cellHeight
115
+ });
116
+
117
+ cellShape.zIndex = (cellShape.style.zIndex || 1) + 1;
118
+ return this.addShape(cellShape);
119
+ }
120
+
121
+ /**
122
+ * 获取热力图颜色
123
+ *
124
+ * @method getHeatmapColor
125
+ * @for jmHeatmapSeries
126
+ * @param {number} value 当前值
127
+ * @param {number} minValue 最小值
128
+ * @param {number} maxValue 最大值
129
+ * @return {string} 颜色值
130
+ */
131
+ getHeatmapColor(value, minValue, maxValue) {
132
+ if(typeof value !== 'number') {
133
+ return this.style.defaultColor || '#ccc';
134
+ }
135
+
136
+ // 计算值的比例
137
+ const ratio = (value - minValue) / (maxValue - minValue);
138
+
139
+ // 使用默认的颜色梯度
140
+ const colors = this.style.colorGradient || [
141
+ '#313695', // 蓝色(最小值)
142
+ '#4575b4',
143
+ '#74add1',
144
+ '#abd9e9',
145
+ '#e0f3f8',
146
+ '#ffffbf',
147
+ '#fee090',
148
+ '#fdae61',
149
+ '#f46d43',
150
+ '#d73027',
151
+ '#a50026' // 红色(最大值)
152
+ ];
153
+
154
+ // 根据比例获取颜色
155
+ const index = Math.min(Math.floor(ratio * (colors.length - 1)), colors.length - 1);
156
+ return colors[index];
157
+ }
158
+
159
+ /**
160
+ * 生成图例
161
+ *
162
+ * @method createLegend
163
+ * @for jmHeatmapSeries
164
+ */
165
+ createLegend() {
166
+ // 生成图例前的图标
167
+ var style = this.graph.utils.clone(this.style);
168
+ style.stroke = style.color;
169
+ style.fill = style.color;
170
+
171
+ var shape = this.graph.createShape('rect', {
172
+ style: style,
173
+ position: {
174
+ x: 0,
175
+ y: 0
176
+ },
177
+ width: this.graph.style.legend.item.shape.width,
178
+ height: this.graph.style.legend.item.shape.height
179
+ });
180
+
181
+ this.graph.legend.append(this, shape);
182
+ }
183
+ }
184
+
185
+ export {
186
+ jmHeatmapSeries
187
+ }
@@ -97,45 +97,62 @@ export default class jmLineSeries extends jmSeries {
97
97
  return this.addShape(pointShape);
98
98
  }
99
99
 
100
- // 根据上下点生成平滑曲线
101
100
  createCurePoints(shapePoints, p) {
102
101
  const startPoint = shapePoints[shapePoints.length - 1];
103
- if(startPoint && startPoint.y != undefined && startPoint.y != null) {
104
- //如果需要画曲线,则计算贝塞尔曲线坐标
105
- const p1 = {x: startPoint.x + (p.x - startPoint.x) / 5, y: startPoint.y};
106
- const p2 = {x: startPoint.x + (p.x - startPoint.x) / 2, y: p.y - (p.y - startPoint.y) / 2};
107
- const p3 = {x: p.x - (p.x - startPoint.x) / 5, y: p.y};
102
+
103
+ if(!startPoint || !p) return shapePoints;
104
+
105
+ if(startPoint.x === undefined || startPoint.x === null ||
106
+ startPoint.y === undefined || startPoint.y === null ||
107
+ p.x === undefined || p.x === null ||
108
+ p.y === undefined || p.y === null) {
109
+ return shapePoints;
110
+ }
108
111
 
109
- //圆滑线条使用的贝塞尔对象
110
- this.__bezier = this.__bezier || this.graph.createShape('bezier');
111
- this.__bezier.cpoints = [
112
- startPoint,
113
- p1,
114
- p2,
115
- p3,
116
- p
117
- ];//设置控制点
118
-
119
- const bzpoints = this.__bezier.initPoints();
120
- shapePoints = shapePoints.concat(bzpoints);
112
+ const p1 = {x: startPoint.x + (p.x - startPoint.x) / 5, y: startPoint.y};
113
+ const p2 = {x: startPoint.x + (p.x - startPoint.x) / 2, y: p.y - (p.y - startPoint.y) / 2};
114
+ const p3 = {x: p.x - (p.x - startPoint.x) / 5, y: p.y};
115
+
116
+ this.__bezier = this.__bezier || this.graph.createShape('bezier');
117
+ this.__bezier.cpoints = [
118
+ startPoint,
119
+ p1,
120
+ p2,
121
+ p3,
122
+ p
123
+ ];
124
+
125
+ const bzpoints = this.__bezier.initPoints();
126
+ if(bzpoints && bzpoints.length) {
127
+ shapePoints = shapePoints.concat(bzpoints);
121
128
  }
129
+
122
130
  return shapePoints;
123
131
  }
124
132
 
125
- // 生成虚线
126
133
  createDotLine(shapePoints, p) {
127
134
  const startPoint = shapePoints[shapePoints.length - 1];
128
- if(startPoint && startPoint.y != undefined && startPoint.y != null) {
129
- //使用线条来画虚线效果
130
- this.__line = this.__line || this.graph.createShape('line', {
131
- style: this.style,
132
- });
133
- this.__line.start = startPoint;
134
- this.__line.end = p;
135
-
136
- const dots = this.__line.initPoints();
137
- shapePoints = shapePoints.concat(dots);
135
+
136
+ if(!startPoint || !p) return shapePoints;
137
+
138
+ if(startPoint.x === undefined || startPoint.x === null ||
139
+ startPoint.y === undefined || startPoint.y === null ||
140
+ p.x === undefined || p.x === null ||
141
+ p.y === undefined || p.y === null) {
142
+ return shapePoints;
143
+ }
144
+
145
+ this.__line = this.__line || this.graph.createShape('line', {
146
+ style: this.style,
147
+ });
148
+ this.__line.start = startPoint;
149
+ this.__line.end = p;
150
+
151
+ const dots = this.__line.initPoints();
152
+ if(dots && dots.length) {
153
+ shapePoints = shapePoints.concat(dots);
138
154
  }
155
+
139
156
  return shapePoints;
140
157
  }
141
158
 
@@ -0,0 +1,149 @@
1
+ import jmLineSeries from './lineSeries.js';
2
+
3
+ /**
4
+ * 范围图(带状图)
5
+ *
6
+ * 范围图用于显示数据的上下限范围,比如温度范围、价格波动范围等。
7
+ * 它由两条线组成,两条线之间的区域用颜色填充,形成带状效果。
8
+ *
9
+ * 数据格式要求:
10
+ * - fields: ['min', 'max'] - 两个字段,分别表示下限和上限
11
+ * - xField: 'category' - X轴字段
12
+ *
13
+ * 样式配置:
14
+ * - color: 线条和填充颜色
15
+ * - showItem: 是否显示数据点
16
+ * - curve: 是否使用平滑曲线
17
+ * - area: 填充样式配置
18
+ *
19
+ * 应用场景:
20
+ * - 温度范围:显示每天的最高温和最低温
21
+ * - 价格波动:显示股票的最高价和最低价
22
+ * - 误差范围:显示测量值的误差范围
23
+ * - 置信区间:显示统计数据的置信区间
24
+ *
25
+ * @class jmRangeSeries
26
+ * @module jmChart
27
+ * @extends jmLineSeries
28
+ * @param {jmChart} chart 当前图表
29
+ * @param {array} mappings 图形字段映射
30
+ * @param {style} style 样式
31
+ */
32
+
33
+ export default class jmRangeSeries extends jmLineSeries {
34
+ constructor(options) {
35
+ options.style = options.style || options.graph.style.range || options.graph.style.stackLine;
36
+ super(options);
37
+ }
38
+
39
+ /**
40
+ * 初始化范围图
41
+ *
42
+ * 绘制逻辑:
43
+ * 1. 遍历所有数据点
44
+ * 2. 绘制两条线(上限线和下限线)
45
+ * 3. 在两条线之间创建填充区域
46
+ */
47
+ init() {
48
+ const {
49
+ points,
50
+ dataChanged
51
+ } = this.initDataPoint();
52
+
53
+ const len = points.length;
54
+
55
+ this.style.stroke = this.style.color;
56
+ this.style.item.stroke = this.style.color;
57
+
58
+ const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0 );
59
+
60
+ let startShapePoints = [];
61
+ let endShapePoints = [];
62
+ const aniCount = (this.style.aniCount || 10);
63
+ const aniStep = Math.floor(len / aniCount) || 1;
64
+
65
+ for(let i=0; i< len;i++) {
66
+ const p = points[i];
67
+
68
+ if(isRunningAni) {
69
+ if(i > this.___animateCounter) {
70
+ break;
71
+ }
72
+ }
73
+
74
+ if(this.style.showItem) {
75
+ this.createPointItem(p.points[0]);
76
+ this.createPointItem(p.points[1]);
77
+ }
78
+
79
+ if(this.style.curve) {
80
+ startShapePoints = this.createCurePoints(startShapePoints, p.points[0]);
81
+ endShapePoints = this.createCurePoints(endShapePoints, p.points[1]);
82
+ }
83
+ else if(this.style.lineType === 'dotted') {
84
+ startShapePoints = this.createDotLine(startShapePoints, p.points[0]);
85
+ endShapePoints = this.createDotLine(endShapePoints, p.points[1]);
86
+ }
87
+
88
+ startShapePoints.push(p.points[0]);
89
+ endShapePoints.push(p.points[1]);
90
+
91
+
92
+ this.emit('onPointCreated', p);
93
+ }
94
+
95
+ if(this.___animateCounter >= len - 1) {
96
+ this.___animateCounter = 0;
97
+ }
98
+ else if(isRunningAni) {
99
+ this.___animateCounter += aniStep;
100
+ this.graph.utils.requestAnimationFrame(()=>{
101
+ this.needUpdate = true;
102
+ });
103
+ }
104
+
105
+ if(endShapePoints.length) endShapePoints[0].m = true;
106
+ this.points = startShapePoints.concat(endShapePoints);
107
+
108
+ const areaPoints = startShapePoints.concat(endShapePoints.reverse());
109
+ const areaEnd = areaPoints[areaPoints.length - 1] = this.graph.utils.clone(areaPoints[areaPoints.length - 1]);
110
+ areaEnd.m = false;
111
+ this.createArea(areaPoints, false);
112
+ }
113
+
114
+ /**
115
+ * 生成图例
116
+ */
117
+ createLegend() {
118
+
119
+ var style = this.graph.utils.clone(this.style);
120
+ style.stroke = style.color;
121
+ var shape = this.graph.createShape('path',{style:style});
122
+
123
+ if(this.curve || this.style.curve) {
124
+ var p1 = {x:0,y: this.graph.style.legend.item.shape.height};
125
+ var p2 = {x:this.graph.style.legend.item.shape.width / 3,y:this.graph.style.legend.item.shape.height/3};
126
+ var p3 = {x:this.graph.style.legend.item.shape.width / 3 * 2,y:this.graph.style.legend.item.shape.height/3*2};
127
+ var p4 = {x:this.graph.style.legend.item.shape.width,y:0};
128
+
129
+ this.__bezier = this.__bezier || this.graph.createShape('bezier');
130
+ this.__bezier.cpoints = [
131
+ p1,p2,p3,p4
132
+ ];
133
+
134
+ shape.points = this.__bezier.initPoints();
135
+ }
136
+ else {
137
+ shape.points = [{
138
+ x:0,y: this.graph.style.legend.item.shape.height/2
139
+ },{
140
+ x: this.graph.style.legend.item.shape.width,y: this.graph.style.legend.item.shape.height/2
141
+ }];
142
+ }
143
+ this.graph.legend.append(this, shape);
144
+ }
145
+ }
146
+
147
+ export {
148
+ jmRangeSeries
149
+ }
@@ -0,0 +1,191 @@
1
+ import jmSeries from './series.js';
2
+
3
+ /**
4
+ * 环形进度图
5
+ *
6
+ * 环形进度图用于展示单个或多个指标的完成进度或占比情况。
7
+ * 每个指标用一个环形表示,进度用填充的弧线表示。
8
+ *
9
+ * 数据格式要求:
10
+ * - field: 'value' - 数值字段
11
+ * - 多个指标时使用多个数据项
12
+ *
13
+ * 样式配置:
14
+ * - radius: 环形半径,默认自动计算
15
+ * - lineWidth: 环形线宽,默认 20
16
+ * - startAngle: 起始角度,默认 -90 (12点钟方向)
17
+ * - max: 最大值,默认 100
18
+ * - showLabel: 是否显示标签,默认 true
19
+ *
20
+ * @class jmRingProgressSeries
21
+ * @module jmChart
22
+ * @extends jmSeries
23
+ *
24
+ * @example
25
+ * chart.createSeries('ringProgress', {
26
+ * field: 'value',
27
+ * style: {
28
+ * lineWidth: 15,
29
+ * max: 100,
30
+ * showLabel: true
31
+ * }
32
+ * });
33
+ */
34
+ export default class jmRingProgressSeries extends jmSeries {
35
+ constructor(options) {
36
+ options.style = options.style || options.graph.style.ringProgress;
37
+ super(options);
38
+ }
39
+
40
+ /**
41
+ * 初始化环形进度图
42
+ */
43
+ init() {
44
+ const data = this.data;
45
+ if(!data || !data.length) return;
46
+
47
+ const chartWidth = this.graph.chartArea.width;
48
+ const chartHeight = this.graph.chartArea.height;
49
+ const centerX = chartWidth / 2;
50
+ const centerY = chartHeight / 2;
51
+
52
+ const maxRadius = Math.min(chartWidth, chartHeight) / 2 - 20;
53
+ const lineWidth = this.style.lineWidth || 20;
54
+ const startAngle = this.style.startAngle !== undefined ? this.style.startAngle : -90;
55
+ const maxValue = this.style.max || 100;
56
+ const field = this.field || 'value';
57
+
58
+ const len = data.length;
59
+ const ringGap = this.style.ringGap || 10;
60
+ const totalRingSpace = len * lineWidth + (len - 1) * ringGap;
61
+ const availableRadius = maxRadius - totalRingSpace / 2;
62
+
63
+ for(let i = 0; i < len; i++) {
64
+ const item = data[i];
65
+ const value = Math.max(0, Math.min(maxValue, item[field] || 0));
66
+ const ratio = value / maxValue;
67
+
68
+ const radius = availableRadius + (len - 1 - i) * (lineWidth + ringGap);
69
+
70
+ this.createRingBackground(centerX, centerY, radius, lineWidth, i);
71
+ this.createRingProgress(centerX, centerY, radius, lineWidth, startAngle, ratio, i);
72
+
73
+ if(this.style.showLabel !== false) {
74
+ this.createRingLabel(centerX, centerY, radius, value, maxValue, i, item);
75
+ }
76
+ }
77
+ }
78
+
79
+ /**
80
+ * 创建环形背景
81
+ */
82
+ createRingBackground(centerX, centerY, radius, lineWidth, index) {
83
+ const bgColor = this.style.backgroundColor || '#e0e0e0';
84
+
85
+ const bgArc = this.graph.createShape('arc', {
86
+ style: {
87
+ stroke: bgColor,
88
+ fill: 'transparent',
89
+ lineWidth: lineWidth,
90
+ zIndex: 0
91
+ },
92
+ center: { x: centerX, y: centerY },
93
+ radius: radius,
94
+ startAngle: 0,
95
+ endAngle: 360
96
+ });
97
+
98
+ this.addShape(bgArc);
99
+ }
100
+
101
+ /**
102
+ * 创建环形进度
103
+ */
104
+ createRingProgress(centerX, centerY, radius, lineWidth, startAngle, ratio, index) {
105
+ if(ratio <= 0) return;
106
+
107
+ const color = this.getColor(null, index);
108
+ const endAngle = startAngle + ratio * 360;
109
+
110
+ const progressArc = this.graph.createShape('arc', {
111
+ style: {
112
+ stroke: color,
113
+ fill: 'transparent',
114
+ lineWidth: lineWidth,
115
+ zIndex: 1,
116
+ ...this.style.progressStyle
117
+ },
118
+ center: { x: centerX, y: centerY },
119
+ radius: radius,
120
+ startAngle: startAngle,
121
+ endAngle: endAngle
122
+ });
123
+
124
+ this.addShape(progressArc);
125
+ }
126
+
127
+ /**
128
+ * 创建环形标签
129
+ */
130
+ createRingLabel(centerX, centerY, radius, value, maxValue, index, item) {
131
+ const text = this.option.itemLabelFormat
132
+ ? this.option.itemLabelFormat.call(this, { yValue: value, index, data: item })
133
+ : `${Math.round(value / maxValue * 100)}%`;
134
+
135
+ if(!text) return;
136
+
137
+ const label = this.graph.createShape('label', {
138
+ style: {
139
+ fill: this.style.labelColor || '#333',
140
+ font: this.style.labelFont || '14px Arial',
141
+ textAlign: 'center',
142
+ textBaseline: 'middle',
143
+ zIndex: 10
144
+ },
145
+ text: text,
146
+ position: {
147
+ x: centerX,
148
+ y: centerY
149
+ }
150
+ });
151
+
152
+ if(index === 0) {
153
+ this.addShape(label);
154
+ }
155
+ }
156
+
157
+ /**
158
+ * 获取颜色
159
+ */
160
+ getColor(point, index) {
161
+ if(this.style.colors && this.style.colors.length > 0) {
162
+ return this.style.colors[index % this.style.colors.length];
163
+ }
164
+ if(typeof this.style.color === 'function') {
165
+ return this.style.color.call(this, point, index);
166
+ }
167
+ return this.graph.getColor(index);
168
+ }
169
+
170
+ /**
171
+ * 生成图例
172
+ */
173
+ createLegend() {
174
+ const style = this.graph.utils.clone(this.style);
175
+ style.fill = this.style.color || this.graph.getColor(0);
176
+ style.stroke = style.fill;
177
+
178
+ const shape = this.graph.createShape('circle', {
179
+ style: style,
180
+ center: {
181
+ x: this.graph.style.legend.item.shape.width / 2,
182
+ y: this.graph.style.legend.item.shape.height / 2
183
+ },
184
+ radius: this.graph.style.legend.item.shape.height / 2
185
+ });
186
+
187
+ this.graph.legend.append(this, shape);
188
+ }
189
+ }
190
+
191
+ export { jmRingProgressSeries };