private Chart createChart(int points) { Chart chart = new Chart(ChartType.SCATTER); Configuration conf = chart.getConfiguration(); conf.getChart().setZoomType(ZoomType.XY); conf.disableCredits(); conf.setTitle("Height vs Weight"); conf.setSubTitle("Polygon series in Vaadin Charts."); Tooltip tooltip = conf.getTooltip(); tooltip.setHeaderFormat("{series.name}"); tooltip.setPointFormat("{point.x} cm, {point.y} kg"); XAxis xAxis = conf.getxAxis(); xAxis.setStartOnTick(true); xAxis.setEndOnTick(true); xAxis.setShowLastLabel(true); xAxis.setTitle("Height (cm)"); YAxis yAxis = conf.getyAxis(); yAxis.setTitle("Weight (kg)"); AbstractLinePlotOptions plotOptions = new PlotOptionsScatter(); plotOptions.setThreshold(0); DataSeries scatter = new DataSeries(); scatter.setPlotOptions(plotOptions); scatter.setName("Observations"); fillScatter(scatter, points); conf.addSeries(scatter); return chart; }
private void fillScatter(DataSeries series, int points) { Random random = new Random(); for (int i = 0; i < points; i++) { double x = random.nextDouble() * 30 + 150; double y = 60; if (random.nextBoolean()) { y += random.nextDouble() * 15; if (random.nextBoolean() && x > 170) { y += random.nextDouble() * 30; } } else { y -= random.nextDouble() * 20; } x = Math.floor(x * 10) / 10; y = Math.floor(y * 10) / 10; int colorNumber = (int) Math.floor((x - 150) / 30 * 255); Color color = new SolidColor(colorNumber, 0, 255 - colorNumber); DataSeriesItem item = new DataSeriesItem(x, y); // item.setColor(color); series.add(item); } }