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 ToolGenerator
and implement its methods.
The constructor will receive a ToolGeneratorTArgs
object as the first parameter.
As custom Generators inherit directly from ToolGenerator
, the constructor of this superclass needs to be called and a ToolGeneratorTConfig
object must be forwarded.
import { ToolGenerator, type ToolGeneratorTArgs } from '@stock3/charting-bundle-chartbreaker';
class MyGenerator extends ToolGenerator {
constructor(tArgs: ToolGeneratorTArgs) {
super(tArgs);
}
}
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.
import { Text as TextDescriptor } from '@stock3/charting-bundle-chartbreaker';
override onStart() {
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 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',
});
};
Once you have implemented your custom generator, you need to register it with Chartbreaker.
import { setClazz } from '@stock3/charting-bundle-chartbreaker';
// Register the custom generator as plugin
setClazz({
clazzType: 'generator',
type: 'MyGenerator',
clazz: MyGenerator
});
The finished generator class can now be provided to the chart. To add an instance of it simply use the Controller.addGenerator method.
To remove a generator again, you can simply call 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);