MPAndroidChart LineChart自定义高亮显示drawable

时间:2015-07-03 08:16:07

标签: android mpandroidchart

我使用MPAndroid Chart库在我的Android应用中绘制折线图。图的状态如下: MPAndroidChart -  Line Chart

我想改变它:

Line Chart

所以我想改变MPAnroid Line Chart的观点,称为" Circle Hole"在图书馆。我想把这个洞换成可画的。我有办法做到这一点吗?

圆孔更改代码在此处:

LineDataSet set1 = new LineDataSet(yVals, "DataSet 1");
set1.setDrawCircleHole( true );
set1.setCircleColor( Color.BLACK );

图书馆参考在这里:MPAndroidChart

1 个答案:

答案 0 :(得分:10)

从版本3.0开始,您可以提供自己的自定义图像,该图像将在选择图表上的给定点时显示。以下说明符合the wiki

总而言之,您现在可以使用名为MarkerImage的类:

MarkerImage myMarkerImage = new MarkerImage(this, R.drawable.my_drawable);

然后:

myChart.setMarker(myMarkerImage);

要调整图像的位置,您可以调用:

setOffset(float x, float y);

如果您不想使用MarkerView,则可能需要创建自己的LineChartRenderer子类,并添加逻辑以绘制自定义高亮显示。

这是一个非常基本的概念证明:

import android.graphics.Bitmap;
import android.graphics.Canvas;

import com.github.mikephil.charting.animation.ChartAnimator;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.renderer.LineChartRenderer;
import com.github.mikephil.charting.utils.Transformer;
import com.github.mikephil.charting.utils.ViewPortHandler;

import java.util.List;

/**
 * Created by David on 3/01/2017.
 */

public class ImageLineChartRenderer extends LineChartRenderer {

    private final LineChart lineChart;
    private final Bitmap image;

    public ImageLineChartRenderer(LineChart chart, ChartAnimator animator, ViewPortHandler viewPortHandler, Bitmap image) {
        super(chart, animator, viewPortHandler);
        this.lineChart = chart;
        this.image = image;
    }

    @Override
    public void drawExtras(Canvas c) {
        super.drawExtras(c);

        Highlight[] highlighted = lineChart.getHighlighted();
        if (highlighted == null) return;

        float phaseY = mAnimator.getPhaseY();

        float[] imageBuffer = new float[2];
        imageBuffer[0] = 0;
        imageBuffer[1] = 0;
        LineData lineData = mChart.getLineData();
        List<ILineDataSet> dataSets = mChart.getLineData().getDataSets();

        Bitmap[] scaledBitmaps = new Bitmap[dataSets.size()];
        float[] scaledBitmapOffsets = new float[dataSets.size()];
        for (int i = 0; i < dataSets.size(); i++) {
            float imageSize = dataSets.get(i).getCircleRadius() * 10;
            scaledBitmapOffsets[i] = imageSize / 2f;
            scaledBitmaps[i] = scaleImage((int) imageSize);
        }

        for (Highlight high : highlighted) {
            int dataSetIndex = high.getDataSetIndex();
            ILineDataSet set = lineData.getDataSetByIndex(dataSetIndex);
            Transformer trans = lineChart.getTransformer(set.getAxisDependency());

            if (set == null || !set.isHighlightEnabled())
                continue;

            Entry e = set.getEntryForXValue(high.getX(), high.getY());

            if (!isInBoundsX(e, set))
                continue;

            imageBuffer[0] = e.getX();
            imageBuffer[1] = e.getY() * phaseY;
            trans.pointValuesToPixel(imageBuffer);

            c.drawBitmap(scaledBitmaps[dataSetIndex],
                    imageBuffer[0] - scaledBitmapOffsets[dataSetIndex],
                    imageBuffer[1] - scaledBitmapOffsets[dataSetIndex],
                    mRenderPaint);
        }
    }

    private Bitmap scaleImage(int radius) {
        return Bitmap.createScaledBitmap(image, radius, radius, false);
    }
}

这样消费:

Bitmap starBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.star);
mChart.setRenderer(new ImageLineChartRenderer(mChart, mChart.getAnimator(), mChart.getViewPortHandler(), starBitmap));

说明:drawExtras中的LineChartRenderer绘制了圆圈。我们仍然需要这些,所以在drawExtras覆盖中我们首先调用super。然后我们为每个DataSet生成一个缩放的位图(DataSet可能有不同的圆半径)。我们将位图缩放到任意大小(圆半径的10倍)。然后,我们遍历通过渲染器的LineChart字段公开的高光,并绘制相应的位图。

以下是截图 - 您可以看到&#39;明星&#39;位图而不是突出显示的索引上的圆圈:

a line chart with a star instead of a circle at the highlighted point