Skip to content

Commit

Permalink
Extract chart renderers into functions and add a Web Component for ea…
Browse files Browse the repository at this point in the history
…ch type
  • Loading branch information
benpickles committed Apr 10, 2024
1 parent 9d4d311 commit df75827
Show file tree
Hide file tree
Showing 19 changed files with 3,054 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/dist
jquery.peity.min.js.gz
/node_modules
3 changes: 1 addition & 2 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
jquery.peity.js
jquery.peity.min.js
dist/*
208 changes: 208 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
"jest-puppeteer": "^10.0.1",
"jsdom": "^24.0.0",
"prettier": "^3.2.5",
"rollup": "^4.12.0",
"uglify-js": "^3.3.7"
},
"scripts": {
"build": "rollup --config",
"pretest": "npm run build",
"test": "jest"
},
"repository": {
Expand Down
16 changes: 16 additions & 0 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import packageJSON from './package.json' assert { type: 'json' }

const banner = `// Piety version ${packageJSON.version}
// Copyright ${new Date().getFullYear()} Ben Pickles
// https://benpickles.github.io/piety`

export default [
{
input: 'src/piety.js',
output: {
banner,
file: 'dist/piety.js',
format: 'es',
},
},
]
65 changes: 65 additions & 0 deletions src/charts/bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
export const defaults = {
fill: ['#4D89F9'],
height: 16,
min: 0,
padding: 0.1,
width: 32,
}

export function renderer(data, opts) {
const { fill, height, padding, width } = opts
const max = Math.max.apply(
Math,
opts.max == null ? data : data.concat(opts.max)
)
const min = Math.min.apply(
Math,
opts.min == null ? data : data.concat(opts.min)
)
const diff = max - min
const xScale = value => (value * width) / data.length
const yScale = value => height - (diff ? ((value - min) / diff) * height : 1)
const parts = data.map((n, i) => {
const x = xScale(i + padding)
const w = xScale(i + 1 - padding) - x
const valueY = yScale(n)
let y1 = valueY
let y2 = valueY
let h

if (!diff) {
h = 1
} else if (n < 0) {
y1 = yScale(Math.min(max, 0))
} else {
y2 = yScale(Math.max(min, 0))
}

h = y2 - y1

if (h == 0) {
h = 1
if (max > 0 && diff) y1--
}

return {
type: 'rect',
props: {
'data-value': n,
fill: fill(n, i, data),
x,
y: y1,
width: w,
height: h,
},
}
})

return {
helpers: {
x: xScale,
y: yScale,
},
parts,
}
}
Loading

0 comments on commit df75827

Please sign in to comment.