Skip to content

Instantly share code, notes, and snippets.

@cbaragao
Created November 17, 2023 03:36
Show Gist options
  • Save cbaragao/d39f14812260cf4ec267c808908cbeed to your computer and use it in GitHub Desktop.
Save cbaragao/d39f14812260cf4ec267c808908cbeed to your computer and use it in GitHub Desktop.
Sparkline =
// dummy value in a table
VAR _vals = {
( 0, 500 ),
( 1, 700 ),
( 2, 1200 ),
( 3, 1250 ),
( 4, 550 ),
( 5, 780 ),
( 6, 990 ),
( 7, 650 ),
( 8, 1250 ),
( 9, 1130 ),
( 10, 1050 ),
( 11, 1125 ),
( 12, 1475 )
} // how many rows are there, use this to figure out horizontal spacing
VAR _count =
COUNTROWS ( _vals ) // what is the target
VAR _target = 1000 // create the header for the SVG
VAR _header = "data:image/svg+xml;utf8,<svg xmlns=""http://www.w3.org/2000/svg"" width=""225"" height=""200"" viewBox=""-20 0 225 200"" fill=""none"">" // set up a footer
VAR _footer =
UNICHAR ( 10 ) & " style=""stroke:black; stroke-width:3; stroke-linecap:round; stroke-linejoin:round; fill:none;""/></svg>" // figure out x values
VAR _x_col =
ADDCOLUMNS (
_vals,
"x",
( [Value1] / _count ) * 200
) // figure out y values and pad it a little so it all render
VAR _y_col =
ADDCOLUMNS (
_x_col,
"y",
200 - ( ( [Value2] / ( _target * 2 ) ) * 200 )
) // set up a reference line with a dash array
VAR _ref_line = "<line x1=""0"" y1=""100"" x2=""225"" y2=""100"" style=""stroke-width:3; stroke:gray; stroke-opacity:0.75; stroke-dasharray: 5,5""/>" // determine the connecting line with polyline
VAR _polyline =
CONCATENATEX (
_y_col,
[x] & " " & [y],
", "
) // convert the polyline to a SVG
VAR _p_svg = "<polyline points= """ & _polyline & """ style=""stroke:black; stroke-width:5; stroke-linecap:round; stroke-linejoin:round; fill:none;""/>" // make the circles and conditionally color them
VAR _circle =
ADDCOLUMNS (
_y_col,
"circle",
"<circle cx=""" & [x] & """" & " cy=""" & [y] & """ r=""5"" stroke=""black"" stroke-width=""2"" fill="""
& IF (
[Value2] <= _target,
"red",
"green"
) & """/>"
) // convert the circles to SVG
VAR _c_svg =
CONCATENATEX (
_circle,
[circle],
UNICHAR ( 10 )
) // create the output
VAR _output = _header & _ref_line & _p_svg & _c_svg & _footer //return it
RETURN
_output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment