简体   繁体   English

一步一步gflot教程?

[英]step by step gflot tutorial?

I feel a bit dumb asking this question here, but I can't seem to find a decent Gflot tutorial. 我觉得这里有点愚蠢地问这个问题,但我似乎无法找到一个像样的Gflot教程。 Yes, there are tons of examples , and you can even download the whole code and try it out, but if you just want to create a uiBinder in a GWT project and add it to the main panel...it becomes really hard. 是的,有大量的例子 ,您甚至可以下载整个代码并进行试用,但如果您只是想在GWT项目中创建一个uiBinder并将其添加到主面板......那就变得非常困难。 I'm trying to add a simple LineChart to my main html file in a test GWT project. 我正在尝试在测试GWT项目中将一个简单的LineChart添加到我的主html文件中。

Here is my LineExample uiBinder, directly copied from the examples: 这是我的LineExample uiBinder,直接从示例中复制:

public class LineExample extends DefaultActivity{

private static Binder binder = GWT.create( Binder.class );

interface Binder extends UiBinder<Widget, LineExample>{}

interface Style extends CssResource{
    String button();
    String darkTheme();
    String whiteTheme();
    String legendLabel();
}

/**
 * Plot
 */
@UiField( provided = true )
SimplePlot plot;

/** 
 * Button switch to dark
 */
@UiField
Button switchDark;

/**
 * Button switch to white
 */
@UiField
Button switchWhite;

/**
 * Access to UiBinder style
 */
@UiField
Style style;


public LineExample( Resources resources ){
    super( resources );
}

/**
 * Create plot
 */
public Widget createPlot(){
    PlotModel model = new PlotModel();
    PlotOptions plotOptions = PlotOptions.create();
    plotOptions.setLegendOptions( LegendOptions.create().setBackgroundOpacity( 0 )
        .setPosition( LegendPosition.NORTH_WEST ) );
    plotOptions.setGridOptions( GridOptions.create().setMargin( 5 ) );
    plotOptions.addXAxisOptions( AxisOptions.create().setFont( FontOptions.create().setColor("black").setWeight( "bold" ).setStyle( "italic" ) ) );
    plotOptions.addYAxisOptions( AxisOptions.create().setFont( FontOptions.create().setColor( "black" ).setWeight( "bold" ).setStyle( "italic" ) ) );

    // create the plot
    plot = new SimplePlot( model, plotOptions );

    // add data
    generateRandomData();

    return binder.createAndBindUi( this );
}

/**
 * On click on the generate button, we clear the current data and generate new ones
 *
 * @param e click event
 */
@UiHandler( "generate" )
void onClickGenerate( ClickEvent e ){
    plot.getModel().removeAllSeries();
    generateRandomData();
    plot.redraw();
}

/**
 * Generate random data
 */
private void generateRandomData(){
    int nbSeries = Random.nextInt( 5 ) + 1;
    for ( int i = 0; i < nbSeries; i++ ){
        plot.getModel().addSeries( Series.of( "Random Series " + i ) );
    }
    for ( int i = 1; i < 13; i++ ){
        for ( SeriesHandler series : plot.getModel().getHandlers() ){
            series.add( DataPoint.of( i, Random.nextInt( 30 ) ) );
        }
    }
}

/**
 * Switch to dark theme
 *
 * @param e click event
 */
@UiHandler( "switchDark" )
void onClickSwitchToDark( ClickEvent e ){
    switchDark.setVisible( false );
    switchWhite.setVisible( true );

    plot.removeStyleName( style.whiteTheme() );
    plot.addStyleName( style.darkTheme() );
    plot.getOptions().getXAxisOptions().getFont().setColor( "white" );
    plot.getOptions().getXAxisOptions().setTickColor( "rgba(255, 255, 255, 0.6)" );
    plot.getOptions().getYAxisOptions().getFont().setColor( "white" );
    plot.getOptions().getYAxisOptions().setTickColor( "rgba(255, 255, 255, 0.6)" );
    plot.getOptions().getGridOptions().setBorderColor( "white" );
    plot.redraw();
}

/**
 * Switch to white theme
 *
 * @param e click event
 */
@UiHandler( "switchWhite" )
void onClickSwitchToWhite( ClickEvent e ){
    switchDark.setVisible( true );
    switchWhite.setVisible( false );

    plot.removeStyleName( style.darkTheme() );
    plot.addStyleName( style.whiteTheme() );
    plot.getOptions().getXAxisOptions().getFont().setColor( "black" );
    plot.getOptions().getXAxisOptions().clearTickColor();
    plot.getOptions().getYAxisOptions().getFont().setColor( "black" );
    plot.getOptions().getYAxisOptions().clearTickColor();
    plot.getOptions().getGridOptions().clearBorderColor();
    plot.redraw();
}

} }

Here is the correspoding LineExample.ui.xml: 这是对应的LineExample.ui.xml:

<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"      xmlns:g="urn:import:com.google.gwt.user.client.ui"
xmlns:p="urn:import:com.googlecode.gflot.client">

<ui:style type='gflot.sample.client.LineExample.Style'>
 .button {
  margin-top: 10px;
  margin-left: 10px;
}

.darkTheme {
  background-color: black;
}

@external legendLabel;
.darkTheme .legendLabel {
    color: white;
}

.whiteTheme .legendLabel {
  color: black;
}

Generate Switch to dark Switch to white 生成切换到暗切换到白色

The Resources file used: 使用的Resources文件:

