开发者

android chart library -- androidplot onclick problem

I am trying to use the library "androidplot". Does it supply onclicklistener? If I put the code in oncreate(), it will work. but if I put into onclik(), it won't work. Could someone tell me why? This is XML file:

 <com.androidplot.xy.XYPlot
    android:id="@+id/mySimpleXYPlot"
    android:layout_width="743px"
    android:layout_height="473px"
    android:layout_marginTop="5px"
    android:layout_marginLeft="5px"
    android:layout_marginRight="0px"
    title="Line Chart testing"
    />

This is java code:

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

 // initialize our XYPlot reference:
    mySimpleXYPlot = (XYPlot) findViewById(R.id.mySimpleXYPlot);

    // add a new series
    mySimpleXYPlot.addSeries(new SimpleXYSeries(), LineAndPointRenderer.class, new LineAndPointFormatter(Color.rgb(0, 200, 0), Color.rgb(200, 0, 0)));

    // reduce the number of range labels
    mySimpleXYPlot.getGraphWidget().setRangeTicksPerLabel(4);

    // reposition the domain label to look a little cleaner:
    Widget domainLabelWidget = mySimpleXYPlot.getDomainLabelWidget();

    mySimpleXYPlot.position(domainLabelWidget,                     // the widget to position
                            45,                                    // x position value, in this case 45 pixels
                            XLayoutStyle.ABSOLUTE_FROM_LEFT,       // how the x position value is applied, in this case from the left
                            0,                                     // y position value
                            YLayoutStyle.ABSOLUTE_FROM_BOTTOM,     // how the y position is applied, in this case from the bottom
                            AnchorPosition.LEFT_BOTTOM);           // point to use as the origin of the widget being positioned

    // get rid of the visual aids for positioning:
    mySimpleXYPlot.disableAllMarkup();}}

It will work, but if i put this in onclicklistener, it won't work. Here are the code:

 Button oneday = (Button) findViewById(R.id.oneday);
    oneday.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
             // initialize our XYPlot reference:
            mySimpleXYPlot = (XYPlot) findViewById(R.id.mySimpleXYPlot);

            // add a new series
            mySimpleXYPlot.addSeries(new SimpleXYSeries2(), LineAndPointRenderer.class, new LineAndPointFormatter(Color.rgb(0, 200, 0), Color.rgb(200, 0, 0)));

            // reduce the number of range labels
            mySimpleXYPlot.getGraphWidget().setRangeTicksPerLabel(4);

            // reposition the domain label to look a little cleaner:
            Widget domainLabelWidget = mySimpleXYPlot.getDomainLabelWidget();

            mySimpleXYPlot.position(domainLabelWidget,                     // the widget to position
                                    45,                                    // x position value, in this case 45 pixels
                                    XLayoutStyle.ABSOLUTE_FROM_LEFT,       // how the x position value is applied, in this case from the left
                                    0,                                     // y position value
                                    YLayoutStyle.ABSOLUTE_FROM_BOTTOM,     // how the y position is applied, in this case from the bottom
                                    AnchorPosition.LEFT_BOTTOM);           // point to use as the origin of the widget being positioned

            // get rid of开发者_如何学Python the visual aids for positioning:
            mySimpleXYPlot.disableAllMarkup();
        }
    });

Many thanks!!!


Try with "mySimpleXYPlot.redraw();" after "mySimpleXYPlot.disableAllMarkup(); "

I'm quite sure that the graph is drawn when the views are created, so when you set it up after it has been drawn the first time, you will have to redraw it. The same goes if you change something on the graph at runtime i think.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