-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchart_demo.html
150 lines (120 loc) · 4.13 KB
/
chart_demo.html
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
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>ZingChart Backbone Plugin - Simple Example</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"></script>
<script src="https://cdn.zingchart.com/zingchart.min.js"></script>
<script src="../lib/backbone.zingchart.min.js"></script>
<script type="text/template" id="chart-template">
<div>
<button class="add-plot">Add Plot</button>
<button class="reset-json">Change JSON</button>
<button class="reset-data">Change Data</button>
<button class="toggle-legend">Toggle Legend</button>
<div id="chartDiv"></div>
</div>
</script>
<body>
<div id="demo"></div>
<script>
var DemoView = Backbone.View.extend({
el: '#demo',
template: _.template($('#chart-template').html()),
initialize: function() {
this.$el.empty();
this.zc = new ZingChart.ZingChartModel({data: [[3,2,3,3,9] , [1,2,3,4,5]],
width: 500,
height: 400});
this.render();
},
render: function() {
this.$el.html(this.template());
this.zcView = new ZingChart.ZingChartView({model: this.zc, el: $('#chartDiv')});
this.zcView.render();
return this;
},
events: {
'click .reset-json': 'changeJSON',
'click .reset-data': 'changeData',
'click .add-plot': 'addPlot',
'click .toggle-legend': 'addLegend'
},
random: function(max){
return Math.floor((Math.random() * max));
},
changeJSON: function(){
console.log('Changing JSON');
var charttype = ["area", "line", "bar"];
var max = 100;
var plots = 3;
var points = 15;
var colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple', 'pink'];
var data = {
type: charttype[this.random(charttype.length)]
};
var series = [];
for (var i=0;i<plots;i++){
var color = colors[this.random(colors.length)];
var plot = {
lineColor: color,
backgroundColor: color
}
var plotdata = [];
for (var j=0;j<points;j++){
plotdata.push(this.random(max) + 1);
}
plot.values = plotdata;
series.push(plot);
}
data.series = series;
this.zc.set("json", data);
},
addPlot: function(){
var values = [];
for (var j=0;j<15;j++){
values.push(this.random(100) + 1);
}
this.zcView.exec('addplot', {
data : {
values : values
}
});
},
addLegend: function(){
var graph = this.zcView.exec('getdata');
if (graph.graphset){
graph = graph.graphset[0];
}
if(!graph.legend){
graph.legend = {};
}
this.zc.set('json', graph);
this.zcView.bind('legend_item_click', this.alertClick);
},
alertClick: function(){
alert("CLICKED");
},
changeData: function(){
console.log('Change Data');
var max = 100;
var plots = 3;
var points = 15;
var series = [];
for (var i=0;i<plots;i++){
var plotdata = [];
for (var j=0;j<points;j++){
plotdata.push(this.random(max) + 1);
}
series.push(plotdata);
}
this.zc.set("data", series);
}
});
var demo = new DemoView();
demo.render();
</script>
</body>
</html>