条形图使用aChartEngine设置显示图表值

时间:2014-10-02 21:25:17

标签: android onclick achartengine

我创建了一个动态条形图布局。在按钮上单击图表重新绘制。所以一切都很好,但是我遇到的问题是setDisplayChartValues不仅显示当前的图表高度,还会显示之前高度的值。

我找到了解决方案,代码已更新

package com.example.viewpagertest2;


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 com.example.viewpagertest2.R;
import com.example.viewpagertest2.R.color;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Tab1Activity extends Fragment {

    Button b99;
    int theValue;
    int mCurCheckPosition = 1;

    //PositionX Satz 1
    int position11;
    int position21;
    int position31;
    int position41;
    int position51;
    int position61;
    //
     private GraphicalView mChartView;
    // Smash Satz 1
    TextView tv;
    TextView tv2;
    TextView tv3;
    TextView tv4;
    TextView tv5;

    //Oberpass Satz 1

    TextView tv6;
    TextView tv7;
    TextView tv8;
    TextView tv9;
    TextView tv10;

    // Finte

    TextView tv11;
    TextView tv12;
    TextView tv13;
    TextView tv14;
    TextView tv15;

    // Manchette

    TextView tv16;
    TextView tv17;
    TextView tv18;
    TextView tv19;
    TextView tv20;

    XYSeriesRenderer renderer = new XYSeriesRenderer(); 
    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
    XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer(); 
     LinearLayout chartContainer;
    @SuppressWarnings("deprecation")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.firsttabview, container, false);
        b99 = (Button) rootView.findViewById(R.id.refreshButton1);


       tv = (TextView) rootView.findViewById(R.id.SP1);
       tv2 = (TextView) rootView.findViewById(R.id.SA1);
       tv3 = (TextView) rootView.findViewById(R.id.SN1);
       tv4 = (TextView) rootView.findViewById(R.id.SB1);
       tv5 = (TextView) rootView.findViewById(R.id.SAB1);

       tv6 = (TextView) rootView.findViewById(R.id.OP1);
       tv7 = (TextView) rootView.findViewById(R.id.OA1);
       tv8 = (TextView) rootView.findViewById(R.id.ON1);
       tv9 = (TextView) rootView.findViewById(R.id.OB1);
       tv10= (TextView) rootView.findViewById(R.id.OAB1);

       tv11 = (TextView) rootView.findViewById(R.id.FP1);
       tv12 = (TextView) rootView.findViewById(R.id.FA1);
       tv13 = (TextView) rootView.findViewById(R.id.FN1);
       tv14 = (TextView) rootView.findViewById(R.id.FB1);
       tv15 = (TextView) rootView.findViewById(R.id.FAB1);

       tv16 = (TextView) rootView.findViewById(R.id.MP1);
       tv17 = (TextView) rootView.findViewById(R.id.MA1);
       tv18 = (TextView) rootView.findViewById(R.id.MN1);
       tv19 = (TextView) rootView.findViewById(R.id.MB1);
       tv20 = (TextView) rootView.findViewById(R.id.MAB1);

       b99.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

//////just textView things////////
                     //theValue = SecondFragment.getSmashValue();
                    theValue = DrawArea.getSmashPunktSatz1Value();
                    tv.setText("" + theValue);
                    int s1sa = DrawArea.getSmashAusSatz1Value();
                    tv2.setText("" + s1sa);
                    int s1sn = DrawArea.getSmashNetzSatz1Value();
                    tv5.setText("" + s1sn);
                    int s1sb = DrawArea.getSmashBlockSatz1Value();
                    tv4.setText("" + s1sb);
                    int s1sab = DrawArea.getSmashAbnahmeSatz1Value();
                    tv3.setText("" + s1sab);

                    int s1op = DrawArea.getOberpassPunktSatz1Value();
                    tv6.setText("" + s1op);
                    int s1oa = DrawArea.getOberpassAusSatz1Value();
                    tv7.setText("" + s1oa);
                    int s1on = DrawArea.getOberpassNetzSatz1Value();
                    tv10.setText("" + s1on);
                    int s1ob = DrawArea.getOberpassBlockSatz1Value();
                    tv9.setText("" + s1ob);
                    int s1oab = DrawArea.getOberpassAbnahmeSatz1Value();
                    tv8.setText("" + s1oab);

                    int s1fp = DrawArea.getFintePunktSatz1Value();
                    tv11.setText("" + s1fp);
                    int s1fa = DrawArea.getFinteAusSatz1Value();
                    tv12.setText("" + s1fa);
                    int s1fn = DrawArea.getFinteNetzSatz1Value();
                    tv15.setText("" + s1fn);
                    int s1fb = DrawArea.getFinteBlockSatz1Value();
                    tv14.setText("" + s1fb);
                    int s1fab = DrawArea.getFinteAbnahmeSatz1Value();
                    tv13.setText("" + s1fab);

                    int s1mp = DrawArea.getManchettePunktSatz1Value();
                    tv16.setText("" + s1mp);
                    int s1ma = DrawArea.getManchetteAusSatz1Value();
                    tv17.setText("" + s1ma);
                    int s1mn = DrawArea.getManchetteNetzSatz1Value();
                    tv20.setText("" + s1mn);
                    int s1mb = DrawArea.getManchetteBlockSatz1Value();
                    tv19.setText("" + s1mb);
                    int s1mab = DrawArea.getManchetteAbnahmeSatz1Value();
                    tv18.setText("" + s1mab);

