All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.dashbuilder.renderer.chartjs.lib.ChartWithAnimation Maven / Gradle / Ivy

The newest version!
package org.dashbuilder.renderer.chartjs.lib;

import org.dashbuilder.renderer.chartjs.lib.event.AnimationCompleteEvent;
import org.dashbuilder.renderer.chartjs.lib.event.AnimationCompleteHandler;
import org.dashbuilder.renderer.chartjs.lib.event.HasAnimationCompleteHandlers;
import org.dashbuilder.renderer.chartjs.lib.options.AnimationCallback;
import org.dashbuilder.renderer.chartjs.lib.options.HasAnimation;
import org.dashbuilder.renderer.chartjs.lib.options.IsResponsive;
import org.dashbuilder.renderer.chartjs.lib.options.Type;

import java.util.ArrayList;
import java.util.List;

public abstract class ChartWithAnimation extends Chart implements IsResponsive, HasAnimation,HasAnimationCompleteHandlers {

    protected List callbackList = new ArrayList();

    public ChartWithAnimation(){
        super();
        registerNativeAnimationHandlers();
    }


    @Override
    public void setResponsive(boolean responsive){
        if(!responsive)
            options.clearProperty(RESPONSIVE);
        else
            options.setProperty(RESPONSIVE, true);
    }

    /**
     * Specify should chart be animated or not
     * Default value is true
     * @param enabled
     */
    public void setAnimationEnabled(boolean enabled){
        if(!enabled) //"animation" : false interpreted by chart.js as "true"
            options.clearProperty(ANIMATION);
        else
            options.setProperty(ANIMATION, enabled);
    }

    /**
     * Specify animation easing
     * Default value is {@link org.dashbuilder.renderer.chartjs.lib.options.Type#EASE_OUT_QUART}
     * @param type
     */
    public void setAnimationType(Type type){
        if(type == null)
            options.clearProperty(ANIMATION_EASING);
        else
            options.setProperty(ANIMATION_EASING, type.getValue());
    }

    /**
     * Add animation callback to handle animation state changes
     * @param callback
     */
    public void addAnimationCallback(AnimationCallback callback){
        if(callback != null)
            callbackList.add(callback);
    }

    @Override
    public void setAnimationSteps(int steps) {
        if(steps <= 0)
            throw new IndexOutOfBoundsException("Number of animation steps should be positive. Found '"+steps+"'");

        options.setProperty(ANIMATION_STEPS, steps);
    }

    @Override
    public void addAnimationCompleteHandler(AnimationCompleteHandler handler) {
        addHandler(handler, AnimationCompleteEvent.getType());
    }


    protected void onAnimationProgress(double progress){
        for(AnimationCallback callback : callbackList){
            if(callback != null)
                callback.onProgress(progress);
        }
    }

    protected void onAnimationComplete(){
        for(AnimationCallback callback : callbackList){
            if(callback != null)
                callback.onAnimationComplete();
        }
    }

    protected native void registerNativeAnimationHandlers()/*-{
        options = [email protected]::constructOptions()();
        self = this;
        options.onAnimationProgress = function(progress){
            [email protected]::onAnimationProgress(D)(progress);
            return;
        }
        options.onAnimationComplete = function(){
            [email protected]::onAnimationComplete()();
            return;
        }
    }-*/;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy