-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.html
627 lines (544 loc) · 18.3 KB
/
index.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Scatter chart</title>
<!-- Bootstrap -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.min.css" />
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<style>
body {
padding: 42px 0;
}
.jumbotron {
background-color: #4DB7EC;
color: white !important;
}
.jumbotron h1 {
margin-top: 0;
}
.jumbotron a {
color: #e6f5fc;
}
.btn-jumbotron {
background-color: transparent;
border-color: white;
color: white !important;
margin-bottom: 4px;
}
.btn-jumbotron:hover {
border-color: transparent;
background-color: white;
color: #4DB7EC !important;
}
</style>
</head>
<body>
<div class="container">
<div class="jumbotron">
<div class="row">
<div class="col-md-6">
<h1>Chart.Scatter
</h1>
<p class="lead">
Scatter chart is a graph in which the values of two variables are plotted along two axes. Chart.Scatter - is an addon for the
<a href="http://www.chartjs.org">Chart.js</a> library.
</p>
<div>
<a class="btn btn-lg btn-jumbotron" href="https://github.com/dima117/Chart.Scatter/archive/master.zip">Download</a>
<a class="btn btn-lg btn-jumbotron" href="https://github.com/dima117/Chart.Scatter">Source on GitHub</a>
</div>
</div>
<div class="col-md-6 hidden-sm hidden-xs">
<canvas id="header-canvas" width="420" height="180"></canvas>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<h2>Creating a chart</h2>
<p>
First we need to include the Chart.js (or Chart.Core.js) and Chart.Scatter.js
libraries on the page.
</p>
<pre>
<script src="Chart.js"></script>
<script src="Chart.Scatter.js"></script>
</pre>
<p>
Alternatively you can use an AMD loader.
</p>
<pre>
// Using requirejs
require(['Chart', 'path/to/Chart.Scatter'], function(Chart){
...
});
</pre>
<p>
Please note, that Chart.Scatter addon requires to Chart.js module named "Chart".
If Chart.js module has another name in your application (not "Chart"),
you can adjust module name mapping.
</p>
<pre>
// Using requirejs
requirejs.config({
...
map: {
'*': { 'Chart': 'myAppChartjsName' }
}
});
</pre>
<p>
You can also grab Chart.Scatter.js using bower:
<pre>
bower install Chart.Scatter.js --save
</pre>
</p>
<p>
... or npm:
<pre>
npm i chart.js-scatter
</pre>
</p>
<p>
To create a chart, we need to instantiate the Chart class. To do this, we
need to pass in the 2d context of where we want to draw the chart. Here's an
example.
</p>
<pre>
<canvas id="myChart" width="400" height="400"></canvas>
// Get the context of the canvas element we want to select
var ctx = document.getElementById("myChart").getContext("2d");
</pre>
<p>
With the Chart class set up, we can go on to create Scatter chart.
</p>
<pre>
new Chart(ctx).Scatter(data, options);
</pre>
<p>
We call a method named Scatter. We pass in the data for that chart type,
and the options for that chart as parameters. Chart.js will merge the
global defaults with chart type specific defaults, then merge any options
passed in as a second argument after data.
</p>
</div>
<div class="col-md-6">
<h3>Number scale</h3>
<div>
<canvas id="myChart" width="430" height="220"></canvas>
</div>
<h3>Date scale</h3>
<div>
<canvas id="myChartWithDates" width="430" height="220"></canvas>
</div>
<h3>Custom point radius</h3>
<div>
<canvas id="myBubbleChart" width="430" height="220"></canvas>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<h2>Chart options</h2>
<p>
These are the customisation options specific to Scatter charts.
These options are merged with the global chart configuration options,
and form the options of the chart.
</p>
<pre>
<strong>// SUPPORTED GLOBAL OPTIONS</strong>
// Boolean - If we should show the scale at all
showScale: <strong>true</strong>,
// String - Colour of the scale line
scaleLineColor: <strong>"rgba(0,0,0,.1)"</strong>,
// Number - Pixel width of the scale line
scaleLineWidth: <strong>1</strong>,
// Boolean - Whether to show labels on the scale
scaleShowLabels: <strong>true</strong>,
// Interpolated JS string - can access value
scaleLabel: <strong>"<%=value%>"</strong>,
// Interpolated JS string - can access value
scaleArgLabel: <strong>"<%=value%>"</strong>,
// String - Message for empty data
emptyDataMessage: <strong>"chart has no data"</strong>,
<strong>// GRID LINES</strong>
// Boolean - Whether grid lines are shown across the chart
scaleShowGridLines: <strong>true</strong>,
// Number - Width of the grid lines
scaleGridLineWidth: <strong>1</strong>,
// String - Colour of the grid lines
scaleGridLineColor: <strong>"rgba(0,0,0,.05)"</strong>,
// Boolean - Whether to show horizontal lines (except X axis)
scaleShowHorizontalLines: <strong>true</strong>,
// Boolean - Whether to show vertical lines (except Y axis)
scaleShowVerticalLines: <strong>true</strong>,
<strong>// HORIZONTAL SCALE RANGE</strong>
// Boolean - If we want to override with a hard coded x scale
xScaleOverride: <strong>false</strong>,
// ** Required if scaleOverride is true **
// Number - The number of steps in a hard coded x scale
xScaleSteps: <strong>null</strong>,
// Number - The value jump in the hard coded x scale
xScaleStepWidth: <strong>null</strong>,
// Number - The x scale starting value
xScaleStartValue: <strong>null</strong>,
<strong>// VERTICAL SCALE RANGE</strong>
// Boolean - If we want to override with a hard coded y scale
scaleOverride: <strong>false</strong>,
// ** Required if scaleOverride is true **
// Number - The number of steps in a hard coded y scale
scaleSteps: <strong>null</strong>,
// Number - The value jump in the hard coded y scale
scaleStepWidth: <strong>null</strong>,
// Number - The y scale starting value
scaleStartValue: <strong>null</strong>,
<strong>// DATE SCALE</strong>
// String - scale type: "number" or "date"
scaleType: <strong>"number"</strong>,
// Boolean - Whether to use UTC dates instead local
useUtc: <strong>true</strong>,
// String - short date format (used for scale labels)
scaleDateFormat: <strong>"mmm d"</strong>,
// String - short time format (used for scale labels)
scaleTimeFormat: <strong>"h:MM"</strong>,
// String - full date format (used for point labels)
scaleDateTimeFormat: <strong>"mmm d, yyyy, hh:MM"</strong>,
<strong>// LINES</strong>
// Boolean - Whether to show a stroke for datasets
datasetStroke: <strong>true</strong>,
// Number - Pixel width of dataset stroke
datasetStrokeWidth: <strong>2</strong>,
// String - Color of dataset stroke
datasetStrokeColor: <strong>'#007ACC'</strong>,
// String - Color of dataset stroke
datasetPointStrokeColor: <strong>'white'</strong>,
// Boolean - Whether the line is curved between points
bezierCurve: <strong>true</strong>,
// Number - Tension of the bezier curve between points
bezierCurveTension: <strong>0.4</strong>,
<strong>// POINTS</strong>
// Boolean - Whether to show a dot for each point
pointDot: <strong>true</strong>,
// Number - Pixel width of point dot stroke
pointDotStrokeWidth: <strong>1</strong>,
// Number - Radius of each point dot in pixels
pointDotRadius: <strong>4</strong>,
// Number - amount extra to add to the radius to cater for hit detection outside the drawn point
pointHitDetectionRadius: <strong>4</strong>,
<strong>// TEMPLATES</strong>
// Interpolated JS string - can access point fields:
// argLabel, valueLabel, arg, value, datasetLabel, size
tooltipTemplate: <strong>"<%if (datasetLabel){%><%=datasetLabel%>: <%}%><%=argLabel%>; <%=valueLabel%>"</strong>
// Interpolated JS string - can access point fields:
// argLabel, valueLabel, arg, value, datasetLabel, size
multiTooltipTemplate: <strong>"<%=argLabel%>; <%=valueLabel%>",</strong>
// Interpolated JS string - can access all chart fields
legendTemplate: <strong>"<ul class=\"<%=name.toLowerCase()%>-legend\"><%for(var i=0;i<datasets.length;i++){%><li><span class=\"<%=name.toLowerCase()%>-legend-marker\" style=\"background-color:<%=datasets[i].strokeColor%>\"></span><%=datasets[i].label%></li><%}%></ul>"</strong>
</pre>
</div>
<div class="col-md-6">
<h2>Data structure</h2>
<p>
Any point can have <code>x</code>, <code>y</code>, <code>r</code> fields.
The fields named "x" and "y" are the coordinates of the point. Optional field "r" is a multiplier
for the point radius (<code>pointDotRadius</code>). The default multiplier is 1.
</p>
<pre>
var data = [
{
label: 'My First dataset',
strokeColor: '#F16220',
pointColor: '#F16220',
pointStrokeColor: '#fff',
data: [
{ x: 19, y: 65 },
{ x: 27, y: 59 },
{ x: 28, y: 69 },
{ x: 40, y: 81 },
{ x: 48, y: 56 }
]
},
{
label: 'My Second dataset',
strokeColor: '#007ACC',
pointColor: '#007ACC',
pointStrokeColor: '#fff',
data: [
{ x: 19, y: 75, r: 4 },
{ x: 27, y: 69, r: 7 },
{ x: 28, y: 70, r: 5 },
{ x: 40, y: 31, r: 3 },
{ x: 48, y: 76, r: 6 },
{ x: 52, y: 23, r: 3 },
{ x: 24, y: 32, r: 4 }
]
}
];
</pre>
<h2>Dataset API</h2>
<p>
You can change the chart data, using <code>datasets</code> collection of the chart.
Use the methods of it elements: <code>addPoint</code>, <code>setPointData</code>, <code>removePoint</code>.
</p>
<pre>
<strong>// Add a new point { x: 5, y: 7 }</strong>
myChart.datasets[0].addPoint(5, 7);
<strong>// Add a new point { x: 5, y: 7, r: 4 } (with radius)</strong>
myChart.datasets[0].addPoint(5, 7, 4);
<strong>// Set point data { x: 11, y: 4 } at index 3</strong>
myChart.datasets[0].setPointData(3, 11, 4);
<strong>// Set point data { x: 11, y: 4, r: 2 } at index 3 (with radius)</strong>
myChart.datasets[0].setPointData(3, 11, 4, 2);
<strong>// Remove the point at index 0</strong>
myChart.datasets[0].removePoint(0);
</pre>
<p>
After all changes call the <code>update</code> method of the chart for render your chart with new data.
</p>
<pre>
myChart.update();
</pre>
<h2>Date Format mask</h2>
<p>
Chart.Scatter uses the
<a href="http://blog.stevenlevithan.com/archives/date-time-format" target="_blank">date format library
</a>
by Steven Levithan.
</p>
<table class="table">
<thead>
<tr>
<th>Mask</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>d</code></td>
<td>Day of the month as digits; no leading zero for single-digit days.</td>
</tr>
<tr>
<td><code>dd</code></td>
<td>Day of the month as digits; leading zero for single-digit days.</td>
</tr>
<tr>
<td><code>ddd</code></td>
<td>Day of the week as a three-letter abbreviation.</td>
</tr>
<tr>
<td><code>dddd</code></td>
<td>Day of the week as its full name.</td>
</tr>
<tr>
<td><code>m</code></td>
<td>Month as digits; no leading zero for single-digit months.</td>
</tr>
<tr>
<td><code>mm</code></td>
<td>Month as digits; leading zero for single-digit months.</td>
</tr>
<tr>
<td><code>mmm</code></td>
<td>Month as a three-letter abbreviation.</td>
</tr>
<tr>
<td><code>mmmm</code></td>
<td>Month as its full name.</td>
</tr>
<tr>
<td><code>yy</code></td>
<td>Year as last two digits; leading zero for years less than 10.</td>
</tr>
<tr>
<td><code>yyyy</code></td>
<td>Year represented by four digits.</td>
</tr>
<tr>
<td><code>h</code></td>
<td>Hours; no leading zero for single-digit hours (12-hour clock).</td>
</tr>
<tr>
<td><code>hh</code></td>
<td>Hours; leading zero for single-digit hours (12-hour clock).</td>
</tr>
<tr>
<td><code>H</code></td>
<td>Hours; no leading zero for single-digit hours (24-hour clock).</td>
</tr>
<tr>
<td><code>HH</code></td>
<td>Hours; leading zero for single-digit hours (24-hour clock).</td>
</tr>
<tr>
<td><code>M</code></td>
<td>Minutes; no leading zero for single-digit minutes.<br>
<span>Uppercase M unlike CF <code>timeFormat</code>'s m to avoid conflict with months.</span></td>
</tr>
<tr>
<td><code>MM</code></td>
<td>Minutes; leading zero for single-digit minutes.<br>
<span>Uppercase MM unlike CF <code>timeFormat</code>'s mm to avoid conflict with months.</span></td>
</tr>
<tr>
<td><code>s</code></td>
<td>Seconds; no leading zero for single-digit seconds.</td>
</tr>
<tr>
<td><code>ss</code></td>
<td>Seconds; leading zero for single-digit seconds.</td>
</tr>
<tr>
<td><code>l</code> <em>or</em> <code>L</code></td>
<td>Milliseconds. <code>l</code> gives 3 digits. <code>L</code> gives 2 digits.</td>
</tr>
<tr>
<td><code>'…'</code> <em>or</em> <code>"…"</code></td>
<td>Literal character sequence. Surrounding quotes are removed.<br>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script src="Chart.Core.min.js"></script>
<script src="Chart.Scatter.min.js"></script>
<script>
Chart.defaults.global.responsive = true;
Chart.defaults.global.animation = false;
$(function () {
//#region header
var data = [{ x: 0, y: 0 }];
for (var x = 1; x < 10; x++) {
var sq = Math.sqrt(x);
data.push({ x: x, y: sq });
data.unshift({ x: x, y: -sq });
}
var dataset = {
label: 'y = sqrt(x)',
strokeColor: 'white',
pointStrokeColor: '#4db7ec',
data: data
}
var ctx = document.getElementById("header-canvas").getContext("2d");
new Chart(ctx).Scatter([dataset], {
responsive: true,
scaleGridLineColor: "rgba(255,255,255,.3)",
scaleLineColor: "rgba(255,255,255,.8)"
});
//#endregion
var data2 = [
{
label: 'complex-1',
strokeColor: '#F16220',
data: [
{ x: 19, y: 65 }, { x: 27, y: 59 }, { x: 28, y: 69 }, { x: 40, y: 81 },
{ x: 48, y: 56 }, { x: 86, y: 55 }, { x: 90, y: 40 }, { x: 95, y: 60 }
]
},
{
label: 'complex-2',
strokeColor: '#007ACC',
data: [
{ x: 19, y: 75 }, { x: 27, y: 69 }, { x: 28, y: 70 }, { x: 40, y: 31 },
{ x: 48, y: 76 }, { x: 52, y: 23 }, { x: 24, y: 32 },
{ x: 86, y: 45 }, { x: 90, y: 50 }
]
}
];
var ctx2 = document.getElementById("myChart").getContext("2d");
var myLineChart = new Chart(ctx2).Scatter(data2, {
bezierCurve: true,
showTooltips: true,
scaleShowHorizontalLines: true,
scaleShowLabels: true,
scaleLabel: "<%=value%>°C",
scaleArgLabel: "<%=value%>mmm",
scaleBeginAtZero: true
});
var data3 = [
{
label: 'temperature',
strokeColor: '#A31515',
data: [
{
x: new Date('2011-04-11T11:45:00'),
y: 25
},
{
x: new Date('2011-04-11T12:51:00'),
y: 28
},
{
x: new Date('2011-04-11T14:10:00'),
y: 22
},
{
x: new Date('2011-04-11T15:15:00'),
y: 18
},
{
x: new Date('2011-04-11T17:00:00'),
y: 25
},
{
x: new Date('2011-04-11T21:00:00'),
y: 24
},
{
x: new Date('2011-04-12T13:00:00'),
y: 24
}
]
}];
var ctx3 = document.getElementById("myChartWithDates").getContext("2d");
var myDateLineChart = new Chart(ctx3).Scatter(data3, {
bezierCurve: true,
showTooltips: true,
scaleShowHorizontalLines: true,
scaleShowLabels: true,
scaleType: "date",
scaleLabel: "<%=value%>°C"
});
var data4 = [
{
label: 'Bubble chart example',
strokeColor: 'rgba(77, 180, 73, 0.3)',
data: [
{ x: 1, y: 10, r: 7 }, { x: 2, y: 12, r: 5 }, { x: 3, y: 14, r: 10 }, { x: 4, y: 18, r: 6 },
{ x: 5, y: 26, r: 9 }, { x: 6, y: 42, r: 4 }, { x: 7, y: 60, r: 8 }
]
}
];
var ctx4 = document.getElementById("myBubbleChart").getContext("2d");
var myBubbleChart = new Chart(ctx4).Scatter(data4, {
bezierCurve: true,
showTooltips: true,
scaleShowHorizontalLines: true,
scaleShowLabels: true,
scaleBeginAtZero: true,
datasetStroke: false
});
});
</script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-12512709-9', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>