Android 每个系列GraphView的背景

Android 每个系列GraphView的背景,android,android-graphview,Android,Android Graphview,我使用Graphview,工作很好,但现在我有一个问题 我想为添加到图表中的每个系列提供背景,而不是所有系列 这可能吗?目前(2014年8月5日)不可能 我需要这个功能,所以我将库分叉,自己实现了这个功能。您可以在my fork的功能/系列特定样式分支上找到更新的代码: 希望将来这些更改将被拉入原始库中 实际的代码更改相对简单 将必需的背景字段添加到GraphViewSeries.GraphViewSeriesStyle 更新了LineGraphView.drawSeries()以查找

我使用Graphview,工作很好,但现在我有一个问题

我想为添加到图表中的每个系列提供背景,而不是所有系列

这可能吗?

目前(2014年8月5日)不可能

我需要这个功能,所以我将库分叉,自己实现了这个功能。您可以在my fork的
功能/系列特定样式
分支上找到更新的代码:

希望将来这些更改将被拉入原始库中


实际的代码更改相对简单

  • 将必需的背景字段添加到
    GraphViewSeries.GraphViewSeriesStyle

  • 更新了
    LineGraphView.drawSeries()
    以查找这些字段,而不是依赖其自身的内部值

我在下面包含了完整的更新,但查看这些更新的最简单方法是在提交页面上:


以下是更新后的
图形视图体验样式
类:

static public class GraphViewSeriesStyle {
    public int color = 0xff0077cc;
    public int thickness = 3;
    private ValueDependentColor valueDependentColor;

    private final Paint paintBackground;
    private boolean drawBackground;
    private boolean drawDataPoints;
    private float dataPointsRadius = 10f;

    public GraphViewSeriesStyle() {
        super();

        paintBackground = new Paint();
        paintBackground.setColor(Color.rgb(20, 40, 60));
        paintBackground.setStrokeWidth(4);
        paintBackground.setAlpha(128);
    }

    public GraphViewSeriesStyle(int color, int thickness) {
        super();
        this.color = color;
        this.thickness = thickness;

        paintBackground = new Paint();
        paintBackground.setColor(Color.rgb(20, 40, 60));
        paintBackground.setStrokeWidth(4);
        paintBackground.setAlpha(128);
    }

    public ValueDependentColor getValueDependentColor() {
        return valueDependentColor;
    }

    /**
     * the color depends on the value of the data.
     * only possible in BarGraphView
     * @param valueDependentColor
     */
    public void setValueDependentColor(ValueDependentColor valueDependentColor) {
        this.valueDependentColor = valueDependentColor;
    }

    public boolean getDrawBackground() {
        return drawBackground;
    }

    public void setDrawBackground(boolean drawBackground) {
        this.drawBackground = drawBackground;
    }

    public Paint getPaintBackground() {
        return paintBackground;
    }

    public int getBackgroundColor() {
        return paintBackground.getColor();
    }

    /**
     * sets the background colour for the series. This is not the background
     * colour of the whole graph.
     */
    public void setBackgroundColor(int color) {
        paintBackground.setColor(color);
    }

    public float getDataPointsRadius() {
        return dataPointsRadius;
    }

    public boolean getDrawDataPoints() {
        return drawDataPoints;
    }

    /**
     * sets the radius of the circles at the data points.
     * @see #setDrawDataPoints(boolean)
     * @param dataPointsRadius
     */
    public void setDataPointsRadius(float dataPointsRadius) {
        this.dataPointsRadius = dataPointsRadius;
    }

    /**
     * You can set the flag to let the GraphView draw circles at the data points
     * @see #setDataPointsRadius(float)
     * @param drawDataPoints
     */
    public void setDrawDataPoints(boolean drawDataPoints) {
        this.drawDataPoints = drawDataPoints;
    }
}

以下是更新的
LineGraphView.drawSeries()
方法:

