无法动态绘制条形图[AChartEngine]

时间:2014-02-25 16:38:43

标签: android achartengine

我一直试图在Android上动态绘制条形图,但无济于事。

我似乎在我的代码中遗漏了一些内容,但我无法弄清楚它是什么。我做了一个新项目,有助于清楚地说明我的问题。此代码应该允许条形图在按下按钮时更改,但是当按下它时,它看起来只是已被清除,但没有重新绘制。这是我的MainActivity.java代码:

    package com.example.dynamicbargraph;

    import org.achartengine.GraphicalView;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.LinearLayout;
    import android.app.Activity;

    public class MainActivity extends Activity {

        private BarGraph bar = new BarGraph();
        private GraphicalView barView;


       @Override
       protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);

            //bar graph view initialize
            barView = bar.getBarView(this);
            LinearLayout barLayout = (LinearLayout) findViewById(R.id.barchart);
            barLayout.addView(barView);


        }

        public void buttonHandler(View view)
        {
            LinearLayout barLayout = (LinearLayout) findViewById(R.id.barchart);
            barLayout.removeView(barView);
            bar.staticPlot();
            barView.repaint();
        }

    }

这是BarGraph.java代码:

    package com.example.dynamicbargraph;

    import org.achartengine.ChartFactory;
    import org.achartengine.GraphicalView;
    import org.achartengine.chart.BarChart.Type;
    import org.achartengine.model.CategorySeries;
    import org.achartengine.model.XYMultipleSeriesDataset;
    import org.achartengine.renderer.XYMultipleSeriesRenderer;
    import org.achartengine.renderer.XYSeriesRenderer;
    import android.content.Context;
    import android.graphics.Color;


    public class BarGraph {

        private GraphicalView view;

        private CategorySeries series = new CategorySeries("Accelerometer");
        private XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        private XYSeriesRenderer renderer = new XYSeriesRenderer(); //used for customizing
        private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();


        @SuppressWarnings("deprecation")
        public BarGraph()
        {


            int[] y ={1,2,3,4,5,6,7,8,9,10};

            for (int i = 0; i < 10; i++)
            {
                series.add("Bar" + (i+1), y[i]);
            }


            dataset.addSeries(series.toXYSeries());


            //below are just customization codes
            renderer.setChartValuesSpacing((float) 0.5);
            renderer.setColor(Color.GRAY);


            mRenderer.addSeriesRenderer(renderer);
            mRenderer.setChartTitle("Arm Steadiness From Device");
            mRenderer.setXTitle("Frequency (Hz)");
            mRenderer.setYTitle("Magnitude");

            float textSize = 30, textSmaller = 20, angle = 270;
            mRenderer.setAxisTitleTextSize(textSize);
            mRenderer.setYLabelsAngle(angle);
            mRenderer.setLabelsTextSize(textSize);
            mRenderer.setChartValuesTextSize(textSize);
            mRenderer.setLegendTextSize(textSmaller);
            mRenderer.setChartTitleTextSize(textSize);
            mRenderer.setAxesColor(Color.YELLOW);
            mRenderer.setGridColor(Color.YELLOW);
            mRenderer.setLabelsColor(Color.rgb(112, 128, 144));

            //adjusting margin width of y-axis (so y axis labels can be seen)
            int[] i = mRenderer.getMargins();
            i[0] += 50;//top
            i[1] += 50;//left (increment here)
            //i[2] = 0;//bottom
            //i[3] = 0;//right   
            mRenderer.setMargins(i);
        }

        public GraphicalView getBarView(Context context)
        {
            view = ChartFactory.getBarChartView(context, dataset, mRenderer, Type.DEFAULT);
            return view;
        }

        public void staticPlot()
        {
            int[] y ={10,9,8,7,6,5,4,3,2,1};

            for (int i = 0; i < 10; i++)
            {
                series.add("Bar" + (i+1), y[i]);
            }
        }
    }

最后,在单击时调用buttonHandler的activity_main.xml代码:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000000"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >

        <LinearLayout
            android:id="@+id/barchart"
            android:layout_width="fill_parent"
            android:layout_height="400dp"
            android:orientation="vertical" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:onClick="buttonHandler"
            android:text="@string/hello_world" />

    </RelativeLayout>

我知道我在buttonHandler中遗漏了一些东西,但我不知道它是什么。任何帮助将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:2)

public void buttonHandler(View view)
{
    LinearLayout barLayout = (LinearLayout) findViewById(R.id.barchart);
    barLayout.removeView(barView);
    bar.staticPlot();

    barView = bar.getBarView(this); //Grab the newest graph

    barLayout.addView(barView);
    barView.repaint();
}

public void staticPlot() {

    dataset = new XYMultipleSeriesDataset(); //Clear the previous dataset (maybe there is a remove instead?)

    int[] y = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
    series.clear(); //Clear your previous values

    for (int i = 0; i < 10; i++) {
        series.add("Bar" + (i + 1), y[i]);
    }

    dataset.addSeries(series.toXYSeries()); //Readd to dataset
}

在我的机器上运行,希望这有帮助,解释在评论中

编辑:更多评论/拼写错误

答案 1 :(得分:0)

在你的onClickListener中,看起来你正在从它的父LinearLayout中删除View,然后尝试重新绘制而不重新添加它。我不确定删除它是什么意思,所以也许试试这个:

public void buttonHandler(View view) {
        if(bar != null) {
            //LinearLayout barLayout = (LinearLayout) findViewById(R.id.barchart);
            //barLayout.removeView(barView);
            bar.staticPlot();
            bar.repaint();
        }
    }

忘记将其从布局中删除,然后重新绘制它。请记住,你的staticPlot方法总是会绘制相同的东西(但你可能知道当你称它为“staticPlot”时),所以除了相同数据点的附加系列之外,不要指望不同的结果。

public void staticPlot() {
        int[] y ={ 10,9,8,7,6,5,4,3,2,1 };
        for (int i = 0; i < 10; i++) series.add("Bar" + (i+1), y[i]);
        // dataset = new XYMultipleSeriesDataset(); <--- You may want to create a new dataset if you're not going to create a new series
        dataset.addSeries(series.toXYSeries());
    }

希望指出你正确的方向〜