android图表库 - androidplot onclick问题

时间:2011-06-29 14:10:50

标签: android androidplot

我正在尝试使用库“androidplot”。它是否提供onclicklistener?如果我将代码放在oncreate()中,它将起作用。但如果我放入onclik(),它将无法正常工作。有人能告诉我为什么吗? 这是XML文件:

 <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"
    />

这是java代码:

 @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();}}

它会工作,但如果我把它放在onclicklistener中,它将无法工作。以下是代码:

 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 the visual aids for positioning:
            mySimpleXYPlot.disableAllMarkup();
        }
    });

非常感谢!!!

1 个答案:

答案 0 :(得分:1)

尝试使用“mySimpleXYPlot.redraw();”在“mySimpleXYPlot.disableAllMarkup();”

之后

我很确定在创建视图时会绘制图形,所以当您在第一次绘制它之后进行设置时,您将不得不重绘它。如果您在运行时在图表上更改某些内容,我也会这样做。