如何使用achartengine在TimeChartView上显示轴上的特定日期

时间:2014-09-06 09:27:39

标签: android charts achartengine

我按x轴显示日期组,按y轴显示分数。但是现在显示的日期从11到27,然后是12整个星期。但每周应该是11-18-25-1

enter image description here

我看到了类似的问题here。但并没有真正了解如何实现它。现在我正在这样做这个图表:

private String[] xValues = null;
  private double[] yValues = null;
  /** The main dataset that includes all the series that go into a chart. */
  private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
  /** The main renderer that includes all the renderers customizing a chart. */
  private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
  /** The most recently added series. */
  private XYSeries mCurrentSeries;
  /** The most recently created renderer, customizing the current series. */
  private XYSeriesRenderer mCurrentRenderer;
  private GraphicalView mChartView;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ....
    // set some properties on the main renderer
    mRenderer.setApplyBackgroundColor(true);
    mRenderer.setBackgroundColor(getResources().getColor(R.color.bkg_green));
    mRenderer.setAxisTitleTextSize(25);
    mRenderer.setChartTitleTextSize(25);
    mRenderer.setLabelsTextSize(20);
    mRenderer.setLegendTextSize(25);
    mRenderer.setShowGrid(true);
    mRenderer.setMargins(new int[] {50, 50, 25, 25 });
    mRenderer.setMarginsColor(Color.argb(0x00, 0x01, 0x01, 0x01));
    mRenderer.setAxesColor(getResources().getColor(R.color.white));
    mRenderer.setLabelsColor(getResources().getColor(R.color.white));
    mRenderer.setXLabelsColor(getResources().getColor(R.color.white));
    mRenderer.setYLabelsColor(0, getResources().getColor(R.color.white));
    mRenderer.setZoomEnabled(true,false);
    mRenderer.setPointSize(5);
    mRenderer.setPanEnabled(true, false);
  }

//init chart with data
@Override public void onSuccess(final GraphResponse response) {
    runOnUiThread(new Runnable() {
      @Override public void run() {
        mProgressBar.setVisibility(View.INVISIBLE);

        response.clearFromNullPoints();

        xValues = new String[response.getData().size()];
        yValues = new double[response.getData().size()];

        for (int i = 0; i < response.getData().size(); i++) {
            xValues[i] = response.getData().get(i).getDate();
            yValues[i] = response.getData().get(i).getValue();
        }

        if (mChartView == null) {
          LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
          mChartView = ChartFactory.getTimeChartView(getApplicationContext(), mDataset, mRenderer,
              "dd.MM.yyyy");
          layout.addView(mChartView,
              new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                  LinearLayout.LayoutParams.MATCH_PARENT));
          addSeries();
        } else {
          mChartView.repaint();
        }
      }
    });
  }

 void addSeries() {
    TimeSeries series = new TimeSeries("Progress");
    // populate data
    for (int i = 0; i < xValues.length; i++) {
      try {
        series.add(dateFormat.parse(xValues[i]), yValues[i]);
      } catch (ParseException e) {
          throw new RuntimeException(e);
      }
    }
    mDataset.addSeries(series);
    mCurrentSeries = series;
    // create a new renderer for the new series
    XYSeriesRenderer renderer = new XYSeriesRenderer();
    mRenderer.addSeriesRenderer(renderer);
    // set some renderer properties
    renderer.setPointStyle(PointStyle.CIRCLE);
    renderer.setFillPoints(true);
    renderer.setDisplayChartValues(true);
    renderer.setDisplayChartValuesDistance(40);
    renderer.setChartValuesTextSize(30);
    renderer.setColor(getResources().getColor(R.color.white));
    renderer.setLineWidth(3.0f);
    mCurrentRenderer = renderer;

    mChartView.repaint();
  }

如何按特定期限x轴显示日期 - 一周?

2 个答案:

答案 0 :(得分:1)

您可能希望图表显示实际的数据标签而不是舍入的数据标签。所以,我建议你这样做:

mRenderer.setXRoundedLabels(false);

更新:设置自己的标签的另一种方法是禁用常规标签并添加自定义标签:

mRenderer.setXLabels(0);
// one such call for every label
mRenderer.addXTextLabel(x, label);

答案 1 :(得分:0)

我最终设置了自己的xAxis显示期:

mRenderer.setXAxisMin(series.getX(0));
mRenderer.setXAxisMax(series.getX(0) + ONE_MONTH);

使用4个标签设置网格:

mRenderer.setXLabels(4);

在显示的全宽度上显示8天和1个月的好时间。