|
| 1 | +package sample.client.examples; |
| 2 | + |
| 3 | +import com.github.timeu.dygraphsgwt.client.Dygraphs; |
| 4 | +import com.github.timeu.dygraphsgwt.client.DygraphsOptions; |
| 5 | +import com.github.timeu.dygraphsgwt.client.callbacks.LegendFormatterCallback; |
| 6 | +import com.github.timeu.dygraphsgwt.client.callbacks.LegendFormatterData; |
| 7 | +import com.github.timeu.dygraphsgwt.client.callbacks.LegendFormatterSeries; |
| 8 | +import com.github.timeu.dygraphsgwt.client.options.HighlightSeriesOptions; |
| 9 | +import com.google.gwt.dom.client.DivElement; |
| 10 | +import com.google.gwt.dom.client.Document; |
| 11 | +import com.google.gwt.dom.client.Style; |
| 12 | +import com.google.gwt.user.client.DOM; |
| 13 | +import com.google.gwt.user.client.ui.Button; |
| 14 | +import com.google.gwt.user.client.ui.Composite; |
| 15 | +import com.google.gwt.user.client.ui.FlowPanel; |
| 16 | +import com.google.gwt.user.client.ui.HTML; |
| 17 | +import com.google.gwt.user.client.ui.HTMLPanel; |
| 18 | +import com.google.gwt.user.client.ui.HorizontalPanel; |
| 19 | +import com.google.gwt.user.client.ui.VerticalPanel; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +/** |
| 25 | + * Created by uemit.seren on 2/3/16. |
| 26 | + */ |
| 27 | +public class LegendFormatterExample extends Composite { |
| 28 | + |
| 29 | + private VerticalPanel panel = new VerticalPanel(); |
| 30 | + private Dygraphs dygraphs; |
| 31 | + DivElement legendElement; |
| 32 | + |
| 33 | + public LegendFormatterExample() { |
| 34 | + |
| 35 | + initWidget(panel); |
| 36 | + legendElement = Document.get().createDivElement(); |
| 37 | + panel.add(new HTML("This page demonstrates use of <a href=\"../options.html#legendFormatter\"><code>legendFormatter</code></a>, " + |
| 38 | + "which can be used to create more complex legends than <code>valueFormatter</code>")); |
| 39 | + panel.getElement().appendChild(legendElement); |
| 40 | + initDygraphs(); |
| 41 | + } |
| 42 | + |
| 43 | + public void initDygraphs() { |
| 44 | + DygraphsOptions options = new DygraphsOptions(); |
| 45 | + options.legend="always"; |
| 46 | + options.xlabel="Date"; |
| 47 | + options.ylabel="Count"; |
| 48 | + options.labelsDiv = legendElement; |
| 49 | + options.legendFormatter = new LegendFormatterCallback() { |
| 50 | + @Override |
| 51 | + public String getFormat(LegendFormatterData data) { |
| 52 | + if (data.getX() == null) { |
| 53 | + // This happens when there's no selection and {legend: 'always'} is set. |
| 54 | + String retval = "<br>"; |
| 55 | + for (LegendFormatterSeries series: data.getSeries()) { |
| 56 | + retval+=series.getDashHTML()+" "+ series.getLabelHTML()+"<br>"; |
| 57 | + } |
| 58 | + return retval; |
| 59 | + } |
| 60 | + String html = data.getDygraph().getLabels()[0] + ": " + data.getXHTML(); |
| 61 | + for (LegendFormatterSeries series:data.getSeries()) { |
| 62 | + if (series.isVisible()) { |
| 63 | + String labeledData = series.getLabelHTML()+": "+ series.getYHTML(); |
| 64 | + if (series.isHighlighted()) { |
| 65 | + labeledData = "<b>" + labeledData+"</b>"; |
| 66 | + } |
| 67 | + html+="<br>" + series.getDashHTML()+" " + labeledData; |
| 68 | + } |
| 69 | + } |
| 70 | + return html; |
| 71 | + } |
| 72 | + }; |
| 73 | + |
| 74 | + HighlightSeriesOptions highlightSeriesOptions = new HighlightSeriesOptions(); |
| 75 | + highlightSeriesOptions.strokeWidth = 2; |
| 76 | + options.highlightSeriesOpts = highlightSeriesOptions; |
| 77 | + |
| 78 | + dygraphs = new Dygraphs(()-> { |
| 79 | + String r = "date,parabola,line,another line,sine wave\n"; |
| 80 | + for (int i = 1; i <= 31; i++) { |
| 81 | + r += "200610" + (i < 10 ? "0"+i : String.valueOf(i)); |
| 82 | + r += "," + 10*(i*(31-i)); |
| 83 | + r += "," + 10*(8*i); |
| 84 | + r += "," + 10*(250 - 8*i); |
| 85 | + r += "," + 10*(125 + 125 * Math.sin(0.3*i)); |
| 86 | + r += "\n"; |
| 87 | + } |
| 88 | + return r; |
| 89 | + },options); |
| 90 | + panel.add(dygraphs); |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments