Skip to content

Commit

Permalink
feat(#6): add support for chart titles
Browse files Browse the repository at this point in the history
  • Loading branch information
usrme committed Jul 26, 2024
1 parent d11845d commit 63b1755
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 12 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ Try the same link as in the demo above, https://gobarchar.usrme.xyz/?2012=8&2013
- Add the `spaces` query parameter and pass `yes` as the value
- Any other value will be disregarded
- The default is to not do any replacing
- Maybe coming: change layout from horizontal to vertical!
- Display a custom title for the chart to make sharing charts better
- Add the `title` query parameter and any value you want
- For `curl` requests the value needs to be HTML encoded
- Something like the following could be used to automate this: `curl "http://localhost:8080/?February=5&March=20&title=$(echo 'Title with spaces in it' | python3 -c 'import urllib.parse,sys; print(urllib.parse.quote(sys.stdin.read().strip()))')"`
- Alternatively, after rendering the chart with the title in the browser, just copy the generated link from the page

## Usage

Expand Down
15 changes: 13 additions & 2 deletions chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gobarchar

import (
"fmt"
"html"
"math/rand"
"net/http"
"sort"
Expand Down Expand Up @@ -142,11 +143,17 @@ func createBarChart(r *http.Request) string {
raw := r.URL.RawQuery
orderedParams := strings.Split(raw, "&")
addSpaces := slices.Contains(orderedParams, "spaces=yes")

var title string
for _, pair := range orderedParams {
kv := strings.Split(pair, "=")
key := kv[0]
// Don't parse certain query parameters as they are not part of the data
if key == "sort" || key == "spaces" {
switch key {
case "sort", "spaces":
continue
case "title":
title = strings.Replace(kv[1], "%20", " ", -1)
continue
}
count, err := strconv.ParseFloat(kv[1], 64)
Expand Down Expand Up @@ -199,6 +206,10 @@ func createBarChart(r *http.Request) string {
}

var chartContent strings.Builder
if title != "" {
chartContent.WriteString(title + "\n\n")
}

maximumBarChunk := 0
for i := range entries {
// Skip parsing the total for now to not interfere with calculating
Expand Down Expand Up @@ -226,7 +237,7 @@ func createBarChart(r *http.Request) string {
bar := calculateBars(maximumBarChunk, 0)
totalStr := formatValue(total)
chartContent.WriteString(fmt.Sprintf("%s %s %s\n", padRight("Total", longestLabelLength), padLeft(totalStr, longestValueLength), bar))
return chartContent.String()
return html.UnescapeString(chartContent.String())
}

func formatValue(value float64) string {
Expand Down
52 changes: 52 additions & 0 deletions chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,58 @@ Avg. 9 █████████████████████
Total 18 █████████████████████████
`),
},
{
name: "Add 'title' parameter with literal spaces",
queryParams: "A=10&B=20&C=15&title=A descriptive title",
expected: strings.TrimSpace(`
A descriptive title
A 10 ████████████▌
B 20 █████████████████████████
C 15 ██████████████████▊
Avg. 15 ██████████████████▊
Total 45 █████████████████████████
`),
},
{
name: "Add 'title' parameter with HTML entity spaces",
queryParams: "A=10&B=20&C=15&title=A%20descriptive%20title",
expected: strings.TrimSpace(`
A descriptive title
A 10 ████████████▌
B 20 █████████████████████████
C 15 ██████████████████▊
Avg. 15 ██████████████████▊
Total 45 █████████████████████████
`),
},
{
name: "Add 'title' parameter as first parameter",
queryParams: "title=A descriptive title&A=10&B=20&C=15",
expected: strings.TrimSpace(`
A descriptive title
A 10 ████████████▌
B 20 █████████████████████████
C 15 ██████████████████▊
Avg. 15 ██████████████████▊
Total 45 █████████████████████████
`),
},
{
name: "Add 'title' parameter with other parameters",
queryParams: "A=10&B=20&C=15&sort=desc&title=A descriptive title",
expected: strings.TrimSpace(`
A descriptive title
B 20 █████████████████████████
C 15 ██████████████████▊
A 10 ████████████▌
Avg. 15 ██████████████████▊
Total 45 █████████████████████████
`),
},
}

for _, testCase := range testCases {
Expand Down
19 changes: 10 additions & 9 deletions examplequery.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,24 @@ type exampleQueries []query
// Using 'map[string]string' would be more readable, but would require additional logic
// to keep insertion order intact
var (
presidentQuery = query{
Title: "Presidents of the United States by age at start of presidency",
presidentQueryTitle string = "Presidents of the United States by age at start of presidency"
presidentQuery = query{
Title: presidentQueryTitle,
Keys: []string{
"George Washington (1789)", "John Adams (1797)", "Thomas Jefferson (1801)", "James Madison (1809)", "James Monroe (1817)", "John Quincy Adams (1825)", "Andrew Jackson (1829)", "Martin Van Buren (1837)", "William Henry Harrison (1841)", "John Tyler (1841)", "James K. Polk (1845)", "Zachary Taylor (1849)", "Millard Fillmore (1850)", "Franklin Pierce (1853)", "James Buchanan (1857)", "Abraham Lincoln (1861)", "Andrew Johnson (1865)", "Ulysses S. Grant (1869)", "Rutherford B. Hayes (1877)", "James A. Garfield (1881)", "Chester A. Arthur (1881)", "Grover Cleveland (first term) (1885)", "Benjamin Harrison (1889)", "Grover Cleveland (second term) (1893)", "William McKinley (1897)", "Theodore Roosevelt (1901)", "William Howard Taft (1909)", "Woodrow Wilson (1913)", "Warren G. Harding (1921)", "Calvin Coolidge (1923)", "Herbert Hoover (1929)", "Franklin D. Roosevelt (1933)", "Harry S. Truman (1945)", "Dwight D. Eisenhower (1953)", "John F. Kennedy (1961)", "Lyndon B. Johnson (1963)", "Richard Nixon (1969)", "Gerald Ford (1974)", "Jimmy Carter (1977)", "Ronald Reagan (1981)", "George H. W. Bush (1989)", "Bill Clinton (1993)", "George W. Bush (2001)", "Barack Obama (2009)", "Donald Trump (2017)", "Joe Biden (2021)", "spaces", "sort",
"George Washington (1789)", "John Adams (1797)", "Thomas Jefferson (1801)", "James Madison (1809)", "James Monroe (1817)", "John Quincy Adams (1825)", "Andrew Jackson (1829)", "Martin Van Buren (1837)", "William Henry Harrison (1841)", "John Tyler (1841)", "James K. Polk (1845)", "Zachary Taylor (1849)", "Millard Fillmore (1850)", "Franklin Pierce (1853)", "James Buchanan (1857)", "Abraham Lincoln (1861)", "Andrew Johnson (1865)", "Ulysses S. Grant (1869)", "Rutherford B. Hayes (1877)", "James A. Garfield (1881)", "Chester A. Arthur (1881)", "Grover Cleveland (first term) (1885)", "Benjamin Harrison (1889)", "Grover Cleveland (second term) (1893)", "William McKinley (1897)", "Theodore Roosevelt (1901)", "William Howard Taft (1909)", "Woodrow Wilson (1913)", "Warren G. Harding (1921)", "Calvin Coolidge (1923)", "Herbert Hoover (1929)", "Franklin D. Roosevelt (1933)", "Harry S. Truman (1945)", "Dwight D. Eisenhower (1953)", "John F. Kennedy (1961)", "Lyndon B. Johnson (1963)", "Richard Nixon (1969)", "Gerald Ford (1974)", "Jimmy Carter (1977)", "Ronald Reagan (1981)", "George H. W. Bush (1989)", "Bill Clinton (1993)", "George W. Bush (2001)", "Barack Obama (2009)", "Donald Trump (2017)", "Joe Biden (2021)", "spaces", "sort", "title",
},
Values: []string{
"57", "61", "57", "57", "58", "57", "61", "54", "68", "51", "49", "64", "50", "48", "65", "52", "56", "46", "54", "49", "51", "47", "55", "55", "54", "42", "51", "56", "55", "51", "54", "51", "60", "62", "43", "55", "56", "61", "52", "69", "64", "46", "54", "47", "70", "78", "yes", "asc",
"57", "61", "57", "57", "58", "57", "61", "54", "68", "51", "49", "64", "50", "48", "65", "52", "56", "46", "54", "49", "51", "47", "55", "55", "54", "42", "51", "56", "55", "51", "54", "51", "60", "62", "43", "55", "56", "61", "52", "69", "64", "46", "54", "47", "70", "78", "yes", "asc", presidentQueryTitle,
},
}
terraformQuery = query{
Title: "% of community PRs opened against Terraform after license change (2023)",
terraformQueryTitle string = "% of community PRs opened against Terraform after license change (2023)"
terraformQuery = query{
Title: terraformQueryTitle,
Keys: []string{
"February", "March", "April", "May", "June", "July", "August", "September",
"February", "March", "April", "May", "June", "July", "August", "September", "title",
},
Values: []string{
"14.71", "22.83", "28.57", "22.58", "23.29", "20.29", "9.30", "9.52",
"14.71", "22.83", "28.57", "22.58", "23.29", "20.29", "9.30", "9.52", terraformQueryTitle,
},
}
Examples = exampleQueries{presidentQuery, terraformQuery}
Expand All @@ -58,4 +60,3 @@ func CreateListItems(url string, elements exampleQueries) string {
}
return fmt.Sprintf("%s%s%s", start, listItems.String(), end)
}

0 comments on commit 63b1755

Please sign in to comment.