如何在BarChart项目的近端位置显示弹出菜单单击?
我创建了一个应用程序来显示条形图项目上的弹出菜单。问题是弹出菜单没有显示在单击的条形图项目附近。它始终显示在顶部。因此,请帮我在条形图项目点击附近显示弹出菜单。
主要活动:
package com.example.saravanakumars.spinnerchart;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
String[] chart = {"Select", "Bar Chart", "Pie Chart"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter aa = new ArrayAdapter(this, android.R.layout.simple_spinner_item, chart);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(aa);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String items = spinner.getSelectedItem().toString();
switch (items) {
case "Select":
Toast.makeText(getApplicationContext(),"Please Select Bar or Pie Chart",Toast.LENGTH_SHORT).show();
break;
case "Bar Chart":
Intent intent = new Intent(MainActivity.this, barchart.class);
startActivity(intent);
break;
case "Pie Chart":
Intent p = new Intent(MainActivity.this, piechart.class);
startActivity(p);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Barchart.java
package com.example.saravanakumars.spinnerchart;
/**
* Created by saravanakumars on 9/18/2017.
*/
import java.util.ArrayList;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.Toast;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.ColorTemplate;
import java.util.ArrayList;
public class barchart extends AppCompatActivity {
BarChart chart ;
ArrayList<BarEntry> BARENTRY ;
ArrayList<String> BarEntryLabels ;
BarDataSet Bardataset ;
BarData BARDATA ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bar_chart);
chart = (BarChart) findViewById(R.id.barchart);
BARENTRY = new ArrayList<>();
BarEntryLabels = new ArrayList<String>();
AddValuesToBARENTRY();
AddValuesToBarEntryLabels();
Bardataset = new BarDataSet(BARENTRY, "Projects");
BARDATA = new BarData(BarEntryLabels, Bardataset);
Bardataset.setColors(ColorTemplate.COLORFUL_COLORS);
chart.setData(BARDATA);
chart.animateY(3000);
// chart.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// PopupMenu popup = new PopupMenu(barchart.this,chart);
//
// popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
// popup.show();
//
// popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
// public boolean onMenuItemClick(MenuItem item) {
// Toast.makeText(barchart.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
// return true;
// }
// });
// }
// });
chart.setOnChartValueSelectedListener( new OnChartValueSelectedListener() {
@Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
PopupMenu popup = new PopupMenu(barchart.this,chart);
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
popup.show();
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(barchart.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
return true;
}
});
}
@Override
public void onNothingSelected() {
}
});
}
public void AddValuesToBARENTRY(){
BARENTRY.add(new BarEntry(2f, 0));
BARENTRY.add(new BarEntry(4f, 1));
BARENTRY.add(new BarEntry(6f, 2));
BARENTRY.add(new BarEntry(8f, 3));
BARENTRY.add(new BarEntry(7f, 4));
BARENTRY.add(new BarEntry(3f, 5));
}
public void AddValuesToBarEntryLabels(){
BarEntryLabels.add("January");
BarEntryLabels.add("February");
BarEntryLabels.add("March");
BarEntryLabels.add("April");
BarEntryLabels.add("May");
BarEntryLabels.add("June");
}
}
popup menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/one"
android:title="Type-1"/>
<item
android:id="@+id/two"
android:title="Type-2"/>
</menu>
感谢。
答案 0 :(得分:2)
尝试在MPAndroidChart中使用MarkerView。 Check this link。我认为这将有助于你完成工作。
答案 1 :(得分:0)
如果要在指定位置的弹出窗口中显示内容视图。您应该使用showAtLocation(View view, int gravity, int x, int y)
方法而不是show()
方法。
在此处详细了解showAtLocation方法showAtLocation()