Skip to content

Commit 84c19bc

Browse files
committed
fix: multiple obj pushing into container
1 parent bd0a5d8 commit 84c19bc

File tree

4 files changed

+116
-17
lines changed

4 files changed

+116
-17
lines changed

chart.svg

-5
This file was deleted.

index.html

+9-2
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,18 @@ <h1>Line Chart Generator</h1>
1616
<label for="real">
1717
Input the values (each separated by a comma)</label
1818
>
19-
<input id="real" type="text" name="real" placeholder="50, 30, 40" />
20-
<button type="submit" class="btn-submit">Generate</button>
19+
<input
20+
id="real"
21+
type="text"
22+
name="real"
23+
placeholder="50, 30, 40, 30, 20, 100"
24+
/>
25+
<button type="submit" class="btn btn-submit">Generate</button>
2126
</form>
2227
</div>
2328
<div id="chartContainer"></div>
29+
<button class="btn btn-download-chart-as-svg">Download as SVG</button>
30+
<!-- <button class="btn btn-download-chart-as-png">Download as PNG</button> -->
2431
</main>
2532
</body>
2633
</html>

script.js

+69-10
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
const svgPathContainer = document.getElementById("chartContainer")
22
const formElement = document.getElementById("form")
33
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")
46

57
const SVG_NAMESPACE_URI = "http://www.w3.org/2000/svg"
68

79
// default values
8-
let values = [50, 30, 40, 30, 20, 50, 75, 100]
10+
let values = [50, 30, 40, 30, 20, 100]
911

1012
function formatInput(input) {
1113
let formattedValues = []
1214
input = input.replace(/,/g, " ")
1315
let values = input.split(" ")
1416
values.forEach((value) => {
1517
if (value !== "") {
16-
formattedValues.push(value)
18+
formattedValues.push(parseInt(value))
1719
}
1820
})
1921
return formattedValues
@@ -37,8 +39,8 @@ function generateLineChart(values, spacing, lineCount) {
3739

3840
const Y_AXIS = values.length
3941
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
4244
const graphLine = highest / (lineCount - 1)
4345

4446
newSVGElement.setAttribute("width", WIDTH)
@@ -53,14 +55,14 @@ function generateLineChart(values, spacing, lineCount) {
5355
gElText.classList.add("graph-text")
5456

5557
// 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")
5961
X_LINE.setAttribute("y2", `${HEIGHT}`)
6062

6163
Y_LINE.setAttribute("x1", `${WIDTH}`)
6264
Y_LINE.setAttribute("y1", `${HEIGHT}`)
63-
Y_LINE.setAttribute("x2", "0")
65+
Y_LINE.setAttribute("x2", "10")
6466
Y_LINE.setAttribute("y2", `${HEIGHT}`)
6567

6668
X_LINE.setAttribute("stroke", "black")
@@ -71,7 +73,7 @@ function generateLineChart(values, spacing, lineCount) {
7173
newSVGElement.appendChild(X_LINE)
7274
newSVGElement.appendChild(Y_LINE)
7375

74-
let d = `M0 ${HEIGHT - values[0]}`
76+
let d = `M10 ${HEIGHT - values[0]}`
7577

7678
values.forEach((value, idx, values) => {
7779
if (idx > 0) {
@@ -87,7 +89,7 @@ function generateLineChart(values, spacing, lineCount) {
8789
let textEl = document.createElementNS(SVG_NAMESPACE_URI, "text")
8890
let yPosition = HEIGHT - l * graphLine
8991

90-
lineEl.setAttribute("x1", "0")
92+
lineEl.setAttribute("x1", "10")
9193
lineEl.setAttribute("y1", yPosition)
9294
lineEl.setAttribute("x2", WIDTH)
9395
lineEl.setAttribute("y2", yPosition)
@@ -116,12 +118,69 @@ function generateLineChart(values, spacing, lineCount) {
116118
svgPathContainer.appendChild(newSVGElement)
117119
}
118120

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+
119162
formElement.addEventListener("submit", (e) => {
120163
e.preventDefault()
121164
if (input.value == "") {
122165
return
123166
}
124167
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+
}
125175
})
126176

177+
// Download Chart
178+
downloadAsSvg.addEventListener("click", (e) => {
179+
downloadSVGAsText()
180+
})
181+
182+
// downloadAsPng.addEventListener("click", (e) => {
183+
// downloadSVGAsPNG()
184+
// })
185+
127186
generateLineChart(values, 50, 5)

style.css

+38
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,27 @@ body {
2020
padding: 1rem;
2121
}
2222

23+
h1 {
24+
font-size: 1.5rem;
25+
}
26+
27+
button {
28+
cursor: pointer;
29+
}
30+
31+
.btn {
32+
padding: 0.5rem 0.75rem;
33+
background-color: black;
34+
color: white;
35+
font-weight: 500;
36+
font-size: 0.875rem;
37+
border: 2px solid black;
38+
}
39+
40+
.btn:hover {
41+
background-color: #232323;
42+
}
43+
2344
svg {
2445
display: block;
2546
max-width: 100%;
@@ -61,3 +82,20 @@ g.graph-text {
6182
text-anchor: middle;
6283
font-size: 1rem;
6384
}
85+
86+
#real {
87+
padding: 0.5rem 0.75rem;
88+
font-size: 1rem;
89+
border: 2px solid lightgray;
90+
}
91+
92+
.btn-download-chart-as-svg,
93+
.btn-download-chart-as-png {
94+
margin-top: 1rem;
95+
}
96+
97+
@media only screen and (min-width: 600px) {
98+
h1 {
99+
font-size: 1.875rem;
100+
}
101+
}

0 commit comments

Comments
 (0)