Android AchartEngine:如何为线图中的每个绘制点附加来自不同来源的其他数据?

时间:2013-07-08 06:15:15

标签: android achartengine

我正在尝试制作能让我在折线图中按下数据点的内容。我创建了一个GraphicalView的子类,并为其添加了一个方法,在单击图形时会调用该方法。

// This goes into the activity
graphicalView.setOnClickListener(new View.OnClickListener() {
    graphicalView.displaySomething();
}

// This one is from the GraphicalView subclass
public void displaySomething() {
    SeriesSelection seriesSelection = getCurrentSeriesAndPoint();
    int pointIndex = seriesSelection.getPointIndex();
    double xValue = seriesSelection.getXValue());

    /*
     Now in this method, I can do some pretty cool stuff like drawing
     additional graphics near the clicked point in the line chart simply
     by redrawing the graph and passing it some data. This is made possible
     by subclassing both LineGraph and ScatterChart.

     For the purposes of this question though, I would simply log the
     values I'm curious about.
     */
     Log.d(TAG, "pointIndex: " + pointIndex + ", xValue: " + xValue);
     repaint();
}

我们只是说我有一张图表显示了我的宠物狗的重量随着时间的推移然后我有一系列的字符串列表解释了为什么我的宠物变得那么重,当时她最喜欢的食物,她的活动,数组中的数据将显示在图形之外的某个位置(即一组TextView)。我期望得到点击点的索引,然后用它来索引数据数组。

pointIndexxValue是我很好奇的变量。在向图表绘制其他图形时,pointIndex非常有用,因为ScatterChart的drawSeries知道如何正确使用此变量。但是,它不能用于我想要实现的目标,因为这个值不是每个点都是常量。例如,我在图中有一个位于(5,10)的点,当图形没有向左平移时,它显示为pointIndex 4的第五个点。但是,当我将图形向左平移以使所提到的点显示为第一个点时,pointIndex将更改为0.这就是为什么我不能使用此变量来索引数据数组。

pointIndex一样,与点相关的大多数值具有相同的行为,但我注意到xValue是常量。我正在考虑使用此值来索引我的外部数据源,可能是使用Map xValue是关键。但是,我无法弄清楚在哪里创建这个数据结构。我查看了achartengine的源代码,看起来Map的初始化的最佳位置是ScatterChart的drawSeries方法。问题是传递给它的浮点数组也相对于图形的平移移动,但我的数据数组不会随之移动。你能指点我正确的方向吗?

2 个答案:

答案 0 :(得分:1)

此代码允许您单击achartengin中的数据点:

mySimpleXYPlot.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // handle the click event on the chart
            if(event.getAction()==MotionEvent.ACTION_DOWN){

                SeriesSelection seriesSelection = mySimpleXYPlot
                        .getCurrentSeriesAndPoint();
                if (seriesSelection == null) {
                    Toast.makeText(Records.this, "No chart element",
                    Toast.LENGTH_SHORT).show();
                } else {
                    // display information of the clicked point
                    Toast.makeText(
                            Records.this,
                            "Chart element in series index "
                                    + seriesSelection.getSeriesIndex()
                                    + " data point index "
                                    + seriesSelection.getPointIndex()
                                    + " was clicked"
                                    + " closest point value X="
                                    + seriesSelection.getXValue() + ", Y="
                                    + seriesSelection.getValue(),
                                    Toast.LENGTH_LONG).show();
                }
            }
            return false;
        }
    });

答案 1 :(得分:0)

我终于明白了。我重写了GraphicalView的onDraw方法,以便我可以提取密钥并将它们附加到我的数据中。它是这样的:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.getClipBounds(mRect);
    int top = mRect.top;
    int left = mRect.left;
    int width = mRect.width();
    int height = mRect.height();

    if (mRenderer.isInScroll()) {
        top = 0;
        left = 0;
        width = getMeasuredWidth();
        height = getMeasuredHeight();
    }

    mChart.draw(canvas, left, top, width, height, mPaint);

    /*
     * This is where we build the map of <data point, info>
     */
    if (mIsFirstDraw && mIsAttachData && mAdditionalPointData != null && mAdditionalPointData.size() > 0) {
        // ClickablePoints is an interface that has a method to get the datapoints rendered by ScatterChart
        List<float[]> pointCoords = ((ClickablePoints) mChart).getDataPointCoords();
        mPointKeys = new double[pointCoords.size()];
        mAdditionalDataMap = new HashMap<Double, String>();

        for (int i = 0, j = mPointKeys.length; i < j; i++)  {
            float[] coords = pointCoords.get(i);
            SeriesSelection seriesSelection = mChart.getSeriesAndPointForScreenCoordinate(
                    new Point(coords[0], coords[1]));
            mPointKeys[i] = seriesSelection.getXValue();
            mAdditionalDataMap.put(seriesSelection.getXValue(), mAdditionalPointData.get(i));
        }

        mIsFirstDraw = false;
    }
}

mIsFirstDraw是一个标志,表示它是第一次绘制图形。之后需要将其删除,以便它不会重复将数据附加到数据上。