paragp / achartengine

AChartEngine is a charting library for Android applications. It currently supports the following chart types: line chart area chart scatter chart time chart bar chart pie chart bubble chart doughnut chart range (high-low) bar chart dial chart / gauge combined (any combination of line, cubic line, scatter, bar, range bar, bubble) chart cubic line chart All the above supported chart types can contain multiple series, can be displayed with the X axis horizontally (default) or vertically and support many other custom features. The charts can be built as a view that can be added to a view group or as an intent, such as it can be used to start an activity. The model and the graphing code is well optimized such as it can handle and display huge number of values. AChartEngine is currently at the 1.0.0 release. New chart types will be added in the following releases. Please keep sending your feedback such as we can continually improve this library. Find us on Facebook, too: http://www.facebook.com/achartengine Read a short introduction to AChartEngine here: http://www.javaadvent.com/2012/12/achartengine-charting-library-for.html Another good tutorial can be read here: http://jaxenter.com/effort-free-graphs-on-android-with-achartengine-46199.html
0 stars 1 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