Skip to content

Instantly share code, notes, and snippets.

@dardison
Created January 30, 2015 17:25
Show Gist options
  • Save dardison/6253bf6e24b97e1adf8b to your computer and use it in GitHub Desktop.
Save dardison/6253bf6e24b97e1adf8b to your computer and use it in GitHub Desktop.
package com.asbitec.vbi.test.client.examples.charts;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.asbitec.vbi.test.client.examples.Example;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.dom.client.StyleInjector;
import com.google.gwt.event.logical.shared.HasResizeHandlers;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.chart.client.chart.Chart;
import com.sencha.gxt.chart.client.chart.Chart.Position;
import com.sencha.gxt.chart.client.chart.axis.CategoryAxis;
import com.sencha.gxt.chart.client.chart.axis.NumericAxis;
import com.sencha.gxt.chart.client.chart.event.SeriesSelectionEvent;
import com.sencha.gxt.chart.client.chart.event.SeriesSelectionEvent.HasSeriesSelectionHandlers;
import com.sencha.gxt.chart.client.chart.event.SeriesSelectionEvent.SeriesSelectionHandler;
import com.sencha.gxt.chart.client.chart.series.LineSeries;
import com.sencha.gxt.chart.client.chart.series.Primitives;
import com.sencha.gxt.chart.client.chart.series.Series;
import com.sencha.gxt.chart.client.chart.series.SeriesHighlighter;
import com.sencha.gxt.chart.client.chart.series.SeriesLabelProvider;
import com.sencha.gxt.chart.client.chart.series.SeriesToolTipConfig;
import com.sencha.gxt.chart.client.draw.Color;
import com.sencha.gxt.chart.client.draw.HSL;
import com.sencha.gxt.chart.client.draw.RGB;
import com.sencha.gxt.chart.client.draw.sprite.RectangleSprite;
import com.sencha.gxt.chart.client.draw.sprite.Sprite;
import com.sencha.gxt.chart.client.draw.sprite.TextSprite;
import com.sencha.gxt.chart.client.draw.sprite.TextSprite.TextAnchor;
import com.sencha.gxt.chart.client.draw.sprite.TextSprite.TextBaseline;
import com.sencha.gxt.core.client.ValueProvider;
import com.sencha.gxt.data.shared.LabelProvider;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.ModelKeyProvider;
import com.sencha.gxt.data.shared.PropertyAccess;
import com.sencha.gxt.data.shared.event.StoreDataChangeEvent;
import com.sencha.gxt.data.shared.event.StoreDataChangeEvent.StoreDataChangeHandler;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.FramedPanel;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.button.ToolButton;
import com.sencha.gxt.widget.core.client.container.CenterLayoutContainer;
import com.sencha.gxt.widget.core.client.container.MarginData;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.form.PropertyEditor;
import com.sencha.gxt.widget.core.client.form.NumberPropertyEditor.DoublePropertyEditor;
import com.sencha.gxt.widget.core.client.grid.AggregationRenderer;
import com.sencha.gxt.widget.core.client.grid.SummaryType;
import com.sencha.gxt.widget.core.client.grid.SummaryType.MaxSummaryType;
import com.sencha.gxt.widget.core.client.grid.SummaryType.MinSummaryType;
import com.sencha.gxt.widget.core.client.grid.SummaryType.SumSummaryType;
import com.sencha.gxt.widget.core.client.toolbar.FillToolItem;
import com.sencha.gxt.widget.core.client.toolbar.ToolBar;
public class LineSerieChartExample extends Example {
private OneSerieLineChartPanel<InventoryCategoryDTO> panel;
private static InventoryCategoryDTOProperties PROPS=GWT.create(InventoryCategoryDTOProperties.class);
public LineSerieChartExample() {
super("LineChart");
this.buildUI();
}
protected void buildUI(){
ListStore<InventoryCategoryDTO> store=new ListStore<InventoryCategoryDTO>(new InventoryDateKey());
store.addAll(TestData.getInventoryFlow());
panel=new OneSerieLineChartPanel<InventoryCategoryDTO>("Inventory looses",
PROPS.losses(),
new DateLabelValueProvider<InventoryCategoryDTO>(new CategoryValueProvider<InventoryCategoryDTO, Date,
DateCategoryValue>()), store);
}
@Override
public Widget asWidget() {
return panel.asWidget();
}
private static class InventoryDateKey extends CategoryKeyProvider<InventoryCategoryDTO, DateCategoryValue> {
public InventoryDateKey() {
super();
this.setCategoryProvider(new CategoryValueProvider<InventoryCategoryDTO, Date, DateCategoryValue>());
}
}
public static class TestData {
public static List<InventoryCategoryDTO> getInventoryFlow(){
List<InventoryCategoryDTO> flows=new ArrayList<InventoryCategoryDTO>();
// Formato: Fecha, Saldo Inicial, Ingresos, Egresos, Ajustes(+), Ajustes (-), mermas, reingresos, Saldo Final
flows.add(new InventoryCategoryDTO(getDateCategoryValue("14/06/2014"), 69000.00, 0.00, 25500.00, 0.00, 200.00, 300.00, 0.00, 43000.00));
flows.add(new InventoryCategoryDTO(getDateCategoryValue("15/06/2014"), 43000.00, 42000.00, 27000.00, 0.00, 0.00, 200.00, 80.00, 57880.00));
flows.add(new InventoryCategoryDTO(getDateCategoryValue("16/06/2014"), 57880.00, 65000.00, 23800.00, 0.00, 0.00, 240.00, 40.00, 98880.00));
flows.add(new InventoryCategoryDTO(getDateCategoryValue("17/06/2014"), 98880.00, 0.00, 22100.00, 0.00, 500.00, 230.00, 65.00, 76115.00));
flows.add(new InventoryCategoryDTO(getDateCategoryValue("18/06/2014"), 76115.00, 45000.00, 31000.00, 0.00, 0.00, 240.00, 20.00, 89895.00));
flows.add(new InventoryCategoryDTO(getDateCategoryValue("19/06/2014"), 89895.00, 0.00, 33900.00, 0.00, 0.00, 260.00, 180.00, 55915.00));
flows.add(new InventoryCategoryDTO(getDateCategoryValue("20/06/2014"), 55915.00, 60000.00, 30700.00, 0.00, 0.00, 30.00, 90.00, 85275.00));
flows.add(new InventoryCategoryDTO(getDateCategoryValue("21/06/2014"), 85275.00, 0.00, 27000.00, 0.00, 0.00, 330.00, 60.00, 58005.00));
flows.add(new InventoryCategoryDTO(getDateCategoryValue("22/06/2014"), 58005.00, 78000.00, 27800.00, 0.00, 600.00, 400.00, 20.00, 107225.00));
flows.add(new InventoryCategoryDTO(getDateCategoryValue("23/06/2014"), 107225.00, 0.00, 21000.00, 0.00, 0.00, 630.00, 180.00, 85775.00));
flows.add(new InventoryCategoryDTO(getDateCategoryValue("24/06/2014"), 85775.00, 0.00, 25500.00, 0.00, 0.00, 860.00, 90.00, 59505.00));
flows.add(new InventoryCategoryDTO(getDateCategoryValue("25/06/2014"), 59505.00, 60000.00, 27000.00, 0.00, 0.00, 840.00, 60.00, 91725.00));
flows.add(new InventoryCategoryDTO(getDateCategoryValue("26/06/2014"), 91725.00, 0.00, 23800.00, 0.00, 0.00, 800.00, 65.00, 67190.00));
return flows;
}
private static DateCategoryValue getDateCategoryValue(String date){
DateTimeFormat f = DateTimeFormat.getFormat("dd/MM/yyyy");
DateCategoryValue value = new DateCategoryValue("date");
value.setValue(f.parse(date));
return value;
}
}
public static class OneSerieLineChartPanel<M> extends OneSerieBaseChartPanel<M> {
private LineSeries<M> line;
public OneSerieLineChartPanel() {
super();
}
public OneSerieLineChartPanel(String title,
ValueProvider<M, ? extends Number> magnitudeProvider,
ValueProvider<M, String> labelProvider, ListStore<M> store) {
super(title, magnitudeProvider, labelProvider, store);
}
@Override
protected void defineSerie(Chart<M> chart) {
super.defineSerie(chart);
this.defineLineSeries(chart);
}
protected void defineLineSeries(Chart<M> chart){
Sprite marker = Primitives.square(0, 0, 4);
Color color=this.getRandomColor(1).get(0);
if(this.serieLabelProvider==null){
NumberFormat format=NumberFormat.getFormat("#,##0.##");
DefaultSerieLabelProvider<M> labelProvider=new DefaultSerieLabelProvider<M>(format, this.labelProvider);
this.serieLabelProvider=labelProvider;
}
line=this.addLineSerie(chart, magnitudeProvider, serieLabelProvider, color, marker);
this.setHighlightSerie(line, color);
}
protected LineSeries<M> addLineSerie(Chart<M> chart,
ValueProvider<M, ? extends Number> magnitudeProvider,
DefaultSerieLabelProvider<M> labelProvider,
Color color,
Sprite marker){
valueAxis.addField(magnitudeProvider);
LineSeries<M> serie=new LineSeries<M>();
SeriesToolTipConfig<M> toolTip=new SeriesToolTipConfig<M>();
toolTip.setLabelProvider(labelProvider);
serie.setToolTipConfig(toolTip);
serie.setYAxisPosition(Position.LEFT);
serie.setXAxisPosition(Position.BOTTOM);
serie.setYField(magnitudeProvider);
serie.setStroke(color);
serie.setShowMarkers(true);
// serie.setFill(color);
marker.setFill(color);
serie.setMarkerConfig(marker);
serie.setHighlighting(true);
chart.addSeries(serie);
return serie;
}
public void highlight(M model){
if(currentModel!=null){
line.unHighlight(this.getStore().indexOf(currentModel));
currentModel=null;
}
final int index=this.getStore().indexOf(model);
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
@Override
public void execute() {
line.highlight(index);
}
});
currentModel=model;
}
@Override
protected void magnitudeChanged(DoubleMagnitudeConfig<M> magnitude) {
// TODO Auto-generated method stub
}
@Override
protected ToolBar buildToolBar(ToolBar toolBar) {
// TODO Auto-generated method stub
return null;
}
public LineSeries<M> getLine() {
return line;
}
@Override
public HandlerRegistration addResizeHandler(ResizeHandler handler) {
return this.chart.addResizeHandler(handler);
}
@Override
public void fireEvent(GwtEvent<?> event) {
// TODO Auto-generated method stub
}
}
public abstract static class OneSerieBaseChartPanel<M> extends OneSerieChartPanel<M> {
protected DefaultSerieLabelProvider<M> serieLabelProvider;
protected List<Color> colors;
protected NumericAxis<M> valueAxis;
protected CategoryAxis<M, String> xAxis;
protected M currentModel;
public OneSerieBaseChartPanel() {
super();
}
public OneSerieBaseChartPanel(String title,
ValueProvider<M, ? extends Number> magnitudeProvider,
ValueProvider<M, String> labelProvider,
ListStore<M> store) {
super(title, magnitudeProvider, labelProvider, store);
}
public OneSerieBaseChartPanel(String title,
ValueProvider<M, ? extends Number> magnitudeProvider,
ValueProvider<M, String> labelProvider,
ListStore<M> store, boolean withToolBar) {
super(title, magnitudeProvider, labelProvider, store, withToolBar);
}
public OneSerieBaseChartPanel(String title,
List<DoubleMagnitudeConfig<M>> magnitudes,
ValueProvider<M, String> labelProvider, ListStore<M> store,
boolean withToolBar) {
super(title, magnitudes, labelProvider, store, withToolBar);
}
@Override
protected void defineSerie(Chart<M> chart) {
this.colors=ColorUtils.getColorList(20);
valueAxis=new NumericAxis<M>();
valueAxis.setPosition(Position.LEFT);
TextSprite valueText=new TextSprite(valuesTitle);
valueText.setFontSize(16);
valueAxis.setTitleConfig(valueText);
valueAxis.setDisplayGrid(true);
this.setAxisScale(valueAxis);
chart.addAxis(valueAxis);
xAxis=this.addXAxis(chart, Position.BOTTOM);
}
public abstract void highlight(M model);
@Override
protected void reconfigure() {
this.currentModel=null;
this.setAxisScale(valueAxis);
}
protected void removeXAxis(Chart<M> chart){
chart.removeAxis(xAxis);
}
protected CategoryAxis<M, String> addXAxis(Chart<M> chart, Position pos){
// Eje X de Categorias
CategoryAxis<M, String> xAxis=new CategoryAxis<M, String>();
xAxis.setPosition(pos);
xAxis.setField(this.labelProvider);
TextSprite label=new TextSprite();
label.setTextAnchor(TextAnchor.START);
label.setTextBaseline(TextBaseline.BOTTOM);
label.setRotation(configXAxis(store));
xAxis.setLabelConfig(label);
LabelProvider<String> provider = new LabelProvider<String>() {
@Override
public String getLabel(String item) {
if(store.getAll().size()> 20){
return "";
}else{
if(item.length() > 10){
return item.substring(0, 10) + "...";
}else{
return item;
}
}
}
};
xAxis.setLabelProvider(provider);
chart.addAxis(xAxis);
return xAxis;
}
protected List<Color> getRandomColor(int size) {
int index=0;
if(size>30){
throw new RuntimeException("Error demasiados colores solicitados");
}
boolean found=false;
while(!found){
index=Random.nextInt(20);
if(index<(20-size)){
found=true;
}
}
List<Color> result=new ArrayList<Color>();
for(int i=0; i<size; i++){
result.add(this.colors.get(index+i));
}
return result;
}
protected void setAxisScale(NumericAxis<M> axis) {
MaxSummaryType<Double> maxSummary=new MaxSummaryType<Double>();
MinSummaryType<Double> minSummary=new MinSummaryType<Double>();
Double max=maxSummary.calculate(store.getAll(), new NumberValueProvider<M>(this.magnitudeProvider));
Double min=minSummary.calculate(store.getAll(), new NumberValueProvider<M>(this.magnitudeProvider));
NumberLabelProvider provider=new NumberLabelProvider();
provider.setMin(min);
provider.setMax(max);
axis.setLabelProvider(provider);
if(min>0){
min=0d;
}
/* NiceScale scale=new NiceScale(min, max);
axis.setMinimum(scale.getNiceMin());
axis.setMaximum(scale.getNiceMax());
axis.setInterval(scale.getTickSpacing());
*/ }
protected int configXAxis(ListStore<M> store) {
if (store.size() == 1) {
return 0;
} else if (store.size() <= 5) {
return 320;
} else if (store.size() <= 15) {
return 300;
} else {
return 270;
}
}
public DefaultSerieLabelProvider<M> getSerieLabelProvider() {
return serieLabelProvider;
}
public void setSerieLabelProvider(DefaultSerieLabelProvider<M> serieLabelProvider) {
this.serieLabelProvider = serieLabelProvider;
}
protected static class NumberValueProvider<M> implements ValueProvider<M, Double> {
private ValueProvider<M, ? extends Number> provider;
public NumberValueProvider(ValueProvider<M, ? extends Number> provider) {
super();
this.provider = provider;
}
@Override
public Double getValue(M model) {
Number value=this.provider.getValue(model);
if(value==null){
return null;
}else{
return value.doubleValue();
}
}
@Override
public void setValue(M object, Double value) {
throw new RuntimeException("Error metodo no implementado");
}
@Override
public String getPath() {
return "NumberValueProvider";
}
}
public static class NumberLabelProvider implements LabelProvider<Number> {
private Number min;
private Number max;
private String label=null;
@Override
public String getLabel(Number item) {
NumberFormat nf=NumberFormat.getFormat("#,##0");
long value=this.getSimplifiedValue(item);
String result;
if(label!=null){
result=nf.format(value)+label;
}else{
result=nf.format(value);
}
return result;
}
protected Long getSimplifiedValue(Number value){
if(max.longValue()>10000){
label="K";
return value.longValue() / 1000;
}else{
return value.longValue();
}
}
public void setMin(Number min) {
this.min = min;
}
@SuppressWarnings("unused")
public Number getMax() {
return max;
}
public void setMax(Number max) {
this.max = max;
}
}
protected void setHighlightSerie(Series<M> serie, Color color){
//bar.setHighlighting(true);
final Color fColor=color;
serie.setHighlighter(new SeriesHighlighter() {
@Override
public void unHighlight(Sprite sprite) {
if(sprite instanceof RectangleSprite){
RectangleSprite bar=(RectangleSprite)sprite;
bar.setStroke(Color.NONE);
bar.setStrokeWidth(0);
bar.setFill(fColor);
bar.redraw();
}
}
@Override
public void highlight(Sprite sprite) {
if(sprite instanceof RectangleSprite){
RectangleSprite bar=(RectangleSprite)sprite;
bar.setStroke(new RGB(85, 85, 204));
bar.setStrokeWidth(3);
bar.setFill(new RGB("#a2b5ca"));
bar.redraw();
}
}
});
serie.addSeriesSelectionHandler(new SeriesSelectionHandler<M>() {
@Override
public void onSeriesSelection(SeriesSelectionEvent<M> event) {
processSerieSelection(event);
}
});
}
}
public abstract static class OneSerieChartPanel<M> implements IsWidget,
HasSeriesSelectionHandlers<M>,
HasResizeHandlers{
protected FramedPanel panel;
protected VerticalLayoutContainer vp;
protected CenterLayoutContainer cl;
protected ValueProvider<M, ? extends Number> magnitudeProvider;
protected ValueProvider<M, String> labelProvider;
protected Chart<M> chart;
protected ListStore<M> store;
protected String valuesTitle;
private String categoryTitle;
private String title;
private List<DoubleMagnitudeConfig<M>> magnitudes;
private boolean magnitudeSelection;
private boolean expanded;
private ToolBar toolBar;
protected boolean withToolBar;
private boolean noData;
private ToolButton expand;
public OneSerieChartPanel() {
super();
}
public OneSerieChartPanel(String title,
ValueProvider<M, ? extends Number> magnitudeProvider,
ValueProvider<M, String> labelProvider,
ListStore<M> store) {
super();
this.init(title, magnitudeProvider, labelProvider, store);
this.buildUI();
}
public OneSerieChartPanel(String title,
ValueProvider<M, ? extends Number> magnitudeProvider,
ValueProvider<M, String> labelProvider,
ListStore<M> store, boolean withToolBar) {
super();
this.init(title, magnitudeProvider, labelProvider, store);
this.magnitudes=new ArrayList<DoubleMagnitudeConfig<M>>();
this.withToolBar=withToolBar;
this.buildUI();
}
public OneSerieChartPanel(String title,
List<DoubleMagnitudeConfig<M>> magnitudes,
ValueProvider<M, String> labelProvider,
ListStore<M> store,
boolean withToolBar){
this.init(title, magnitudes.get(0).getProvider(), labelProvider, store);
this.magnitudes=magnitudes;
this.withToolBar=withToolBar;
this.buildUI();
}
protected void init(String title,
ValueProvider<M, ? extends Number> magnitudeProvider,
ValueProvider<M, String> labelProvider,
ListStore<M> store){
this.title=title;
this.magnitudeProvider = magnitudeProvider;
this.labelProvider = labelProvider;
this.store = store;
this.magnitudes=new ArrayList<DoubleMagnitudeConfig<M>>();
this.magnitudeSelection=false;
this.withToolBar=false;
this.noData=false;
}
protected void buildUI(){
this.panel=new FramedPanel();
this.panel.setLayoutData(new MarginData(10));
if(this.magnitudeSelection){
ToolBar toolBar=new ToolBar();
HTML titleHtml=new HTML(this.buildTitle(title));
toolBar.add(titleHtml);
toolBar.add(new FillToolItem());
for(DoubleMagnitudeConfig<M> mag : this.magnitudes){
TextButton button=new TextButton(mag.getLabel());
button.setData("mag", mag);
button.addSelectHandler(new SelectHandler() {
@Override
public void onSelect(SelectEvent event) {
processMagnitudeButton(event);
}
});
}
}
this.chart=new Chart<M>(600, 50);
this.chart.setStore(store);
this.defineSerie(chart);
this.store.addStoreDataChangeHandler(new StoreDataChangeHandler<M>() {
@Override
public void onDataChange(StoreDataChangeEvent<M> event) {
if(noData && !event.getSource().getAll().isEmpty()){
removeNoDataMessage();
reconfigure();
chart.redrawChart();
chart.redrawSurface();
}else{
if(event.getSource().getAll().isEmpty()){
showNoDataMessage();
}else{
reconfigure();
chart.redrawChart();
chart.redrawSurface();
}
}
}
});
if(this.withToolBar){
vp=new VerticalLayoutContainer();
StyleInjector.inject(".container {background-image:none !important; background-color: #157FCC !important;}", true);
this.toolBar=new ToolBar();
HTML titleHtml=new HTML(this.buildTitle(title));
this.toolBar.addStyleName("container");
this.toolBar.add(titleHtml);
this.toolBar=this.buildToolBar(toolBar);
if(this.toolBar!=null){
this.toolBar.setBorders(true);
this.panel.setHeaderVisible(false);
vp.add(toolBar, new VerticalLayoutData(1, -1));
chart.setBorders(true);
VerticalLayoutData vld=new VerticalLayoutData(1, 1);
vp.add(chart, vld);
this.panel.setWidget(vp);
}else{
this.panel.setHeadingText(title);
this.panel.setWidget(chart);
}
}else{
this.panel.setHeadingText(title);
this.panel.setWidget(chart);
}
expand=new ToolButton(ToolButton.MAXIMIZE);
if(this.withToolBar){
this.toolBar.add(new FillToolItem());
addToolBarButtonsOnRight(this.toolBar);
this.toolBar.add(expand);
}else{
this.panel.getHeader().addTool(expand);
}
expand.addSelectHandler(new SelectHandler() {
@Override
public void onSelect(SelectEvent event) {
processExpand();
}
});
}
protected void addToolBarButtonsOnRight(ToolBar toolbar){
}
/**
* Este metodo es util para remover el boton de Expand cuando el grafico no lo utiliza
*/
public void removeExpand(){
if(this.withToolBar && expand!=null){
this.toolBar.remove(expand);
}else{
if(expand!=null){
this.panel.getHeader().removeTool(expand);
}
}
}
public void showNoDataMessage(){
if(!this.noData){
this.chart.removeFromParent();
cl=new CenterLayoutContainer();
ContentPanel msg = new ContentPanel();
msg.setBorders(true);
msg.setBodyStyle("padding: 6px");
SafeHtmlBuilder sb=new SafeHtmlBuilder();
sb.appendHtmlConstant("<span style='align:center'>Sin Datos</span>");
msg.setHeadingHtml(sb.toSafeHtml());
msg.add(new Label("No hay Datos para la Seleccion"));
msg.setWidth(200);
cl.add(msg);
if(this.withToolBar && this.toolBar!=null){
vp.add(cl, new VerticalLayoutData(1, 1));
vp.forceLayout();
}else{
this.panel.setWidget(cl);
}
this.noData=true;
}
}
public void removeNoDataMessage(){
if(this.noData){
this.noData=false;
cl.removeFromParent();
if(this.withToolBar && this.toolBar!=null){
vp.add(chart, new VerticalLayoutData(1, 1));
vp.forceLayout();
}else{
this.panel.setWidget(chart);
}
}
}
public void setHeaderVisible(boolean visible){
this.panel.setHeaderVisible(visible);
}
public void setBorders(boolean show){
this.panel.setBorders(show);
}
protected void processMagnitudeButton(SelectEvent event){
TextButton button=(TextButton)event.getSource();
DoubleMagnitudeConfig<M> mag=button.getData("mag");
this.magnitudeChanged(mag);
}
protected void processExpand(){
}
public void disableExpand(){
if(expand!=null)
expand.setEnabled(false);
}
public void enableExpand(){
if(expand!=null)
expand.setEnabled(true);
}
public void reset(){
this.expanded=false;
this.enableExpand();
}
public void removeFromParent(){
this.panel.removeFromParent();
}
protected SafeHtml buildTitle(String title){
SafeHtmlBuilder sb=new SafeHtmlBuilder();
sb.appendHtmlConstant("<span style='font-size:13px;color:white'><b>"+title+"</b></span>");
return sb.toSafeHtml();
}
/**
* Las subclases deben implementar este metodo para definir las tareas que se deben realizar si
* cambia el store
*/
protected abstract void reconfigure();
protected abstract void defineSerie(Chart<M> chart);
protected abstract void magnitudeChanged(DoubleMagnitudeConfig<M> magnitude);
protected abstract ToolBar buildToolBar(ToolBar toolBar);
protected void processSerieSelection(SeriesSelectionEvent<M> event) {
}
public String getValuesTitle() {
return valuesTitle;
}
public void setValuesTitle(String valuesTitle) {
this.valuesTitle = valuesTitle;
}
public String getCategoryTitle() {
return categoryTitle;
}
public void setCategoryTitle(String categoryTitle) {
this.categoryTitle = categoryTitle;
}
@Override
public Widget asWidget() {
return panel;
}
@Override
public HandlerRegistration addSeriesSelectionHandler(SeriesSelectionHandler<M> handler) {
if(this.chart!=null && this.chart.getSeries().size()>0){
return this.chart.getSeries(0).addSeriesSelectionHandler(handler);
}else{
throw new RuntimeException("Error la Serie del grafico no esta definida");
}
}
public void mask(){
this.panel.mask("Loading...");
}
public void unmask(){
this.panel.unmask();
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List<DoubleMagnitudeConfig<M>> getMagnitudes() {
return magnitudes;
}
public void setMagnitudes(List<DoubleMagnitudeConfig<M>> magnitudes) {
this.magnitudes = magnitudes;
}
public boolean isExpanded() {
return expanded;
}
protected void setExpanded(boolean expanded) {
this.expanded = expanded;
}
public ListStore<M> getStore() {
return store;
}
@Override
public HandlerRegistration addResizeHandler(ResizeHandler handler) {
return this.chart.addResizeHandler(handler);
}
@Override
public void fireEvent(GwtEvent<?> event) {
}
}
public static class CategoryFiguresDTO implements Serializable {
private Category value;
public CategoryFiguresDTO(){
}
public CategoryFiguresDTO(Category value){
this.value = value;
}
public Category getValue() {
return value;
}
public void setValue(Category value) {
this.value = value;
}
}
public abstract static class Category implements Serializable {
/**
* ID Propiedad
*/
private String property;
/**
* Etiqueta localizada en el cliente
*/
private String label;
/**
* Valor provisto por el {@link PropertyEditor}
*/
private String textValue;
private boolean reminder;
public Category() {
super();
this.reminder=false;
}
public Category(String property) {
super();
this.reminder = false;
this.property = property;
}
public String getProperty() {
return property;
}
public void setProperty(String property) {
this.property = property;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((property == null) ? 0 : property.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Category other = (Category) obj;
if (property == null) {
if (other.property != null)
return false;
} else if (!property.equals(other.property))
return false;
return true;
}
@Override
public String toString() {
if(this.label!=null){
return label;
}else{
return property;
}
}
public String getTextValue() {
return textValue;
}
public void setTextValue(String textValue) {
this.textValue = textValue;
}
public boolean isReminder() {
return reminder;
}
public void setReminder(boolean reminder) {
this.reminder = reminder;
}
}
public static class InventoryCategoryDTO extends CategoryFiguresDTO {
private Double openingBalance;
private Double quantityIn;
private Double quantityOut;
private Double debits;
private Double credits;
private Double losses;
private Double readmissions;
private Double actualBalance;
private Double physicalBalance;
private Double movements;
private Double adjustments;
public InventoryCategoryDTO() {
super();
}
public InventoryCategoryDTO(CategoryValue<?> value, Double openingBalance, Double quantityIn,
Double quantityOut, Double debits, Double credits, Double losses,
Double readmissions, Double actualBalance) {
super(value);
this.openingBalance = openingBalance;
this.quantityIn = quantityIn;
this.quantityOut = quantityOut;
this.debits = debits;
this.credits = credits;
this.losses = losses;
this.readmissions = readmissions;
this.actualBalance = actualBalance;
}
public Double getOpeningBalance() {
return openingBalance;
}
public void setOpeningBalance(Double openingBalance) {
this.openingBalance = openingBalance;
}
public Double getQuantityIn() {
return quantityIn;
}
public void setQuantityIn(Double quantityIn) {
this.quantityIn = quantityIn;
}
public Double getQuantityOut() {
return quantityOut;
}
public void setQuantityOut(Double quantityOut) {
this.quantityOut = quantityOut;
}
public Double getDebits() {
return debits;
}
public void setDebits(Double debits) {
this.debits = debits;
}
public Double getCredits() {
return credits;
}
public void setCredits(Double credits) {
this.credits = credits;
}
public Double getLosses() {
return losses;
}
public void setLosses(Double losses) {
this.losses = losses;
}
public Double getReadmissions() {
return readmissions;
}
public void setReadmissions(Double readmissions) {
this.readmissions = readmissions;
}
public Double getActualBalance() {
return actualBalance;
}
public void setActualBalance(Double actualBalance) {
this.actualBalance = actualBalance;
}
public Double getPhysicalBalance() {
return physicalBalance;
}
public void setPhysicalBalance(Double physicalBalance) {
this.physicalBalance = physicalBalance;
}
public Double getMovements() {
return movements;
}
public void setMovements(Double movements) {
this.movements = movements;
}
public Double getAdjustments() {
return adjustments;
}
public void setAdjustments(Double adjustments) {
this.adjustments = adjustments;
}
}
public abstract static class CategoryValue<V extends Comparable<V>> extends Category implements Comparable<CategoryValue<V>> {
private V value;
public CategoryValue() {
super();
}
public CategoryValue(String property) {
super(property);
}
public CategoryValue(Category category){
super(category.getProperty());
this.setLabel(category.getLabel());
}
public V getValue() {
return value;
}
public void setValue(V value) {
this.value = value;
}
@Override
public int hashCode() {
int result = super.hashCode();
result = result
+ ((getValue() == null) ? 0 : getValue().hashCode());
return result;
}
@Override
public int compareTo(CategoryValue<V> o) {
if(this.getProperty().compareTo(o.getProperty())==0){
return value.compareTo(o.getValue());
}else{
return this.getProperty().compareTo(o.getProperty());
}
}
}
public static class CategoryValueProvider<M extends CategoryFiguresDTO,
V extends Comparable<V>,
R extends CategoryValue<V>>
implements ValueProvider<M, V> {
private ValueProvider<M, ? super R> provider;
public CategoryValueProvider() {
super();
this.provider=new CategoryConverter<M>();
}
@SuppressWarnings("unchecked")
@Override
public V getValue(M model) {
R value=(R)provider.getValue(model);
return value.getValue();
}
@Override
public void setValue(M object, V value) {
throw new RuntimeException("Error metodo no implementado");
}
@SuppressWarnings("unchecked")
public boolean isReminder(M model){
R value=(R)provider.getValue(model);
return value.isReminder();
}
@Override
public String getPath() {
return "categoryValue";
}
}
public static class CategoryConverter<M extends CategoryFiguresDTO> implements ValueProvider<M, Category> {
@Override
public Category getValue(M model) {
return model.getValue();
}
@Override
public void setValue(M object, Category value) {
}
@Override
public String getPath() {
return "categoryConverter";
}
}
public static class DateLabelValueProvider<M> implements ValueProvider<M, String> {
private ValueProvider<M, Date> dateProvider;
public DateLabelValueProvider(ValueProvider<M, Date> dateProvider) {
super();
this.dateProvider = dateProvider;
}
@Override
public String getValue(M model) {
DateTimeFormat dateFormat=DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT);
return dateFormat.format(this.dateProvider.getValue(model));
}
@Override
public void setValue(M object, String value) {
throw new RuntimeException("No soportado");
}
@Override
public String getPath() {
return "dayOfWeek";
}
}
public static class DateCategoryValue extends CategoryValue<Date> {
public DateCategoryValue() {
super();
}
public DateCategoryValue(String property) {
super(property);
}
public DateCategoryValue(Category category){
super(category.getProperty());
this.setLabel(category.getLabel());
}
}
public static class CategoryKeyProvider<M extends CategoryFiguresDTO,
R extends CategoryValue<?>>
implements ModelKeyProvider<M> {
private CategoryValueProvider<M, ?, R> categoryProvider;
public CategoryKeyProvider() {
super();
}
@Override
public String getKey(M model) {
Object value=this.categoryProvider.getValue(model);
if(value!=null)
return value.toString();
else
return null;
}
public CategoryValueProvider<M, ?, R> getCategoryProvider() {
return categoryProvider;
}
public void setCategoryProvider(CategoryValueProvider<M, ?, R> categoryProvider) {
this.categoryProvider = categoryProvider;
}
}
public interface InventoryCategoryDTOProperties extends PropertyAccess<InventoryCategoryDTO> {
ValueProvider<InventoryCategoryDTO, Double> openingBalance();
ValueProvider<InventoryCategoryDTO, Double> quantityIn();
ValueProvider<InventoryCategoryDTO, Double> quantityOut();
ValueProvider<InventoryCategoryDTO, Double> debits();
ValueProvider<InventoryCategoryDTO, Double> credits();
ValueProvider<InventoryCategoryDTO, Double> losses();
ValueProvider<InventoryCategoryDTO, Double> readmissions();
ValueProvider<InventoryCategoryDTO, Double> actualBalance();
ValueProvider<InventoryCategoryDTO, Double> movements();
ValueProvider<InventoryCategoryDTO, Double> adjustments();
}
public static class ColorUtils {
public static List<Color> getColorList(Integer size){
RGB base=new RGB(138, 86, 226);
List<Color> result=new ArrayList<Color>();
result.add(base);
double baseHue = (new HSL(base)).getHue();
double step = (240.0 / (double)size);
for(int i=1; i<size; i++){
HSL nextColor=new HSL(base);
nextColor.setHue((baseHue + step * ((double)i)) % 240.0);
result.add(nextColor);
}
if(size>2){
int i, j;
Color holderI, holderJ;
// reordena los adjacentes para que no sean iguales
for(i=0, j=size / 2;i<size / 2; i+=2, j+=2){
holderI=result.get(i);
holderJ=result.get(j);
result.add(i, holderJ);
result.remove(i+1);
result.add(j, holderI);
result.remove(j+1);
}
}
return result;
}
}
public static class DefaultSerieLabelProvider<M> implements SeriesLabelProvider<M> {
private NumberFormat numberFormat;
private ValueProvider<M, String> labelProvider;
private String title;
private String category;
public DefaultSerieLabelProvider(NumberFormat numberFormat) {
super();
this.numberFormat = numberFormat;
}
public DefaultSerieLabelProvider(NumberFormat numberFormat,
ValueProvider<M, String> labelProvider) {
super();
this.numberFormat = numberFormat;
this.labelProvider = labelProvider;
}
public DefaultSerieLabelProvider(NumberFormat numberFormat,
ValueProvider<M, String> labelProvider, String category) {
super();
this.numberFormat = numberFormat;
this.labelProvider = labelProvider;
this.category = category;
}
@Override
public String getLabel(M item,
ValueProvider<? super M, ? extends Number> valueProvider) {
String result = "";
if (labelProvider != null)
result=labelProvider.getValue(item)+" "+this.numberFormat.format(valueProvider.getValue(item));
if (category != null)
result=category + " - " + result;
if(this.title!=null){
result=title+": "+result;
}
return result;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
public static class MagnitudeConfig<M, N> {
private ValueProvider<M, N> provider;
private String label;
private String property;
private NumberFormat numberFormat;
private PropertyEditor<N> editor;
private AggregationRenderer<M> aggregationRenderer;
private SummaryType<N, Double> summaryType;
public MagnitudeConfig() {
super();
}
public MagnitudeConfig(String property) {
super();
this.property = property;
}
public MagnitudeConfig(String label, String property) {
super();
this.label = label;
this.property = property;
}
public ValueProvider<M, N> getProvider() {
return provider;
}
public void setProvider(ValueProvider<M, N> provider) {
this.provider = provider;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public PropertyEditor<N> getEditor() {
return editor;
}
public void setEditor(PropertyEditor<N> editor) {
this.editor = editor;
}
public String getProperty() {
return property;
}
public void setProperty(String property) {
this.property = property;
}
public AggregationRenderer<M> getAggregationRenderer() {
if(this.aggregationRenderer!=null){
return aggregationRenderer;
}else{
if(this.summaryType!=null && this.numberFormat!=null){
//VBIAggregationNumberSummaryRenderer<M, N> renderer=new VBIAggregationNumberSummaryRenderer<M, N>(numberFormat, summaryType);
return null;
}else{
return null;
}
}
}
public void setAggregationRenderer(AggregationRenderer<M> aggregationRenderer) {
this.aggregationRenderer = aggregationRenderer;
}
public SummaryType<N, Double> getSummaryType() {
return summaryType;
}
public void setSummaryType(SummaryType<N, Double> summaryType) {
this.summaryType = summaryType;
}
public NumberFormat getNumberFormat() {
return numberFormat;
}
public void setNumberFormat(NumberFormat numberFormat) {
this.numberFormat = numberFormat;
}
}
public static class DoubleMagnitudeConfig<M> extends MagnitudeConfig<M, Double> {
public DoubleMagnitudeConfig() {
super();
this.setNumberFormat(NumberFormat.getFormat("#,##0.00"));
this.setEditor(new DoublePropertyEditor(this.getNumberFormat()));
this.setSummaryType(new SumSummaryType<Double>());
}
public DoubleMagnitudeConfig(String label, String property) {
this(property);
this.setLabel(label);
}
public DoubleMagnitudeConfig(String property) {
this();
this.setProperty(property);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment