Custom points on graph using AndroidPlot

删除回忆录丶 提交于 2019-12-22 11:06:52

问题


Anyone that has used the AndroidPlot library tell me how would I go about drawing custom points on a graph. So far I'm using LineAndPointRenderer class and settings lines to transparent. I would like to at least change the size of the dot but if possible have a custom image instead.

P.S someone with 1500 rep needs to create a "AndroidPlot" tag.


回答1:


Solved the problem by creating my own renderer.

import android.graphics.*;

import com.androidplot.series.XYSeries;
import com.androidplot.exception.PlotRenderException;
import com.androidplot.util.ValPixConverter;
import com.androidplot.xy.LineAndPointFormatter;
import com.androidplot.xy.XYPlot;
import com.androidplot.xy.XYSeriesRenderer;


public class CustomPointRenderer<FormatterType extends LineAndPointFormatter> extends XYSeriesRenderer<FormatterType> {

    private float circleWidth = 1;

    public CustomPointRenderer(XYPlot plot) {
        super(plot);
    }

    @Override
    public void onRender(Canvas canvas, RectF plotArea) throws PlotRenderException {
        for(XYSeries series : getPlot().getSeriesListForRenderer(this.getClass())) {
            drawSeries(canvas, plotArea, series, getFormatter(series));
        }
    }
    @Override
    protected void doDrawLegendIcon(Canvas canvas, RectF rect, FormatterType formatter) {
        // horizontal icon:
        float centerY = rect.centerY();
        float centerX = rect.centerX();

        if(formatter.getFillPaint() != null) {
            canvas.drawRect(rect, formatter.getFillPaint());
        }
        if(formatter.getLinePaint() != null) {
            canvas.drawLine(rect.left, rect.bottom, rect.right, rect.top, formatter.getLinePaint());
        }

        if(formatter.getVertexPaint() != null) {
            canvas.drawPoint(centerX, centerY, formatter.getVertexPaint());
        }
    }

    private void drawSeries(Canvas canvas, RectF plotArea, XYSeries series, LineAndPointFormatter formatter) throws PlotRenderException {
        PointF p = null;
        XYPlot plot = getPlot();
        int size = series.size();

        for (int i = 0; i < size; i++) {
            Number y = series.getY(i);
            Number x = series.getX(i);

            if (y != null && x != null) {
                p = ValPixConverter.valToPix(x, y, plotArea,
                            plot.getCalculatedMinX(),
                            plot.getCalculatedMaxX(),
                            plot.getCalculatedMinY(),
                            plot.getCalculatedMaxY());

                if (formatter.getVertexPaint() != null) {
                    boolean offScreen = p.x > plotArea.right || p.y > plotArea.bottom || p.x < plotArea.left || p.y < plotArea.top; 
                    if(!offScreen)
                        canvas.drawCircle(p.x, p.y - circleWidth, circleWidth, formatter.getVertexPaint());
                }               
            }
        } 
    }

    public void setWidth(float width){
        circleWidth = width;
    }
}


来源:https://stackoverflow.com/questions/10169080/custom-points-on-graph-using-androidplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!