簡體   English   中英

使用MPAndroid圖表庫,如何在條形圖中單擊x軸或y軸項目上的每個單獨項目時打開不同的活動?

[英]How to open different activity on click of every individual item on x-axis or y- axis item in bar chart with MPAndroid chart library?

在MPAndroid圖表庫中,如何使條形圖中的x軸或y軸項目中的每個單個項目都可單擊?

例如:如果條形圖的x軸為1,2,3,4,5,6,7,條形圖的y軸為100,200,300,400,500,600,700

如果單擊1或100,則應打開一個活動,如果單擊2或200,則應打開另一個活動,依此類推。

MainActivity.java

 public class MainActivity extends AppCompatActivity implements OnChartValueSelectedListener {

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

            BarChart barChart = (BarChart) findViewById(R.id.chart);

    barChart.setHighlightPerTapEnabled(false);


            barChart.setDescription("");
            // set xaxis at bottom
            barChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
            // set xaxis
            barChart.getXAxis().setGridColor(Color.rgb(255,255,255));
            // set x axis text color
            barChart.getXAxis().setTextColor(Color.argb(42,44,44,44));
           // HorizontalBarChart barChart= (HorizontalBarChart) findViewById(R.id.chart);

            barChart.getAxisRight().setEnabled(false);
            barChart.setScaleEnabled(false);

            barChart.getLegend().setEnabled(false);
            barChart.setTouchEnabled(true);
            barChart.setDragEnabled(false);
            barChart.setScaleEnabled(false);
            barChart.setScaleXEnabled(false);
            barChart.setScaleYEnabled(false);
            barChart.setPinchZoom(false);
            barChart.setHighlightPerDragEnabled(false);
            barChart.setHighlightPerTapEnabled(false);
            barChart.setDrawGridBackground(false);
            barChart.setGridBackgroundColor(Color.rgb(255,255,255));
            barChart.getXAxis().setGridColor(Color.rgb(255,255,255));
            ArrayList<BarEntry> entries = new ArrayList<>();
            entries.add(new BarEntry(100, 0));
            entries.add(new BarEntry(200, 1));
            entries.add(new BarEntry(300, 2));
            entries.add(new BarEntry(400, 3));
            entries.add(new BarEntry(500, 4));
            entries.add(new BarEntry(600, 5));
            entries.add(new BarEntry(700, 6));
            barChart.setOnChartValueSelectedListener(this);

            BarDataSet dataset = new BarDataSet(entries, "# of Calls");

            ArrayList<String> labels = new ArrayList<String>();
            labels.add("1");
            labels.add("2");
            labels.add("3");
            labels.add("4");
            labels.add("5");
            labels.add("6");
            labels.add("7");




            BarData data = new BarData(labels, dataset);
            dataset.setColor(Color.rgb(33,200,215));

            barChart.setData(data);
            barChart.animateY(5000);

        }

        @Override
        public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {

                Intent i = new Intent(MainActivity.this, Second.class);
                startActivity(i);

        }

        @Override
        public void onNothingSelected() {

        }
    }

答案應該是這樣的(對於舊版本的MPAndroidChart):

通過e.getXIndex(); 我們可以找到x軸索引,然后對其施加條件。 每個單獨的項目點擊都會進入不同的活動。

@Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
    int x = e.getXIndex(); 

    if (x == 0) {
        Intent j = new Intent(MainActivity.this, Second.class);
        startActivity(j);
    }
    else if (x==1) {
        Intent i= new Intent(MainActivity.this, Third.class);
        startActivity(i);
        }
    else if (x==2) {
        Intent k= new Intent(MainActivity.this, Fourth.class);
        startActivity(k);
    }
    else if (x==3) {
        Intent k1 = new Intent(MainActivity.this, Fifth.class);
        startActivity(k1);
    }
    else if (x==4) {
        Intent k2 = new Intent(MainActivity.this, Sixth.class);
        startActivity(k2);
    }
    else if (x==5) {
        Intent k3 = new Intent(MainActivity.this, Seventh.class);
        startActivity(k3);
    }
    else if (x==6) {
        Intent k4 = new Intent(MainActivity.this, Eight.class);
        startActivity(k4);
    }
}

請花一些時間閱讀MPAndroidChart的Wiki和Javadoc,以了解基本概念。 請記住,您可以在PC上使用Ctr-Q或在Mac上使用Cmd-J來獲取您在Android Studio中光標所在的類的工具提示。

MPAndroidChart 3.0.2中的解決方案如下所示:

    @Override
    public void onValueSelected(Entry e, Highlight h) {
        float x = e.getX(); //get the x value
        int roundedX = (int) Math.round(x); //round it to an integer
        Intent nextActivity;
        if (roundedX == 1) {
            nextActivity = new Intent(MainActivity.this, FirstActivity.class);
            startActivity(nextActivity);
        }
    }

一個更聰明的解決方案是將類對象存儲在數組或列表中,而不是使用條件語句,但最好先嘗試簡單的方法。 請花一些時間來學習Java和Android的基本概念,否則您將很難取得進步。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM