Automatic gauge refresh

Hi,
I have a problem with the autoimatic refresh of a gauge. I have tried to refresh automatically after 5 second with a thread, but after 5 seconds nothing happend. How can I make an automatic refresh?

package com.example.testgauge;

import java.text.NumberFormat;
import com.vaadin.addon.charts.Chart;
import com.vaadin.addon.charts.model.Background;
import com.vaadin.addon.charts.model.ChartType;
import com.vaadin.addon.charts.model.Configuration;
import com.vaadin.addon.charts.model.Labels;
import com.vaadin.addon.charts.model.ListSeries;
import com.vaadin.addon.charts.model.Pane;
import com.vaadin.addon.charts.model.PlotOptionsSolidGauge;
import com.vaadin.addon.charts.model.YAxis;
import com.vaadin.addon.charts.model.style.SolidColor;
import com.vaadin.addon.charts.model.YAxis.Stop;

public class ProcessGauge extends Chart 
{
    
    private static final long serialVersionUID = 8741598808436923567L;

    public ProcessGauge(Integer totale,Integer errori)
    {
        String percentuale = getPercentuale(errori, totale);
        
    
        this.setWidth("300px");
        this.setHeight("200px");
        
         Configuration configuration = this.getConfiguration();
        configuration.getChart().setType(ChartType.SOLIDGAUGE);
         
        
        configuration.getTitle().setText("");
        
        Pane pane = new Pane();
        pane.setCenterXY("50%", "50%");
        pane.setSize("100%");
        pane.setStartAngle(-180);
        pane.setEndAngle(180);
        configuration.addPane(pane);
        
        configuration.getTooltip().setEnabled(false);
        
        Background bkg = new Background();
        bkg.setBackgroundColor(new SolidColor("#eeeeee"));
        bkg.setInnerRadius("60%");
        bkg.setOuterRadius("100%");
        bkg.setShape("arc");
        bkg.setBorderWidth(0);
        pane.setBackground(bkg);
        
        
        
        
        YAxis yaxis = configuration.getyAxis();
        yaxis.setLineWidth(3);
        yaxis.setTickInterval(1);
        yaxis.setTickWidth(0);
        yaxis.setMin(0);
        yaxis.setMax(1);
        yaxis.getLabels().setY(16);
        yaxis.getLabels().setEnabled(false);
        Stop stop1 = new Stop(0.85f, SolidColor.RED);
        Stop stop2 = new Stop(0.9f, SolidColor.ORANGE);
        Stop stop3 = new Stop(0.95f, SolidColor.GREEN);
        yaxis.setStops(stop1, stop2, stop3);
        
        
        PlotOptionsSolidGauge plotOptions = new PlotOptionsSolidGauge();
        
        Labels labels = new Labels();
        labels.setY(5);
        labels.setBorderWidth(0);
        labels.setUseHTML(true);
        //        labels.setFormat(percentuale);
        labels.setFormat("<p style=\"text-align:center;margin:0;padding:0;font-size:14px\">" + percentuale + "</p>");
        //        labels.setEnabled(false);
        plotOptions.setDataLabels(labels);
        configuration.setPlotOptions(plotOptions);
    
        final ListSeries series = new ListSeries("Speed", (1 - (double)errori/totale));
         System.out.println(series.getData()[0]
); 
        configuration.setSeries(series);
        this.drawChart(configuration);
        
        MyRun run = new MyRun(series);
        Thread t = new Thread(run);
        t.start();
        
        
        
    
    }

    private static class MyRun implements Runnable{
        ListSeries s;
     public MyRun(ListSeries series){
            s=series;
        }
     
        @Override
        public void run() {
                try {
                    
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                s.updatePoint(0, 0.3);
                System.out.println(s.getData()[0]
);
       }
        
    }
    
    
    private String getPercentuale(int errori, int totale)
    {
    
        NumberFormat percentFormat = NumberFormat.getPercentInstance();
        percentFormat.setMaximumFractionDigits(1);
        percentFormat.setMinimumFractionDigits(1);
        if (errori>=totale)
            return "0.0";
        else
            return percentFormat.format(1 -((double)errori/totale));
    }
}

You have two choices:

  • Enabled Push
  • Set a valid polling interval on you UI (UI.getCurrent().setPollInteval(ms))

Than you have to enclose your code that modifies the chart in the “access method” of the ui class:

[code]
getUi().access(new Runnable(){

    @Override
    public void run(){
         s.updatePoint(0, 0.3);
    }

})
[/code]If you won’t do this, your gauge will be update only on the next “client-initiated” httprequest. (you can try by adding a button on the layout and click it, you’ll see that after the click, your gauge will be updated).

Thanks a lot,
I tried to insert UI.getCurrent().setPollInteval(5000) on my UI ad it works, I also tried to insert in the Chart class
getUi().access(new Runnable(){
@Override
public void run(){
series.updatePoint(0, 0.3);
}
})

but it doesn’t work, it returns this error: SEVERE: Servlet.service() for servlet [com.example.testgauge.TestgaugeUI$Servlet]
in context with path
[/TestGauge] threw exception [com.vaadin.server.ServiceException: java.lang.NullPointerException]
with root cause.

I think this is because of the Thread-Local nature of the getUI() method. Try passing the ui instance to your thread or call UI.getCurrent().access

I have tried but only UI.getCurrent().setPollInteval(5000) worked correctly. Thanks for the help :slight_smile: