2

I'm using Achartengine to generate Bar Charts and I want to polish the way they look. I was thinking to add strokes(outlines?) for each bar that is displayed.

Is there a way to use BasicStroke for a XYMultipleSeriesRenderer?

This is what I'm trying to achieve, based on what I have (I have a XYMultipleSeriesRenderer to which I want to add a stroke):

screenshot example

I have tried to extend the XYMultipleSeriesRenderer with the BasicStroke.class methods to see if I could use the setStroke on a XYMultipleSeriesRenderer(just an experiment so don't jump on me). Didn't expect it to work but I'm providing the code so you can better understand what I have in mind.

Preferably, I would like to reuse as much of the code from Achartengine without having to modify the .jar file. If there's a method where I can extend one of the classes... it would be a life/time saver.

package com.example.android.fragments_proto.aChartEngine;

import java.util.ArrayList;
import java.util.List;

import org.achartengine.chart.PointStyle;
import org.achartengine.renderer.BasicStroke;
import org.achartengine.renderer.XYMultipleSeriesRenderer;

import android.graphics.Color;


public class BarChartStyling extends XYMultipleSeriesRenderer {

    /** The stroke style. */
      private BasicStroke mStroke;

      /**
       * Returns the stroke style.
       * 
       * @return the stroke style
       */
      public BasicStroke getStroke() {
        return mStroke;
      }

      /**
       * Sets the stroke style.
       * 
       * @param stroke the stroke style
       */
      public void setStroke(BasicStroke stroke) {
        mStroke = stroke;
      }






SOLUTION

Although, there is a more elegant way to achieve this I found a shortcut in the BarChart.class. It involves editing the source code but it's a decent solution.

I will polish this when I have the time and submit it to the repo. on GoogleCode.

Dan, if you have any comments or warnings in using this method... please do indulge in criticism.

  private void drawBar(Canvas canvas, float xMin, float yMin, float xMax, float yMax, int scale,
      int seriesIndex, Paint paint) {
    SimpleSeriesRenderer renderer = mRenderer.getSeriesRendererAt(seriesIndex);

    // set the color to the first drawRect 
    // using the paint param.
    paint.setColor(Color.BLACK);

    // use the first drawRect to draw a bar 
    // with (left, top, right, bottom, paint)
    canvas.drawRect(Math.round(xMin), Math.round(yMin), Math.round(xMax), Math.round(yMax), paint);
    if (renderer.isGradientEnabled()) {
      float minY = (float) toScreenPoint(new double[] { 0, renderer.getGradientStopValue() }, scale)[1];
      float maxY = (float) toScreenPoint(new double[] { 0, renderer.getGradientStartValue() },
          scale)[1];
      float gradientMinY = Math.max(minY, Math.min(yMin, yMax));
      float gradientMaxY = Math.min(maxY, Math.max(yMin, yMax));
      int gradientMinColor = renderer.getGradientStopColor();
      int gradientMaxColor = renderer.getGradientStartColor();
      int gradientStartColor = gradientMaxColor;
      int gradientStopColor = gradientMinColor;

      if (yMin < minY) {
        paint.setColor(gradientMinColor);
        canvas.drawRect(Math.round(xMin), Math.round(yMin), Math.round(xMax),
            Math.round(gradientMinY), paint);
      } else {
        gradientStopColor = getGradientPartialColor(gradientMinColor, gradientMaxColor,
            (maxY - gradientMinY) / (maxY - minY));
      }
      if (yMax > maxY) {
        paint.setColor(gradientMaxColor);
        canvas.drawRect(Math.round(xMin), Math.round(gradientMaxY), Math.round(xMax),
            Math.round(yMax), paint);
      } else {
        gradientStartColor = getGradientPartialColor(gradientMaxColor, gradientMinColor,
            (gradientMaxY - minY) / (maxY - minY));
      }
      GradientDrawable gradient = new GradientDrawable(Orientation.BOTTOM_TOP, new int[] {
          gradientStartColor, gradientStopColor });
      gradient.setBounds(Math.round(xMin), Math.round(gradientMinY), Math.round(xMax),
          Math.round(gradientMaxY));
      gradient.draw(canvas);
    } else {
      if (Math.abs(yMin - yMax) < 1) {
        if (yMin < yMax) {
          yMax = yMin + 1;
        } else {
          yMax = yMin - 1;
        }
      }

      // set the color to the second drawRect 
      // using the paint param.
      paint.setColor(renderer.getColor());

      // modify the drawRect size and position to create a 
      // smaller bar above the first one 
      // while modifying it's size proportionally 
      // (left + 5, top + 5, right - 5, bottom - 5, paint)
      canvas.drawRect(Math.round(xMin + 5), Math.round(yMin + 5), Math.round(xMax - 5), Math.round(yMax - 5), paint); 
    }
  }
4

1 に答える 1

1

レンダラー クラスを拡張するだけでは簡単ではないと思います。BarChart棒グラフのバーを実際にレンダリングするクラスも拡張する必要があります。ソースコードをチェックアウトして、そこに機能を追加することをお勧めします。次に、他の人があなたの変更から恩恵を受けられるようにしたい場合は、Issue を作成してコード パッチを添付することで、遠慮なく AChartEngine コミュニティに貢献してください。

于 2013-04-22T15:03:26.350 に答える