-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvee3.html
More file actions
177 lines (161 loc) · 5.73 KB
/
vee3.html
File metadata and controls
177 lines (161 loc) · 5.73 KB
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>画图</title>
<script src="Chart.bundle.js"></script>
<script src="utils.js"></script>
<script type="text/javascript" src="jquery-latest.js"></script>
<style>
canvas{
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
.chart-container {
width: 500px;
margin-left: 40px;
margin-right: 40px;
margin-bottom: 40px;
}
.container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
}
.chartjs-tooltip {
opacity: 1;
position: absolute;
background: rgba(0, 0, 0, .7);
color: yellow;
border-radius: 3px;
-webkit-transition: all .1s ease;
transition: all .1s ease;
pointer-events: none;
-webkit-transform: translate(-50%, 0);
transform: translate(-50%, 0);
padding: 4px;
}
</style>
</head>
<body>
<!-- 展示canvas -->
<div class="container">
</div>
<!--引入 chartjs-->
<div class="container">
</div>
<div id="canvas-holder1" style="width:75%;">
<div class="chartjs-tooltip" id="tooltip-0"></div>
<div class="chartjs-tooltip" id="tooltip-1"></div>
</div>
<script>
var customTooltips = function (tooltip) {
$(this._chart.canvas).css("cursor", "pointer");
var positionY = this._chart.canvas.offsetTop;
var positionX = this._chart.canvas.offsetLeft;
$(".chartjs-tooltip").css({
opacity: 0,
});
if (!tooltip || !tooltip.opacity) {
return;
}
if (tooltip.dataPoints.length > 0) {
tooltip.dataPoints.forEach(function (dataPoint) {
var content = [dataPoint.xLabel, dataPoint.yLabel].join(": ");
var $tooltip = $("#tooltip-" + dataPoint.datasetIndex);
$tooltip.html(content);
$tooltip.css({
opacity: 1,
top: positionY + dataPoint.y + "px",
left: positionX + dataPoint.x + "px",
});
});
}
};
function createConfig(hotelItem) {
var priceDict = hotelItem['price'];
var name_cn = hotelItem['name_cn'];
var name_en = hotelItem['name_en'];
var date = Object.keys(priceDict);
var price = Object.values(priceDict);
var avg_price = Math.ceil(price.reduce(function(a,b) {return a+b})/price.length);
var avg_price_list = Array.from( new Array(price.length),(v,i)=>{return avg_price});
return {
type: 'line',
data: {
labels: date,
datasets: [
{ data: price,
label: "Lowest Price",//图例名字
backgroundColor: 'rgb(255, 99, 132)',
borderColor: 'rgb(255, 99, 132)',
pointRadius: 4,//点标记大小
pointHoverRadius: 8,//移动到点上时的大小
fill: false,
},
{ data: avg_price_list,
label: "Average Price",//图例名字
backgroundColor: 'rgb(32, 178, 170)',
borderColor: 'rgb(32, 178, 170)',
pointRadius: 4,//点标记大小
pointHoverRadius: 8,//移动到点上时的大小
fill: false,
},
]},
options: {
responsive: true,
title:{
display:true,
text:name_cn,
},
elements: {
point: {
pointStyle: 'circle'
}
},
scales: {
xAxes: [{
//gridLines: gridlines
}],
yAxes: [{
//gridLines: gridlines,
display: true,
ticks: {
min: 0,
max: (Math.ceil(Math.max.apply(Math,price)/100+1)*100),
stepSize: 100
}
}]
},
tooltips: {
enabled: false,
mode: 'index',
intersect: false,
custom: customTooltips
}
},
};
};
window.onload = function() {
var container = document.querySelector('.container');
$.getJSON('details.json',function(data){
hotel = Object.values(data);
//alert("Totle Hotels:"+hotel.length);
hotel.forEach(function(hotelItem){
var div = document.createElement('div');
div.classList.add('chart-container');
var canvas = document.createElement('canvas');
div.appendChild(canvas);
container.appendChild(div);
div.style.width = '800px';
div.style.height = '400px';
var ctx = canvas.getContext("2d");
new Chart(ctx,createConfig(hotelItem));
});
});
};
</script>
</body>
</html>