/** The number of milliseconds in a day. */
public static final long DAY = 24 * 60 * 60 * 1000;
/** The date format pattern to be used in formatting the X axis labels. */
private String mDateFormat;
/**
* Builds a new time chart instance.
*
* @param dataset the multiple series dataset
* @param renderer the multiple series renderer
*/
public TimeBarChart(XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer,Type Type,String format) {
super(dataset, renderer, Type);
mDateFormat = format;
}
/**
* Returns the date format pattern to be used for formatting the X axis labels.
* @return the date format pattern for the X axis labels
*/
public String getDateFormat() {
return mDateFormat;
}
/**
* Sets the date format pattern to be used for formatting the X axis labels.
* @param format the date format pattern for the X axis labels.
* If null, an appropriate default format will be used.
*/
public void setDateFormat(String format) {
mDateFormat = format;
}
/**
* The graphical representation of the labels on the X axis.
* @param xLabels the X labels values
* @param xTextLabelLocations the X text label locations
* @param canvas the canvas to paint to
* @param paint the paint to be used for drawing
* @param left the left value of the labels area
* @param top the top value of the labels area
* @param bottom the bottom value of the labels area
* @param xPixelsPerUnit the amount of pixels per one unit in the chart labels
* @param minX the minimum value on the X axis in the chart
*/
@Override
protected void drawXLabels(List<Double> xLabels, Double[] xTextLabelLocations, Canvas canvas, Paint paint, int left,
int top, int bottom, double xPixelsPerUnit, double minX) {
int length = xLabels.size();
boolean showLabels = mRenderer.isShowLabels();
boolean showGrid = mRenderer.isShowGrid();
DateFormat format = getDateFormat(xLabels.get(0), xLabels.get(length - 1));
for (int i = 0; i < length; i++) {
long label = Math.round(xLabels.get(i));
float xLabel = (float) (left + xPixelsPerUnit * (label - minX));
if (showLabels) {
paint.setColor(mRenderer.getLabelsColor());
canvas.drawLine(xLabel, bottom, xLabel, bottom + 4, paint);
drawText(canvas, format.format(new Date(label)), xLabel, bottom + 12, paint, mRenderer.getXLabels());//TODO getXLabels getXLabelsAngle
}
if (showGrid) {
paint.setColor(GRID_COLOR);
canvas.drawLine(xLabel, bottom, xLabel, top, paint);
}
}
}
/**
* Returns the date format pattern to be used, based on the date range.
* @param start the start date in milliseconds
* @param end the end date in milliseconds
* @return the date format
*/
private DateFormat getDateFormat(double start, double end) {
if (mDateFormat != null) {
SimpleDateFormat format = null;
try {
format = new SimpleDateFormat(mDateFormat);
return format;
} catch (Exception e) {
// do nothing here
}
}
DateFormat format = SimpleDateFormat.getDateInstance(SimpleDateFormat.MEDIUM);
double diff = end - start;
if (diff > DAY && diff < 5 * DAY) {
format = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.SHORT, SimpleDateFormat.SHORT);
} else if (diff < DAY) {
format = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM);
}
return format;
}
public static final GraphicalView getTimeBarChartView(Context context,
XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer,Type type, String format) {
// checkParameters(dataset, renderer);
TimeBarChart chart = new TimeBarChart(dataset, renderer,type, format);
chart.setDateFormat(format);
return new GraphicalView(context, chart);
}
댓글