<commit_before>/*
 * Copyright 2016 <NAME> <<EMAIL>>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package ca.fe.examples.charts;

import com.vaadin.addon.charts.Chart;
import com.vaadin.addon.charts.model.AxisTitle;
import com.vaadin.addon.charts.model.ChartType;
import com.vaadin.addon.charts.model.Configuration;
import com.vaadin.addon.charts.model.DataSeries;
import com.vaadin.addon.charts.model.DataSeriesItem3d;
import com.vaadin.addon.charts.model.Labels;
import com.vaadin.addon.charts.model.Marker;
import com.vaadin.addon.charts.model.PlotOptionsBubble;
import com.vaadin.addon.charts.model.TickPosition;
import com.vaadin.addon.charts.model.XAxis;
import com.vaadin.addon.charts.model.YAxis;
import com.vaadin.addon.charts.model.style.GradientColor;
import com.vaadin.addon.charts.model.style.SolidColor;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.VerticalLayout;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by fefendi on 2016-08-11.
 */
public class BubbleChartExample extends AbstractChartExample {
    private static final long serialVersionUID = 1L;

    private static final transient Logger logger = LogManager.getLogger(BubbleChartExample.class);

    String context;

    public void init(String context) {
        this.context = context;
        setCompositionRoot(new com.vaadin.ui.Label("Uninitialized"));
    }

    @Override
    public void attach() {
        VerticalLayout layout = new VerticalLayout();
        if ("bubble".equals(context))
            bubblechart(layout);
        else
            setCompositionRoot(new com.vaadin.ui.Label("Invalid Context"));
        setCompositionRoot(layout);
    }

