Example #1
0
  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;
  }
Example #2
0
  @Test
  @Ignore("Phantomjs not installed on our build server")
  public void testWide() throws InterruptedException, URISyntaxException {

    Configuration conf = new Configuration();
    conf.getChart().setType(ChartType.COLUMN);
    conf.getChart().setMarginRight(200);
    Legend legend = conf.getLegend();
    legend.setLayout(LayoutDirection.VERTICAL);
    legend.setHorizontalAlign(HorizontalAlign.RIGHT);
    legend.setVerticalAlign(VerticalAlign.MIDDLE);
    legend.setBorderWidth(0);

    Random r = new Random();

    for (int i = 0; i < 20; i++) {
      String name = RandomStringUtils.randomAlphabetic(r.nextInt(20));
      DataSeries dataSeries = new DataSeries(name);
      dataSeries.add(new DataSeriesItem(name, r.nextInt(100)));
      conf.addSeries(dataSeries);
    }

    SVGGenerator instance = SVGGenerator.getInstance();
    String generatedSVG = instance.generate(conf, 1200, 400);

    Assert.assertTrue(generatedSVG.contains("width=\"1200\""));
    Assert.assertTrue(generatedSVG.contains("height=\"400\""));

    SVGGenerator.getInstance().destroy();
  }
Example #3
0
 @Test
 public void testSetMetrics() throws Exception {
   IndicatorsModel model = new IndicatorsModel();
   model.setTimingMetricsMap(timingMetricsMap);
   ChartManager.setMetrics(chart, model);
   List<Series> series = conf.getSeries();
   assertEquals(2, series.size());
   DataSeries dataSeries1 = (DataSeries) series.get(0);
   DataSeries dataSeries2 = (DataSeries) series.get(1);
   assertEquals(METRIC1.getDescription(), dataSeries1.getName());
   assertEquals(METRIC2.getDescription(), dataSeries2.getName());
   DataSeriesItem item1 = dataSeries1.get(1);
   assertEquals(metricItem.getT95(), item1.getY());
   assertEquals(DATE_TIME2, new LocalDateTime(item1.getX(), DateTimeZone.UTC));
 }
Example #4
0
  @Override
  protected Component getChart() {
    chart = new Chart();

    chart.getConfiguration().setTitle("Box Plot Example");

    Legend legend = new Legend();
    legend.setEnabled(false);
    chart.getConfiguration().setLegend(legend);

    XAxis xaxis = chart.getConfiguration().getxAxis();
    xaxis.setTitle("Experiment No.");
    xaxis.setCategories("1", "2", "3", "4", "5");

    YAxis yAxis = chart.getConfiguration().getyAxis();

    yAxis.setTitle("Observations");
    PlotLine plotLine = new PlotLine();
    plotLine.setColor(new SolidColor("red"));
    plotLine.setValue(932);
    plotLine.setWidth(1);
    PlotBandLabel label = new PlotBandLabel("Theoretical mean: 932");
    label.setAlign(HorizontalAlign.CENTER);
    Style style = new Style();
    style.setColor(new SolidColor("gray"));
    label.setStyle(style);
    plotLine.setLabel(label);
    yAxis.setPlotLines(plotLine);

    observations = new DataSeries();
    observations.setName("Observations");

    // Add PlotBoxItems contain all fields relevant for plot box chart
    observations.add(new BoxPlotItem(760, 801, 848, 895, 965));

    // Example with no arg constructor
    BoxPlotItem plotBoxItem = new BoxPlotItem();
    plotBoxItem.setLow(733);
    plotBoxItem.setLowerQuartile(853);
    plotBoxItem.setMedian(939);
    plotBoxItem.setUpperQuartile(980);
    plotBoxItem.setHigh(1080);
    observations.add(plotBoxItem);

    observations.add(new BoxPlotItem(714, 762, 817, 870, 918));
    observations.add(new BoxPlotItem(724, 802, 806, 871, 950));
    observations.add(new BoxPlotItem(834, 836, 864, 882, 910));
    observations.setPlotOptions(getPlotBoxOptions());
    chart.getConfiguration().addSeries(observations);

    return chart;
  }
