如何使用AchartEngine在折线图中在x轴上设置日期?

时间:2012-10-05 11:44:48

标签: android achartengine linechart

我已尝试在SO上发布各种链接,但没有得到可靠的答案。

这是我的代码:

 TimeSeries series = new TimeSeries("Burned Calories");

    for (int i = 0; i < x_axis.size(); i++) {

            String date = x_axis.get(i);

            int y = Integer.parseInt(y_axis.get(i));

            Date d;

            try {

                    d = df.parse(date);

                    series.add(d, y);

            } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

      XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

      dataset.addSeries(series);

XYSeriesRenderer render = new XYSeriesRenderer();
    render.setColor(Color.GREEN);
render.setPointStyle(PointStyle.SQUARE);
render.setFillPoints(true);

      XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
  renderer.addSeriesRenderer(render);

        renderer.setChartTitle("Line Chart");
    renderer.setAxesColor(Color.BLUE);
    renderer.setGridColor(Color.WHITE);

    renderer.setShowGrid(true);
    renderer.setShowAxes(true);
    renderer.setShowGridX(true);
    renderer.setShowGridY(true);
    renderer.setShowLabels(true);
    renderer.setPanEnabled(true, true);
    renderer.setFitLegend(true);
    renderer.setXTitle("Date");
    renderer.setAxisTitleTextSize(15);
    renderer.setXLabelsAlign(Align.CENTER);
    renderer.setYTitle("Values");

    chart = ChartFactory.getLineChartView(this, dataset, renderer);

我在x轴上得到奇怪的值。

任何帮助将不胜感激。 enter image description here

2 个答案:

答案 0 :(得分:3)

这些是我在我的代码中所做的基本更改,它对我有用..

TimeSeries series2 = new TimeSeries("Protein");

            for (int i = 0; i < x_axis.size(); i++) {

                int y = Integer.parseInt(y1_axis.get(i));

                // Adding y as protein value
                series2.add(i + 1, y);

            }


    for (int i = 0; i < x_axis.size(); i++) {
        renderer.addXTextLabel(i + 1, x_axis.get(i));
    }

    renderer.setXLabels(0);

enter image description here

答案 1 :(得分:2)

XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();

之后添加以下代码...... 根据您的要求设置如下图...

            String MainTitle = "Graph";
            String XTitle = "Date";
            String YTitle = "Values";
            float minValueX = 0.0f;
            float maxValueX = 30.00f;
            float minValueY = 0f;
            float maxValueY = 50f;



setChartSettings(renderer, MainTitle, XTitle, YTitle, minValueX,
                        maxValueX, minValueY, maxValueY, Color.BLACK, Color.BLACK);


protected void setChartSettings(XYMultipleSeriesRenderer renderer,
                String title, String xTitle, String yTitle, double xMin,
                double xMax, double yMin, double yMax, int axesColor,
                int labelsColor) {
            renderer.setChartTitle(title);
            renderer.setXTitle(xTitle);
            renderer.setYTitle(yTitle);
            renderer.setXAxisMin(xMin);
            renderer.setXAxisMax(xMax);
            renderer.setYAxisMin(yMin);
            renderer.setYAxisMax(yMax);
            renderer.setAxesColor(axesColor);
            renderer.setLabelsColor(labelsColor);
        }

在Chartengine上显示日期或月份仅数字数字,因此您设置数字并设置标签日期...因为x轴上的显示日期不确定但我不提供图表引擎