-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
161 lines (143 loc) · 4.75 KB
/
index.js
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
import Visualization from 'zeppelin-vis'
import ColumnselectorTransformation from 'zeppelin-tabledata/columnselector'
import Highcharts from 'highcharts/highcharts'
require('highcharts/highcharts-more')(Highcharts);
require('highcharts/modules/exporting')(Highcharts);
require('highcharts/highcharts-3d')(Highcharts);
export default class m3dChart extends Visualization {
constructor(targetEl, config) {
super(targetEl, config)
this.props = [
{ name: 'xAxis', },
{ name: 'yAxis', },
{ name: 'zAxis', }
]
this.transformation = new ColumnselectorTransformation(
config, this.props)
}
/**
* @param tableData {Object} includes cols and rows. For example,
* `{columns: Array[2], rows: Array[11], comment: ""}`
*
* Each column includes `aggr`, `index`, `name` fields.
* For example, `{ aggr: "sum", index: 0, name: "age"}`
*
* Each row is an array including values.
* For example, `["19", "4"]`
*/
render(tableData) {
const conf = this.config
/** can be rendered when all axises are defined */
if (!conf.xAxis || !conf.yAxis || !conf.zAxis ) {
return
}
const rows = tableData.rows
const [ xAxisName, xAxisIndex, ] = [ conf.xAxis.name, conf.xAxis.index, ]
const [ yAxisName, yAxisIndex, ] = [ conf.yAxis.name, conf.yAxis.index, ]
const [ zAxisName, zAxisIndex, ] = [ conf.zAxis.name, conf.zAxis.index, ]
const data = createDataStructure(xAxisIndex, yAxisIndex, zAxisIndex, rows)
const chartOption = createHighchartOption(xAxisName, yAxisName, zAxisName,data);
var chart=new Highcharts.chart(this.targetEl[0].id, chartOption);
// Add mouse events for rotation
$(chart.container).bind('mousedown.hc touchstart.hc', function (e) {
e = chart.pointer.normalize(e);
var posX = e.pageX,
posY = e.pageY,
alpha = chart.options.chart.options3d.alpha,
beta = chart.options.chart.options3d.beta,
newAlpha,
newBeta,
sensitivity = 5; // lower is more sensitive
$(document).bind({
'mousemove.hc touchdrag.hc': function (e) {
// Run beta
newBeta = beta + (posX - e.pageX) / sensitivity;
newBeta = Math.min(100, Math.max(-100, newBeta));
chart.options.chart.options3d.beta = newBeta;
// Run alpha
newAlpha = alpha + (e.pageY - posY) / sensitivity;
newAlpha = Math.min(100, Math.max(-100, newAlpha));
chart.options.chart.options3d.alpha = newAlpha;
chart.redraw(false);
},
'mouseup touchend': function () {
$(document).unbind('.hc');
}
});
});
}
getTransformation() {
return this.transformation
}
}
/**
* creates data structure by converting Zeppelin tabledata.rows
*
* @return {Array<Object>}
*
* See also: * http://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/highcharts/demo/bubble/
*/
export function createDataStructure(xAxisIndex, yAxisIndex, zAxisIndex,rows) {
const data = []
for (let i = 0; i < rows.length; i++) {
const row = rows[i];
const xAxisValue = parseFloat(row[xAxisIndex])
const yAxisValue = parseFloat(row[yAxisIndex])
const zAxisValue = parseFloat(row[zAxisIndex])
data.push({
x: xAxisValue,
y: yAxisValue,
z: zAxisValue
});
}
return data
}
export function createHighchartOption(xAxisName, yAxisName, zAxisName,data) {
return {
chart: {
margin: 100,
type: 'scatter',
options3d: {
enabled: true,
alpha: 10,
beta: 30,
depth: 250,
viewDistance: 5,
frame: {
bottom: { size: 1, color: 'rgba(0,0,0,0.02)' },
back: { size: 1, color: 'rgba(0,0,0,0.04)' },
side: { size: 1, color: 'rgba(0,0,0,0.06)' }
}
}
},
title: {
text: '电池散点图'
},
subtitle: {
text: '单击并拖动鼠标可旋转绘图区'
},
plotOptions: {
scatter: {
width: 10,
height: 10,
depth: 10
}
},
yAxis: {
title: null
},
xAxis: {
gridLineWidth: 1
},
zAxis: {
},
legend: {
enabled: false
},
series: [{
name: '电池数据',
colorByPoint: false,
data: data
}]
}
}