public void drawSeries(Canvas canvas, GraphViewDataInterface[] values, float graphwidth, float graphheight, float border, double minX, double minY, double diffX, double diffY, float horstart, GraphViewSeriesStyle style) {
    // draw background
    double lastEndY = 0;
    double lastEndX = 0;

    // draw data
    paint.setStrokeWidth(style.thickness);
    paint.setColor(style.color);


    Path bgPath = null;
    if ((drawBackground) || (style.getDrawBackground())) {
        bgPath = new Path();
    }

    lastEndY = 0;
    lastEndX = 0;
    float firstX = 0;
    for (int i = 0; i < values.length; i++) {
        double valY = values[i].getY() - minY;
        double ratY = valY / diffY;
        double y = graphheight * ratY;

        double valX = values[i].getX() - minX;
        double ratX = valX / diffX;
        double x = graphwidth * ratX;

        if (i > 0) {
            float startX = (float) lastEndX + (horstart + 1);
            float startY = (float) (border - lastEndY) + graphheight;
            float endX = (float) x + (horstart + 1);
            float endY = (float) (border - y) + graphheight;

            // draw data point
            if (drawDataPoints) {
                //fix: last value was not drawn. Draw here now the end values
                canvas.drawCircle(endX, endY, dataPointsRadius, paint);
            } else if (style.getDrawDataPoints()) {
                canvas.drawCircle(endX, endY, style.getDataPointsRadius(), paint);
            }

            canvas.drawLine(startX, startY, endX, endY, paint);
            if (bgPath != null) {
                if (i==1) {
                    firstX = startX;
                    bgPath.moveTo(startX, startY);
                }
                bgPath.lineTo(endX, endY);
            }
        } else if ((drawDataPoints) || (style.getDrawDataPoints())) {
            //fix: last value not drawn as datapoint. Draw first point here, and then on every step the end values (above)
            float first_X = (float) x + (horstart + 1);
            float first_Y = (float) (border - y) + graphheight;
            if (drawDataPoints) {
                canvas.drawCircle(first_X, first_Y, dataPointsRadius, paint);
            } else if (style.getDrawDataPoints()) {
                canvas.drawCircle(first_X, first_Y, style.getDataPointsRadius(), paint);
            }
        }
        lastEndY = y;
        lastEndX = x;
    }

    if (bgPath != null) {
        // end / close path
        bgPath.lineTo((float) lastEndX, graphheight + border);
        bgPath.lineTo(firstX, graphheight + border);
        bgPath.close();
        if (style.getDrawBackground()) {
            canvas.drawPath(bgPath, style.getPaintBackground());
        } else {
            canvas.drawPath(bgPath, paintBackground);
        }
    }
}
public void drawSeries(画布画布、GraphViewDataInterface[]值、浮点图形宽度、浮点图形高度、浮点边框、双minX、双minY、双diffX、双diffY、浮点horstart、GraphViewSeriesStyle){
//画背景
双lastEndY=0;
双lastEndX=0;
//绘制数据
油漆.设置行程宽度(样式.厚度);
绘制.setColor(样式.颜色);
路径bgPath=null;
if((牵引地面)| |(style.get牵引地面())){
bgPath=新路径();
}
lastEndY=0;
lastEndX=0;
float firstX=0;
对于(int i=0;i0){
float startX=(float)lastEndX+(horstart+1);
float startY=(float)(border-lastEndY)+图高;
浮动端点x=(浮动)x+(水平起点+1);
float-endY=(float)(border-y)+图形高度;
//绘制数据点
if(提取数据点){
//修复:未绘制最后一个值。现在在此处绘制结束值
画布.画圈(endX,endY,dataPointsRadius,paint);
}else if(style.getDrawDataPoints()){
drawCircle(endX,endY,style.getDataPointsRadius(),paint);
}
帆布.抽绳(startX、startY、endX、endY、油漆);
如果(bgPath!=null){
如果(i==1){
firstX=startX;
bgPath.moveTo(startX,startY);
}
bgPath.lineTo(endX,endY);
}
}else如果((drawDataPoints)| |(style.getDrawDataPoints())){
//修正:最后一个值未绘制为数据点。在此处绘制第一个点,然后在每个步骤上绘制结束值(如上)
第一浮点数X=(浮点数)X+(horstart+1);
第一个浮动Y=(浮动)(边框-Y)+图形高度;
if(提取数据点){
canvas.drawCircle(first_X,first_Y,dataPointsRadius,paint);
}else if(style.getDrawDataPoints()){
drawCircle(first_X,first_Y,style.getDataPointsRadius(),paint);
}
}
lastEndY=y;
lastEndX=x;
}
如果(bgPath!=null){
//结束/关闭路径
bgPath.lineTo((float)lastEndX,图形高度+边框);
bgPath.lineTo(firstX,graphheight+border);
bgPath.close();
if(style.getDruckGround()){
drawPath(bgPath,style.getPaintBackground());
}否则{
canvas.drawPath(bgPath,paintBackground);
}
}
}

出于兴趣,该分支还允许为每个系列配置数据点-此处可见代码更改: