Search in sources :

Example 21 with DataSeriesItem

use of com.vaadin.addon.charts.model.DataSeriesItem in project charts by vaadin.

the class PieSite method getChart.

@Override
protected Component getChart() {
    Chart chart = new Chart(ChartType.PIE);
    Configuration conf = chart.getConfiguration();
    conf.setTitle("Revenue by industry");
    conf.setSubTitle("2015");
    Tooltip tooltip = conf.getTooltip();
    tooltip.setPointFormat("<b>{point.percentage:.1f}%</b>");
    PlotOptionsPie plotOptions = new PlotOptionsPie();
    plotOptions.setAllowPointSelect(true);
    plotOptions.setCursor(Cursor.POINTER);
    plotOptions.setShowInLegend(true);
    DataLabels dataLabels = plotOptions.getDataLabels();
    dataLabels.setEnabled(true);
    dataLabels.setFormat("{point.name}: {point.y:.1f} M€");
    conf.setPlotOptions(plotOptions);
    DataSeries series = new DataSeries("Revenue");
    series.add(new DataSeriesItem("Aerospace", 90.0));
    series.add(new DataSeriesItem("Medical", 53.6));
    series.add(new DataSeriesItem("Agriculture", 25.6));
    series.add(new DataSeriesItem("Automotive", 17.0));
    series.add(new DataSeriesItem("Consumers", 12.4));
    series.add(new DataSeriesItem("Subsidies", 1.4));
    conf.setSeries(series);
    return chart;
}
Also used : PlotOptionsPie(com.vaadin.addon.charts.model.PlotOptionsPie) DataLabels(com.vaadin.addon.charts.model.DataLabels) Configuration(com.vaadin.addon.charts.model.Configuration) Tooltip(com.vaadin.addon.charts.model.Tooltip) DataSeries(com.vaadin.addon.charts.model.DataSeries) Chart(com.vaadin.addon.charts.Chart) DataSeriesItem(com.vaadin.addon.charts.model.DataSeriesItem)

Example 22 with DataSeriesItem

use of com.vaadin.addon.charts.model.DataSeriesItem in project charts by vaadin.

the class LargeDataSet method getChart.

@Override
protected Component getChart() {
    Chart chart = new Chart();
    chart.getConfiguration().setTitle("Large data set");
    // Force zoom and enable scrollbar
    chart.getConfiguration().getxAxis().setMin(5000);
    chart.getConfiguration().getScrollbar().setEnabled(true);
    DataSeries series = new DataSeries();
    Number[] data = TimeSeriesZoomable.USD_TO_EUR_EXCHANGE_RATES;
    Random r = new Random(0);
    int x = 0;
    for (int j = 0; j < ROUNDS; j++) {
        for (Number number : data) {
            DataSeriesItem item;
            if (xyPairs()) {
                x += r.nextInt(4);
                item = new DataSeriesItem(x, number);
            } else {
                // interval data (x == index of data point), performs better
                // and consumes less memory/bandwidth
                item = new DataSeriesItem();
                item.setY(number);
            }
            /*
                 * Note, with large datasets, avoid settings like
                 * item.setColor(myColorX), or item.setName(myName) for data
                 * items. Without them the framework is able to optimize the
                 * rendering and can survive from rather large datasets.
                 * 
                 * Also note, that if data set is very large, the library might
                 * ignore it if there are custom settings for data items. This
                 * threshold is called turboThreshHold in plot options. See
                 * example below.
                 */
            // item.setName("x " + x);
            series.add(item);
        }
    }
    /*
         * If you play with this example your might try uncomment next line to
         * keep the amount of transfered data sane. You might also try to
         * increase the ROUNDS to increase the input data. By default commented
         * out to stress client side performance
         */
    // reduce(series, 1000);
    /*
         * If data can contain high/low peaks, one can use more sophisticated
         * algorithms
         */
    // ramerDouglasPeuckerReduce(series, 300);
    chart.getConfiguration().setSeries(series);
    PlotOptionsLine plotOptionsLine = new PlotOptionsLine();
    // Showing points with thousands of data items looks odd (on top of each
    // other), also renders faster without markers
    plotOptionsLine.setMarker(new Marker(false));
    // To render shadow, library must create additional element, without it
    // performance will be better
    plotOptionsLine.setShadow(false);
    plotOptionsLine.setAnimation(false);
    /*
         * If developers need to use large data sets and point specific
         * settings, they can override the default turbo threshold. Here we set
         * it to 200000 (default 1000). Turbo threshold is configuration that
         * works as a "sanity threshold" so that old browsers wont drop to their
         * knees under load. Without this Vaadin Charts might not render chart
         * if data items have e.g. name set.
         */
    plotOptionsLine.setTurboThreshold(200000);
    chart.getConfiguration().setPlotOptions(plotOptionsLine);
    return chart;
}
Also used : Random(java.util.Random) PlotOptionsLine(com.vaadin.addon.charts.model.PlotOptionsLine) DataSeries(com.vaadin.addon.charts.model.DataSeries) Marker(com.vaadin.addon.charts.model.Marker) Chart(com.vaadin.addon.charts.Chart) DataSeriesItem(com.vaadin.addon.charts.model.DataSeriesItem)

Example 23 with DataSeriesItem

use of com.vaadin.addon.charts.model.DataSeriesItem in project charts by vaadin.

the class LargeDataSet method ramerDouglasPeuckerReduce.

/**
 * Reduce data series with bit more sophisticated method.
 *
 * @param series
 *            with x y data pairs
 * @param pixels
 */
public static void ramerDouglasPeuckerReduce(DataSeries series, int pixels) {
    // Calculate rough estimate for visual ratio on x-y, might be bad guess
    // if axis have been set manually
    DataSeriesItem dataSeriesItem = series.get(0);
    double xMax = dataSeriesItem.getX().doubleValue();
    double xMin = xMax;
    double yMax = dataSeriesItem.getY().doubleValue();
    double yMin = yMax;
    for (int i = 1; i < series.size(); i++) {
        DataSeriesItem item = series.get(i);
        double x = item.getX().doubleValue();
        if (xMax < x) {
            xMax = x;
        }
        if (xMin > x) {
            xMin = x;
        }
        double y = item.getY().doubleValue();
        if (yMax < y) {
            yMax = y;
        }
        if (yMin > y) {
            yMin = y;
        }
    }
    double xyRatio = (xMax - xMin) / (yMax - yMin);
    // rough estimate for sane epsilon (1px)
    double epsilon = (xMax - xMin) / pixels;
    List<DataSeriesItem> rawData = series.getData();
    List<DataSeriesItem> reduced = ramerDouglasPeucker(rawData, epsilon, xyRatio);
    series.setData(reduced);
}
Also used : DataSeriesItem(com.vaadin.addon.charts.model.DataSeriesItem)

Example 24 with DataSeriesItem

use of com.vaadin.addon.charts.model.DataSeriesItem in project charts by vaadin.

the class LargeDataSet method simpleReduce.

/**
 * An example how dataset can be reduced simply on the server side.
 *
 * <p>
 * Using more datapoints than there are pixels on the devices makes usually
 * no sense. Excess data just consumes bandwidth and client side CPU. This
 * method reduces a large dataset with a very simple method. If you scale up
 * data set by modifying the ROUNDS in this example and toggle passing it
 * through this method, you'll see that visually the chart don't change
 * much, but the amount of data and with huge amounts also the rendering
 * time drops dramatically.
 *
 * @param series
 *            the series to be reduced
 * @param pixels
 *            the pixel size for which the data should be adjusted
 */