    // BEGIN-EXAMPLE: charts.charttype.bubble
    void bubblechart(VerticalLayout layout) {
        // Create a bubble chart
        Chart chart = new Chart(ChartType.BUBBLE);
        chart.setWidth("640px");
        chart.setHeight("350px");
        Configuration conf = chart.getConfiguration();
        conf.setTitle("Distribution of Users by Country");
        conf.getLegend().setEnabled(false); // Disable legend
        conf.getTooltip().setFormatter("this.point.name + ': ' + " +
                "Math.round(this.point.z * 100000) + " +
                "' profiles'");
        // World map as background
        String url =
                VaadinServlet.getCurrent().getServletContext().getContextPath() +
                        "/VAADIN/themes/elasticsearch-vaadin-demo/img/neocreo_Blue_World_Map_640x.png";
        conf.getChart().setPlotBackgroundImage(url);
        // Show more bubbly bubbles with spherical color gradient
        PlotOptionsBubble plotOptions = new PlotOptionsBubble();
        Marker marker = new Marker();
        GradientColor color = GradientColor.createRadial(0.4, 0.3, 0.7);
        color.addColorStop(0.0, new SolidColor(255, 255, 255, 0.5));
        color.addColorStop(1.0, new SolidColor(170, 70, 67, 0.5));
        marker.setFillColor(color);
        plotOptions.setMarker(marker);
        conf.setPlotOptions(plotOptions);
        DataSeries series = new DataSeries("Countries");
        List<Bucket> buckets = searchCountries();
        List<Object[]> dataset = new ArrayList<>();
        for (Bucket bucket : buckets) {
            String country = bucket.getKeyAsString();
            long count = bucket.getDocCount();
            Coordinate pos = getCountryCoordinates(country);
            if (pos == null) logger.warn("country coordinates not found: {}", country);
            else dataset.add(new Object[]{country, count});
        }
        for (Object[] country : dataset) {
            String name = (String) country[0];
            long amount = (long) country[1];
            Coordinate pos = getCountryCoordinates(name);
            DataSeriesItem3d item = new DataSeriesItem3d();
            item.setX(pos.longitude * Math.cos(pos.latitude / 2.0 * (Math.PI / 160)));
            item.setY(pos.latitude * 1.2);
            item.setZ(((double) amount) / 100000);
            item.setName(name);
            series.add(item);
        }
        conf.addSeries(series);
        // Set the category labels on the axis correspondingly
        XAxis xaxis = new XAxis();
        xaxis.setTitle(new AxisTitle(null));
        xaxis.setExtremes(-180, 180);
        xaxis.setLabels(new Labels(false));
        xaxis.setLineWidth(0);
        xaxis.setLineColor(new SolidColor(0, 0, 0, 0.0)); // Invisible
        conf.addxAxis(xaxis);
        // Set the Y axis title
        YAxis yaxis = new YAxis();
        yaxis.setTitle("");
        yaxis.setExtremes(-90, 90);
        yaxis.setTickPosition(TickPosition.OUTSIDE);
        yaxis.setTickLength(1);
        yaxis.setTickInterval(100);
        yaxis.setTickColor(new SolidColor(0, 0, 0, 0.5)); // Invisible
        yaxis.setTickWidth(0);
        yaxis.setLabels(new Labels(false));
        yaxis.setLineWidth(0);
        yaxis.setLineColor(new SolidColor(0, 0, 0, 0.0)); // Invisible
        conf.addyAxis(yaxis);
        layout.addComponent(chart);<commit_msg>trivial improvement<commit_after>/*
 * Copyright 2016 <NAME> <<EMAIL>>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package ca.fe.examples.charts;

import com.vaadin.addon.charts.Chart;
import com.vaadin.addon.charts.model.AxisTitle;
import com.vaadin.addon.charts.model.ChartType;
import com.vaadin.addon.charts.model.Configuration;
import com.vaadin.addon.charts.model.DataSeries;
import com.vaadin.addon.charts.model.DataSeriesItem3d;
import com.vaadin.addon.charts.model.Labels;
import com.vaadin.addon.charts.model.Marker;
import com.vaadin.addon.charts.model.PlotOptionsBubble;
import com.vaadin.addon.charts.model.TickPosition;
import com.vaadin.addon.charts.model.XAxis;
import com.vaadin.addon.charts.model.YAxis;
import com.vaadin.addon.charts.model.style.GradientColor;
import com.vaadin.addon.charts.model.style.SolidColor;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.VerticalLayout;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by fefendi on 2016-08-11.
 */
public class BubbleChartExample extends AbstractChartExample {
    private static final long serialVersionUID = 1L;

    private static final transient Logger logger = LogManager.getLogger(BubbleChartExample.class);

    String context;

    public void init(String context) {
        this.context = context;
        setCompositionRoot(new com.vaadin.ui.Label("Uninitialized"));
    }

    @Override
    public void attach() {
        VerticalLayout layout = new VerticalLayout();
        if ("bubble".equals(context))
            bubblechart(layout);
        else
            setCompositionRoot(new com.vaadin.ui.Label("Invalid Context"));
        setCompositionRoot(layout);
    }

    // BEGIN-EXAMPLE: charts.charttype.bubble
    void bubblechart(VerticalLayout layout) {
        // Create a bubble chart
        Chart chart = new Chart(ChartType.BUBBLE);
        chart.setWidth("640px");
        chart.setHeight("350px");
        Configuration conf = chart.getConfiguration();
        conf.setTitle("Distribution of Users by Country");
        conf.getLegend().setEnabled(false);
        conf.getTooltip().setFormatter("this.point.name + ': ' + Math.round(this.point.z * this.point.z) + ' profiles'");
        // World map as background
        String url =
                VaadinServlet.getCurrent().getServletContext().getContextPath() +
                        "/VAADIN/themes/elasticsearch-vaadin-demo/img/neocreo_Blue_World_Map_640x.png";
        conf.getChart().setPlotBackgroundImage(url);
        // Show more bubbly bubbles with spherical color gradient
        PlotOptionsBubble plotOptions = new PlotOptionsBubble();
        Marker marker = new Marker();
        GradientColor color = GradientColor.createRadial(0.4, 0.3, 0.7);
        color.addColorStop(0.0, new SolidColor(255, 255, 255, 0.5));
        color.addColorStop(1.0, new SolidColor(170, 70, 67, 0.5));
        marker.setFillColor(color);
        plotOptions.setMarker(marker);
        conf.setPlotOptions(plotOptions);
        DataSeries series = new DataSeries("Countries");
        List<Bucket> buckets = searchCountries();
        for (Bucket bucket : buckets) {
            String country = bucket.getKeyAsString();
            long count = bucket.getDocCount();
            Coordinate pos = getCountryCoordinates(country);
            if (pos == null) {
                logger.warn("country coordinates not found: {}", country);
                continue;
            }
            DataSeriesItem3d item = new DataSeriesItem3d();
            item.setX(pos.longitude * Math.cos(pos.latitude / 2.0 * (Math.PI / 160)));
            item.setY(pos.latitude * 1.2);
            item.setZ(Math.sqrt(count));
            item.setName(country);
            series.add(item);
        }
        conf.addSeries(series);
        // Set the category labels on the axis correspondingly
        XAxis xaxis = new XAxis();
        xaxis.setTitle(new AxisTitle(null));
        xaxis.setExtremes(-180, 180);
        xaxis.setLabels(new Labels(false));
        xaxis.setLineWidth(0);
        xaxis.setLineColor(new SolidColor(0, 0, 0, 0.0)); // Invisible
        conf.addxAxis(xaxis);
        // Set the Y axis title
        YAxis yaxis = new YAxis();
        yaxis.setTitle("");
        yaxis.setExtremes(-90, 90);
        yaxis.setTickPosition(TickPosition.OUTSIDE);
        yaxis.setTickLength(1);
        yaxis.setTickInterval(100);
        yaxis.setTickColor(new SolidColor(0, 0, 0, 0.5)); // Invisible
        yaxis.setTickWidth(0);
        yaxis.setLabels(new Labels(false));
        yaxis.setLineWidth(0);
        yaxis.setLineColor(new SolidColor(0, 0, 0, 0.0)); // Invisible
        conf.addyAxis(yaxis);
        layout.addComponent(chart);
{'fraction_non_alphanumeric': 0.11352534950311605, 'fraction_numerical': 0.0180225703217113, 'mean_word_length': 3.0391156462585034, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3225464', 'n_tokens_mistral': 3680, 'n_tokens_neox': 3418, 'n_words': 760}