0

我正在尝试制作一些可以让我在折线图中按下数据点的东西。我创建了 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)。我期望的是获得点击点的索引,然后用它来索引数据数组。

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

pointIndex一样,与点相关的大多数值都具有相同的行为,但我注意到这xValue是恒定的。我正在考虑使用这个值来索引我的外部数据源,也许是通过使用 Map where xValueis key。但是,我不知道在哪里创建这个数据结构。我查看了 achartengine 的源代码,似乎进行地图初始化的最佳位置是在 ScatterChart 的 drawSeries 方法中。问题是传递给它的浮点数组也相对于图形的平移移动,但我的数据数组不会随之移动。你能指出我正确的方向吗?

4

2 回答 2

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;
        }
    });
于 2013-07-08T06:50:07.000 回答
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 是一个标志,表示这是第一次绘制图形。之后需要将其删除,以免重复将密钥附加到数据。

于 2013-07-08T08:40:14.607 回答