AchartEngine得到触摸点位置 [英] AchartEngine get position of touched point
问题描述
我用achartengine绘制折线图。我用这个code来获得当前点sellected
`view.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
//图表上处理click事件,
I use achartengine to draw a line chart. I use this code to get current point sellected `view.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // handle the click event on the chart
quickAction.show(v);
SeriesSelection seriesSelection = view.getCurrentSeriesAndPoint();
if (seriesSelection != null) {
if(mToast == null){
Toast.makeText( mContext , "" , Toast.LENGTH_SHORT );
}
mToast.setText( "" + seriesSelection.getValue() + "mg/dL");
mToast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0);
mToast.show();
} else {
Log.i(this.toString(), "OnClickListener" + v.getX() + "y:" + v.getY());
}
}
});`
现在我想这点或触摸的位置的位置显示一个气泡显示细节点,为帮助任何想法
对于examp
Now i want to get position of this point or position of touch to display a bubble to show detail point, any idea for help for examp
https://dl.dropboxusercontent.com/u/5860710/Untitled.jpg
推荐答案
你试过吗?
SeriesSelection seriesSelection = view.getCurrentSeriesAndPoint();
double[] xy = view.toRealPoint(0);
Log.i(this.toString(), "OnClickListener" + xy[0] + "y:" + xy[1]);
或<一个更好看href=\"http://$c$c.google.com/p/achartengine/source/browse/trunk/achartengine/demo/org/achartengine/chartdemo/demo/chart/XYChartBuilder.java\"相对=nofollow>这个例子的行:167-172
or much better look at this example rows: 167-172
好吧,试试这个,对于数据集中的每个点:
Ok, try this, for each point in your dataset:
- 变换真正点到屏幕上点
- 从触摸事件计算距离
- 如果该距离小于指定量
(2 *的pointsize在这个例子中),然后抓住价值,并显示您的弹出
我不喜欢这里要说的是,在最坏的情况下,你已经遍历所有的点...但我希望这会给你一些提示。
What I don't like here is that, in the worst case, you've to iterate all the points...but i hope that this will give you some hints.
final XYChart chart = new LineChart(mDataset, mRenderer);
mChartView = new GraphicalView(this, chart);
mChartView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
XYSeries series = mDataset.getSeriesAt(0);
for(int i = 0; i < series.getItemCount(); i++) {
double[] xy = chart.toScreenPoint(new double[] { series.getX(i), series.getY(i) }, 0);
double dx = (xy[0] - event.getX());
double dy = (xy[1] - event.getY());
double distance = Math.sqrt(dx*dx + dy*dy);
if (distance <= 2*pointSize) { //.pointSize that you've specified in your renderer
SeriesSelection sel =
chart.getSeriesAndPointForScreenCoordinate(new Point((float)xy[0], (float)xy[1]));
if (sel != null) {
Toast.makeText(XYChartBuilder.this, "Touched: " + sel.getValue(), Toast.LENGTH_SHORT).show();
}
break;
}
Log.i("LuS", "dist: " + distance);
}
return true;
}
});
这篇关于AchartEngine得到触摸点位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!