5 <title>Line Chart
</title>
6 <script src=
"../dist/Chart.bundle.js"></script>
7 <script src=
"http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
10 -moz-user-select: none;
11 -webkit-user-select: none;
12 -ms-user-select: none;
18 <div style=
"width:75%;">
19 <canvas id=
"canvas"></canvas>
23 <button id=
"randomizeData">Randomize Data
</button>
24 <button id=
"addDataset">Add Dataset
</button>
25 <button id=
"removeDataset">Remove Dataset
</button>
26 <button id=
"addData">Add Data
</button>
27 <button id=
"removeData">Remove Data
</button>
29 var MONTHS
= ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
31 var randomScalingFactor = function() {
32 return Math
.round(Math
.random() * 100 * (Math
.random() > 0.5 ? -1 : 1));
34 var randomColorFactor = function() {
35 return Math
.round(Math
.random() * 255);
37 var randomColor = function(opacity
) {
38 return 'rgba(' + randomColorFactor() + ',' + randomColorFactor() + ',' + randomColorFactor() + ',' + (opacity
|| '1') + ')';
44 labels
: ["January", "February", "March", "April", "May", "June", "July"],
46 label
: "My First dataset",
47 data
: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()],
49 label
: "My Second dataset",
50 data
: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()],
52 label
: "My Third dataset",
53 data
: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()],
55 label
: "My Third dataset",
56 data
: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()],
63 text
:"Chart.js Line Chart - Stacked Area"
89 $.each(config
.data
.datasets
, function(i
, dataset
) {
90 var color
= randomColor(1);
91 dataset
.borderColor
= color
;
92 dataset
.backgroundColor
= color
;
93 dataset
.pointBorderColor
= color
;
94 dataset
.pointBackgroundColor
= color
;
95 dataset
.pointBorderWidth
= 1;
98 window
.onload = function() {
99 var ctx
= document
.getElementById("canvas").getContext("2d");
100 window
.myLine
= new Chart(ctx
, config
);
103 $('#randomizeData').click(function() {
104 $.each(config
.data
.datasets
, function(i
, dataset
) {
105 dataset
.data
= dataset
.data
.map(function() {
106 return randomScalingFactor();
111 window
.myLine
.update();
114 $('#addDataset').click(function() {
116 label
: 'Dataset ' + config
.data
.datasets
.length
,
117 borderColor
: randomColor(0.4),
118 backgroundColor
: randomColor(0.5),
119 pointBorderColor
: randomColor(0.7),
120 pointBackgroundColor
: randomColor(0.5),
125 for (var index
= 0; index
< config
.data
.labels
.length
; ++index
) {
126 newDataset
.data
.push(randomScalingFactor());
129 config
.data
.datasets
.push(newDataset
);
130 window
.myLine
.update();
133 $('#addData').click(function() {
134 if (config
.data
.datasets
.length
> 0) {
135 var month
= MONTHS
[config
.data
.labels
.length
% MONTHS
.length
];
136 config
.data
.labels
.push(month
);
138 $.each(config
.data
.datasets
, function(i
, dataset
) {
139 dataset
.data
.push(randomScalingFactor());
142 window
.myLine
.update();
146 $('#removeDataset').click(function() {
147 config
.data
.datasets
.splice(0, 1);
148 window
.myLine
.update();
151 $('#removeData').click(function() {
152 config
.data
.labels
.splice(-1, 1); // remove the label first
154 config
.data
.datasets
.forEach(function(dataset
, datasetIndex
) {
158 window
.myLine
.update();