-
Notifications
You must be signed in to change notification settings - Fork 401
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reimplement jQuery plugin using the extracted chart renderer functions
- Loading branch information
1 parent
df75827
commit b126937
Showing
10 changed files
with
417 additions
and
79 deletions.
There are no files selected for viewing
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
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,102 @@ | ||
import * as bar from './charts/bar' | ||
import * as line from './charts/line' | ||
import * as pie from './charts/pie' | ||
import { | ||
normaliseData, | ||
normaliseFill, | ||
normalisePieData, | ||
svgElement, | ||
} from './utils' | ||
|
||
const piety = ($.fn.piety = function (type, options) { | ||
return this.each(function () { | ||
const $this = $(this) | ||
let chart = $this.data('_peity') | ||
|
||
if (chart) { | ||
if (type) chart.type = type | ||
$.extend(chart.opts, options) | ||
} else { | ||
chart = new Piety($this, type, { | ||
...piety.defaults[type], | ||
...$this.data('piety'), | ||
...options, | ||
}) | ||
|
||
$this.change(() => chart.render()).data('_peity', chart) | ||
} | ||
|
||
chart.render() | ||
}) | ||
}) | ||
|
||
piety.defaults = {} | ||
piety.graphers = {} | ||
|
||
piety.register = function (type, defaults, grapher) { | ||
this.defaults[type] = defaults | ||
this.graphers[type] = grapher | ||
} | ||
|
||
class Piety { | ||
constructor($el, type, opts) { | ||
this.$el = $el | ||
this.type = type | ||
this.opts = opts | ||
} | ||
|
||
svgElement = svgElement | ||
|
||
get data() { | ||
return normaliseData(this.$el.text()) | ||
} | ||
|
||
prepare(width, height) { | ||
if (!this.$svg) { | ||
const svg = svgElement('svg', { class: 'piety' }) | ||
this.svg = svg | ||
this.$svg = $(svg) | ||
this.$el.hide().after(svg) | ||
} | ||
|
||
return this.$svg.empty().data('_peity', this).attr({ | ||
height: height, | ||
width: width, | ||
}) | ||
} | ||
|
||
render() { | ||
const opts = { | ||
...this.opts, | ||
fill: normaliseFill(this.opts.fill), | ||
} | ||
const { helpers, parts } = piety.graphers[this.type](this, opts) | ||
|
||
this.prepare(opts.width, opts.height) | ||
|
||
parts.forEach(({ props, type }) => | ||
this.$svg.append(svgElement(type, props)) | ||
) | ||
|
||
this.helpers = helpers | ||
|
||
if (opts.after) opts.after(this, helpers) | ||
} | ||
} | ||
|
||
piety.register('bar', bar.defaults, (obj, opts) => bar.renderer(obj.data, opts)) | ||
|
||
piety.register('donut', { ...pie.defaults }, (obj, opts) => { | ||
if (!opts.innerRadius) opts.innerRadius = opts.radius / 2 | ||
return piety.graphers.pie(obj, opts) | ||
}) | ||
|
||
piety.register('line', line.defaults, (obj, opts) => | ||
line.renderer(obj.data, opts) | ||
) | ||
|
||
piety.register('pie', pie.defaults, (obj, opts) => { | ||
if (!opts.width) opts.width = opts.radius * 2 | ||
if (!opts.height) opts.height = opts.radius * 2 | ||
return pie.renderer(normalisePieData(obj.$el.text()), opts) | ||
}) |
Oops, something went wrong.