Android中的Achartengine自定义点样式

时间:2014-05-28 11:37:16

标签: android graph achartengine

您好我想在 achartengine 中的分散图表中绘制自定义点样式。 enter image description here

是否可以使用achartengine?如果是,那该怎么办?

2 个答案:

答案 0 :(得分:1)

我能够通过覆盖图形视图的onDraw(Canvas canvas)方法来使用自定义点样式

 protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (mCommonPointBitmap != null ) {
        float halfWitdhCommonPointBitmap = (float) mCommonPointBitmap.getWidth() / 2;
        float halfHeightCommonPointBitmap = (float) mCommonPointBitmap.getHeight() / 2;
        // float halfWitdhyouPointBitmap = (float) mYouPointBitmap.getWidth() / 2;
        // float halfHeightyouPointBitmap = (float) mYouPointBitmap.getHeight() / 2;
        List<double[]> screenPoints = getSeriesSelectionList();
        if (!CommonUtils.isCollectionNullOrEmpty(screenPoints)) {
            int[] margins = mXYChart.getRenderer().getMargins();
            // Info: margin order: top, left, bottom, right
            canvas.clipRect(getLeft() + margins[1], getTop() - margins[0], getRight() - margins[3], getBottom()
                    - margins[2]);
            for (int i = 0; i < screenPoints.size(); i++) {
                double[] screenPoint = screenPoints.get(i);
                if (screenPoint != null) {
                    float pointLeftX = (float) screenPoint[0] - halfWitdhCommonPointBitmap;
                    float pointTopY = (float) screenPoint[1] - halfHeightCommonPointBitmap;
                    canvas.drawBitmap(mCommonPointBitmap, pointLeftX, pointTopY, null);
                }
            }
        }
    }
}



 List<double[]> screenPoints = null;
    XYSeries xySeries = mXYChart.getDataset().getSeriesAt(0);
    int itemCount = xySeries.getItemCount();
    if (itemCount > 0) {
        screenPoints = new ArrayList<double[]>(itemCount);
        double[] realPoints = new double[2];
        for (int itemIndex = 0; itemIndex < itemCount; itemIndex++) {
            realPoints[0] = xySeries.getX(itemIndex);
            realPoints[1] = xySeries.getY(itemIndex);
            screenPoints.add(mXYChart.toScreenPoint(realPoints));
        }
    }
    return screenPoints;
}

答案 1 :(得分:1)

在我看来,一个稍微简单的解决方案是通过子类化Scatterchart,然后覆盖它的绘制系列方法

这样的事情:

public class BitmapChart extends ScatterChart{

public Bitmap awesomeBitmap; 
private static final long serialVersionUID = 1L;

public BitmapChart(XYMultipleSeriesDataset dataset,
        XYMultipleSeriesRenderer renderer, Context context) {
    super(dataset, renderer);
    awesomeBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_awesome_bitmap_resource);
}

@Override
public void drawSeries(Canvas canvas, Paint paint, List<Float> points,
        SimpleSeriesRenderer renderer, float yAxisValue, int seriesIndex, int startIndex) {
    int length = points.size();
    for (int i = 0; i < length; i += 2) {
        canvas.drawBitmap(awesomeBitmap, points.get(i), points.get(i+1), paint);
    }
}

}

然后你可以像这样使用它:

    BitmapChart chart = new BitmapChart(yourDataset,yourChartRenderer,thisContext);
    yourChartView = new GraphicalView(thisContext, chart);