public static void simpleReduce(DataSeries series, int pixels) {
    DataSeriesItem first = series.get(0);
    DataSeriesItem last = series.get(series.size() - 1);
    ArrayList<DataSeriesItem> reducedDataSet = new ArrayList<DataSeriesItem>();
    if (first.getX() != null) {
        // xy pairs
        double startX = first.getX().doubleValue();
        double endX = last.getX().doubleValue();
        double minDistance = (endX - startX) / pixels;
        reducedDataSet.add(first);
        double lastPoint = first.getX().doubleValue();
        for (int i = 0; i < series.size(); i++) {
            DataSeriesItem item = series.get(i);
            if (item.getX().doubleValue() - lastPoint > minDistance) {
                reducedDataSet.add(item);
                lastPoint = item.getX().doubleValue();
            }
        }
        series.setData(reducedDataSet);
    } else {
        // interval data
        int k = series.size() / pixels;
        if (k > 1) {
            for (int i = 0; i < series.size(); i++) {
                if (i % k == 0) {
                    DataSeriesItem item = series.get(i);
                    reducedDataSet.add(item);
                }
            }
            series.setData(reducedDataSet);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) DataSeriesItem(com.vaadin.addon.charts.model.DataSeriesItem)

Example 25 with DataSeriesItem

use of com.vaadin.addon.charts.model.DataSeriesItem in project charts by vaadin.

the class LargeDataSet method ramerDouglasPeucker.

/**
 * @see http://en.wikipedia.org/wiki/Ramer–Douglas–Peucker_algorithm
 *
 * @param points
 * @param epsilon
 * @param xyRatio
 *            y values are multiplied with this to make distance calculation
 *            in algorithm sane
 * @return
 */
public static List<DataSeriesItem> ramerDouglasPeucker(List<DataSeriesItem> points, double epsilon, final double xyRatio) {
    // Find the point with the maximum distance
    double dmax = 0;
    int index = 0;
    DataSeriesItem start = points.get(0);
    DataSeriesItem end = points.get(points.size() - 1);
    for (int i = 1; i < points.size() - 1; i++) {
        DataSeriesItem point = points.get(i);
        double d = pointToLineDistance(start, end, point, xyRatio);
        if (d > dmax) {
            index = i;
            dmax = d;
        }
    }
    ArrayList<DataSeriesItem> reduced = new ArrayList<DataSeriesItem>();
    if (dmax >= epsilon) {
        // max distance is greater than epsilon, keep the most relevant
        // point and recursively simplify
        List<DataSeriesItem> startToRelevant = ramerDouglasPeucker(points.subList(0, index + 1), epsilon, xyRatio);
        reduced.addAll(startToRelevant);
        List<DataSeriesItem> relevantToEnd = ramerDouglasPeucker(points.subList(index, points.size() - 1), epsilon, xyRatio);
        reduced.addAll(relevantToEnd.subList(1, relevantToEnd.size()));
    } else {
        // no relevant points, drop all but ends
        reduced.add(start);
        reduced.add(end);
    }
    return reduced;
}
Also used : ArrayList(java.util.ArrayList) DataSeriesItem(com.vaadin.addon.charts.model.DataSeriesItem)

Aggregations

DataSeriesItem (com.vaadin.addon.charts.model.DataSeriesItem)88 DataSeries (com.vaadin.addon.charts.model.DataSeries)81 Chart (com.vaadin.addon.charts.Chart)57 Configuration (com.vaadin.addon.charts.model.Configuration)51 YAxis (com.vaadin.addon.charts.model.YAxis)27 SolidColor (com.vaadin.addon.charts.model.style.SolidColor)19 DataLabels (com.vaadin.addon.charts.model.DataLabels)17 XAxis (com.vaadin.addon.charts.model.XAxis)17 AxisTitle (com.vaadin.addon.charts.model.AxisTitle)12 Random (java.util.Random)12 PlotOptionsPie (com.vaadin.addon.charts.model.PlotOptionsPie)11 Test (org.junit.Test)11 Tooltip (com.vaadin.addon.charts.model.Tooltip)10 Marker (com.vaadin.addon.charts.model.Marker)9 PlotOptionsSpline (com.vaadin.addon.charts.model.PlotOptionsSpline)8 PlotLine (com.vaadin.addon.charts.model.PlotLine)7 PlotOptionsColumn (com.vaadin.addon.charts.model.PlotOptionsColumn)7 StockPrices (com.vaadin.addon.charts.examples.timeline.util.StockPrices)6 ListSeries (com.vaadin.addon.charts.model.ListSeries)6 Style (com.vaadin.addon.charts.model.style.Style)6