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
|
function format_rollover_number(args) {
var num;
if (args.format === 'count') {
num = function(d) {
var is_float = d % 1 !== 0;
var pf;
if (is_float) {
pf = d3.format(',.' + args.decimals + 'f');
} else {
pf = d3.format(',.0f');
}
// are we adding units after the value or before?
if (args.yax_units_append) {
return pf(d) + args.yax_units;
} else {
return args.yax_units + pf(d);
}
};
} else {
num = function(d_) {
var fmt_string = (isNumeric(args.decimals) ? '.' + args.decimals : '') + '%';
var pf = d3.format(fmt_string);
return pf(d_);
};
}
return num;
}
var time_rollover_format = function(f, d, accessor, utc) {
var fd;
if (typeof f === 'string') {
fd = MG.time_format(utc, f)(d[accessor]);
} else if (typeof f === 'function') {
fd = f(d);
} else {
fd = d[accessor];
}
return fd;
};
// define our rollover format for numbers
var number_rollover_format = function(f, d, accessor) {
var fd;
if (typeof f === 'string') {
fd = d3.format('s')(d[accessor]);
} else if (typeof f === 'function') {
fd = f(d);
} else {
fd = d[accessor];
}
return fd;
};
function mg_format_y_rollover(args, num, d) {
var formatted_y;
if (args.y_mouseover !== null) {
if (args.aggregate_rollover) {
formatted_y = number_rollover_format(args.y_mouseover, d, args.y_accessor);
} else {
formatted_y = number_rollover_format(args.y_mouseover, d, args.y_accessor);
}
} else {
if (args.time_series) {
if (args.aggregate_rollover) {
formatted_y = num(d[args.y_accessor]);
} else {
formatted_y = args.yax_units + num(d[args.y_accessor]);
}
} else {
formatted_y = args.y_accessor + ': ' + args.yax_units + num(d[args.y_accessor]);
}
}
return formatted_y;
}
function mg_format_x_rollover(args, fmt, d) {
var formatted_x;
if (args.x_mouseover !== null) {
if (args.time_series) {
if (args.aggregate_rollover) {
formatted_x = time_rollover_format(args.x_mouseover, d, 'key', args.utc);
} else {
formatted_x = time_rollover_format(args.x_mouseover, d, args.x_accessor, args.utc);
}
} else {
formatted_x = number_rollover_format(args.x_mouseover, d, args.x_accessor);
}
} else {
if (args.time_series) {
var date;
if (args.aggregate_rollover && args.data.length > 1) {
date = new Date(d.key);
} else {
date = new Date(+d[args.x_accessor]);
date.setDate(date.getDate());
}
formatted_x = fmt(date) + ' ';
} else {
formatted_x = args.x_accessor + ': ' + d[args.x_accessor] + ' ';
}
}
return formatted_x;
}
function mg_format_data_for_mouseover(args, d, mouseover_fcn, accessor, check_time) {
var formatted_data, formatter;
var time_fmt = mg_get_rollover_time_format(args);
if (typeof d[accessor] === 'string') {
formatter = function(d) {
return d;
};
} else {
formatter = format_rollover_number(args);
}
if (mouseover_fcn !== null) {
if (check_time) formatted_data = time_rollover_format(mouseover_fcn, d, accessor, args.utc);
else formatted_data = number_rollover_format(mouseover_fcn, d, accessor);
} else {
if (check_time) formatted_data = time_fmt(new Date(+d[accessor])) + ' ';
else formatted_data = (args.time_series ? '' : accessor + ': ') + formatter(d[accessor]) + ' ';
}
return formatted_data;
}
function mg_format_number_mouseover(args, d) {
return mg_format_data_for_mouseover(args, d, args.x_mouseover, args.x_accessor, false);
}
function mg_format_x_mouseover(args, d) {
return mg_format_data_for_mouseover(args, d, args.x_mouseover, args.x_accessor, args.time_series);
}
function mg_format_y_mouseover(args, d) {
return mg_format_data_for_mouseover(args, d, args.y_mouseover, args.y_accessor, false);
}
function mg_format_x_aggregate_mouseover(args, d) {
return mg_format_data_for_mouseover(args, d, args.x_mouseover, 'key', args.time_series);
}
MG.format_rollover_number = format_rollover_number;
|