MPAndroidChart:如何在同一高亮位置显示多个标记

4

我希望在点击时显示三个工具提示/标记,只使用一个数据集。

就像下面的图片一样:

enter image description here

我该怎么做?


也许这个答案可以解决你的问题 https://dev59.com/KqPia4cB1Zd3GeqP16cc#45276098 - Mia
1
谢谢您的回答,但那不是我要找的答案。我想知道的是如何仅使用一个数据集来显示三个标签(一个在X轴上,一个在Y轴上,一个在图表上X和Y的结果点上方)。您在链接中提供的答案是针对当您有三个数据集并且在用户单击时显示每个数据集的标签的情况。 - Juan Rodríguez
1个回答

0
我创建了一个LineChart并向其提供了数据。 为了多个标记,我创建了一个实现IMarker的类,并创建了两个对象 我为每个标记的信息制作了一个类。 第二个标记显示的方式取决于第一个标记的位置。 我知道这可能不是一个好的实现,但它起作用了... 以下是对我有用的代码:
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();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接