public interface Resources extends ClientBundle {
@Source( "gflot.css" )
Style style();

public interface Style extends CssResource{
    String headerContainer();
    String headerTitle();
    String headerDescription();
    String headerHomePageLink();
    String menuScrollContainer();
    String menuContainer();
    String menuCategory();
    String menuLink();
    String menuLinkSelected();
    String sourceContainer();
    String sourceLink();
    String sourceLinkSelected();
    String mainScrollContainer();
    String mainContainer();
}

} }

And the css file, gflot.css: 和css文件,gflot.css:

@def headerBgColor #0D0D0D;
@def mainBgColor #FFF7FF;
body {
    font-family: 'Ubuntu', sans-serif;
    font-size: 13px;
    background-color: mainBgColor;
    color: #0D0D0D;
}

@external gwt-Button;
.gwt-Button {
/*         background-color: #D14836; */
/*         background-image: -webkit-linear-gradient(top, #DD4B39, #D14836); */
/*         background-image: -moz-linear-gradient(top, #DD4B39, #D14836); */
/*         background-image: -ms-linear-gradient(top, #DD4B39, #D14836); */
/*         background-image: -o-linear-gradient(top, #DD4B39, #D14836); */
/*         background-image: linear-gradient(top, #DD4B39, #D14836); */
/*         border: 1px solid transparent; */
/*         height: 27px; */
/*         line-height: 27px; */
/*         padding: 0px 8px; */
/*         outline: 0; */
/*         font-weight: bold; */
/*         -webkit-border-radius: 5px; */
/*         -moz-border-radius: 5px; */
/*         border-radius: 5px; */
/*         cursor: pointer; */
}

.headerContainer {
    margin: 8px;
    padding: 10px;
    background-color: headerBgColor;
    color: white;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    border-radius: 5px;
    -moz-box-shadow: 5px 5px 5px #888;
    -webkit-box-shadow: 5px 5px 5px #888;
    box-shadow: 5px 5px 5px #888;
}

.headerContainer a {
    color: white;
}

.headerTitle {
    font-size: 20px;
    font-weight: bold;
}

 .headerDescription {
    font-style: italic;
    margin-left: 10px;
}

.headerHomePageLink {
    float: right;
    margin-top: 3px;
}

.menuScrollContainer {

}

.menuContainer {

}

.menuCategory {
    margin: 5px;
    font-size: 16px;
}

.menuLink {
    margin: 0px 10px;
}

.menuLink a {
    display: block;
    padding: 5px 8px;
    color: black;
    outline: 0px;
}

.menuLinkSelected a {
    background-color: #8C2E0B;
    color: white;
    -moz-box-shadow: 5px 5px 5px #888;
    -webkit-box-shadow: 5px 5px 5px #888;
    box-shadow: 5px 5px 5px #888;
    border-radius: 5px;
    -moz-border-radius: 5px;
}

.menuLink a:hover {
    background-color: #8C501C;
    color: white;
    -moz-box-shadow: 5px 5px 5px #888;
    -webkit-box-shadow: 5px 5px 5px #888;
    box-shadow: 5px 5px 5px #888;
    border-radius: 5px;
    -moz-border-radius: 5px;
}

.sourceContainer {
    padding: 10px;
}

.sourceLink {
    font-weight: bold;
    padding: 3px;
    color: black;
}

.sourceLink:hover {
    cursor: pointer;
    text-decoration: underline;
}

.sourceLinkSelected {
    color: grey;
 }

.sourceLinkSelected:hover {
    cursor: auto;
    text-decoration: none;
}

.mainScrollContainer {
    margin: 5px;
}

.mainContainer {
    margin: 5px;
}

Now, what I don't know how to proceed is adding the widget to my main panel...here is my EntryPoint class, where I'm adding an already existing widget I created before: 现在,我不知道如何继续将小部件添加到我的主面板......这是我的EntryPoint类,我在这里添加一个我之前创建的现有小部件:

public class Gflot_example implements EntryPoint {
public void onModuleLoad() {
  RootPanel.get().add(new Login());
  //RootPanel.get().add(???);   >> new LineExample() does not look like the way to proceed here 
}    

I've checked the EntryPoint in the examples, but it calls a MainWidow class that does lots of stuff I really don't know what is going on there...anyone has a working example NOT from the official examples? 我在示例中检查了EntryPoint ,但是它调用了一个MainWidow类,它做了很多我真的不知道发生了什么的东西......任何人都有一个工作的例子,不是来自官方的例子吗?

Thanks! 谢谢! Alex 亚历克斯

The important part of the examples is the createPlot() method in each one. 示例的重要部分是每个示例中的createPlot()方法。 Rest of the stuff is used to handle history and source code viewing. 其余的东西用于处理历史和源代码查看。

To add a SimplePlot to your RootPanel , just do the following : 要将SimplePlot添加到RootPanel ,只需执行以下操作:

  • Create a PlotModel and add your data to it 创建PlotModel并将数据添加到其中
  • Create a PlotOptions and define the options you want 创建PlotOptions并定义所需的选项
  • Create a SimplePlot with the model and options you created 使用您创建的模型和选项创建SimplePlot
  • Add the SimplePlot to your RootPanel SimplePlot添加到RootPanel

Basically, just copy the createPlot() method, put it inside your EntryPoint and do RootPanel.get().add(createPlot()); 基本上,只需复制createPlot()方法,将其放入EntryPoint并执行RootPanel.get().add(createPlot());

If you haven't done it yet, you also need to include the gflot module. 如果还没有完成,还需要包含gflot模块。 Add <inherits name='com.googlecode.gflot.GFlot'/> to your module descriptor XML file. <inherits name='com.googlecode.gflot.GFlot'/>到模块描述符XML文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM