Search code examples
achartengine

how to draw an achartengine TimeSeries with points but no lines between them?


I currently fake this feature by setting the XYSeriesRenderer color to white:

    final XYSeriesRenderer goodEventSeriesRenderer = new XYSeriesRenderer();
    goodEventSeriesRenderer.setColor(Color.WHITE);
    goodEventSeriesRenderer.setPointStyle(PointStyle.POINT);
    goodEventSeriesRenderer.setShowLegendItem(false);
    goodEventSeriesRenderer.setAnnotationsTextSize(textSize + 2);
    goodEventSeriesRenderer.setAnnotationsColor(Color.GREEN);

I tried calling setLineWidth(0), which typically works in other graphical systems to zilch the line, but that just returned hairlines between the points.

So how do I get a time series chart where some TimeSeries have lines between their points, and some don't? I run with achartengine compiled into my program, not from a jar file, so feel free to help me hack the source.


Solution

  • Here's the diff of the changes to AChartEngine to allow .setLineWidth(0) to zilch the lines:

    --- a/app/src/main/java/org/achartengine/chart/AbstractChart.java
    +++ b/app/src/main/java/org/achartengine/chart/AbstractChart.java
    @@ -302,6 +302,7 @@ public abstract class AbstractChart implements Serializable {
         Path path = new Path();
         int height = canvas.getHeight();
         int width = canvas.getWidth();
    +    float strokeWidth = paint.getStrokeWidth();
    
         float[] tempDrawPoints;
         if (points.size() < 4) {
    @@ -310,7 +311,10 @@ public abstract class AbstractChart implements Serializable {
         tempDrawPoints = calculateDrawPoints(points.get(0), points.get(1), points.get(2),
             points.get(3), height, width);
         path.moveTo(tempDrawPoints[0], tempDrawPoints[1]);
    -    path.lineTo(tempDrawPoints[2], tempDrawPoints[3]);
    +    if (strokeWidth > 0.0f)
    +      path.lineTo(tempDrawPoints[2], tempDrawPoints[3]);
    +    else
    +      path.moveTo(tempDrawPoints[2], tempDrawPoints[3]);
    
         int length = points.size();
         for (int i = 4; i < length; i += 2) {
    @@ -323,7 +327,10 @@ public abstract class AbstractChart implements Serializable {
           if (!circular) {
             path.moveTo(tempDrawPoints[0], tempDrawPoints[1]);
           }
    -      path.lineTo(tempDrawPoints[2], tempDrawPoints[3]);
    +      if (strokeWidth > 0.0)
    +        path.lineTo(tempDrawPoints[2], tempDrawPoints[3]);
    +      else
    +        path.moveTo(tempDrawPoints[2], tempDrawPoints[3]);
         }
         if (circular) {
           path.lineTo(points.get(0), points.get(1));
    diff --git a/app/src/main/java/org/achartengine/chart/LineChart.java b/app/src/main/java/org/achartengine/chart/LineChart.java
    index 55365e3..be0937b 100644
    --- a/app/src/main/java/org/achartengine/chart/LineChart.java
    +++ b/app/src/main/java/org/achartengine/chart/LineChart.java
    @@ -81,7 +81,8 @@ public class LineChart extends XYChart {
       public void drawSeries(Canvas canvas, Paint paint, List<Float> points, XYSeriesRenderer renderer,
           float yAxisValue, int seriesIndex, int startIndex) {
         float lineWidth = paint.getStrokeWidth();
    -    paint.setStrokeWidth(renderer.getLineWidth());
    +    float lineWidth1 = renderer.getLineWidth();
    +    paint.setStrokeWidth(lineWidth1);
         final FillOutsideLine[] fillOutsideLine = renderer.getFillOutsideLine();
    
         for (FillOutsideLine fill : fillOutsideLine) {
    @@ -227,8 +228,13 @@ public class LineChart extends XYChart {
       public void drawLegendShape(Canvas canvas, SimpleSeriesRenderer renderer, float x, float y,
           int seriesIndex, Paint paint) {
         float oldWidth = paint.getStrokeWidth();
    -    paint.setStrokeWidth(((XYSeriesRenderer) renderer).getLineWidth());
    -    canvas.drawLine(x, y, x + SHAPE_WIDTH, y, paint);
    +    float lineWidth = ((XYSeriesRenderer) renderer).getLineWidth();
    +
    +    if (lineWidth > 0.0) {
    +      paint.setStrokeWidth(lineWidth);
    +      canvas.drawLine(x, y, x + SHAPE_WIDTH, y, paint);
    +    }
    +
         paint.setStrokeWidth(oldWidth);
         if (isRenderPoints(renderer)) {
           pointsChart.drawLegendShape(canvas, renderer, x + 5, y, seriesIndex, paint);