-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvee2.html
More file actions
154 lines (141 loc) · 5.29 KB
/
vee2.html
File metadata and controls
154 lines (141 loc) · 5.29 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
<!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;
}
</style>
</head>
<body>
<!-- 展示canvas -->
<div class="container">
</div>
<!--引入 chartjs-->
<script type="text/javascript">
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
}
}]
}
},
};
};
// Define a plugin to provide data labels
Chart.plugins.register({
afterDatasetsDraw: function(chart, easing) {
// To only draw at the end of animation, check for easing === 1
var ctx = chart.ctx;
chart.data.datasets.forEach(function (dataset, i) {
var meta = chart.getDatasetMeta(i);
if (!meta.hidden) {
meta.data.forEach(function(element, index) {
// Draw the text in black, with the specified font
ctx.fillStyle = 'rgb(0, 0, 0)';
var fontSize = 10;
var fontStyle = 'normal';
var fontFamily = 'Helvetica Neue';
ctx.font = Chart.helpers.fontString(fontSize, fontStyle, fontFamily);
// Just naively convert to string for now
var dataString = dataset.data[index].toString();
// Make sure alignment settings are correct
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
var padding = 5;
var position = element.tooltipPosition();
ctx.fillText(dataString, position.x, position.y - (fontSize / 2) - padding);
});
}
});
}
});
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>