Package org.pepstock.charba.client
Class AbstractChart
- java.lang.Object
-
- org.pepstock.charba.client.events.HandlerManager
-
- org.pepstock.charba.client.AbstractChart
-
- Direct Known Subclasses:
BarChart
,BubbleChart
,BubbleMapChart
,ChoroplethChart
,DoughnutChart
,GaugeChart
,LineChart
,MatrixChart
,MeterChart
,PieChart
,PolarAreaChart
,RadarChart
,SankeyChart
,ScatterChart
,StackedAreaChart
,StackedBarChart
,TimeSeriesBarChart
,TimeSeriesLineChart
,TreeMapChart
public abstract class AbstractChart extends HandlerManager implements IsChart, HasAxes
Base class of all charts.
It contains Chart.js initialization.- Author:
- Andrea "Stock" Stocchero
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractChart(Type type)
Initializes simple panel and canvas which are used by CHART.JS.
It sets also some default behaviors (width in percentage) for resizing
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description HandlerRegistration
addHandler(EventHandler handler, EventType type)
Adds a event handler.protected void
applyConfiguration()
Called before any drawing or reconfiguration in order that the chart which implements it can override optionsvoid
checkAxes(Axis... axes)
Checks if axes, requested to be stored, are an acceptable type or amount.
If a axis type or the amount of axes are not consistent for the chart, aIllegalArgumentException
will be thrown.protected abstract boolean
checkDataset(Dataset dataset)
Returnstrue
if the dataset can be managed by a this chart type.void
checkDatasets(Dataset... datasets)
Checks if datasets, requested to be stored, are an acceptable type or amount.
If a dataset type or the amount of datasets are not consistent for the chart, aIllegalArgumentException
will be thrown.void
clear()
Will clear the chart canvas.void
createAndSetTimer(ChartTimerTask task, int interval)
Creates aCTimer
instance inside chart.
It can be created only once during the life cycle of the chart.void
destroy()
Use this to destroy any chart instances that are created.
This will clean up any references stored to the chart object within Chart.js, along with any associated event listeners attached by Chart.js.void
draw()
Draws the chart.
It can be invoked once during the life cycle of the chart.List<ActiveDatasetElement>
getActiveElements()
Returns the active (hovered) elements for the chart.Type
getBaseType()
Returns the base type of chart that in case ofChartType
is the same ofIsChart.getType()
otherwise, in case the type of the chart is aControllerType
is the chart type extension if there is ornull
.Canvas
getCanvas()
Returns the canvas element used to draw the chart.Div
getChartElement()
Gets a handle to the object's underlying DOM element.Data
getData()
Returns the data object with all passed datasets.List<DatasetReference>
getDatasetAtEvent(BaseNativeEvent event)
Looks for the data sets that matches the event and returns the data set items references as a list.DatasetItem
getDatasetItem(int index)
Looks for the data set that matches the current index.IsDefaultScaledOptions
getDefaultChartOptions()
Returns the default options created based on chart type.DatasetReference
getElementAtEvent(BaseNativeEvent event)
Calling on your chart instance passing an argument of an event, will return the single element at the event position.
If there are multiple items within range, only the first is returned.List<DatasetReference>
getElementsAtEvent(BaseNativeEvent event)
Looks for the element under the event point, then returns all elements at the same data index.
Calling it on your chart instance passing an argument of an event, will return the point elements that are at that the same position of that event.String
getId()
Returns the ID of chart.
It could be considered as chart unique ID.CursorType
getInitialCursor()
Returns the initial cursor of the chart.protected int
getMaximumAxesCount()
Returns the maximum amount of axes that the chart can manage.protected int
getMaximumDatasetsCount()
Returns the maximum amount of datasets that the chart can manage.ChartNode
getNode()
Returns the chart node with runtime data.Plugins
getPlugins()
Returns the plugins element to manage inline plugins.protected IsChart
getSource()
Returns a chart instance as source for all events.CTimer
getTimer()
Returns aCTimer
instance inside chart.List<ActiveDatasetElement>
getTooltipActiveElements()
Returns the active tooltip elements for the chart.Type
getType()
Returns the type of chart.int
getVisibleDatasetCount()
Returns the amount of data sets which are visibleIsDefaultScaledOptions
getWholeOptions()
Returns the default options by a chart instance, merging global, chart type global and chart options.
If the options is not consistent, it will returns thegetDefaultChartOptions()
.void
hide(int datasetIndex)
Sets the visibility for the given data set to false.
Updates the chart and animates the data set with 'hide' mode.
This animation can be configured under the hide key in animation options.void
hide(int datasetIndex, int dataIndex)
Sets the hidden flag of that element index totrue
and updates the chart.boolean
isChartAttached()
Returnstrue
whether this chart is currently attached to the browser's document.boolean
isDatasetVisible(int index)
Looks for the data set if it's visible or not, selected by index.boolean
isDataVisible(int index)
Returns the stored visibility state of an data index for all data sets.boolean
isDestroyOnDetach()
Returnstrue
if the chart is configured to be destroyed on the detach from DIV element, otherwisefalse
.boolean
isDrawOnAttach()
Returnstrue
if the chart is configured to be drawn on the attach of DIV element, otherwisefalse
.boolean
isInitialized()
Returnstrue
if CHART.JS chart has been initialized, otherwisefalse
.void
onAttach(MutationItem item)
void
onDetach(MutationItem item)
void
reconfigure()
Triggers an update of the chart.
This can be safely called after updating the data object.
This will update the options, mutating the options property in place.void
reconfigure(TransitionKey mode)
Triggers an update of the chart.
This can be safely called after updating the data object.
This will update the options, mutating the options property in place.
A animation mode key can be provided for the update process using a specific animation configuration.
This is useful when update is manually called inside an event handler and some different animation is desired.void
reconfigure(UpdateConfiguration configuration)
Triggers an update of the chart.
This can be safely called after updating the data object.
This will update the options, mutating the options property in place.
A configuration object can be provided with additional configuration for the update process.
This is useful when update is manually called inside an event handler and some different animation is desired.void
removeCanvasPreventDefault()
Remove the registration of prevent default mouse listener from canvas.
This is necessary when you will add your mouse down listener.void
render()
Triggers a redraw of all chart elements.
Note, this does not update elements for new data.
Use.update()
in that case.void
reset()
Reset the chart to it's state before the initial animation.
A new animation can then be triggered using update.void
resetActiveElements()
Resets the active (hovered) elements for the chart.void
resetTooltipActiveElements()
Resets the active tooltip elements for the chart.void
resize()
Use this to manually resize the canvas element.
This is run each time the canvas container is resized, but can be called this method manually if you change the size of the canvas nodes container element.void
resize(int width, int height)
Use this to manually resize the canvas element.
This is run each time the canvas container is resized, but can be called this method manually if you change the size of the canvas nodes container element.void
setActiveElements(List<ActiveDatasetElement> elements)
Sets the active (hovered) elements for the chart.void
setActiveElements(ActiveDatasetElement... elements)
Sets the active (hovered) elements for the chart.void
setDatasetVisibility(int datasetIndex, boolean visibility)
Sets the visibility for a given data set.
This can be used to build a chart legend in HTML.
During click on one of the HTML items, you can call it to change the appropriate data set.void
setDestroyOnDetach(boolean destroyOnDetach)
Setstrue
if the chart is configured to be destroyed on the detach from DIV element, otherwisefalse
.void
setDrawOnAttach(boolean drawOnAttach)
Setstrue
if the chart is configured to be draw on the attach of DIV element, otherwisefalse
.void
setTooltipActiveElements(List<ActiveDatasetElement> elements)
Sets the active tooltip elements for the chart.void
setTooltipActiveElements(ActiveDatasetElement... elements)
Sets the active tooltip elements for the chart.void
show(int datasetIndex)
Sets the visibility for the given dataset to true.
Updates the chart and animates the dataset with 'show' mode.
This animation can be configured under the show key in animation options.void
show(int datasetIndex, int dataIndex)
Sets the hidden flag of that element index tofalse
and updates the chart.void
stop()
Use this to stop any current animation loop.
This will pause the chart during any current animation frame.
Call.render()
to re-animate.String
toBase64Image(ImageMimeType type, double encoderOptions)
Returns a base 64 encoded string of the chart in it's current state.void
toggleDataVisibility(int index)
Toggles the visibility of an item in all data sets.
A data set needs to explicitly support this feature for it to have an effect.
From internal chart types, doughnut / pie and polar area use this.void
update()
Triggers an update of the chart.
This can be safely called after updating the data object.
This will update all scales, legends, and then re-render the chart.void
update(TransitionKey mode)
Triggers an update of the chart.
This can be safely called after updating the data object.
This will update all scales, legends, and then re-render the chart.
A animation mode key can be provided for the update process using a specific animation configuration.
This is useful when update is manually called inside an event handler and some different animation is desired.void
update(UpdateConfiguration configuration)
Triggers an update of the chart.
This can be safely called after updating the data object.
This will update all scales, legends, and then re-render the chart.
A configuration object can be provided with additional configuration for the update process.
This is useful when update is manually called inside an event handler and some different animation is desired.-
Methods inherited from class org.pepstock.charba.client.events.HandlerManager
fireEvent, getHandlerCount, isEventHandled
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.pepstock.charba.client.IsChart
fireEvent, getHandlerCount, getOptions, isEventHandled, toBase64Image, toBase64Image, toBase64Image
-
-
-
-
Constructor Detail
-
AbstractChart
protected AbstractChart(Type type)
Initializes simple panel and canvas which are used by CHART.JS.
It sets also some default behaviors (width in percentage) for resizing- Parameters:
type
- the type of chart
-
-
Method Detail
-
getSource
protected final IsChart getSource()
Description copied from class:HandlerManager
Returns a chart instance as source for all events.- Specified by:
getSource
in classHandlerManager
- Returns:
- a chart instance as source for all events
-
addHandler
public final HandlerRegistration addHandler(EventHandler handler, EventType type)
Description copied from class:HandlerManager
Adds a event handler.- Specified by:
addHandler
in interfaceIsChart
- Overrides:
addHandler
in classHandlerManager
- Parameters:
handler
- handler instance to addtype
- the event type associated with handler- Returns:
- the handler registration in order to remove the handler later
-
getChartElement
public final Div getChartElement()
Description copied from interface:IsChart
Gets a handle to the object's underlying DOM element.- Specified by:
getChartElement
in interfaceIsChart
- Returns:
- the object's browser element
-
getType
public final Type getType()
Description copied from interface:IsChart
Returns the type of chart.
-
getId
public final String getId()
Returns the ID of chart.
It could be considered as chart unique ID.
-
getBaseType
public final Type getBaseType()
Description copied from interface:IsChart
Returns the base type of chart that in case ofChartType
is the same ofIsChart.getType()
otherwise, in case the type of the chart is aControllerType
is the chart type extension if there is ornull
.- Specified by:
getBaseType
in interfaceIsChart
- Returns:
- the base type of chart.
-
getCanvas
public final Canvas getCanvas()
Returns the canvas element used to draw the chart.
-
removeCanvasPreventDefault
public final void removeCanvasPreventDefault()
Remove the registration of prevent default mouse listener from canvas.
This is necessary when you will add your mouse down listener.- Specified by:
removeCanvasPreventDefault
in interfaceIsChart
-
isChartAttached
public final boolean isChartAttached()
Returnstrue
whether this chart is currently attached to the browser's document.- Specified by:
isChartAttached
in interfaceIsChart
- Returns:
true
whether this chart is currently attached to the browser's document
-
getInitialCursor
public final CursorType getInitialCursor()
Returns the initial cursor of the chart.- Specified by:
getInitialCursor
in interfaceIsChart
- Returns:
- the initial cursor of the chart.
-
isInitialized
public final boolean isInitialized()
Returnstrue
if CHART.JS chart has been initialized, otherwisefalse
.- Specified by:
isInitialized
in interfaceIsChart
- Returns:
true
if CHART.JS chart has been initialized, otherwisefalse
.
-
getNode
public final ChartNode getNode()
Returns the chart node with runtime data.
-
getData
public final Data getData()
Returns the data object with all passed datasets.
-
getPlugins
public final Plugins getPlugins()
Returns the plugins element to manage inline plugins.- Specified by:
getPlugins
in interfaceIsChart
- Returns:
- the plugins configuration object
-
getDefaultChartOptions
public final IsDefaultScaledOptions getDefaultChartOptions()
Returns the default options created based on chart type.- Specified by:
getDefaultChartOptions
in interfaceIsChart
- Returns:
- the default options of the chart
-
getWholeOptions
public final IsDefaultScaledOptions getWholeOptions()
Returns the default options by a chart instance, merging global, chart type global and chart options.
If the options is not consistent, it will returns thegetDefaultChartOptions()
.- Specified by:
getWholeOptions
in interfaceIsChart
- Returns:
- the default options by a chart instance, merging global, chart type global and chart options
-
createAndSetTimer
public void createAndSetTimer(ChartTimerTask task, int interval)
Creates aCTimer
instance inside chart.
It can be created only once during the life cycle of the chart.- Specified by:
createAndSetTimer
in interfaceIsChart
- Parameters:
task
- the task to be executed repeatedlyinterval
- the time, in milliseconds (thousands of a second), the timer should delay in between executions of the specified task.
Must be greater than 0.
-
isDrawOnAttach
public final boolean isDrawOnAttach()
Returnstrue
if the chart is configured to be drawn on the attach of DIV element, otherwisefalse
.- Specified by:
isDrawOnAttach
in interfaceIsChart
- Returns:
- the drawOnAttach
true
if the chart is configured to be drawn on the attach of DIV element, otherwisefalse
.
-
setDrawOnAttach
public final void setDrawOnAttach(boolean drawOnAttach)
Setstrue
if the chart is configured to be draw on the attach of DIV element, otherwisefalse
.- Specified by:
setDrawOnAttach
in interfaceIsChart
- Parameters:
drawOnAttach
- the drawOnAttach to set
-
isDestroyOnDetach
public final boolean isDestroyOnDetach()
Returnstrue
if the chart is configured to be destroyed on the detach from DIV element, otherwisefalse
.- Specified by:
isDestroyOnDetach
in interfaceIsChart
- Returns:
- the destroyOnDetach
true
if the chart is configured to be destroyed on the detach from DIV element, otherwisefalse
.
-
setDestroyOnDetach
public final void setDestroyOnDetach(boolean destroyOnDetach)
Setstrue
if the chart is configured to be destroyed on the detach from DIV element, otherwisefalse
.- Specified by:
setDestroyOnDetach
in interfaceIsChart
- Parameters:
destroyOnDetach
- the destroyOnDetach to set
-
onAttach
public final void onAttach(MutationItem item)
-
onDetach
public final void onDetach(MutationItem item)
-
setActiveElements
public final void setActiveElements(List<ActiveDatasetElement> elements)
Sets the active (hovered) elements for the chart.- Specified by:
setActiveElements
in interfaceIsChart
- Parameters:
elements
- list of active elements
-
setActiveElements
public final void setActiveElements(ActiveDatasetElement... elements)
Sets the active (hovered) elements for the chart.- Specified by:
setActiveElements
in interfaceIsChart
- Parameters:
elements
- array of active elements
-
resetActiveElements
public final void resetActiveElements()
Resets the active (hovered) elements for the chart.- Specified by:
resetActiveElements
in interfaceIsChart
-
getActiveElements
public final List<ActiveDatasetElement> getActiveElements()
Returns the active (hovered) elements for the chart.- Specified by:
getActiveElements
in interfaceIsChart
- Returns:
- the array of active elements
-
setTooltipActiveElements
public final void setTooltipActiveElements(List<ActiveDatasetElement> elements)
Sets the active tooltip elements for the chart.- Specified by:
setTooltipActiveElements
in interfaceIsChart
- Parameters:
elements
- list of active tooltip elements
-
setTooltipActiveElements
public final void setTooltipActiveElements(ActiveDatasetElement... elements)
Sets the active tooltip elements for the chart.- Specified by:
setTooltipActiveElements
in interfaceIsChart
- Parameters:
elements
- array of active tooltip elements
-
resetTooltipActiveElements
public final void resetTooltipActiveElements()
Resets the active tooltip elements for the chart.- Specified by:
resetTooltipActiveElements
in interfaceIsChart
-
getTooltipActiveElements
public final List<ActiveDatasetElement> getTooltipActiveElements()
Returns the active tooltip elements for the chart.- Specified by:
getTooltipActiveElements
in interfaceIsChart
- Returns:
- the array of active tooltip elements
-
destroy
public final void destroy()
Use this to destroy any chart instances that are created.
This will clean up any references stored to the chart object within Chart.js, along with any associated event listeners attached by Chart.js.
-
stop
public final void stop()
Use this to stop any current animation loop.
This will pause the chart during any current animation frame.
Call.render()
to re-animate.
-
clear
public final void clear()
Will clear the chart canvas.
-
reset
public final void reset()
Reset the chart to it's state before the initial animation.
A new animation can then be triggered using update.
-
toBase64Image
public final String toBase64Image(ImageMimeType type, double encoderOptions)
Returns a base 64 encoded string of the chart in it's current state.- Specified by:
toBase64Image
in interfaceIsChart
- Parameters:
type
- indicating the image formatencoderOptions
- between 0 and 1 indicating the image quality to use for image formats that use lossy compression.
If this argument is anything else, the default value for image quality is used. The default value is 0.92.- Returns:
- base 64 image or
Undefined.STRING
if chart is not initialized.
-
resize
public final void resize()
Use this to manually resize the canvas element.
This is run each time the canvas container is resized, but can be called this method manually if you change the size of the canvas nodes container element.
-
resize
public final void resize(int width, int height)
Use this to manually resize the canvas element.
This is run each time the canvas container is resized, but can be called this method manually if you change the size of the canvas nodes container element.
-
update
public final void update()
Triggers an update of the chart.
This can be safely called after updating the data object.
This will update all scales, legends, and then re-render the chart.
-
update
public final void update(TransitionKey mode)
Triggers an update of the chart.
This can be safely called after updating the data object.
This will update all scales, legends, and then re-render the chart.
A animation mode key can be provided for the update process using a specific animation configuration.
This is useful when update is manually called inside an event handler and some different animation is desired.
-
update
public final void update(UpdateConfiguration configuration)
Triggers an update of the chart.
This can be safely called after updating the data object.
This will update all scales, legends, and then re-render the chart.
A configuration object can be provided with additional configuration for the update process.
This is useful when update is manually called inside an event handler and some different animation is desired.
-
reconfigure
public final void reconfigure()
Triggers an update of the chart.
This can be safely called after updating the data object.
This will update the options, mutating the options property in place.- Specified by:
reconfigure
in interfaceIsChart
-
reconfigure
public final void reconfigure(TransitionKey mode)
Triggers an update of the chart.
This can be safely called after updating the data object.
This will update the options, mutating the options property in place.
A animation mode key can be provided for the update process using a specific animation configuration.
This is useful when update is manually called inside an event handler and some different animation is desired.- Specified by:
reconfigure
in interfaceIsChart
- Parameters:
mode
- an animation mode can be provided to indicate what should be updated and what animation configuration should be used
-
reconfigure
public final void reconfigure(UpdateConfiguration configuration)
Triggers an update of the chart.
This can be safely called after updating the data object.
This will update the options, mutating the options property in place.
A configuration object can be provided with additional configuration for the update process.
This is useful when update is manually called inside an event handler and some different animation is desired.- Specified by:
reconfigure
in interfaceIsChart
- Parameters:
configuration
- a configuration object can be provided with additional configuration for the update process
-
render
public final void render()
Triggers a redraw of all chart elements.
Note, this does not update elements for new data.
Use.update()
in that case.
-
getDatasetItem
public final DatasetItem getDatasetItem(int index)
Looks for the data set that matches the current index.- Specified by:
getDatasetItem
in interfaceIsChart
- Parameters:
index
- data set index- Returns:
- data set item or
null
if the index is out of range of data sets count.
-
getDatasetAtEvent
public final List<DatasetReference> getDatasetAtEvent(BaseNativeEvent event)
Looks for the data sets that matches the event and returns the data set items references as a list.- Specified by:
getDatasetAtEvent
in interfaceIsChart
- Parameters:
event
- event of chart.- Returns:
- data set items references list or or an empty list.
-
isDatasetVisible
public final boolean isDatasetVisible(int index)
Looks for the data set if it's visible or not, selected by index.- Specified by:
isDatasetVisible
in interfaceIsChart
- Parameters:
index
- data set index- Returns:
true
if data set is visible otherwisefalse
.
-
getVisibleDatasetCount
public final int getVisibleDatasetCount()
Returns the amount of data sets which are visible- Specified by:
getVisibleDatasetCount
in interfaceIsChart
- Returns:
- the amount of data sets which are visible.
If chart is not initialized, returnUndefined.INTEGER
.
-
setDatasetVisibility
public final void setDatasetVisibility(int datasetIndex, boolean visibility)
Sets the visibility for a given data set.
This can be used to build a chart legend in HTML.
During click on one of the HTML items, you can call it to change the appropriate data set.- Specified by:
setDatasetVisibility
in interfaceIsChart
- Parameters:
datasetIndex
- data set indexvisibility
- iftrue
enables the visibility otherwisefalse
-
toggleDataVisibility
public final void toggleDataVisibility(int index)
Toggles the visibility of an item in all data sets.
A data set needs to explicitly support this feature for it to have an effect.
From internal chart types, doughnut / pie and polar area use this.- Specified by:
toggleDataVisibility
in interfaceIsChart
- Parameters:
index
- data index
-
isDataVisible
public final boolean isDataVisible(int index)
Returns the stored visibility state of an data index for all data sets.- Specified by:
isDataVisible
in interfaceIsChart
- Parameters:
index
- data index- Returns:
true
if the data item is visible
-
hide
public final void hide(int datasetIndex)
Sets the visibility for the given data set to false.
Updates the chart and animates the data set with 'hide' mode.
This animation can be configured under the hide key in animation options.
-
hide
public final void hide(int datasetIndex, int dataIndex)
Sets the hidden flag of that element index totrue
and updates the chart.
-
show
public final void show(int datasetIndex)
Sets the visibility for the given dataset to true.
Updates the chart and animates the dataset with 'show' mode.
This animation can be configured under the show key in animation options.
-
show
public final void show(int datasetIndex, int dataIndex)
Sets the hidden flag of that element index tofalse
and updates the chart.
-
getElementAtEvent
public final DatasetReference getElementAtEvent(BaseNativeEvent event)
Calling on your chart instance passing an argument of an event, will return the single element at the event position.
If there are multiple items within range, only the first is returned.- Specified by:
getElementAtEvent
in interfaceIsChart
- Parameters:
event
- event of chart.- Returns:
- single element at the event position or
null
if event is not consistent
-
getElementsAtEvent
public final List<DatasetReference> getElementsAtEvent(BaseNativeEvent event)
Looks for the element under the event point, then returns all elements at the same data index.
Calling it on your chart instance passing an argument of an event, will return the point elements that are at that the same position of that event.- Specified by:
getElementsAtEvent
in interfaceIsChart
- Parameters:
event
- event of chart.- Returns:
- all elements at the same data index or an empty list.
-
draw
public final void draw()
Draws the chart.
It can be invoked once during the life cycle of the chart.
-
applyConfiguration
protected void applyConfiguration()
Called before any drawing or reconfiguration in order that the chart which implements it can override options
-
checkDatasets
public final void checkDatasets(Dataset... datasets)
Description copied from interface:IsChart
Checks if datasets, requested to be stored, are an acceptable type or amount.
If a dataset type or the amount of datasets are not consistent for the chart, aIllegalArgumentException
will be thrown.- Specified by:
checkDatasets
in interfaceIsChart
- Parameters:
datasets
- list of datasets to check.
-
checkDataset
protected abstract boolean checkDataset(Dataset dataset)
Returnstrue
if the dataset can be managed by a this chart type.- Parameters:
dataset
- dataset to check- Returns:
true
if the dataset can be managed by a this chart type
-
getMaximumDatasetsCount
protected int getMaximumDatasetsCount()
Returns the maximum amount of datasets that the chart can manage.- Returns:
- the maximum amount of datasets that the chart can manage.
-
checkAxes
public void checkAxes(Axis... axes)
Description copied from interface:IsChart
Checks if axes, requested to be stored, are an acceptable type or amount.
If a axis type or the amount of axes are not consistent for the chart, aIllegalArgumentException
will be thrown.
-
getMaximumAxesCount
protected int getMaximumAxesCount()
Returns the maximum amount of axes that the chart can manage.- Returns:
- the maximum amount of axes that the chart can manage.
-
-