MPAndroidChart:同一个高亮显示的多个标记

时间:2018-04-13 13:20:03

标签: android mpandroidchart

我希望点击展示三个tooltips/markers,只展示一个dataset

如下图所示:

enter image description here

我该怎么做?

1 个答案:

答案 0 :(得分:1)

我创建了一个 LineChart 并为其提供了数据。 对于多个标记,我创建了一个实现 IMarker 的类并创建了 2 个对象 我为每个标记的信息制作的类。 第二个标记的显示方式取决于第一个标记的位置。 我知道这可能不是一个好的实现,但有效......

这是对我有用的代码:

public class ImageMakerTwoINOne implements IMarker {
    private Context mContext;
    public static final String TAG = "marker class";

    ObjectImage o1 = new ObjectImage();
    ObjectImage o2 = new ObjectImage();

    //constructor for initialization
    public ImageMakerTwoINOne(Context context, int drawableResourceId,int drawableResourceId2) {
        mContext = context;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            o1.drawable = mContext.getResources().getDrawable(drawableResourceId, null);
            o2.drawable = mContext.getResources().getDrawable(drawableResourceId2,null);
        } else {
             o1.drawable = mContext.getResources().getDrawable(drawableResourceId);
             o2.drawable = mContext.getResources().getDrawable(drawableResourceId2);
         }
    }


//////////////////////////////offset////////////////////////////
//setting the offsets by the users
public void setOffset(MPPointF offset,MPPointF offset2) {
    o1.offset = offset;
    o2.offset = offset2;
    //in case they were null :
    if (offset == null) o1.offset = new MPPointF();
    else if (offset2 == null) o2.offset = new MPPointF();
}
//setting the offsets by the user
public void setOffset(float offsetX, float offsetY , float offsetX2 , float offsetY2) {
    o1.offset.x = offsetX;
    o1.offset.y = offsetY;
    o2.offset.x = offsetX2;
    o2.offset.y = offsetY2;
}

@Override
public MPPointF getOffset() { return o2.offset; }

public MPPointF getOffset2(){ return o2.offset; }
////////////////////////size//////////////////////////////
//setting the size by the user
public void setSize(FSize size , FSize size2) {
    o1.size = size;
    o2.size = size2;
}
//getting the size of the objects
public FSize getSize1() { return o1.size; }
public FSize getSize2() { return o2.size; }
//////////////////chart///////////////////////////////////
//setting the charts
public void setChartView(Chart chart,Chart chart2) {
    o1.weakChart = new WeakReference<>(chart);
    o2.weakChart = new WeakReference<>(chart2);
}
//getting chart
public Chart getChartView1() {return o1.weakChart == null ? null : o1.weakChart.get(); }
public Chart getChartView2() { return o2.weakChart == null ? null : o2.weakChart.get(); }

//getting offset
@Override
public MPPointF getOffsetForDrawingAtPoint(float posX, float posY) {
    MPPointF offset = getOffset();
    Chart chart = getChartView1();

    float width1 = o1.size.width;
    float height1 = o1.size.height;
    if (width1==0.f){ width1 = o1.drawable.getIntrinsicWidth(); }
    if (height1==0.f){ height1 = o1.drawable.getIntrinsicHeight(); }
    MPPointF alaki = getOffsetForDrawingAtPoint2(posX,posY);
    return o1.offset;
}
public MPPointF getOffsetForDrawingAtPoint2(float posx , float posY){
    MPPointF offset = getOffset();
    Chart chart = getChartView1();

    float width1 = o2.size.width;
    float height1 = o2.size.height;
    if (width1==0.f){ width1 = o2.drawable.getIntrinsicWidth(); }
    if (height1==0.f){ height1 = o2.drawable.getIntrinsicHeight(); }
    return o2.offset;
}

@Override
public void refreshContent(Entry e, Highlight highlight) {

}

     @Override
     public void draw(Canvas canvas, float posX, float posY) {
        Canvas canvas2 = canvas;
        if (o1.drawable == null) return;
        MPPointF offset = getOffsetForDrawingAtPoint(posX,posY); //main position
        MPPointF offset2 = o2.offset; //main position obj 2
        float width = o1.size.width;
        float height = o1.size.height;
        if (width == 0.f) { width = o1.drawable.getIntrinsicWidth(); }
        if (height == 0.f) { height = o1.drawable.getIntrinsicHeight();}

        float width2 = 128;//o2.size.width;
        float height2 = 129;//o2.size.height;
        if (width == 0.f) { width = o2.drawable.getIntrinsicWidth(); }
        if (height == 0.f) { height = o2.drawable.getIntrinsicHeight();}

        o1.drawable.copyBounds(o1.drawableBoundsCache);
        o1.drawable.setBounds(
            o1.drawableBoundsCache.left,
            o1.drawableBoundsCache.top,
            (int) (o1.drawableBoundsCache.left+width),
            (int) (o1.drawableBoundsCache.top+height)
        );

        o2.drawable.copyBounds(o2.drawableBoundsCache);
        o2.drawable.setBounds(
            o2.drawableBoundsCache.left,
            o2.drawableBoundsCache.top,
            (int) (o2.drawableBoundsCache.left+width2),
            (int) (o2.drawableBoundsCache.top+height2)
        );

        int saveId = canvas.save();
        int saveId2 = canvas2.save();
    
        canvas.translate(posX + offset.x , posY + offset.y);
        o1.drawable.draw(canvas);
        canvas2.translate( 75 , -45 );

        o2.drawable.draw(canvas2);

        canvas2.restoreToCount(saveId2);
        canvas.restoreToCount(saveId);

        o1.drawable.setBounds(o1.drawableBoundsCache);
        o2.drawable.setBounds(o2.drawableBoundsCache);
    }
}


public class ObjectImage {
    public Drawable drawable;
    public MPPointF offset = new MPPointF();
    public WeakReference<Chart> weakChart;
    public FSize size = new FSize();
    public Rect drawableBoundsCache = new Rect();
}