class GraphInitializer extends AbstractDemoChart
{
public Intent execute(Context context)
{
String[] titles = new String[] { "" };
dates.add(dateList);
values.add(glucoseAvgDaily);
String ndates[] = new String[]{"Sun","Mon","Teu","Wed","Thurs","Fri","Sat"};
int[] colors = new int[] { Color.GREEN };
PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE };
XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
setChartSettings(renderer, "", "", "", dateList[0].getTime(),
dateList[dateList.length - 1].getTime(), getMinReadingValue(readingList), getMaxReadingValue(readingList), Color.WHITE, Color.LTGRAY);
int max = (int)getMaxReadingValue(readingList);
max += 100;
renderer.setYLabels(5);
renderer.setZoomLimits(new double[]{dateList[0].getTime(), dateList[dateList.length - 1].getTime(), -10, max });
renderer.setZoomEnabled(true, true);
renderer.setZoomButtonsVisible(false);
return ChartFactory.getLineChartIntent(context, buildDateDataset(titles, ndates, values),
renderer);
}
这是我的AbstractDemoChart:
package com.db.Graphs;
/**
* Copyright (C) 2009, 2010 SC 4ViewSoft SRL
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.MultipleCategorySeries;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import android.graphics.Paint.Align;
import android.util.Log;
/**
* An abstract class for the demo charts to extend. It contains some methods for
* building datasets and renderers.
*/
public abstract class AbstractDemoChart implements IDemoChart {
/**
* Builds an XY multiple 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 dataset
*/
protected XYMultipleSeriesDataset buildDataset(String[] titles,
List<double[]> xValues, List<double[]> yValues) {
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
addXYSeries(dataset, titles, xValues, yValues, 0);
return dataset;
}
public void addXYSeries(XYMultipleSeriesDataset dataset, String[] titles,
List<double[]> xValues, List<double[]> yValues, int scale) {
int length = titles.length;
for (int i = 0; i < length; i++) {
XYSeries series = new XYSeries(titles[i], scale);
double[] xV = xValues.get(i);
double[] yV = yValues.get(i);
int seriesLength = xV.length;
System.out.println("X = "+seriesLength);
seriesLength = yV.length;
System.out.println("y = "+seriesLength);
for (int k = 0; k < seriesLength; k++) {
series.add(xV[k], yV[k]);
}
dataset.addSeries(series);
}
}
/**
* Builds an XY multiple series renderer.
*
* @param colors
* the series rendering colors
* @param styles
* the series point styles
* @return the XY multiple series renderers
*/
protected XYMultipleSeriesRenderer buildRenderer(int[] colors,
PointStyle[] styles) {
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
setRenderer(renderer, colors, styles);
return renderer;
}
protected void setRenderer(XYMultipleSeriesRenderer renderer, int[] colors,
PointStyle[] styles) {
renderer.setAxisTitleTextSize(16);
renderer.setChartTitleTextSize(20);
renderer.setLabelsTextSize(15);
renderer.setLegendTextSize(15);
renderer.setPointSize(5f);
renderer.setMargins(new int[] { 20, 30, 15, 20 });
int length = colors.length;
for (int i = 0; i < length; i++) {
XYSeriesRenderer r = new XYSeriesRenderer();
r.setColor(colors[i]);
r.setPointStyle(styles[i]);
r.setFillPoints(true);
r.setLineWidth(2);
r.setChartValuesTextSize(6);
r.setChartValuesTextAlign(Align.CENTER);
r.setChartValuesTextSize(10);
renderer.addSeriesRenderer(r);
}
}
/**
* 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);
renderer.setXTitle(xTitle);
renderer.setYTitle(yTitle);
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();
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
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++) {
//formattedDate = formatter.format(todaysDate);
System.out.println("DATE = "+xV[k]+" GRAPHDATA "+yV[k]);//+new Double(yV[k]).doubleValue());
series.add(xV[k], yV[k]);
}
dataset.addSeries(series);
}
return dataset;
}
protected XYMultipleSeriesDataset buildDateDataset(String[] titles,
String[] ndates, List<double []> yValues) {
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
int length = titles.length;
for (int i = 0; i < length; i++) {
TimeSeries series = new TimeSeries(titles[i]);
double[] yV = yValues.get(i);
int seriesLength = ndates.length;
for (int k = 0; k<seriesLength; k++) {
//formattedDate = formatter.format(todaysDate);
series.addString(k, yV[k]);
}
dataset.addSeries(series);
}
return dataset;
}
/**
* Builds a category series using the provided values.
*
* @param titles
* the series titles
* @param values
* the values
* @return the category series
*/
protected CategorySeries buildCategoryDataset(String title, double[] values) {
CategorySeries series = new CategorySeries(title);
int k = 0;
for (double value : values) {
series.add("Project " + ++k, value);
}
return series;
}
/**
* Builds a multiple category series using the provided values.
*
* @param titles
* the series titles
* @param values
* the values
* @return the category series
*/
protected MultipleCategorySeries buildMultipleCategoryDataset(String title,
List<String[]> titles, List<double[]> values) {
MultipleCategorySeries series = new MultipleCategorySeries(title);
int k = 0;
for (double[] value : values) {
series.add(2007 + k + "", titles.get(k), value);
k++;
}
return series;
}
/**
* Builds a category renderer to use the provided colors.
*
* @param colors
* the colors
* @return the category renderer
*/
protected DefaultRenderer buildCategoryRenderer(int[] colors) {
DefaultRenderer renderer = new DefaultRenderer();
renderer.setLabelsTextSize(15);
renderer.setLegendTextSize(15);
renderer.setMargins(new int[] { 20, 30, 15, 0 });
for (int color : colors) {
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
r.setColor(color);
renderer.addSeriesRenderer(r);
}
return renderer;
}
/**
* Builds a bar multiple series dataset using the provided values.
*
* @param titles
* the series titles
* @param values
* the values
* @return the XY multiple bar dataset
*/
protected XYMultipleSeriesDataset buildBarDataset(String[] titles,
List<double[]> values) {
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
int length = titles.length;
for (int i = 0; i < length; i++) {
CategorySeries series = new CategorySeries(titles[i]);
double[] v = values.get(i);
int seriesLength = v.length;
for (int k = 0; k < seriesLength; k++) {
series.add(v[k]);
}
dataset.addSeries(series.toXYSeries());
}
return dataset;
}
/**
* Builds a bar multiple series renderer to use the provided colors.
*
* @param colors
* the series renderers colors
* @return the bar multiple series renderer
*/
protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors) {
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
renderer.setAxisTitleTextSize(16);
renderer.setChartTitleTextSize(20);
renderer.setLabelsTextSize(15);
renderer.setLegendTextSize(15);
int length = colors.length;
for (int i = 0; i < length; i++) {
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
r.setColor(colors[i]);
renderer.addSeriesRenderer(r);
}
return renderer;
}
}
目前,我能够计划时间,但我无法为平日标签绘图。
所以,周日说X轴将在Y轴上绘制一个数字。
我该怎么做?
答案 0 :(得分:1)
要添加自定义文字标签,您可以执行以下操作:
renderer.addXTextLabel(x, "label");
renderer.addYTextLabel(y, "label");
答案 1 :(得分:0)
您可以根据具体要求更改drawXLabels()
XYChart.java
,如下所示 -
protected void drawXLabels(List<Double> xLabels, Double[] xTextLabelLocations, Canvas canvas,
Paint paint, int left, int top, int bottom, double xPixelsPerUnit, double minX, double maxX) {
int length = xLabels.size();
boolean showLabels = mRenderer.isShowLabels();
boolean showGridY = mRenderer.isShowGridY();
boolean showTickMarks = mRenderer.isShowTickMarks();
for (int i = 0; i < length; i++) {
double label = xLabels.get(i);
float xLabel = (float) (left + xPixelsPerUnit * (label - minX));
if (showLabels) {
paint.setColor(mRenderer.getXLabelsColor());
if (showTickMarks) {
canvas
.drawLine(xLabel, bottom, xLabel, bottom + mRenderer.getLabelsTextSize() / 3, paint);
}
// drawText(canvas, getLabel(mRenderer.getLabelFormat(), label), xLabel,
// bottom + mRenderer.getLabelsTextSize() * 4 / 3 + mRenderer.getXLabelsPadding(), paint,
// mRenderer.getXLabelsAngle());
int key = (int) label;
switch(key)
{
case 0:
drawText(canvas,"Sun", xLabel,
bottom + mRenderer.getLabelsTextSize() * 4 / 3 + mRenderer.getXLabelsPadding(), paint,
mRenderer.getXLabelsAngle());
break;
case 1:
drawText(canvas,"Mon", xLabel,
bottom + mRenderer.getLabelsTextSize() * 4 / 3 + mRenderer.getXLabelsPadding(), paint,
mRenderer.getXLabelsAngle());
break;
case 2:
drawText(canvas,"Tue", xLabel,
bottom + mRenderer.getLabelsTextSize() * 4 / 3 + mRenderer.getXLabelsPadding(), paint,
mRenderer.getXLabelsAngle());
break;
case 3:
drawText(canvas,"Wed", xLabel,
bottom + mRenderer.getLabelsTextSize() * 4 / 3 + mRenderer.getXLabelsPadding(), paint,
mRenderer.getXLabelsAngle());
break;
case 4:
drawText(canvas,"Thu", xLabel,
bottom + mRenderer.getLabelsTextSize() * 4 / 3 + mRenderer.getXLabelsPadding(), paint,
mRenderer.getXLabelsAngle());
break;
case 5:
drawText(canvas,"Fri", xLabel,
bottom + mRenderer.getLabelsTextSize() * 4 / 3 + mRenderer.getXLabelsPadding(), paint,
mRenderer.getXLabelsAngle());
break;
default:
break;
}
}
if (showGridY) {
paint.setColor(mRenderer.getGridColor(0));
canvas.drawLine(xLabel, bottom, xLabel, top, paint);
}
}
drawXTextLabels(xTextLabelLocations, canvas, paint, showLabels, left, top, bottom,
xPixelsPerUnit, minX, maxX);
}