The base chart class.
This is the base class for charts built with KotoJS.
Best Practices: It is our recommendation that you name space your charts by prefixing it with
Koto
so if you were making a bar chart, you might name itKotoBarChart
.
Here is where you will write most of the code to initialize
your chart by setting up static things like default configs, scales, and layers.
Note: If you don’t have the luxury of authoring your chart in ES6 (ECMAScript 2015) class syntax, then you can accomplish the same thing by extending the base Koto class with it’s
extend
method and then passing in aninitialize
function. More info on that in theKoto.extend
docs.
A “hook” method that you may define to modify input data before it is used to draw the chart’s layers and attachments. It is passed in the data that was passed into the Koto.draw
method and must return an array of data that will be passed to each of your chart’s layers.
RETURNS: {array} the data that will be passed to chart’s layer’s draw methods.
Note: You will most likely never call this method directly, but rather include it as part of a chart definition, and then rely on KotoJS to invoke it when you draw the chart with
Koto.draw
.
A “hook” method that you may define to choose which mutation of the input data is sent to which of the attached charts (by name). This only applies to charts that use the { Koto#attach} method.
RETURNS: {array} the data that will be passed to attached chart (with given name) layer’s draw methods.
Note: You will most likely never call this method directly, but rather include it as part of a chart definition, and then rely on KotoJS to invoke it when you draw the chart with
Koto.draw
.
A “hook” method that you may define that will be called after Koto.transform
and before each of your layers’ draw
method will be called. This is a convenient place to setup chart attributes that are dependent on data (like scales).
Also an Event: A ‘preDraw’ event is called on your chart just AFTER this function is called in the case that you have something that needs to respond to that event that is outside of the chart itself.
Note: You will most likely never call this method directly, but rather include it as part of a chart definition, and then rely on KotoJS to invoke it when you draw the chart with
Koto.draw
.
A “hook” method that you may define that is called after all of your layers’ draw
method is called. This method does NOT wait for transitions on your layers to finish. If you need that functionality, then you should use the Koto.postTransition
hook.
Also an Event: A ‘postDraw’ event is called on your chart just AFTER this function is called in the case that you have something that needs to respond to that event that is outside of the chart itself.
Note: You will most likely never call this method directly, but rather include it as part of a chart definition, and then rely on KotoJS to invoke it when you draw the chart with
Koto.draw
.
A “hook” method that you may define that is called after all of your layers’ draw
method is called AND all of your layers’ transitions have finished.
Pro Tip: A ‘postDraw’ event is called on your chart just AFTER this function is called in the case that you have something that needs to respond to that event that is outside of the chart itself.
Note: You will most likely never call this method directly, but rather include it as part of a chart definition, and then rely on KotoJS to invoke it when you draw the chart with
Koto.draw
.
Interact with the chart’s Layers
The Layer.draw
method of attached layers will be invoked whenever this chart’s Kotot#draw
is invoked and will receive the data (optionally modified by the chart’s ` Koto#transform` method.
dataBind
and insert
methodIf only a name
is provided, simply return the layer registered to that name (if any).
If a name
and selection
are provided, treat the selection
as a previously-created layer and attach it to the chart with the specified name
.
If all three arguments are specified, initialize a new Layer using the specified selection
as a base passing along the specified options
.
Removes a layer from the chart. This is usefull if you are extending a chart that has a layer that you don’t need.
RETURNS: {Layer} the layer that was removed from the chart
or this can be done from inside a chart’s constructor function
Register or retrieve an “attachment” Chart. The “attachment” chart’s draw
method will be invoked whenever the containing chart’s draw
method is invoked.
If only name is provided, then it will return the attached chart with given name
If name and chart is provided then it will attach the given chart with given name and return the ‘parent chart’ instance (for chaining).
Update the chart’s representation in the DOM, drawing all of its layers and any “attachment” charts (as attached via Koto#attach
).
Note: The first time you call this method, the property
hasDrawn
on your chart will be set to true. This is helpful if you want to only run some code on the first time the chart is drawn.
Subscribe a callback function to an event triggered on the chart. See Kotot#once
to subscribe a callback function to an event for one occurrence.
There are a few preset events that Koto will fire as part of each chart’s lifecycle. You can also manually trigger custom events using the Koto.trigger
method.
Subscribe a callback function to an event triggered on the chart. This function will be invoked at the next occurrence of the event and immediately unsubscribed. See Koto#on
to subscribe a callback function to an event indefinitely.
There are a few preset events that Koto will fire as part of each chart’s lifecycle. You can also manually trigger custom events using the Koto.trigger
method.
Unsubscribe one or more callback functions from an event triggered on the chart.
name
is specified, all handlers subscribed to that event will be unsubscribed.name
and callback
are specified, only that function will be unsubscribed from that event.name
and context
are specified (but callback
is omitted), all events bound to the given event with the given context will be unsubscribed.Publish an event on the chart with the given name
.
Clean up chart and remove base node from DOM.
Getter / Setter for chart’s config options. This function operates similar to how d3’s attr
or style
functions operate.
Pro Tip: This method as been aliased as
Koto.c
for convenience.
Getter / Setter for chart’s data accessors. This function operates similar to how d3’s attr
or style
functions operate.
Pro Tip: This method as been aliased as
Koto.a
for convenience.
We have exposed the internal Layer class so that it can be subclassed for reusing common layers such as a “tooltip” layer.
Pro Tip: You can use object destructuring to pull Layer out of Koto namespace
This will extend a chart by passing in an initialization function or object with methods that you would like to overwrite from the base chart. Because the constructor method is a reserved method, we have renamed that option to initialize
.
RETURNS: {Chart} new, extended constructor function
Pro Tip: This is useful if you don’t have the luxury using ES6 (ECMAScript 2015) class syntax for composing charts.
The layer class.
The Layer class is used internally by the Koto.layer
method to create and attach layers to charts. The selection
and options
parameters are passed into the layer constructor to create a new layer.
dataBind
method AND an insert
method. Optionally, you can include an ‘events’ object with keys that are the layer’s ‘life-cycle’ eventsReturns: Layer instance
Note: You will most likely not instantiate a new ‘Layer’ instance directly, you’ll use the
Koto.layer
method to create a new layer instance.
Create a layer using the provided base
selection.
base
d3.selection The containing DOM node for the layer.options
Object Overrides for databind, insert and event methods.
options.databind
Function databind overrideoptions.insert
Function insert overrideoptions.events
[Function] life-cycle event handler overrides.Possible values are [enter, update, merge, exit] with or without the ‘transition postfix’.This method is invoked by the Layer.draw
method which is invoked by the Koto.draw
method. The purpose of this function is to join data to the layer’s DOM nodes. This method must be overwritten for each layer instance.
The this
context of the function is the layers base selection.
This method is invoked by the Layer.draw
method which is invoked by the Koto.draw
method. The purpose of this function is to insert missing DOM nodes based on the data. This method must be overwritten for each layer instance.
The this
context of this function is the ‘enter’ selection of the ‘data bound’ selection returned by dataBind
method.
Subscribe a handler to a “lifecycle event”. These events (and only these events) are triggered when Layer#draw
is invoked. More information on ‘life-cycle events’ will follow.
Valid ‘life-cycle’ events are:
Also, subscribe handlers to a ‘life-cycle’ event’s transition selection by attaching ‘:transition’ to the event name. In other words, the following are also valid event names:
For more information on ‘life-cycle’ events see this blog (post.)[http://bost.ocks.org/mike/join/]
Unsubscribe the specified handler from the specified event. If no handler is supplied, remove all handlers from the event.
Render the layer according to the input data:
Layer#dataBind
),Layer#insert
),Layer#on
) with the lifecycle selections.Note: You will probably never call this method directly but rely on the
Koto.draw
to call it.