2

我正在使用 Achartengine 生成条形图,我想改进它们的外观。我正在考虑为显示的每个条添加笔画(轮廓?)。

有没有办法将 BasicStroke 用于 XYMultipleSeriesRenderer?

这就是我想要实现的目标,基于我所拥有的(我有一个 XYMultipleSeriesRenderer,我想在其中添加一个笔划):

截图示例

我尝试使用 BasicStroke.class 方法扩展 XYMultipleSeriesRenderer,以查看是否可以在 XYMultipleSeriesRenderer 上使用 setStroke(只是一个实验,所以不要跳到我身上)。没想到它会起作用,但我提供了代码,以便您更好地理解我的想法。

最好,我希望尽可能多地重用来自 Achartengine 的代码,而不必修改 .jar 文件。如果有一种方法可以扩展其中一个类……那将是节省生命/时间的方法。

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;
      }






解决方案

虽然,有一种更优雅的方式来实现这一点,但我在 BarChart.class 中找到了一个快捷方式。它涉及编辑源代码,但它是一个不错的解决方案。

我会在有时间的时候完善它并将其提交给 repo。在谷歌代码上。

丹,如果您对使用此方法有任何意见或警告……请尽情批评。

  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条形图中实际呈现条形的类。我宁愿建议您检查源代码并在那里添加您的功能。然后,如果您希望其他人从您的更改中受益,请随时通过创建问题并附加代码补丁将其回馈给 AChartEngine 社区。

于 2013-04-22T15:03:26.350 回答