Tool Generator
Tool Generators let you programmatically create tool objects and insert them into the current chart. This is useful for many applications, such as inserting additional information regarding specific points in time with the text tool or emphasizing high/low values with horizontal lines.
A generator must inherit from BG.charts.ToolGenerator
and implement its methods.
The constructor will receive a BG.charts.ToolGenerator.TArgs
object as the first parameter.
As custom Generators inherit directly from BG.charts.ToolGenerator
, the constructor of this superclass needs to be called and a BG.charts.ToolGenerator.TConfig
object must be forwarded.
/**
* @constructor
* @param {string} identifier
*/
class MyGenerator extends BG.charts.ToolGenertor {
constructor(tArgs) {
super(tArgs);
}
}
Creating tools
Generators can create new tools once their onStart method has been called by calling the each tool's constructor. The new tool then needs to submitted to the Controller by emitting the 'addTool' event.
onStart(context) {
const data = this.getData();
const input = this.getInput();
if (!data || !input) return;
const lastIndex = data.get('lastIndex');
const values = data.getBuffer(input.ohlcSet.C).getValues();
const tool = new BG.charts.plugins.Text.clazz({
identifier: 'Text',
type: 'Text',
translator: this.getTranslator(),
});
// the `addTool` event needs to be triggered BEFORE the tool can be positioned!
this.emit('addTool', tool);
tool.setHandles([[
data.getIdentifierByIndex(lastIndex),
values[lastIndex]
]]);
tool.set({
'text': 'Hello World',
});
};
Registering a Generator with Chartbreaker
Once you have implemented your custom generator, you need to register it with Chartbreaker.
// Register the custom generator as plugin
BG.charts.setClazz({
clazzType: 'generator',
type: 'MyGenerator',
clazz: MyGenerator
});
Add or remove Generators
The finished generator class can now be provided to the chart. To add an instance of it simply use the BG.charts.Controller.addGenerator method.
To remove a generator again, you can simply call BG.charts.Controller.removeObject and pass the generator object as the first parameter.
// add a generator
const obj = controller.addGenerator('MyGenerator');
// remove it again
controller.removeObject(obj);