ChartGridView.java revision ab2d8d3a38857b8c155e6c6393c5821f5a341aae
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings.widget;
18
19import android.content.Context;
20import android.graphics.Canvas;
21import android.graphics.Color;
22import android.graphics.Paint;
23import android.graphics.Paint.Style;
24import android.view.View;
25
26import com.google.common.base.Preconditions;
27
28/**
29 * Background of {@link ChartView} that renders grid lines as requested by
30 * {@link ChartAxis#getTickPoints()}.
31 */
32public class ChartGridView extends View {
33
34    private final ChartAxis mHoriz;
35    private final ChartAxis mVert;
36
37    private final Paint mPaintHoriz;
38    private final Paint mPaintVert;
39
40    public ChartGridView(Context context, ChartAxis horiz, ChartAxis vert) {
41        super(context);
42
43        mHoriz = Preconditions.checkNotNull(horiz, "missing horiz");
44        mVert = Preconditions.checkNotNull(vert, "missing vert");
45
46        setWillNotDraw(false);
47
48        // TODO: convert these colors to resources
49        mPaintHoriz = new Paint();
50        mPaintHoriz.setColor(Color.parseColor("#667bb5"));
51        mPaintHoriz.setStrokeWidth(2.0f);
52        mPaintHoriz.setStyle(Style.STROKE);
53        mPaintHoriz.setAntiAlias(true);
54
55        mPaintVert = new Paint();
56        mPaintVert.setColor(Color.parseColor("#28262c"));
57        mPaintVert.setStrokeWidth(1.0f);
58        mPaintVert.setStyle(Style.STROKE);
59        mPaintVert.setAntiAlias(true);
60    }
61
62    @Override
63    protected void onDraw(Canvas canvas) {
64        final int width = getWidth();
65        final int height = getHeight();
66
67        final float[] vertTicks = mVert.getTickPoints();
68        for (float y : vertTicks) {
69            canvas.drawLine(0, y, width, y, mPaintVert);
70        }
71
72        final float[] horizTicks = mHoriz.getTickPoints();
73        for (float x : horizTicks) {
74            canvas.drawLine(x, 0, x, height, mPaintHoriz);
75        }
76
77        canvas.drawRect(0, 0, width, height, mPaintHoriz);
78    }
79}
80