android - 每个系列的背景 GraphView

标签 android android-graphview

我使用 Graphview 并且工作正常,但现在我遇到了问题。

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

这可能吗?

最佳答案

目前(2014 年 8 月 5 日)这在 the original GraphView library 上是不可能的.

我需要这个功能,所以我 fork 了这个库并自己实现了这个功能。您可以在我的分支的 feature/series_specific_styles 分支上找到更新的代码:

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


实际的代码改动比较简单。

  • GraphViewSeries.GraphViewSeriesStyle

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

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


这是更新后的 GraphViewSeriesStyle 类:

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);
        }
    }
}

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

关于android - 每个系列的背景 GraphView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20753332/

相关文章:

Android 在对话框工作时更改 ProgressDialog 可绘制

java - 在数据库中添加第二个表

android - 使用 RecyclerView 适配器时 ViewHolder 未回收

java - 启动 Activity 时未显示 "Enable Device Admin"的对话框

android - 使用系统 key 签署应用程序

java - AndroidPlot - 从 GraphWidget 中删除域值

android-graphview - Android GraphView 中的平滑线图

android - 如何自动滚动 MPAndroidChart 折线图?

android - Android-GraphView 中的 java.util.ConcurrentModificationException

java - 在图 TableView 中将日期显示为 X 轴不起作用(每次显示相同)