-
Notifications
You must be signed in to change notification settings - Fork 400
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract chart renderers into functions and add a Web Component for ea…
…ch type
- Loading branch information
1 parent
9d4d311
commit df75827
Showing
19 changed files
with
3,054 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/dist | ||
jquery.peity.min.js.gz | ||
/node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
jquery.peity.js | ||
jquery.peity.min.js | ||
dist/* |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
}, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
Oops, something went wrong.