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 のグループに)。私が期待しているのは、クリックされたポイントのインデックスを取得し、それを使用してデータの配列にインデックスを付けることです。

pointIndexxValueは、私が興味を持っている変数です。pointIndexScatterChart の drawSeries はこの変数を適切に使用する方法を知っているため、グラフに追加のグラフィックを描画するときに非常に役立ちました。ただし、この値はポイントごとに一定ではないため、達成したい目的には使用できません。たとえば、(5, 10) にあるグラフ内のポイントがあり、グラフが左にパンされていない場合、pointIndex 4 の 5 番目のポイントとして表示されます。ただし、グラフを左にパンして、前述のポイントが最初のポイントとして表示されるようにすると、pointIndex が 0 に変わります。これが、この変数を使用してデータ配列のインデックスを作成できない理由です。

と同様に、ポイントに関連するほとんどの値は同じ動作をしますが、一定pointIndexであることに気付きました。xValueこの値を使用して外部データ ソースのインデックスを作成することを検討しています。おそらくxValue、キーである Map を使用します。ただし、このデータ構造を作成する場所がわかりません。achartengine のソース コードを調べたところ、Map の初期化を行うのに最適な場所は 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 に答える