html5jp_graphs 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitigore +2 -0
- data/README.rdoc +82 -0
- data/Rakefile +22 -0
- data/html5jp_graphs.gemspec +17 -0
- data/lib/examples/sample_graph_circle_1.html +54 -0
- data/lib/examples/sample_graph_circle_2.html +61 -0
- data/lib/examples/sample_graph_line_1.html +44 -0
- data/lib/examples/sample_graph_line_2.html +53 -0
- data/lib/examples/sample_graph_radar_1.html +47 -0
- data/lib/examples/sample_graph_radar_2.html +52 -0
- data/lib/examples/sample_graph_vbar_1.html +51 -0
- data/lib/examples/sample_graph_vbar_2.html +52 -0
- data/lib/examples/sample_graph_vbar_3.html +52 -0
- data/lib/examples/sample_graph_vbar_4.html +52 -0
- data/lib/generators/html5jp_graphs/USAGE +8 -0
- data/lib/generators/html5jp_graphs/html5jp_graphs_generator.rb +12 -0
- data/lib/generators/html5jp_graphs/templates/excanvas/AUTHORS +10 -0
- data/lib/generators/html5jp_graphs/templates/excanvas/COPYING +202 -0
- data/lib/generators/html5jp_graphs/templates/excanvas/README +22 -0
- data/lib/generators/html5jp_graphs/templates/excanvas/examples/example1.html +93 -0
- data/lib/generators/html5jp_graphs/templates/excanvas/examples/example2.html +513 -0
- data/lib/generators/html5jp_graphs/templates/excanvas/examples/example3.html +284 -0
- data/lib/generators/html5jp_graphs/templates/excanvas/examples/ff.jpg +0 -0
- data/lib/generators/html5jp_graphs/templates/excanvas/excanvas-compressed.js +19 -0
- data/lib/generators/html5jp_graphs/templates/excanvas/excanvas.js +785 -0
- data/lib/generators/html5jp_graphs/templates/excanvas/testcases/arc.html +49 -0
- data/lib/generators/html5jp_graphs/templates/excanvas/testcases/linewidth.html +47 -0
- data/lib/generators/html5jp_graphs/templates/excanvas/testcases/overflow.html +37 -0
- data/lib/generators/html5jp_graphs/templates/excanvas/testcases/quadraticcurve.html +74 -0
- data/lib/generators/html5jp_graphs/templates/excanvas/testcases/resizing.html +65 -0
- data/lib/generators/html5jp_graphs/templates/graph/circle.js +407 -0
- data/lib/generators/html5jp_graphs/templates/graph/line.js +577 -0
- data/lib/generators/html5jp_graphs/templates/graph/radar.js +545 -0
- data/lib/generators/html5jp_graphs/templates/graph/vbar.js +1156 -0
- data/lib/html5jp_graphs.rb +1 -0
- data/lib/html5jp_graphs/version.rb +3 -0
- data/lib/html5jp_graphs_helper.rb +255 -0
- data/tasks/html5jp_graphs_tasks.rake +4 -0
- data/test/html5jp_graphs_test.rb +8 -0
- metadata +88 -0
@@ -0,0 +1,577 @@
|
|
1
|
+
// Copyright 2007 futomi http://www.html5.jp/
|
2
|
+
//
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
// you may not use this file except in compliance with the License.
|
5
|
+
// You may obtain a copy of the License at
|
6
|
+
//
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
//
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
// See the License for the specific language governing permissions and
|
13
|
+
// limitations under the License.
|
14
|
+
|
15
|
+
if( typeof html5jp == 'undefined' ) {
|
16
|
+
html5jp = new Object();
|
17
|
+
}
|
18
|
+
if( typeof html5jp.graph == 'undefined' ) {
|
19
|
+
html5jp.graph = new Object();
|
20
|
+
}
|
21
|
+
|
22
|
+
/* ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
23
|
+
* コンストラクタ
|
24
|
+
* ---------------------------------------------------------------- */
|
25
|
+
html5jp.graph.line = function (id) {
|
26
|
+
var elm = document.getElementById(id);
|
27
|
+
if(! elm) { return; }
|
28
|
+
if(elm.nodeName != "CANVAS") { return; }
|
29
|
+
if(elm.parentNode.nodeName != "DIV") { return; };
|
30
|
+
this.canvas = elm;
|
31
|
+
/* CANVAS要素 */
|
32
|
+
if ( ! this.canvas ){ return; }
|
33
|
+
if ( ! this.canvas.getContext ){ return; }
|
34
|
+
/* 2D コンテクストの生成 */
|
35
|
+
this.ctx = this.canvas.getContext('2d');
|
36
|
+
this.canvas.style.margin = "0";
|
37
|
+
this.canvas.parentNode.style.position = "relative";
|
38
|
+
this.canvas.parentNode.style.padding = "0";
|
39
|
+
};
|
40
|
+
/* ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
41
|
+
* 描画
|
42
|
+
* ---------------------------------------------------------------- */
|
43
|
+
html5jp.graph.line.prototype.draw = function(items, inparams) {
|
44
|
+
if( ! this.ctx ) {return;}
|
45
|
+
/* パラメータの初期化 */
|
46
|
+
var params = {
|
47
|
+
x: [],
|
48
|
+
y: [],
|
49
|
+
yMax: null,
|
50
|
+
yMin: 0,
|
51
|
+
backgroundColor: "#ffffff",
|
52
|
+
gbackgroundColor: "#dddddd",
|
53
|
+
gGradation: true,
|
54
|
+
lineWidth: 1,
|
55
|
+
dotRadius: 3,
|
56
|
+
dotType: "disc",
|
57
|
+
hLineWidth: 2,
|
58
|
+
hLineType: "dotted",
|
59
|
+
hLineColor: "#aaaaaa",
|
60
|
+
xAxisWidth: 2,
|
61
|
+
xAxisColor: "#000000",
|
62
|
+
yAxisWidth: 2,
|
63
|
+
yAxisColor: "#000000",
|
64
|
+
xScaleColor: "#000000",
|
65
|
+
xScaleFontSize: "10px",
|
66
|
+
xScaleFontFamily: "Arial,sans-serif",
|
67
|
+
yScaleColor: "#000000",
|
68
|
+
yScaleFontSize: "10px",
|
69
|
+
yScaleFontFamily: "Arial,sans-serif",
|
70
|
+
xCaptionColor: "#000000",
|
71
|
+
xCaptionFontSize: "12px",
|
72
|
+
xCaptionFontFamily: "Arial,sans-serif",
|
73
|
+
yCaptionColor: "#000000",
|
74
|
+
yCaptionFontSize: "12px",
|
75
|
+
yCaptionFontFamily: "Arial,sans-serif",
|
76
|
+
dLabel: true,
|
77
|
+
dLabelColor: "#000000",
|
78
|
+
dLabelFontSize: "10px",
|
79
|
+
dLabelFontFamily: "Arial,sans-serif",
|
80
|
+
legend: true,
|
81
|
+
legendFontSize: "12px",
|
82
|
+
legendFontFamily: "Arial,sans-serif",
|
83
|
+
legendColor: "#000000"
|
84
|
+
};
|
85
|
+
if( inparams && typeof(inparams) == 'object' ) {
|
86
|
+
for( var key in inparams ) {
|
87
|
+
params[key] = inparams[key];
|
88
|
+
}
|
89
|
+
}
|
90
|
+
this.params = params;
|
91
|
+
/* CANVASの背景を塗る */
|
92
|
+
if( params.backgroundColor ) {
|
93
|
+
this.ctx.beginPath();
|
94
|
+
this.ctx.fillStyle = params.backgroundColor;
|
95
|
+
this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
|
96
|
+
}
|
97
|
+
/* CANVAS要素の横幅が縦幅の1.5倍未満、または縦幅が200未満であれば凡例は強制的に非表示 */
|
98
|
+
if(this.canvas.width / this.canvas.height < 1.5 || this.canvas.height < 200) {
|
99
|
+
params.legend == false;
|
100
|
+
}
|
101
|
+
/* CANVAS要素の座標 */
|
102
|
+
var canvas_pos = this._getElementAbsPos(this.canvas);
|
103
|
+
/* 折線グラフの軸のcanvas内座標 */
|
104
|
+
var cpos = {
|
105
|
+
x0: this.canvas.width * 0.1,
|
106
|
+
y0: this.canvas.height * 0.9,
|
107
|
+
x1: this.canvas.width * 0.9,
|
108
|
+
y1: this.canvas.height * 0.1
|
109
|
+
};
|
110
|
+
if( typeof(params.x) == "object" && params.x.length > 0) {
|
111
|
+
cpos.y0 = this.canvas.height * 0.8;
|
112
|
+
}
|
113
|
+
if( typeof(params.y) == "object" && params.y.length > 0) {
|
114
|
+
cpos.x0 = this.canvas.width * 0.15;
|
115
|
+
cpos.y1 = this.canvas.height * 0.15
|
116
|
+
}
|
117
|
+
if(params.legend == true) {
|
118
|
+
cpos.x1 = this.canvas.width * 0.7;
|
119
|
+
}
|
120
|
+
cpos.w = cpos.x1 - cpos.x0;
|
121
|
+
cpos.h = cpos.y0 - cpos.y1;
|
122
|
+
/* 項目の数(最大10個)*/
|
123
|
+
var item_num = items.length;
|
124
|
+
if(item_num > 10) { item_num = 10; }
|
125
|
+
/* 凡例の各種座標を算出 */
|
126
|
+
if(params.legend == true) {
|
127
|
+
/* DIV要素を仮に挿入してみて高さを調べる(1行分の高さ) */
|
128
|
+
var legend_tmp_s = this._getTextBoxSize('あTEST', params.legendFontSize, params.legendFontFamily);
|
129
|
+
/* 凡例の各種座標を算出 */
|
130
|
+
var lpos = {
|
131
|
+
x: Math.round( cpos.x1 + this.canvas.width * 0.03 ),
|
132
|
+
y: Math.round( ( this.canvas.height - ( legend_tmp_s.h * item_num + legend_tmp_s.h * 0.2 * (item_num - 1) ) ) / 2 ),
|
133
|
+
h: legend_tmp_s.h
|
134
|
+
};
|
135
|
+
lpos.cx = lpos.x + Math.round( lpos.h * 2.5 ); // 文字表示開始位置(x座標)
|
136
|
+
lpos.cw = this.canvas.width - lpos.cx; // 文字表示幅
|
137
|
+
}
|
138
|
+
/* グラフの背景を塗りつぶす */
|
139
|
+
if(params.gGradation == true) {
|
140
|
+
this.ctx.beginPath();
|
141
|
+
this.ctx.moveTo(cpos.x0, cpos.y0);
|
142
|
+
this.ctx.lineTo(cpos.x1, cpos.y0);
|
143
|
+
this.ctx.lineTo(cpos.x1, cpos.y1);
|
144
|
+
this.ctx.lineTo(cpos.x0, cpos.y1);
|
145
|
+
this.ctx.closePath();
|
146
|
+
var radgrad = this.ctx.createLinearGradient(cpos.x0,cpos.y1,cpos.x0,cpos.y0);
|
147
|
+
var o_gbc = this._csscolor2rgb(params.gbackgroundColor);
|
148
|
+
var gbc = o_gbc.r + "," + o_gbc.g + "," + o_gbc.b;
|
149
|
+
radgrad.addColorStop(0, "rgb(" + gbc + ")");
|
150
|
+
radgrad.addColorStop(1, "rgb(255,255,255)");
|
151
|
+
this.ctx.fillStyle = radgrad;
|
152
|
+
this.ctx.fill();
|
153
|
+
} else {
|
154
|
+
this.ctx.fillStyle = params.gbackgroundColor;
|
155
|
+
this.ctx.fillRect(cpos.x0, cpos.y1, cpos.w, cpos.h);
|
156
|
+
}
|
157
|
+
/* 全項目の最大値・最小値と項目数を算出 */
|
158
|
+
var max_v = null;
|
159
|
+
var min_v = null;
|
160
|
+
var max_n = 0;
|
161
|
+
if(params.y.length > 1) {
|
162
|
+
max = params.y[ params.y.length - 1 ];
|
163
|
+
}
|
164
|
+
for(var i=0; i<item_num; i++) {
|
165
|
+
var n = items[i].length;
|
166
|
+
if(n < 2) { continue; }
|
167
|
+
for(var j=1; j<n; j++) {
|
168
|
+
var v = items[i][j];
|
169
|
+
if( isNaN(v) ) {
|
170
|
+
throw new Error('invalid graph item data.' + n);
|
171
|
+
}
|
172
|
+
if(max_v == null) {
|
173
|
+
max_v = v;
|
174
|
+
} else if(v >= max_v) {
|
175
|
+
max_v = v;
|
176
|
+
}
|
177
|
+
if(min_v == null) {
|
178
|
+
min_v = v;
|
179
|
+
} else if(v <= min_v) {
|
180
|
+
min_v = v;
|
181
|
+
}
|
182
|
+
}
|
183
|
+
if(n - 1 >= max_n) {
|
184
|
+
max_n = n - 1;
|
185
|
+
}
|
186
|
+
}
|
187
|
+
if( typeof(params.yMin) != "number" ) {
|
188
|
+
params.yMin = 0;
|
189
|
+
}
|
190
|
+
if( typeof(params.yMax) != "number" ) {
|
191
|
+
params.yMax = max_v + Math.abs(max_v - min_v) * 0.1;
|
192
|
+
}
|
193
|
+
var v_range = Math.abs( params.yMax - params.yMin );
|
194
|
+
/* 水平補助線 */
|
195
|
+
if( typeof(params.hLineWidth) == "number" && params.hLineWidth > 0 ) {
|
196
|
+
var h_line_type = "dashed";
|
197
|
+
if( params.hLineType.match(/^(solid|dashed|dotted)$/i) ) {
|
198
|
+
h_line_type = params.hLineType;
|
199
|
+
}
|
200
|
+
for(var i=1; i<params.y.length; i++) {
|
201
|
+
var aline_x0 = cpos.x0;
|
202
|
+
var aline_y0 = Math.round( cpos.y0 - cpos.h * ( params.y[i] - params.yMin ) / v_range );
|
203
|
+
var aline_x1 = cpos.x1;
|
204
|
+
this._draw_h_aline(aline_x0, aline_y0, aline_x1, params.hLineWidth, h_line_type, params.hLineColor);
|
205
|
+
}
|
206
|
+
}
|
207
|
+
/* 各項目のデフォルト色を定義 */
|
208
|
+
var colors = ["24,41,206", "198,0,148", "214,0,0", "255,156,0", "33,156,0", "33,41,107", "115,0,90", "132,0,0", "165,99,0", "24,123,0"];
|
209
|
+
/* 折線グラフを描写 */
|
210
|
+
var plots = new Array();
|
211
|
+
for(var i=0; i<item_num; i++) {
|
212
|
+
this.ctx.beginPath();
|
213
|
+
this.ctx.lineJoin = "round";
|
214
|
+
plots[i] = new Array();
|
215
|
+
var n = items[i].length;
|
216
|
+
for(var j=1; j<n; j++) {
|
217
|
+
/* 項目の値 */
|
218
|
+
var v = items[i][j];
|
219
|
+
/* canvas座標を算出 */
|
220
|
+
var p = {
|
221
|
+
x: Math.round( cpos.x0 + cpos.w * ( j - 0.5 ) / max_n ),
|
222
|
+
y: Math.round( cpos.y0 - cpos.h * ( v - params.yMin ) / v_range ),
|
223
|
+
v: v
|
224
|
+
}
|
225
|
+
plots[i].push(p);
|
226
|
+
/* 線を描画 */
|
227
|
+
if(j == 1) {
|
228
|
+
this.ctx.moveTo(p.x, p.y);
|
229
|
+
} else {
|
230
|
+
this.ctx.lineTo(p.x, p.y);
|
231
|
+
}
|
232
|
+
}
|
233
|
+
/* 線の太さを定義 */
|
234
|
+
var line_width = 1;
|
235
|
+
if( typeof(params.lineWidth) == "object" && ! isNaN(params.lineWidth[i])) {
|
236
|
+
line_width = params.lineWidth[i];
|
237
|
+
} else if( typeof(params.lineWidth) == "number" && ! isNaN(params.lineWidth)) {
|
238
|
+
line_width = params.lineWidth;
|
239
|
+
}
|
240
|
+
this.ctx.lineWidth = line_width;
|
241
|
+
/* 線の色を定義 */
|
242
|
+
var line_color = "rgb(" + colors[i] + ")";
|
243
|
+
this.ctx.strokeStyle = line_color;
|
244
|
+
/* 線を描画 */
|
245
|
+
this.ctx.stroke();
|
246
|
+
/* ドットの半径を特定 */
|
247
|
+
var dot_rad = null;
|
248
|
+
if( typeof(params.dotRadius) == "object" && ! isNaN(params.dotRadius[i]) && params.dotRadius[i] > 0 ) {
|
249
|
+
dot_rad = params.dotRadius[i];
|
250
|
+
} else if( typeof(params.dotRadius) == "number" && ! isNaN(params.dotRadius) && params.dotRadius > 0 ) {
|
251
|
+
dot_rad = params.dotRadius;
|
252
|
+
}
|
253
|
+
/* ドットのタイプを特定 */
|
254
|
+
var dot_type = null;
|
255
|
+
if( typeof(params.dotType) == "object" && typeof(params.dotType[i]) == "string" ) {
|
256
|
+
dot_type = params.dotType[i];
|
257
|
+
} else if( typeof(params.dotType) == "string" ) {
|
258
|
+
dot_type = params.dotType;
|
259
|
+
} else {
|
260
|
+
dot_type = "disc";
|
261
|
+
}
|
262
|
+
/* ドットを描画 */
|
263
|
+
if(dot_rad > 0) {
|
264
|
+
for(var k=0; k<plots[i].length; k++) {
|
265
|
+
this._draw_dot(plots[i][k].x, plots[i][k].y, dot_rad, dot_type, colors[i]);
|
266
|
+
}
|
267
|
+
}
|
268
|
+
/* データラベルを描画 */
|
269
|
+
if(params.dLabel == true) {
|
270
|
+
for(var k=0; k<plots[i].length; k++) {
|
271
|
+
if(plots[i][k].x < cpos.x0 || plots[i][k].x > cpos.x1 || plots[i][k].y > cpos.y0 || plots[i][k].y < cpos.y1) {
|
272
|
+
continue;
|
273
|
+
}
|
274
|
+
var dlabel = plots[i][k].v.toString();
|
275
|
+
var margin = 1;
|
276
|
+
if(dot_rad != null && dot_rad > 0) {
|
277
|
+
margin += dot_rad;
|
278
|
+
}
|
279
|
+
var s = this._getTextBoxSize(dlabel, params.dLabelFontSize, params.dLabelFontFamily);
|
280
|
+
var dlabel_x = plots[i][k].x - Math.round( s.w / 2 );
|
281
|
+
var dlabel_y = plots[i][k].y - Math.round( s.h ) - margin;
|
282
|
+
this._drawText(dlabel_x, dlabel_y, dlabel, params.dLabelFontSize, params.dLabelFontFamily, params.dLabelColor);
|
283
|
+
}
|
284
|
+
}
|
285
|
+
/* 凡例を描画 */
|
286
|
+
if(params.legend == true) {
|
287
|
+
/* 文字 */
|
288
|
+
this._drawText(lpos.cx, lpos.y, items[i][0], params.legendFontSize, params.legendFontFamily, params.legendColor);
|
289
|
+
/* 記号(罫線) */
|
290
|
+
this._draw_h_aline(lpos.x, Math.round(lpos.y+lpos.h/2), lpos.x + lpos.h*2, line_width, "solid", line_color);
|
291
|
+
/* 記号(ドット) */
|
292
|
+
if(dot_rad > 0) {
|
293
|
+
this._draw_dot(Math.round(lpos.x+lpos.h), Math.round(lpos.y+lpos.h/2), dot_rad, dot_type, colors[i]);
|
294
|
+
}
|
295
|
+
/* */
|
296
|
+
lpos.y = lpos.y + lpos.h * 1.2;
|
297
|
+
}
|
298
|
+
}
|
299
|
+
/* グラフ描画領域外の上下を背景色で塗りつぶす */
|
300
|
+
this.ctx.fillStyle = params.backgroundColor;
|
301
|
+
this.ctx.fillRect(cpos.x0, 0, cpos.w, cpos.y1);
|
302
|
+
this.ctx.fillRect(cpos.x0, cpos.y0, cpos.w, this.canvas.height - cpos.y0);
|
303
|
+
/* x軸 */
|
304
|
+
if( typeof(params.xAxisWidth) == "number" && params.xAxisWidth > 0 ) {
|
305
|
+
this._draw_h_aline(cpos.x0, cpos.y0, cpos.x1, params.xAxisWidth, "solid", params.xAxisColor);
|
306
|
+
}
|
307
|
+
/* y軸 */
|
308
|
+
if( typeof(params.yAxisWidth) == "number" && params.yAxisWidth > 0 ) {
|
309
|
+
this._draw_v_aline(cpos.x0, cpos.y0, cpos.y1, params.yAxisWidth, "solid", params.yAxisColor);
|
310
|
+
}
|
311
|
+
/* x軸の目盛文字列を描画 */
|
312
|
+
var xscale_y_under = 0;
|
313
|
+
for(var i=1; i<=max_n; i++) {
|
314
|
+
if( typeof(params.x[i] ) != "string" ) { continue; }
|
315
|
+
if(params.x[i] == "") { continue; }
|
316
|
+
var s = this._getTextBoxSize(params.x[i], params.xScaleFontSize, params.xScaleFontFamily);
|
317
|
+
var xscale_x = Math.round( cpos.x0 + cpos.w * ( i - 0.5 ) / max_n ) - Math.round( s.w / 2 );
|
318
|
+
var xscale_y = cpos.y0 + 5;
|
319
|
+
this._drawText(xscale_x, xscale_y, params.x[i], params.xScaleFontSize, params.xScaleFontFamily, params.xScaleColor);
|
320
|
+
if(xscale_y + s.h >= xscale_y_under) {
|
321
|
+
xscale_y_under = xscale_y + s.h;
|
322
|
+
}
|
323
|
+
}
|
324
|
+
/* y軸の目盛数値を描画 */
|
325
|
+
var yscale_y_top = this.canvas.height;
|
326
|
+
for(var i=1; i<params.y.length; i++) {
|
327
|
+
if( typeof(params.y[i] ) != "number" ) { continue; }
|
328
|
+
var v = params.y[i].toString();
|
329
|
+
if(v == "") { continue; }
|
330
|
+
if(params.y[i] < params.yMin || params.y[i] > params.yMax) { continue; }
|
331
|
+
var s = this._getTextBoxSize(v, params.yScaleFontSize, params.yScaleFontFamily);
|
332
|
+
var yscale_y = Math.round( cpos.y0 - cpos.h * ( params.y[i] - params.yMin ) / v_range ) - Math.round( s.h / 2 );
|
333
|
+
var yscale_x = Math.round( cpos.x0 - s.w ) - 5;
|
334
|
+
this._drawText(yscale_x, yscale_y, v, params.yScaleFontSize, params.yScaleFontFamily, params.yScaleColor);
|
335
|
+
if(yscale_y <= yscale_y_top) {
|
336
|
+
yscale_y_top = yscale_y;
|
337
|
+
}
|
338
|
+
}
|
339
|
+
/* x軸のキャプションを描画 */
|
340
|
+
if( typeof(params.x[0]) == "string" && params.x[0] != "" ) {
|
341
|
+
var s = this._getTextBoxSize(params.x[0], params.xCaptionFontSize, params.xCaptionFontFamily);
|
342
|
+
var xcaption_y = cpos.y0 + 5;
|
343
|
+
if(xscale_y_under > 0) {
|
344
|
+
xcaption_y = xscale_y_under + 5;
|
345
|
+
}
|
346
|
+
var xcaption_x = Math.round( cpos.x0 + ( cpos.w / 2 ) - ( s.w / 2 ) );
|
347
|
+
this._drawText(xcaption_x, xcaption_y, params.x[0], params.xCaptionFontSize, params.xCaptionFontFamily, params.xCaptionColor);
|
348
|
+
}
|
349
|
+
/* y軸のキャプションを描画 */
|
350
|
+
if( typeof(params.y[0]) == "string" && params.y[0] != "" ) {
|
351
|
+
var s = this._getTextBoxSize(params.y[0], params.yCaptionFontSize, params.yCaptionFontFamily);
|
352
|
+
var ycaption_y = yscale_y_top - s.h - 5;
|
353
|
+
if(yscale_y_top > cpos.y1) {
|
354
|
+
ycaption_y = cpos.y1 - s.h - 5;
|
355
|
+
}
|
356
|
+
var ycaption_x = Math.round( cpos.x0 - ( s.w / 2 ) );
|
357
|
+
if(ycaption_x < 5) {
|
358
|
+
ycaption_x = 5;
|
359
|
+
}
|
360
|
+
this._drawText(ycaption_x, ycaption_y, params.y[0], params.yCaptionFontSize, params.yCaptionFontFamily, params.yCaptionColor);
|
361
|
+
}
|
362
|
+
};
|
363
|
+
|
364
|
+
/* ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
365
|
+
* 以下、内部関数
|
366
|
+
* ──────────────────────────────── */
|
367
|
+
|
368
|
+
/* ------------------------------------------------------------------
|
369
|
+
垂直補助線を描画
|
370
|
+
* ---------------------------------------------------------------- */
|
371
|
+
html5jp.graph.line.prototype._draw_v_aline = function(x0, y0, y1, width, type, color) {
|
372
|
+
color = this._csscolor2rgb(color);
|
373
|
+
this.ctx.beginPath();
|
374
|
+
color = "rgb(" + color.r + "," + color.g + "," + color.b + ")"
|
375
|
+
this.ctx.strokeStyle = color;
|
376
|
+
this.ctx.lineWidth = width;
|
377
|
+
if(type == "solid") {
|
378
|
+
this.ctx.moveTo(x0, y0);
|
379
|
+
this.ctx.lineTo(x0, y1);
|
380
|
+
this.ctx.stroke();
|
381
|
+
} else if( type == "dashed" || (type == "dotted" && width < 2) ) {
|
382
|
+
var y = y0;
|
383
|
+
while(1) {
|
384
|
+
if(y - width*4 < y1) { break; }
|
385
|
+
this.ctx.moveTo(x0, y);
|
386
|
+
y = y - width * 4;
|
387
|
+
this.ctx.lineTo(x0, y);
|
388
|
+
this.ctx.stroke();
|
389
|
+
if(y - width*2 < y1) { break; }
|
390
|
+
y = y - width*2;
|
391
|
+
}
|
392
|
+
} else if(type == "dotted") {
|
393
|
+
this.ctx.fillStyle = color;
|
394
|
+
var y = y0;
|
395
|
+
while(1) {
|
396
|
+
if(y - width*2 < y1) { break; }
|
397
|
+
this.ctx.arc(x0, y, width/2, 0, Math.PI*2, false);
|
398
|
+
this.ctx.fill();
|
399
|
+
if(y - width*2 < y1) { break; }
|
400
|
+
y = y - width*2;
|
401
|
+
}
|
402
|
+
}
|
403
|
+
};
|
404
|
+
/* ------------------------------------------------------------------
|
405
|
+
水平補助線を描画
|
406
|
+
* ---------------------------------------------------------------- */
|
407
|
+
html5jp.graph.line.prototype._draw_h_aline = function(x0, y0, x1, width, type, color) {
|
408
|
+
color = this._csscolor2rgb(color);
|
409
|
+
this.ctx.beginPath();
|
410
|
+
color = "rgb(" + color.r + "," + color.g + "," + color.b + ")"
|
411
|
+
this.ctx.strokeStyle = color;
|
412
|
+
this.ctx.lineWidth = width;
|
413
|
+
if(type == "solid") {
|
414
|
+
this.ctx.moveTo(x0, y0);
|
415
|
+
this.ctx.lineTo(x1, y0);
|
416
|
+
this.ctx.stroke();
|
417
|
+
} else if( type == "dashed" || (type == "dotted" && width < 2) ) {
|
418
|
+
var x = x0;
|
419
|
+
while(1) {
|
420
|
+
if(x + width*4 > x1) { break; }
|
421
|
+
this.ctx.moveTo(x, y0);
|
422
|
+
x = x + width * 4;
|
423
|
+
this.ctx.lineTo(x, y0);
|
424
|
+
this.ctx.stroke();
|
425
|
+
if(x + width*2 > x1) { break; }
|
426
|
+
x = x + width*2;
|
427
|
+
}
|
428
|
+
} else if(type == "dotted") {
|
429
|
+
this.ctx.fillStyle = color;
|
430
|
+
var x = x0;
|
431
|
+
while(1) {
|
432
|
+
if(x + width*2 > x1) { break; }
|
433
|
+
this.ctx.arc(x, y0, width/2, 0, Math.PI*2, false);
|
434
|
+
this.ctx.fill();
|
435
|
+
if(x + width*2 > x1) { break; }
|
436
|
+
x = x + width*2;
|
437
|
+
}
|
438
|
+
}
|
439
|
+
};
|
440
|
+
/* ------------------------------------------------------------------
|
441
|
+
折線のドットを描画
|
442
|
+
* ---------------------------------------------------------------- */
|
443
|
+
html5jp.graph.line.prototype._draw_dot = function(x, y, rad, type, color) {
|
444
|
+
this.ctx.beginPath();
|
445
|
+
this.ctx.fillStyle = "rgb(" + color + ")";
|
446
|
+
if( type == "disc" ) {
|
447
|
+
this.ctx.arc(x, y, rad, 0, Math.PI*2, false);
|
448
|
+
} else if( type == "square" ) {
|
449
|
+
this.ctx.moveTo(x-rad, y-rad);
|
450
|
+
this.ctx.lineTo(x+rad, y-rad);
|
451
|
+
this.ctx.lineTo(x+rad, y+rad);
|
452
|
+
this.ctx.lineTo(x-rad, y+rad);
|
453
|
+
} else if( type == "triangle" ) {
|
454
|
+
this.ctx.moveTo(x, y-rad);
|
455
|
+
this.ctx.lineTo(x+rad, y+rad);
|
456
|
+
this.ctx.lineTo(x-rad, y+rad);
|
457
|
+
} else if( type == "i-triangle" ) {
|
458
|
+
this.ctx.moveTo(x, y+rad);
|
459
|
+
this.ctx.lineTo(x+rad, y-rad);
|
460
|
+
this.ctx.lineTo(x-rad, y-rad);
|
461
|
+
} else if( type == "diamond" ) {
|
462
|
+
this.ctx.moveTo(x, y-rad);
|
463
|
+
this.ctx.lineTo(x+rad, y);
|
464
|
+
this.ctx.lineTo(x, y+rad);
|
465
|
+
this.ctx.lineTo(x-rad, y);
|
466
|
+
} else {
|
467
|
+
this.ctx.arc(x, y, rad, 0, Math.PI*2, false);
|
468
|
+
}
|
469
|
+
this.ctx.closePath();
|
470
|
+
this.ctx.fill();
|
471
|
+
};
|
472
|
+
|
473
|
+
/* ------------------------------------------------------------------
|
474
|
+
文字列を描画
|
475
|
+
* ---------------------------------------------------------------- */
|
476
|
+
html5jp.graph.line.prototype._drawText = function(x, y, text, font_size, font_family, color) {
|
477
|
+
var div = document.createElement('DIV');
|
478
|
+
div.appendChild( document.createTextNode(text) );
|
479
|
+
div.style.fontSize = font_size;
|
480
|
+
div.style.fontFamily = font_family;
|
481
|
+
div.style.color = color;
|
482
|
+
div.style.margin = "0";
|
483
|
+
div.style.padding = "0";
|
484
|
+
div.style.position = "absolute";
|
485
|
+
div.style.left = x.toString() + "px";
|
486
|
+
div.style.top = y.toString() + "px";
|
487
|
+
this.canvas.parentNode.appendChild(div);
|
488
|
+
}
|
489
|
+
/* ------------------------------------------------------------------
|
490
|
+
文字列表示領域のサイズを取得
|
491
|
+
* ---------------------------------------------------------------- */
|
492
|
+
html5jp.graph.line.prototype._getTextBoxSize = function(text, font_size, font_family) {
|
493
|
+
var tmpdiv = document.createElement('DIV');
|
494
|
+
tmpdiv.appendChild( document.createTextNode(text) );
|
495
|
+
tmpdiv.style.fontSize = font_size;
|
496
|
+
tmpdiv.style.fontFamily = font_family;
|
497
|
+
tmpdiv.style.margin = "0";
|
498
|
+
tmpdiv.style.padding = "0";
|
499
|
+
tmpdiv.style.visible = "hidden";
|
500
|
+
tmpdiv.style.position = "absolute";
|
501
|
+
tmpdiv.style.left = "0px";
|
502
|
+
tmpdiv.style.top = "0px";
|
503
|
+
this.canvas.parentNode.appendChild(tmpdiv);
|
504
|
+
var o = {
|
505
|
+
w: tmpdiv.offsetWidth,
|
506
|
+
h: tmpdiv.offsetHeight
|
507
|
+
};
|
508
|
+
tmpdiv.parentNode.removeChild(tmpdiv);
|
509
|
+
return o;
|
510
|
+
}
|
511
|
+
|
512
|
+
/* ------------------------------------------------------------------
|
513
|
+
ブラウザー表示領域左上端を基点とする座標系におけるelmの左上端の座標
|
514
|
+
* ---------------------------------------------------------------- */
|
515
|
+
html5jp.graph.line.prototype._getElementAbsPos = function(elm) {
|
516
|
+
var obj = new Object();
|
517
|
+
obj.x = elm.offsetLeft;
|
518
|
+
obj.y = elm.offsetTop;
|
519
|
+
while(elm.offsetParent) {
|
520
|
+
elm = elm.offsetParent;
|
521
|
+
obj.x += elm.offsetLeft;
|
522
|
+
obj.y += elm.offsetTop;
|
523
|
+
}
|
524
|
+
return obj;
|
525
|
+
};
|
526
|
+
|
527
|
+
/* ------------------------------------------------------------------
|
528
|
+
* CSS色文字列をRGBに変換
|
529
|
+
* ---------------------------------------------------------------- */
|
530
|
+
html5jp.graph.line.prototype._csscolor2rgb = function (c) {
|
531
|
+
if( ! c ) { return null; }
|
532
|
+
var color_map = {
|
533
|
+
black: "#000000",
|
534
|
+
gray: "#808080",
|
535
|
+
silver: "#c0c0c0",
|
536
|
+
white: "#ffffff",
|
537
|
+
maroon: "#800000",
|
538
|
+
red: "#ff0000",
|
539
|
+
purple: "#800080",
|
540
|
+
fuchsia: "#ff00ff",
|
541
|
+
green: "#008000",
|
542
|
+
lime: "#00FF00",
|
543
|
+
olive: "#808000",
|
544
|
+
yellow: "#FFFF00",
|
545
|
+
navy: "#000080",
|
546
|
+
blue: "#0000FF",
|
547
|
+
teal: "#008080",
|
548
|
+
aqua: "#00FFFF"
|
549
|
+
};
|
550
|
+
c = c.toLowerCase();
|
551
|
+
var o = new Object();
|
552
|
+
if( c.match(/^[a-zA-Z]+$/) && color_map[c] ) {
|
553
|
+
c = color_map[c];
|
554
|
+
}
|
555
|
+
var m = null;
|
556
|
+
if( m = c.match(/^\#([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i) ) {
|
557
|
+
o.r = parseInt(m[1], 16);
|
558
|
+
o.g = parseInt(m[2], 16);
|
559
|
+
o.b = parseInt(m[3], 16);
|
560
|
+
} else if( m = c.match(/^\#([a-f\d]{1})([a-f\d]{1})([a-f\d]{1})$/i) ) {
|
561
|
+
o.r = parseInt(m[1]+"0", 16);
|
562
|
+
o.g = parseInt(m[2]+"0", 16);
|
563
|
+
o.b = parseInt(m[3]+"0", 16);
|
564
|
+
} else if( m = c.match(/^rgba*\(\s*(\d+),\s*(\d+),\s*(\d+)/i) ) {
|
565
|
+
o.r = m[1];
|
566
|
+
o.g = m[2];
|
567
|
+
o.b = m[3];
|
568
|
+
} else if( m = c.match(/^rgba*\(\s*(\d+)\%,\s*(\d+)\%,\s*(\d+)\%/i) ) {
|
569
|
+
o.r = Math.round(m[1] * 255 / 100);
|
570
|
+
o.g = Math.round(m[2] * 255 / 100);
|
571
|
+
o.b = Math.round(m[3] * 255 / 100);
|
572
|
+
} else {
|
573
|
+
return null;
|
574
|
+
}
|
575
|
+
return o;
|
576
|
+
};
|
577
|
+
|