-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
186 lines (159 loc) · 5.69 KB
/
script.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const svgPathContainer = document.getElementById("chartContainer")
const formElement = document.getElementById("form")
const input = document.getElementById("real")
const downloadAsSvg = document.querySelector(".btn-download-chart-as-svg")
// const downloadAsPng = document.querySelector(".btn-download-chart-as-png")
const SVG_NAMESPACE_URI = "http://www.w3.org/2000/svg"
// default values
let values = [50, 30, 40, 30, 20, 100]
function formatInput(input) {
let formattedValues = []
input = input.replace(/,/g, " ")
let values = input.split(" ")
values.forEach((value) => {
if (value !== "") {
formattedValues.push(parseInt(value))
}
})
return formattedValues
}
function findMax(values) {
let max = values[0]
values.forEach((value) => {
if (value > max) {
max = value
}
})
return max
}
function generateLineChart(values, spacing, lineCount) {
const newSVGElement = document.createElementNS(SVG_NAMESPACE_URI, "svg")
const newSVGPathElement = document.createElementNS(SVG_NAMESPACE_URI, "path")
const X_LINE = document.createElementNS(SVG_NAMESPACE_URI, "line")
const Y_LINE = document.createElementNS(SVG_NAMESPACE_URI, "line")
const Y_AXIS = values.length
const highest = findMax(values)
const WIDTH = Y_AXIS * spacing + 50 < 200 ? 200 : Y_AXIS * spacing + 50
const HEIGHT = highest + 50 < 200 ? 200 : highest + 50
const graphLine = highest / (lineCount - 1)
newSVGElement.setAttribute("width", WIDTH)
newSVGElement.setAttribute("height", HEIGHT)
// g tags for grouping other tags
const gElCircle = document.createElementNS(SVG_NAMESPACE_URI, "g")
gElCircle.classList.add("graph-point")
const gElLine = document.createElementNS(SVG_NAMESPACE_URI, "g")
gElLine.classList.add("graph-line")
const gElText = document.createElementNS(SVG_NAMESPACE_URI, "g")
gElText.classList.add("graph-text")
// X-Axis and Y-Axis for the chart
X_LINE.setAttribute("x1", "10")
X_LINE.setAttribute("y1", "10")
X_LINE.setAttribute("x2", "10")
X_LINE.setAttribute("y2", `${HEIGHT}`)
Y_LINE.setAttribute("x1", `${WIDTH}`)
Y_LINE.setAttribute("y1", `${HEIGHT}`)
Y_LINE.setAttribute("x2", "10")
Y_LINE.setAttribute("y2", `${HEIGHT}`)
X_LINE.setAttribute("stroke", "black")
X_LINE.setAttribute("stroke-width", "3")
Y_LINE.setAttribute("stroke", "black")
Y_LINE.setAttribute("stroke-width", "3")
newSVGElement.appendChild(X_LINE)
newSVGElement.appendChild(Y_LINE)
let d = `M10 ${HEIGHT - values[0]}`
values.forEach((value, idx, values) => {
if (idx > 0) {
let x = idx * spacing
let y = HEIGHT - values[idx]
let dd = ` L${x} ${y}`
d += dd
}
})
for (let l = 0; l < lineCount; l++) {
let lineEl = document.createElementNS(SVG_NAMESPACE_URI, "line")
let textEl = document.createElementNS(SVG_NAMESPACE_URI, "text")
let yPosition = HEIGHT - l * graphLine
lineEl.setAttribute("x1", "10")
lineEl.setAttribute("y1", yPosition)
lineEl.setAttribute("x2", WIDTH)
lineEl.setAttribute("y2", yPosition)
gElLine.appendChild(lineEl)
let txt = l * graphLine
textEl.setAttribute("dx", "-20")
textEl.setAttribute("dy", "-2")
textEl.setAttribute("x", `${WIDTH}`)
textEl.setAttribute("y", yPosition)
textEl.textContent = txt
gElText.appendChild(textEl)
}
newSVGElement.appendChild(gElCircle)
newSVGElement.appendChild(gElLine)
newSVGElement.appendChild(gElText)
newSVGPathElement.setAttribute("d", d)
newSVGPathElement.setAttribute("stroke", "red")
newSVGPathElement.setAttribute("stroke-width", "1")
newSVGPathElement.setAttribute("fill", "none")
newSVGElement.appendChild(newSVGPathElement)
svgPathContainer.appendChild(newSVGElement)
}
function downloadSVGAsText() {
const svg = document.querySelector("svg")
svg.setAttribute("xmlns", "http://www.w3.org/2000/svg")
const base64doc = btoa(decodeURIComponent(encodeURIComponent(svg.outerHTML)))
const a = document.createElement("a")
const e = new MouseEvent("click")
a.download = "line_chart.svg"
a.href = "data:image/svg+xml;base64," + base64doc
a.dispatchEvent(e)
}
function downloadSVGAsPNG(e) {
const canvas = document.createElement("canvas")
const svg = document.querySelector("svg")
const base64doc = btoa(decodeURIComponent(encodeURIComponent(svg.outerHTML)))
const w = parseInt(svg.getAttribute("width"))
const h = parseInt(svg.getAttribute("height"))
const img_to_download = document.createElement("img")
img_to_download.src = "data:image/svg+xml;base64," + base64doc
console.log(w, h)
img_to_download.onload = function () {
canvas.setAttribute("width", w)
canvas.setAttribute("height", h)
const context = canvas.getContext("2d")
//context.clearRect(0, 0, w, h);
context.drawImage(img_to_download, 0, 0, w, h)
const dataURL = canvas.toDataURL("image/png")
if (window.navigator.msSaveBlob) {
window.navigator.msSaveBlob(canvas.msToBlob(), "line_chart.png")
e.preventDefault()
} else {
const a = document.createElement("a")
const my_evt = new MouseEvent("click")
a.download = "line_chart.png"
a.href = dataURL
a.dispatchEvent(my_evt)
}
//canvas.parentNode.removeChild(canvas);
}
}
formElement.addEventListener("submit", (e) => {
e.preventDefault()
if (input.value == "") {
return
}
values = formatInput(input.value.trim())
if (values.length < 2) {
return
}
if (svgPathContainer.childElementCount >= 1) {
svgPathContainer.removeChild(svgPathContainer.firstChild)
generateLineChart(values, 50, 5)
}
})
// Download Chart
downloadAsSvg.addEventListener("click", (e) => {
downloadSVGAsText()
})
// downloadAsPng.addEventListener("click", (e) => {
// downloadSVGAsPNG()
// })
generateLineChart(values, 50, 5)