使用achartengine绘制折线图

 //从ListView适配器中得到数据
String[] item = (String[]) adapter.getItem(position);
// 要绘制的四条折线的名字
String[] titles = new String[] { "体重" };
// 折线图的x坐标
List<double[]> x = new ArrayList<double[]>();
// 从数据库中查询数据
WeightDao dao = new WeightDao(getApplicationContext());
String item_weight[] = dao.GeyBuyEarNum(Long
                                    .parseLong(item[0]));
//初始化x轴坐标
double[] XValues = new double[item_weight.length];
for (int i = 0; i < XValues.length; i++) {
            XValues[i] = i;
}

x.add(XValues);
// 折线图的y坐标
List<double[]> values = new ArrayList<double[]>();
double yValues[] = new double[item_weight.length];
for (int i = 0; i < yValues.length; i++) {
    if (item_weight[i].endsWith("KG")) {
    yValues[i] = Double.valueOf(item_weight[i]
                                            .subSequence(0,                         item_weight[i].length() - 2).toString());
} else {
yValues[i] = Double.valueOf(item_weight[i]);

}

}
values.add(yValues);

// 折线的颜色
int[] colors = new int[] { Color.BLUE };
                            // 折线拐点的样式
PointStyle[] styles = new PointStyle[]{PointStyle.CIRCLE };
XYMultipleSeriesRenderer renderer = buildRenderer(
colors, styles);
int length = renderer.getSeriesRendererCount();
for (int i = 0; i < length; i++) {
// 设置为实心
XYSeriesRenderer xySeriesRenderer = ((XYSeriesRenderer) renderer.getSeriesRendererAt(i));                                                                   xySeriesRenderer.setFillPoints(true);
// 显示数值 xySeriesRenderer.setDisplayChartValues(true);
// 设置显示数值距离折线点的距离
xySeriesRenderer.setDisplayChartValuesDistance(25);                                 
}
// 设置第一次呈现出的折线图显示的左边范围
                            setChartSettings(renderer, "体重折线图", "时间", "体重", -1,12.5, -1, 40, Color.LTGRAY, Color.LTGRAY);
renderer.setXLabels(12);
renderer.setYLabels(10);                            renderer.setShowGrid(true);                         renderer.setXLabelsAlign(Align.RIGHT);                          renderer.setYLabelsAlign(Align.RIGHT);                          renderer.setZoomButtonsVisible(true);
//设置缩放范围
renderer.setPanLimits(new double[] { -1, 50, -1, 100 });                            renderer.setZoomLimits(new double[] { -1, 50, -1,100});
// 上 左 下 右
renderer.setMargins(new int[] { 50, 50, 0, 20 });
XYMultipleSeriesDataset dataset = buildDataset(
titles, x, values);
XYSeries series = dataset.getSeriesAt(0);
series.addAnnotation("图标注释", 6, 30);
Intent intent = ChartFactory.getLineChartIntent(
                                    getApplicationContext(), dataset, renderer,
"体重折线图");
startActivity(intent);

/** * Sets a few of the series renderer settings. * * @param renderer * the renderer to set the properties to * @param title * the chart title * @param xTitle * the title for the X axis * @param yTitle * the title for the Y axis * @param xMin * the minimum value on the X axis * @param xMax * the maximum value on the X axis * @param yMin * the minimum value on the Y axis * @param yMax * the maximum value on the Y axis * @param axesColor * the axes color * @param labelsColor * the labels color */
    protected void setChartSettings(XYMultipleSeriesRenderer renderer,
            String title, String xTitle, String yTitle, double xMin,
            double xMax, double yMin, double yMax, int axesColor,
            int labelsColor) {
        //设置标题
        renderer.setChartTitle(title);
        //x标题
        renderer.setXTitle(xTitle);
        //y标题
        renderer.setYTitle(yTitle);
        //设置x轴显示的最小值。下边类似
        renderer.setXAxisMin(xMin);
        renderer.setXAxisMax(xMax);
        renderer.setYAxisMin(yMin);
        renderer.setYAxisMax(yMax);
        renderer.setAxesColor(axesColor);
        renderer.setLabelsColor(labelsColor);
    }
/** * Builds an XY multiple time dataset using the provided values. * * @param titles * the series titles * @param xValues * the values for the X axis * @param yValues * the values for the Y axis * @return the XY multiple time dataset */
    protected XYMultipleSeriesDataset buildDateDataset(String[] titles,
            List<Date[]> xValues, List<double[]> yValues) {
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        int length = titles.length;
        for (int i = 0; i < length; i++) {
            TimeSeries series = new TimeSeries(titles[i]);
            Date[] xV = xValues.get(i);
            double[] yV = yValues.get(i);
            int seriesLength = xV.length;
            for (int k = 0; k < seriesLength; k++) {
                series.add(xV[k], yV[k]);
            }
            dataset.addSeries(series);
        }
        return dataset;
    }

下面附效果图:

代码来自Google提供的demo。我在这里添加了比较详细的注释。希望对初学者有帮助,同时也是作为自己的笔记

你可能感兴趣的:(安卓-折线图)