wangyao5 / achartengine

Automatically exported from code.google.com/p/achartengine
0 stars 0 forks source link

XYChart can support ChartValue label #227

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
i am using the LineChart to show temperature in one week, and want the point on 
the line to show 30℃ but not only 30

What is the expected output? What do you see instead?
the latest version cann't support the feature, and i have to override the 
method XYChart.drawChartValuesText like this:

@Override
protected void drawChartValuesText(Canvas canvas, XYSeries series, 
SimpleSeriesRenderer renderer, Paint paint,
        float[] points, int seriesIndex, int startIndex) {
    if (points.length > 1) { // there are more than one point
        // record the first point's position
        float previousPointX = points[0];
        float previousPointY = points[1];
        for (int k = 0; k < points.length; k += 2) {
            if (k == 2) { // decide whether to display first two points'
                            // values or not
                if (Math.abs(points[2] - points[0]) > 100 || Math.abs(points[3] - points[1]) > 100) {
                    // first point
                    drawText(canvas, getLabel(series, series.getX(startIndex), series.getY(startIndex)), points[0],
                            points[1] - renderer.getChartValuesSpacing(), paint, 0);
                    // second point
                    drawText(canvas, getLabel(series, series.getX(startIndex + 1), series.getY(startIndex + 1)),
                            points[2], points[3] - renderer.getChartValuesSpacing(), paint, 0);

                    previousPointX = points[2];
                    previousPointY = points[3];
                }
            } else if (k > 2) {
                // compare current point's position with the previous
                // point's, if they are not too close, display
                if (Math.abs(points[k] - previousPointX) > 100
                        || Math.abs(points[k + 1] - previousPointY) > distance) {
                    drawText(canvas,
                            getLabel(series, series.getX(startIndex + k / 2), series.getY(startIndex + k / 2)),
                            points[k], points[k + 1] - renderer.getChartValuesSpacing(), paint, 0);
                    previousPointX = points[k];
                    previousPointY = points[k + 1];
                }
            }
        }
    } else { // if only one point, display it
        for (int k = 0; k < points.length; k += 2) {
            drawText(canvas, getLabel(series.getY(startIndex + k / 2)), points[k],
                    points[k + 1] - renderer.getChartValuesSpacing(), paint, 0);
        }
    }
}

protected String getLabel(XYSeries series, double x, double y) {
    if (series instanceof MyXYSeries) {
        return ((MyXYSeries) series).getLabel(x, y);
    }
    return getLabel(y);
}

and MyXYSeries is:
public class MyXYSeries extends XYSeries {

    private static final long serialVersionUID = 2516000745378907666L;
    private static final String seperator = "#";

    private Map<String, String> labels = new HashMap<String, String>();

    public MyXYSeries(String title) {
        super(title);
    }

    public MyXYSeries(String title, int scaleNumber) {
        super(title, scaleNumber);
    }

    public synchronized void add(double x, double y, String label) {
        add(x, y);
        labels.put(normalize(x) + seperator + normalize(y), label);
    }

    public String getLabel(double x, double y) {
        String label = labels.get(normalize(x) + seperator + normalize(y));
        if (label == null) {
            label = normalize(y);
        }
        return label;
    }

    String normalize(double value) {
        if (value == Math.round(value)) {
            return String.valueOf(Math.round(value));
        } else {
            return String.valueOf(value);
        }
    }

}

Please provide a source code snippet that we can use to replicate the issue.

What version of the product binary library are you using?
1.0.0

Please provide any additional information below.

Original issue reported on code.google.com by geng...@gmail.com on 3 Jul 2012 at 5:01

GoogleCodeExporter commented 9 years ago

Original comment by dandrome...@gmail.com on 11 Jul 2012 at 11:04