Example #5
0
  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);
    }
  }
  @Override
  protected Component getChart() {
    final Chart chart = new Chart(ChartType.COLUMN);
    chart.setId("chart");

    final Configuration conf = chart.getConfiguration();

    conf.setTitle("Browser market share, April, 2011");
    conf.setSubTitle("Click the columns to view versions. Click again to view brands.");
    conf.getLegend().setEnabled(false);

    XAxis x = new XAxis();
    x.setType(AxisType.CATEGORY);
    conf.addxAxis(x);

    YAxis y = new YAxis();
    y.setTitle("Total percent market share");
    conf.addyAxis(y);

    PlotOptionsColumn column = new PlotOptionsColumn();
    column.setCursor(Cursor.POINTER);
    column.setDataLabels(new Labels(true));
    column.getDataLabels().setFormatter("this.y +'%'");

    conf.setPlotOptions(column);

    Tooltip tooltip = new Tooltip();
    tooltip.setHeaderFormat("<span style=\"font-size:11px\">{series.name}</span><br>");
    tooltip.setPointFormat(
        "<span style=\"color:{point.color}\">{point.name}</span>: <b>{point.y:.2f}%</b> of total<br/>");
    conf.setTooltip(tooltip);

    DataSeries series = new DataSeries();
    series.setName("Browser brands");
    PlotOptionsColumn plotOptionsColumn = new PlotOptionsColumn();
    plotOptionsColumn.setColorByPoint(true);
    series.setPlotOptions(plotOptionsColumn);

    DataSeriesItem item = new DataSeriesItem("MSIE", 55.11);
    DataSeries drillSeries = new DataSeries("MSIE versions");
    drillSeries.setId("MSIE");
    String[] categories = new String[] {"MSIE 6.0", "MSIE 7.0", "MSIE 8.0", "MSIE 9.0"};
    Number[] ys = new Number[] {10.85, 7.35, 33.06, 2.81};
    drillSeries.setData(categories, ys);
    series.addItemWithDrilldown(item, drillSeries);

    item = new DataSeriesItem("Firefox", 21.63);
    drillSeries = new DataSeries("Firefox versions");
    drillSeries.setId("Firefox");
    categories =
        new String[] {"Firefox 2.0", "Firefox 3.0", "Firefox 3.5", "Firefox 3.6", "Firefox 4.0"};
    ys = new Number[] {0.20, 0.83, 1.58, 13.12, 5.43};
    drillSeries.setData(categories, ys);
    series.addItemWithDrilldown(item, drillSeries);

    item = new DataSeriesItem("Chrome", 11.94);
    drillSeries = new DataSeries("Chrome versions");
    drillSeries.setId("Chrome");
    categories =
        new String[] {
          "Chrome 5.0",
          "Chrome 6.0",
          "Chrome 7.0",
          "Chrome 8.0",
          "Chrome 9.0",
          "Chrome 10.0",
          "Chrome 11.0",
          "Chrome 12.0"
        };
    ys = new Number[] {0.12, 0.19, 0.12, 0.36, 0.32, 9.91, 0.50, 0.22};
    drillSeries.setData(categories, ys);
    series.addItemWithDrilldown(item, drillSeries);

    item = new DataSeriesItem("Safari", 7.15);
    drillSeries = new DataSeries("Safari versions");
    drillSeries.setId("Safari");
    categories =
        new String[] {
          "Safari 5.0",
          "Safari 4.0",
          "Safari Win 5.0",
          "Safari 4.1",
          "Safari/Maxthon",
          "Safari 3.1",
          "Safari 4.1"
        };
    ys = new Number[] {4.55, 1.42, 0.23, 0.21, 0.20, 0.19, 0.14};
    drillSeries.setData(categories, ys);
    series.addItemWithDrilldown(item, drillSeries);

    item = new DataSeriesItem("Opera", 2.14);
    drillSeries = new DataSeries("Opera versions");
    drillSeries.setId("Opera");
    categories = new String[] {"Opera 9.x", "Opera 10.x", "Opera 11.x"};
    ys = new Number[] {0.12, 0.37, 1.65};
    drillSeries.setData(categories, ys);
    series.addItemWithDrilldown(item, drillSeries);
    conf.addSeries(series);

    return chart;
  }
  @Override
  protected Component getChart() {
    VerticalLayout layout = new VerticalLayout();
    final Chart chart = new Chart(ChartType.COLUMN);
    chart.setId("chart");

    conf = chart.getConfiguration();

    conf.setTitle("Global happiness index");
    conf.setSubTitle("Source: www.happyplanetindex.org");
    conf.getLegend().setEnabled(false);

    XAxis x = new XAxis();
    // FIXME remove toString() once enums are used in model (CHARTS-159)
    x.setType(AxisType.CATEGORY.toString());
    conf.addxAxis(x);

    YAxis y = new YAxis();
    y.setTitle(new Title("Total"));
    conf.addyAxis(y);

    PlotOptionsColumn column = new PlotOptionsColumn();
    column.setCursor(Cursor.POINTER.toString());
    column.setDataLabels(new DataLabels(true));

    conf.setPlotOptions(column);

    DataSeries series = new DataSeries();
    series.setName("Regions");
    PlotOptionsColumn plotOptionsColumn = new PlotOptionsColumn();
    plotOptionsColumn.setColorByPoint(true);
    series.setPlotOptions(plotOptionsColumn);

    DataSeriesItem item = new DataSeriesItem("Latin America and Carribean", 60);
    item.setId("Latin America and Carribean");
    series.addItemWithDrilldown(item);

    item = new DataSeriesItem("Western Nations", 50);
    item.setId("Western Nations");
    series.addItemWithDrilldown(item);

    conf.addSeries(series);

    drillSeries = new HashMap<String, DataSeries>();

    DataSeries drill = new DataSeries("Latin America and Carribean Countries");

    item = new DataSeriesItem("Costa Rica", 64);
    item.setId("Costa Rica");
    drill.addItemWithDrilldown(item);

    item = new DataSeriesItem("Colombia", 59.8);
    item.setId("Colombia");
    drill.addItemWithDrilldown(item);

    item = new DataSeriesItem("Belize", 59.3);
    item.setId("Belize");
    drill.addItemWithDrilldown(item);

    drillSeries.put("Latin America and Carribean", drill);

    drill = new DataSeries("Western Nations Countries");

    item = new DataSeriesItem("New Zealand", 51.6);
    item.setId("New Zealand");
    drill.addItemWithDrilldown(item);

    item = new DataSeriesItem("Norway", 51.4);
    item.setId("Norway");
    drill.addItemWithDrilldown(item);

    item = new DataSeriesItem("Switzerland", 50.3);
    item.setId("Switzerland");
    drill.addItemWithDrilldown(item);

    drillSeries.put("Western Nations", drill);

    drill = new DataSeries("Details Costa Rica");
    drill.setId("Details Costa Rica");
    String[] categories =
        new String[] {"Life Expectancy", "Well-being (0-10)", "Footprint (gha/capita)"};
    Number[] ys = new Number[] {79.3, 7.3, 2.5};
    drill.setData(categories, ys);
    drillSeries.put("Costa Rica", drill);

    drill = new DataSeries("Details Colombia");
    drill.setId("Details Colombia");
    ys = new Number[] {73.7, 6.4, 1.8};
    drill.setData(categories, ys);
    drillSeries.put("Colombia", drill);

    drill = new DataSeries("Details Belize");
    drill.setId("Details Belize");
    ys = new Number[] {76.1, 6.5, 2.1};
    drill.setData(categories, ys);
    drillSeries.put("Belize", drill);

    drill = new DataSeries("Details New Zealand");
    drill.setId("Details New Zealand");
    ys = new Number[] {80.7, 7.2, 4.3};

    drill.setData(categories, ys);
    drillSeries.put("New Zealand", drill);

    drill = new DataSeries("Details Norway");
    drill.setId("Details Norway");
    ys = new Number[] {81.1, 7.6, 4.8};
    drill.setData(categories, ys);
    drillSeries.put("Norway", drill);

    drill = new DataSeries("Details Switzerland");
    drill.setId("Details Switzerland");
    ys = new Number[] {82.3, 7.5, 5.0};
    drill.setData(categories, ys);
    drillSeries.put("Switzerland", drill);

    chart.setDrilldownCallback(
        new DrilldownCallback() {

          @Override
          public Series handleDrilldown(DrilldownEvent event) {
            log("DrilldownEvent: " + event.getItem().getId());
            return getPointDrilldown(event.getItem());
          }
        });

    chart.addPointClickListener(
        new PointClickListener() {

          @Override
          public void onClick(PointClickEvent event) {
            log(
                "PointClickEvent: "
                    + event.getSeries().getName()
                    + " index :"
                    + event.getPointIndex());
          }
        });

    chart.addChartDrillupListener(
        new ChartDrillupListener() {

          @Override
          public void onDrillup(ChartDrillupEvent event) {
            log("ChartDrillupEvent");
          }
        });

    layout.addComponent(chart);
    layout.addComponent(log);
    return layout;
  }