如何在AChartEngine中为CombinedXYChart设置X -Axis的日期格式

时间:2014-09-17 13:35:37

标签: android achartengine

我想创建一个CombinedXYChartIntent,但我需要x轴中的日期格式。 我知道如何制作一个简单的TimeChartIntent。

  Intent intent = ChartFactory.getTimeChartIntent(context, dataset, mRenderer, "HH:mm");

但在ComninedXYChart

的情况下我也做不到同样的事情

我已经阅读了以下条目,但是他解决它的方式不起作用。 XYMultipleSeriesRenderer类中没有方法setXLabelFormat()。

How to set the Date Format for CombinedXYChart in AChartEngine?

我基本上有完全相同的问题,也许有人找到了为CombinedXYChart设置X -Axis的日期格式的方法。

1 个答案:

答案 0 :(得分:1)

我已经找到了解决方案,并想向您介绍我的解决方案。

public class LineGraph extends AbstractDemoChart {
      private static final long HOUR = 3600 * 1000;
      private static final long DAY = HOUR * 24;
      private static final int HOURS = 24;

    public Intent getIntent(Context context,double[] personCount, Date[] timeValue) {

        //Create XYSeries, fill it with 24 int Values for X-Axis and PersonCount for Y-Axis
        XYSeries series = new XYSeries("Test");
        for (int i = 0; i < timeValue.length; i++) {
            series.add(i, personCount[i]);
        }
        // Creating a dataset to hold each series
        XYMultipleSeriesDataset dataset =new XYMultipleSeriesDataset(); 
        dataset.addSeries(series);

        // Now I create the Series renderer
        XYSeriesRenderer renderer =new XYSeriesRenderer();
        renderer.setColor(Color.WHITE);

        /*last step, Creating the renderer that controls the full charts and add the   single renderer for each series */
        XYMultipleSeriesRenderer mRenderer =new XYMultipleSeriesRenderer();

        mRenderer.addSeriesRenderer(renderer);

        renderer.setPointStyle(PointStyle.SQUARE);
        renderer.setFillPoints(true);

        // Use DateFormatter for having X Labels in the form of HH:mm
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
        for (int i = 0; i < timeValue.length; i++) {
            // add custom Labels with correct format
            if(i%2==0){
                mRenderer.addXTextLabel(i, formatter.format(timeValue[i]));     
            }
        }
        mRenderer.setYLabels(10);
        mRenderer.setShowGrid(true);
        mRenderer.setXLabelsAlign(Align.CENTER);
        mRenderer.setYLabelsAlign(Align.RIGHT);
        // disable the default labels
        mRenderer.setXLabels(0);
        mRenderer.setShowCustomTextGrid(true);
        mRenderer.setZoomButtonsVisible(true);
        mRenderer.setPanLimits(new double[] { -10, 20, -10, 40 });
        mRenderer.setZoomLimits(new double[] { -10, 20, -10, 40 });
        mRenderer.setApplyBackgroundColor(true);
        mRenderer.setBackgroundColor(Color.BLACK);
        mRenderer.setChartTitle("Persons");


        Intent intent = ChartFactory.getCombinedXYChartIntent(context, dataset, mRenderer,  new String[]{BarChart.TYPE},
                    "Weather parameters");
        return intent;
    }


执行此操作后,我的X轴的时间值为HH:mm

我希望这可以帮助你进一步