////// that's the important code, above are just some text fields ///////////
                     position11 = DrawArea.getPosition11Value();
                     position21 = DrawArea.getPosition21Value();
                     position31 = DrawArea.getPosition31Value();
                     position41 = DrawArea.getPosition41Value();
                     position51 = DrawArea.getPosition51Value();
                     position61 = DrawArea.getPosition61Value();

                int[] y = { position11,position21,position31,position41,position51, position61};

                CategorySeries series = new CategorySeries("Bälle pro Position");
                for (int i = 0; i < y.length; i++) {
                    series.add("Bar " + (i+1), y[i]);
                }

                renderer = new XYSeriesRenderer(); 
                dataset = new XYMultipleSeriesDataset();
                mRenderer = new XYMultipleSeriesRenderer();

                dataset.addSeries(series.toXYSeries());





                mRenderer.setChartTitle("Ballverteilung");
                mRenderer.setXTitle("Position");
                mRenderer.setYTitle("Bälle");
                mRenderer.setAxesColor(Color.BLACK);
                mRenderer.setLabelsColor(Color.BLACK);
                mRenderer.setMarginsColor(1);
                mRenderer.setXLabelsColor(Color.BLACK);
                mRenderer.setYLabelsColor(getTargetRequestCode(), Color.BLACK);
                mRenderer.setXAxisMax(6.5);
                mRenderer.setXLabels(0);
                mRenderer.addTextLabel(1, "Position 1");
                mRenderer.addTextLabel(2, "Position 2");
                mRenderer.addTextLabel(3, "Position 3");
                mRenderer.addTextLabel(4, "Position 4");
                mRenderer.addTextLabel(5, "Position 5");
                mRenderer.addTextLabel(6, "Position 6");
                mRenderer.setXAxisMin(0.5);
                mRenderer.setYAxisMax(30);
                mRenderer.setYAxisMin(0);
                mRenderer.setZoomEnabled(false,false);
                mRenderer.setPanEnabled(false, true);


                renderer.setDisplayChartValues(true);
                renderer.setChartValuesSpacing((float) 0.5);
                renderer.setColor(Color.rgb(255,139,97));
                renderer.setShowLegendItem(false);
                renderer.setChartValuesTextSize(40);
                mRenderer.addSeriesRenderer(renderer);
                /






                 if(mChartView == null){  

             mChartView = ChartFactory.getBarChartView(getActivity(), dataset, mRenderer, Type.STACKED);
             chartContainer.addView(mChartView);
         }

         if (mChartView != null) {
            chartContainer.removeView(mChartView);

            mChartView = ChartFactory.getBarChartView(getActivity(), dataset, mRenderer, Type.STACKED);

            mChartView.repaint();
            chartContainer.addView(mChartView);// to refresh the graph 

            }

        }

    });




   mChartView = ChartFactory.getBarChartView(getActivity(), dataset, mRenderer, Type.STACKED);
 // Bar 1
    chartContainer = (LinearLayout) rootView.findViewById(
           R.id.LinearLayout);



    // Getting PieChartView to add to the custom layout

   //chartContainer.addView(mChartView);
    b99.performClick();

    return rootView;


} 

2 个答案:

答案 0 :(得分:1)

所以我首先喜欢@ashoke告诉我。我补充说:

renderer = new XYSeriesRenderer(); 
dataset = new XYMultipleSeriesDataset();
mRenderer = new XYMultipleSeriesRenderer();
之后,酒吧不再出现了。

所以我改变了onClick方法中的if语句: 我添加了第一个if语句,以防View不存在。之后,图表被添加,并在第二个参数中重新绘制。

 if(mChartView == null){  

                 mChartView = ChartFactory.getBarChartView(getActivity(), dataset, mRenderer, Type.STACKED);
                 chartContainer.addView(mChartView);
             }

             if (mChartView != null) {
                chartContainer.removeView(mChartView);

                mChartView = ChartFactory.getBarChartView(getActivity(), dataset, mRenderer, Type.STACKED);

                mChartView.repaint();
                chartContainer.addView(mChartView);// to refresh the graph 

                }

答案 1 :(得分:0)

您正在重复使用相同的渲染器和数据集对象。将以下内容添加到onclick方法的开头

renderer = new XYSeriesRenderer(); 
dataset = new XYMultipleSeriesDataset();
mRenderer = new XYMultipleSeriesRenderer();

每次单击按钮时,都会创建新的渲染器和数据集对象。或者,您可以在添加新标签之前尝试拨打onClick中的clearXTextLabels and clearYTextLabels