我想让饼图可点击。我正在研究 afreechart 网站上发布的示例(用于制作条形图和饼图)。我试图理解它,我会通过使饼图可点击来进一步了解它。该示例可以在这里找到:https ://code.google.com/p/afreechart/
查看课程
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
bitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(),
Bitmap.Config.ARGB_8888);
canvas.setBitmap(bitmap);
inertialMove();
paintComponent(canvas);
}
public void paintComponent(Canvas canvas) {
// first determine the size of the chart rendering area...
Dimension size = getSize();
RectangleInsets insets = getInsets();
RectShape available = new RectShape(insets.getLeft(), insets.getTop(),
size.getWidth() - insets.getLeft() - insets.getRight(),
size.getHeight() - insets.getTop() - insets.getBottom());
double drawWidth = available.getWidth();
double drawHeight = available.getHeight();
this.scaleX = 1.0;
this.scaleY = 1.0;
if (drawWidth < this.minimumDrawWidth) {
this.scaleX = drawWidth / this.minimumDrawWidth;
drawWidth = this.minimumDrawWidth;
}
else if (drawWidth > this.maximumDrawWidth) {
this.scaleX = drawWidth / this.maximumDrawWidth;
drawWidth = this.maximumDrawWidth;
}
if (drawHeight < this.minimumDrawHeight) {
this.scaleY = drawHeight / this.minimumDrawHeight;
drawHeight = this.minimumDrawHeight;
}
else if (drawHeight > this.maximumDrawHeight) {
this.scaleY = drawHeight / this.maximumDrawHeight;
drawHeight = this.maximumDrawHeight;
}
RectShape chartArea = new RectShape(0.0, 0.0, drawWidth,
drawHeight);
this.chart.draw(canvas,chartArea, this.anchor, this.info);
}
活动
然后我想点击该区域(例如红色区域)以显示一条消息。问题是因为我知道图表是从 afreechart 网站的全部示例代码中绘制的,我可以转到不同颜色的每个区域,单击并显示适当的消息,但图表隐藏在某个地方(它不可见)。问题可能是因为我使用位图和画布的方式......
public boolean onTouchEvent(MotionEvent event){
if(event.getAction()==MotionEvent.ACTION_DOWN){
}
else if (event.getAction()==MotionEvent.ACTION_UP){
int color = bitmap.getPixel((int) event.getX(), (int) event.getY());
System.out.println("color =" +color);
int redValue = Color.red(color);
int blueValue = Color.blue(color);
int greenValue = Color.green(color);
System.out.println("redValue =" +redValue);
System.out.println("blueValue =" +blueValue);
System.out.println("greenValue =" +greenValue);
if (redValue == 255){
Toast.makeText(getBaseContext(), "Is Red", Toast.LENGTH_SHORT).show();
}
if (blueValue == 255){
Toast.makeText(getBaseContext(), "Is BLUE", Toast.LENGTH_SHORT).show();
}
if (greenValue == 255){
Toast.makeText(getBaseContext(), "Is GREEN", Toast.LENGTH_SHORT).show();
}
//}
}
return true;
}
}
请我很高兴得到您的帮助...这真的很令人沮丧,因为首先我是 android 新手,我可以在没有实际看到图表的情况下看到正确的消息