0

I'm using achartengine to visualize some data in time. The use of a TimeChart and TimeSeries is perfect but I would like following result which doesn't seem possible at the moment.

Currently I'm using this:

GraphicalView view = ChartFactory.getTimeChartView(this, dataset, renderer, "MMM");

This results in following output on x axis:

nov.          apr.        aug.

But I would like a marker for each month and only the first character of each month, like this:

N  D  J  F  M  A  M  J  J  A  S  O

Ideas on how to accomplish this? Thanks!

Sander Versluys
  • 72,737
  • 23
  • 84
  • 91

1 Answers1

1

I've found the solution myself where I override the drawXLabels method in a custom TimeChart class.

private class CustomTimeChart extends TimeChart {

        public CustomTimeChart(XYMultipleSeriesDataset dataset,
                XYMultipleSeriesRenderer renderer) {
            super(dataset, renderer);
        }

        @Override
        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();
            if (length > 0) {
                boolean showLabels = mRenderer.isShowLabels();
                boolean showGridY = mRenderer.isShowGridY();
                DateFormat format = new SimpleDateFormat("MMMM");
                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.getXLabelsColor());
                        canvas.drawLine(xLabel, bottom, xLabel, bottom
                                + mRenderer.getLabelsTextSize() / 3, paint);
                        drawText(canvas, format.format(new Date(label)).substring(0, 1).toUpperCase(Locale.getDefault()),
                                xLabel, bottom + mRenderer.getLabelsTextSize()
                                        * 4 / 3, paint,
                                mRenderer.getXLabelsAngle());
                    }
                    if (showGridY) {
                        paint.setColor(mRenderer.getGridColor());
                        canvas.drawLine(xLabel, bottom, xLabel, top, paint);
                    }
                }
            }
            drawXTextLabels(xTextLabelLocations, canvas, paint, true, left,
                    top, bottom, xPixelsPerUnit, minX, maxX);
        }

Also make sure to use the setXLabels(int i) on the XYMultipleSeriesRenderer instance to set how many months you want to show on x axis.

Sander Versluys
  • 72,737
  • 23
  • 84
  • 91
  • :hello , can you help me please with this ? http://stackoverflow.com/questions/16014103/achartengine-cant-figure-how-to-use-dates-as-x-axis-the-file-i-save-is-empt .Thanks! – George Apr 16 '13 at 18:08
  • :Sorry to disturb again , if you can help me with the above i'll appreciate. – George Apr 17 '13 at 15:34