0

Get Component inner html on server side

asked 2017-04-11 13:27:16 +0800

purser gravatar image purser
1

Sorry for confusion, I am newbie in ZK... I create Highchart on my page with wrapper. Can I get html(svg) contents of this chart on server side, despite it rendered on client by svg engine ? I need to generate a report on server side, including chart, rendered on client side.

//----------------------This is wrapper class--------------------------//

package org.morphos.zkext.components.highcharts;

import org.morphos.zkext.components.highcharts.events.HChartClickEvent; import org.morphos.zkext.components.highcharts.events.HChartEvents; import org.zkoss.zk.au.out.AuInvoke; import org.zkoss.zk.ui.Desktop; import org.zkoss.zk.ui.Page; import org.zkoss.zk.ui.event.Events; import org.zkoss.zul.impl.XulElement;

import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Arrays; import java.util.List;

/** * ZK wrapper for HighCharts component. */ public class HChart extends XulElement implements IMapConverter {

static {
    Util.init();
    addClientEvent(HChart.class, HChartEvents.ON_CLICK, CE_IMPORTANT);
}

private static final long serialVersionUID = 1L;

private static final String GLOBAL_SETTINGS = HChart.class.getName() + ".global";

public static class ChartSettings extends Options {

    public final ChartOptions chart = new ChartOptions();

    public final List<String> colors = new ArrayList<String>();

    public final CreditsOptions credits = new CreditsOptions();

    public final ExportingOptions exporting = new ExportingOptions();

    public final LegendOptions legend = new LegendOptions();

    public final LoadingOptions loading = new LoadingOptions();

    public final NavigationOptions navigation = new NavigationOptions();

    public final PaneOptions pane = new PaneOptions();

    public final PlotOptions plotOptions = null;

    public final List<Series> series = new ArrayList<Series>();

    public final TitleOptions subtitle = new TitleOptions();

    public final TitleOptions title = new TitleOptions();

    public final TooltipOptions tooltip = new TooltipOptions();

    public final List<Axis> xAxis = new ArrayList<Axis>();

    public final List<Axis> yAxis = new ArrayList<Axis>();

}

public final ChartSettings options = new ChartSettings();

private boolean _running;

public HChart() {
    super();
    addXAxis();
    addYAxis();
    setType(PlotType.LINE);
}


public void setDefaultColors(String... colors) {
    options.colors.clear();

    if (colors != null) {
        options.colors.addAll(Arrays.asList(colors));
    }
}


public String getType() {
    return options.chart.type;
}


public void setType(PlotType type) {
    try {
        Field field = ChartSettings.class.getField("plotOptions");
        field.setAccessible(true);
        PlotOptions plotOptions = Util.getPlotType(type.getValue());
        plotOptions.type = type.getValue();
        field.set(options, plotOptions);
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    options.chart.type = type.getValue();
    options.series.clear();
}


public Axis getXAxis() {
    return options.xAxis.isEmpty() ? null : options.xAxis.get(0);
}


public Axis getYAxis() {
    return options.yAxis.isEmpty() ? null : options.yAxis.get(0);
}


public Series addSeries() {
    return addSeries(PlotType.valueOf(options.chart.type));
}


public Series addSeries(PlotType type) {
    Series series = new Series(type.getValue());
    options.series.add(series);
    return series;
}


public Axis addXAxis() {
    return new Axis(options.xAxis);
}


public Axis addYAxis() {
    return new Axis(options.yAxis);
}


public void run() {
    init();
    invoke("_run", toMap());
    _running = true;
}


public boolean isRunning() {
    return _running;
}


public void clear() {
    _running = false;
    options.series.clear();
    invoke("_clear", null);
}


public void redraw() {
    if (_running) {
        invoke("_redraw", null);
    } else {
        run();
    }
}


private void init() {
    if (shouldInitialize()) {
        invoke("_init", new GlobalSettings().toMap());
    }
}


private boolean shouldInitialize() {
    Desktop dt = getDesktop();

    if (dt != null && !dt.hasAttribute(GLOBAL_SETTINGS)) {
        dt.setAttribute(GLOBAL_SETTINGS, true);
        return true;
    }

    return false;
}


@Override
public OptionsMap toMap() {
    options.chart.renderTo = getUuid();
    return options.toMap();
}


public String getTitle() {
    return options.title.text;
}


public void setTitle(String text) {
    options.title.text = text;
    updateTitle();
}


public String getSubtitle() {
    return options.subtitle.text;
}


public void setSubtitle(String text) {
    options.subtitle.text = text;
    updateTitle();
}


public void export() {
    ensureRunning("Exporting");
    invokeJS("_export", options.exporting.buttons_exportButton.onclick);
}


public void print() {
    ensureRunning("Printing");
    invokeJS("_print", options.exporting.buttons_printButton.onclick);
}


private void invokeJS(String func, String js) {
    invoke(func, Util.toJS(js));
}


private void invoke(String func, Object arg) {
    response(new AuInvoke(this, func, arg));
}


private void updateTitle() {
    if (_running) {
        OptionsMap map = new OptionsMap();
        map.put("title", options.title);
        map.put("subtitle", options.subtitle);
        invoke("_title", map);
    }
}


private void ensureRunning(String operation) {
    if (!_running) {
        throw new RuntimeException(operation + " requires an active chart.");
    }
}

@Override
public String getWidgetClass() {
    return "morphosext.Hchart";
}

@Override
public void onPageDetached(Page page) {
    _running = false;
    super.onPageDetached(page);
}

@Override
public void service(org.zkoss.zk.au.AuRequest request, boolean everError) {
    final String cmd = request.getCommand();
    if (cmd.equals(HChartEvents.ON_CLICK)) {
        HChartClickEvent event = HChartClickEvent.getHChartClickEvent(request);
        //Events.postEvent(HChartEvents.ON_CLICK, this, event);
        Events.postEvent(event);
    } else
        super.service(request, everError);
}

}

delete flag offensive retag edit
Be the first one to answer this question!
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2017-04-11 13:27:16 +0800

Seen: 18 times

Last updated: Apr 11 '17

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More