-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlineChartScript.js
172 lines (162 loc) · 5.17 KB
/
lineChartScript.js
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
var MakeLineChart = function(d,c,t){
var enumCount = Object.keys(d).length;
c.width = (60 * enumCount) + 65;
c.height = 320;
function drawLine(ctx, startX, startY, endX, endY,color){
ctx.save();
ctx.strokeStyle = color;
ctx.beginPath();
ctx.moveTo(startX,startY);
ctx.lineTo(endX,endY);
ctx.stroke();
ctx.restore();
}
class LineChart {
constructor(opt) {
var ChartX = 15; // To adjust the Entire Chart X Axis
var ChartY = 10; // To adjust the Entire Chart Y Axis
var gridScale = 5; // Default is Divide the max value by 5
this.opt = opt;
this.canvas = c;
this.ctx = c.getContext("2d");
this.ctx.translate(0.5, 0.5);
//Chart BackgroundColor START
this.ctx.fillStyle = "rgba(150, 150, 150, 0.1)";
this.ctx.fillRect(0, 0, c.width, c.height);
//Chart BackgroundColor END
this.draw = function () {
var TopValue = 0;
for (var ChartEnum in d) {
TopValue = Math.max(TopValue, d[ChartEnum]);
}
if (TopValue >= 7) // This adjust the gridScale that it won't create excessive Y values
{
gridScale = TopValue / 7; //(Max is 8 including the base line zero 0)
}
TopValue = TopValue + (TopValue % gridScale);
var ChartHeight = this.canvas.height - this.opt.padding * 2 - ChartY;
var ChartWidth = this.canvas.width - this.opt.padding * 2 + ChartX;
//drawing the grid lines
var gridValue = 0;
var lineColor = this.opt.gridColor;
var alt = true;
this.ctx.save();
while (gridValue <= TopValue) {
var gridY = Math.floor(ChartHeight * (1 - gridValue / TopValue) + this.opt.padding);
if (gridValue == 0)
lineColor = "#000";
else if (alt)
lineColor = "#AAA";
else
lineColor = "#DDD";
alt = !alt;
drawLine(this.ctx, 20 + ChartX, gridY, this.canvas.width - 30 + ChartX, gridY, lineColor);
//writing grid markers (numbers on left side)
this.ctx.fillStyle = "#787878";
this.ctx.font = "bold 10px Arial";
var gridLabel = nShort(Math.floor(gridValue));
this.ctx.textAlign = "right";
this.ctx.fillText(gridLabel, ChartX + 17, gridY + 2);
gridValue += gridScale;
}
this.ctx.restore();
//drawing the line
var lineIndex = 0;
var numberOfLines = Object.keys(d).length;
var lineWidth = (ChartWidth) / numberOfLines;
var curX = 0;
var curY = 0;
for (ChartEnum in d) {
var val = d[ChartEnum];
var lineHeight = Math.round(ChartHeight * val / TopValue);
var x = this.opt.padding + lineIndex * lineWidth;
var y = this.canvas.height - lineHeight - this.opt.padding - ChartY;
if(curY == 0) curY = y;
if(curX ==0) curX = x + ChartX;
this.ctx.save();
this.ctx.lineWidth = 2;
drawLine(this.ctx, curX, curY, x + ChartX, y, 'rgba(0, 50, 255, 0.4)')
curX = x + ChartX;
curY = y;
//drawing the Point Circle
this.ctx.save();
//this.ctx.strokeStyle = 'rgba(0, 50, 255, 0.8)';
this.ctx.strokeStyle = '#DC4C3F';
this.ctx.lineWidth = 3;
this.ctx.beginPath();
this.ctx.arc(curX, curY, 2, 2, 3 * Math.PI);
this.ctx.stroke();
x = x + 20 + ChartX;
// Label on each line
this.ctx.restore();
this.ctx.fillStyle = "#000000";
this.ctx.font = "normal 10px Arial";
this.ctx.textAlign = "center";
if (ChartEnum.indexOf(" ") > 1) {
var LabelArray = ChartEnum.split(' ');
var label_y = this.canvas.height - 20 - ChartY;
for (var i = 0; i < LabelArray.length; i++) {
this.ctx.fillText(LabelArray[i], x - 21, label_y);
label_y = label_y + 10;
}
}
else {
this.ctx.fillText(ChartEnum, x - 21, this.canvas.height - 20 - ChartY);
}
// Dotted Line
this.ctx.save();
this.ctx.lineWidth = 1;
this.ctx.setLineDash([1, 2]);/*Dash width and spaces between dashes.*/
drawLine(this.ctx, curX, curY, curX, this.canvas.height - this.opt.padding - ChartY, 'rgba(5, 5, 5, 0.3)')
this.ctx.restore();
// Number Value on Each Line
x = x - 10;
this.ctx.save();
this.ctx.font = "bold 11px Arial";
this.ctx.textAlign = "center";
this.ctx.fillStyle = "black";
this.ctx.shadowOffsetX = 0;
this.ctx.shadowOffsetY = 0;
this.ctx.shadowColor = "#AAA";
this.ctx.shadowBlur = 3;
this.ctx.fillText(val.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","), curX + 0, curY - 8);
this.ctx.restore();
lineIndex++;
}
//drawing chart Title
this.ctx.save();
this.ctx.textBaseline = "bottom";
this.ctx.textAlign = "center";
this.ctx.fillStyle = "#000000";
this.ctx.font = "bold 12px Arial";
this.ctx.fillText(t, this.canvas.width / 2, 25);
this.ctx.restore();
};
}
}
function nShort(num) {
var si = [
{ v: 1, s: "" },
{ v: 1E3, s: "K" },
{ v: 1E6, s: "M" },
{ v: 1E9, s: "B" },
{ v: 1E12, s: "T" },
{ v: 1E15, s: "Q" },
{ v: 1E18, s: "P" }
];
var rx = /\.0+$|(\.[0-9]*[1-9])0+$/;
var i;
for (i = si.length - 1; i > 0; i--) {
if (num >= si[i].v) {
break;
}
}
if(si[i].s != "")
return (num / si[i].v).toFixed(1).replace(rx, "$1") + si[i].s;
else
return (num / si[i].v).toFixed(0).replace(rx, "$1") + si[i].s;
}
new LineChart({
padding:35
}).draw();
}