protected Component getChart() { final Chart chart = new Chart(); chart.setWidth("500px"); final Configuration configuration = new Configuration(); configuration.getChart().setType(ChartType.GAUGE); configuration.getChart().setAlignTicks(false); configuration.getChart().setPlotBackgroundColor(null); configuration.getChart().setPlotBackgroundImage(null); configuration.getChart().setPlotBorderWidth(0); configuration.getChart().setPlotShadow(false); configuration.setTitle("Temperature"); configuration.getPane().setStartAngle(-150); configuration.getPane().setEndAngle(150); YAxis yAxis = new YAxis(); yAxis.setMin(-30); yAxis.setMax(50); yAxis.setLineColor(new SolidColor("#339")); yAxis.setTickColor(new SolidColor("#339")); yAxis.setMinorTickColor(new SolidColor("#339")); yAxis.setOffset(-25); yAxis.setLineWidth(2); yAxis.setLabels(new Labels()); yAxis.getLabels().setDistance(-20); yAxis.getLabels().setRotationPerpendicular(); yAxis.setTickLength(5); yAxis.setMinorTickLength(5); yAxis.setEndOnTick(false); configuration.addyAxis(yAxis); final ListSeries series = new ListSeries("Temperature", 12); PlotOptionsGauge plotOptionsGauge = new PlotOptionsGauge(); plotOptionsGauge.setDataLabels(new Labels()); plotOptionsGauge.getDataLabels().setFormatter("function() {return '' + this.y + ' °C';}"); GradientColor gradient = GradientColor.createLinear(0, 0, 0, 1); gradient.addColorStop(0, new SolidColor("#DDD")); gradient.addColorStop(1, new SolidColor("#FFF")); plotOptionsGauge.getDataLabels().setBackgroundColor(gradient); plotOptionsGauge.getTooltip().setValueSuffix(" °C"); series.setPlotOptions(plotOptionsGauge); configuration.setSeries(series); chart.drawChart(configuration); return chart; }
@Override public void run() { try { while (true) { // Update the data for a while Thread.sleep(10000); if (!lastPushed.equals(lastMessage)) { access( () -> chart .getConfiguration() .getSeries() .forEach( s -> { Double newValue = Double.valueOf(lastMessage.split(":")[3]); ((ListSeries) s).updatePoint(0, newValue); })); System.out.println("pushed -> lastMessage = " + lastMessage); lastPushed = lastMessage; } } } catch (InterruptedException e) { e.printStackTrace(); } }