Search in sources :

Example 1 with FileDownloader

use of com.vaadin.server.FileDownloader in project charts by vaadin.

the class SVGGeneratorExample method getChart.

@Override
protected Component getChart() {
    final Chart chart;
    chart = new Chart();
    chart.setHeight("450px");
    chart.setWidth("100%");
    Configuration configuration = chart.getConfiguration();
    configuration.getChart().setZoomType(ZoomType.X);
    configuration.getChart().setSpacingRight(20);
    configuration.getTitle().setText("USD to EUR exchange rate from 2006 through 2008");
    String title = Page.getCurrent().getWebBrowser().isTouchDevice() ? "Drag your finger over the plot to zoom in" : "Click and drag in the plot area to zoom in";
    configuration.getSubTitle().setText(title);
    configuration.getxAxis().setType(AxisType.DATETIME);
    configuration.getxAxis().setMinRange(TWO_WEEKS);
    configuration.getxAxis().setTitle(new AxisTitle(""));
    configuration.getLegend().setEnabled(false);
    YAxis yAxis = configuration.getyAxis();
    yAxis.setTitle(new AxisTitle("Exchange rate"));
    yAxis.setMin(0.6);
    yAxis.setStartOnTick(false);
    yAxis.setShowFirstLabel(false);
    configuration.getTooltip().setShared(true);
    PlotOptionsArea plotOptions = new PlotOptionsArea();
    GradientColor fillColor = GradientColor.createLinear(0, 0, 0, 1);
    fillColor.addColorStop(0, SolidColor.AQUA);
    fillColor.addColorStop(1, SolidColor.BLACK);
    plotOptions.setFillColor(fillColor);
    plotOptions.setLineWidth(1);
    plotOptions.setShadow(false);
    Marker marker = new Marker();
    marker.setEnabled(false);
    Hover hoverState = new Hover(true);
    hoverState.setRadius(5);
    States states = new States();
    states.setHover(hoverState);
    plotOptions.setStates(states);
    plotOptions.setMarker(marker);
    plotOptions.setShadow(false);
    configuration.setPlotOptions(plotOptions);
    SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
    ListSeries ls = new ListSeries();
    PlotOptionsArea options = new PlotOptionsArea();
    options.setPointInterval(DAY_IN_MILLIS);
    ls.setPlotOptions(options);
    ls.setName("USD to EUR");
    try {
        options.setPointStart(df.parse("2006/01/02").getTime());
    } catch (ParseException e) {
        e.printStackTrace();
    }
    ls.setData(USD_TO_EUR_EXCHANGE_RATES);
    configuration.setSeries(ls);
    chart.drawChart(configuration);
    Button export = new Button("Export");
    StreamResource.StreamSource svgStreamSource = createSVGStreamSource(chart);
    FileDownloader fileDownloader = new FileDownloader(new StreamResource(svgStreamSource, "chart.svg"));
    fileDownloader.extend(export);
    Button timelineToggle = new Button("Timeline toggle");
    timelineToggle.addClickListener(new Button.ClickListener() {

        boolean timeline = false;

        @Override
        public void buttonClick(Button.ClickEvent event) {
            timeline = !timeline;
            chart.setTimeline(timeline);
        }
    });
    HorizontalLayout controls = new HorizontalLayout(export, timelineToggle);
    controls.setSpacing(true);
    addComponent(controls);
    setSpacing(true);
    return chart;
}
Also used : Configuration(com.vaadin.addon.charts.model.Configuration) GradientColor(com.vaadin.addon.charts.model.style.GradientColor) String(java.lang.String) Marker(com.vaadin.addon.charts.model.Marker) States(com.vaadin.addon.charts.model.States) StreamResource(com.vaadin.server.StreamResource) PlotOptionsArea(com.vaadin.addon.charts.model.PlotOptionsArea) ListSeries(com.vaadin.addon.charts.model.ListSeries) Button(com.vaadin.ui.Button) FileDownloader(com.vaadin.server.FileDownloader) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Override(java.lang.Override) Chart(com.vaadin.addon.charts.Chart) Override(java.lang.Override)

Example 2 with FileDownloader

use of com.vaadin.server.FileDownloader in project charts by vaadin.

the class ChartExportDemo method createExportButton.

private Button createExportButton(String caption, String filename, StreamSource ss) {
    Button b = new Button(caption);
    FileDownloader downloader = new FileDownloader(new StreamResource(ss, filename));
    downloader.extend(b);
    return b;
}
Also used : StreamResource(com.vaadin.server.StreamResource) Button(com.vaadin.ui.Button) FileDownloader(com.vaadin.server.FileDownloader)

Aggregations

FileDownloader (com.vaadin.server.FileDownloader)2 StreamResource (com.vaadin.server.StreamResource)2 Button (com.vaadin.ui.Button)2 Chart (com.vaadin.addon.charts.Chart)1 Configuration (com.vaadin.addon.charts.model.Configuration)1 ListSeries (com.vaadin.addon.charts.model.ListSeries)1 Marker (com.vaadin.addon.charts.model.Marker)1 PlotOptionsArea (com.vaadin.addon.charts.model.PlotOptionsArea)1 States (com.vaadin.addon.charts.model.States)1 GradientColor (com.vaadin.addon.charts.model.style.GradientColor)1 Override (java.lang.Override)1 String (java.lang.String)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1