1
1
const svgPathContainer = document . getElementById ( "chartContainer" )
2
2
const formElement = document . getElementById ( "form" )
3
3
const input = document . getElementById ( "real" )
4
+ const downloadAsSvg = document . querySelector ( ".btn-download-chart-as-svg" )
5
+ // const downloadAsPng = document.querySelector(".btn-download-chart-as-png")
4
6
5
7
const SVG_NAMESPACE_URI = "http://www.w3.org/2000/svg"
6
8
7
9
// default values
8
- let values = [ 50 , 30 , 40 , 30 , 20 , 50 , 75 , 100 ]
10
+ let values = [ 50 , 30 , 40 , 30 , 20 , 100 ]
9
11
10
12
function formatInput ( input ) {
11
13
let formattedValues = [ ]
12
14
input = input . replace ( / , / g, " " )
13
15
let values = input . split ( " " )
14
16
values . forEach ( ( value ) => {
15
17
if ( value !== "" ) {
16
- formattedValues . push ( value )
18
+ formattedValues . push ( parseInt ( value ) )
17
19
}
18
20
} )
19
21
return formattedValues
@@ -37,8 +39,8 @@ function generateLineChart(values, spacing, lineCount) {
37
39
38
40
const Y_AXIS = values . length
39
41
const highest = findMax ( values )
40
- const WIDTH = Y_AXIS * spacing
41
- const HEIGHT = highest + 50
42
+ const WIDTH = Y_AXIS * spacing + 50 < 200 ? 200 : Y_AXIS * spacing + 50
43
+ const HEIGHT = highest + 50 < 200 ? 200 : highest + 50
42
44
const graphLine = highest / ( lineCount - 1 )
43
45
44
46
newSVGElement . setAttribute ( "width" , WIDTH )
@@ -53,14 +55,14 @@ function generateLineChart(values, spacing, lineCount) {
53
55
gElText . classList . add ( "graph-text" )
54
56
55
57
// X-Axis and Y-Axis for the chart
56
- X_LINE . setAttribute ( "x1" , "0 " )
57
- X_LINE . setAttribute ( "y1" , "0 " )
58
- X_LINE . setAttribute ( "x2" , "0 " )
58
+ X_LINE . setAttribute ( "x1" , "10 " )
59
+ X_LINE . setAttribute ( "y1" , "10 " )
60
+ X_LINE . setAttribute ( "x2" , "10 " )
59
61
X_LINE . setAttribute ( "y2" , `${ HEIGHT } ` )
60
62
61
63
Y_LINE . setAttribute ( "x1" , `${ WIDTH } ` )
62
64
Y_LINE . setAttribute ( "y1" , `${ HEIGHT } ` )
63
- Y_LINE . setAttribute ( "x2" , "0 " )
65
+ Y_LINE . setAttribute ( "x2" , "10 " )
64
66
Y_LINE . setAttribute ( "y2" , `${ HEIGHT } ` )
65
67
66
68
X_LINE . setAttribute ( "stroke" , "black" )
@@ -71,7 +73,7 @@ function generateLineChart(values, spacing, lineCount) {
71
73
newSVGElement . appendChild ( X_LINE )
72
74
newSVGElement . appendChild ( Y_LINE )
73
75
74
- let d = `M0 ${ HEIGHT - values [ 0 ] } `
76
+ let d = `M10 ${ HEIGHT - values [ 0 ] } `
75
77
76
78
values . forEach ( ( value , idx , values ) => {
77
79
if ( idx > 0 ) {
@@ -87,7 +89,7 @@ function generateLineChart(values, spacing, lineCount) {
87
89
let textEl = document . createElementNS ( SVG_NAMESPACE_URI , "text" )
88
90
let yPosition = HEIGHT - l * graphLine
89
91
90
- lineEl . setAttribute ( "x1" , "0 " )
92
+ lineEl . setAttribute ( "x1" , "10 " )
91
93
lineEl . setAttribute ( "y1" , yPosition )
92
94
lineEl . setAttribute ( "x2" , WIDTH )
93
95
lineEl . setAttribute ( "y2" , yPosition )
@@ -116,12 +118,69 @@ function generateLineChart(values, spacing, lineCount) {
116
118
svgPathContainer . appendChild ( newSVGElement )
117
119
}
118
120
121
+ function downloadSVGAsText ( ) {
122
+ const svg = document . querySelector ( "svg" )
123
+ svg . setAttribute ( "xmlns" , "http://www.w3.org/2000/svg" )
124
+ const base64doc = btoa ( decodeURIComponent ( encodeURIComponent ( svg . outerHTML ) ) )
125
+ const a = document . createElement ( "a" )
126
+ const e = new MouseEvent ( "click" )
127
+ a . download = "line_chart.svg"
128
+ a . href = "data:image/svg+xml;base64," + base64doc
129
+ a . dispatchEvent ( e )
130
+ }
131
+
132
+ function downloadSVGAsPNG ( e ) {
133
+ const canvas = document . createElement ( "canvas" )
134
+ const svg = document . querySelector ( "svg" )
135
+ const base64doc = btoa ( decodeURIComponent ( encodeURIComponent ( svg . outerHTML ) ) )
136
+ const w = parseInt ( svg . getAttribute ( "width" ) )
137
+ const h = parseInt ( svg . getAttribute ( "height" ) )
138
+ const img_to_download = document . createElement ( "img" )
139
+ img_to_download . src = "data:image/svg+xml;base64," + base64doc
140
+ console . log ( w , h )
141
+ img_to_download . onload = function ( ) {
142
+ canvas . setAttribute ( "width" , w )
143
+ canvas . setAttribute ( "height" , h )
144
+ const context = canvas . getContext ( "2d" )
145
+ //context.clearRect(0, 0, w, h);
146
+ context . drawImage ( img_to_download , 0 , 0 , w , h )
147
+ const dataURL = canvas . toDataURL ( "image/png" )
148
+ if ( window . navigator . msSaveBlob ) {
149
+ window . navigator . msSaveBlob ( canvas . msToBlob ( ) , "line_chart.png" )
150
+ e . preventDefault ( )
151
+ } else {
152
+ const a = document . createElement ( "a" )
153
+ const my_evt = new MouseEvent ( "click" )
154
+ a . download = "line_chart.png"
155
+ a . href = dataURL
156
+ a . dispatchEvent ( my_evt )
157
+ }
158
+ //canvas.parentNode.removeChild(canvas);
159
+ }
160
+ }
161
+
119
162
formElement . addEventListener ( "submit" , ( e ) => {
120
163
e . preventDefault ( )
121
164
if ( input . value == "" ) {
122
165
return
123
166
}
124
167
values = formatInput ( input . value . trim ( ) )
168
+ if ( values . length < 2 ) {
169
+ return
170
+ }
171
+ if ( svgPathContainer . childElementCount >= 1 ) {
172
+ svgPathContainer . removeChild ( svgPathContainer . firstChild )
173
+ generateLineChart ( values , 50 , 5 )
174
+ }
125
175
} )
126
176
177
+ // Download Chart
178
+ downloadAsSvg . addEventListener ( "click" , ( e ) => {
179
+ downloadSVGAsText ( )
180
+ } )
181
+
182
+ // downloadAsPng.addEventListener("click", (e) => {
183
+ // downloadSVGAsPNG()
184
+ // })
185
+
127
186
generateLineChart ( values , 50 , 5 )
0 commit comments