如何在android中的aChartEngine中的折线图中正确格式化XAxis值

时间:2012-10-22 11:39:11

标签: android achartengine linechart

如何正确使用achartEngine格式的XAxis?我希望它只有日期值在X轴上正确间隔日期值,但我似乎无法让它正常工作。

这就是我的尝试:

 XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
     dataset.addSeries(series);

     XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
     mRenderer.setBackgroundColor(Color.BLACK);
     mRenderer.setApplyBackgroundColor(true);
     mRenderer.setGridColor(Color.LTGRAY);
     mRenderer.setXLabels(0);
    // mRenderer.setMargins(new int[]{0, 20, 0, 0});

     XYSeriesRenderer renderer = new XYSeriesRenderer();
     //renderer.setDisplayChartValuesDistance(1);
     renderer.setColor(Color.GREEN);

     mRenderer.setXLabels(10);
        if(date_value.length < 10){ //date_value is a large array of dates for the X-Axis

            for(int i = 0; i < date_value.length; i++){
                mRenderer.addXTextLabel(i+1, date_value[i]);    
            }

        }

        else if(date_value.length > 10){

            int mod = date_value.length  %  10;
            int add_mod = mod;
            int last = date_value.length;

            mRenderer.addXTextLabel(0, date_value[0]);

            for(int i=0; i < 10; i++){
                mRenderer.addXTextLabel(i, date_value[mod]);

                     mod+=add_mod; 


            }
            mRenderer.setXLabelsAngle(90);
            //mRenderer.addXTextLabel(date_value.length, date_value[last]);

        }
    /*get the last elements in the array and parse as double to set the maximum range for X*/
        Calendar ist = Calendar.getInstance();
        Calendar last = Calendar.getInstance();
        ist.setTime(formatter.stringToDateReport(date_value[0]));
        last.setTime(formatter.stringToDateReport(date_value[date_value.length-1]));

    mRenderer.setXAxisMin(ist.get(Calendar.DATE));
     mRenderer.setXAxisMax(last.get(Calendar.DATE));
     mRenderer.addSeriesRenderer(renderer); 
     chartView = ChartFactory.getLineChartView(context, dataset, mRenderer);

return chartView;

这就是我得到的:

enter image description here

正如您所看到的,它们没有正确间隔,它们位于x轴线上并且具有不需要的值,例如10,15,20 ..我该如何纠正?谢谢。

2 个答案:

答案 0 :(得分:3)

乍一看,你应该删除该行

mRenderer.setXLabels(10);

正如你想要setXLabels(0)一样,你在另一条线上。我认为该代码告诉AChartEngine尝试打印10个标签以及文本标签。

编辑:您还需要使用您希望标签所在的x位置参数调用addXTextLabel()。所以renderer.addXTextLabel(date_value_that_should_have_a_string, date_value_string)

答案 1 :(得分:0)

以下代码显示记录的正负值(PCM 16bit)数据.....

 public void onDraw(Canvas canvas)
    {       

                paint.setColor(Color.WHITE);
                canvas.drawLine(width/16+(width/8)+(width/100)-1,0,width/16+(width/8)+(width/100)-1,height, paint);
                canvas.drawLine(0,height/2,width,height/2, paint);

                canvas.drawText("32767",(width/20),height/30, paint); 
                canvas.drawText("0",(width/12),(height/2)+(height/80)-(height/2)/2, paint); 
                canvas.drawText("-32767",(width/25),height/2-height/50, paint); 

                canvas.drawText("32767",(width/20),height/2+height/50, paint); 
                canvas.drawText("0",(width/12),(height/2)+(height/80)+(height/2)/2, paint); 
                canvas.drawText("-32767",(width/25),height-height/30, paint); 

                paint.setColor(Color.GREEN);


         for (i=0; i < second.par_second_data ; i++)
         { 

             if (i >= 1)
             {  
                canvas.drawLine(width/16+second.final_width-1+(width/8)+(width/100),(float) (height/2-(height/4)-(height/2*(second.audioData[i-1]-1200)/second.graph_y)),width/16+second.final_width+(width/8)+(width/100),(float) (height/2-(height/4)-(height/2*(second.audioData[i]-1200)/second.graph_y)), paint);
                canvas.drawLine(width/16+second.final_width-1+(width/8)+(width/100),(float) (height/2+(height/4)-(height/2*(second.fftArray[i-1].re())/second.graph_z)),width/16+second.final_width+(width/8)+(width/100),(float) (height/2+(height/4)-(height/2*(second.fftArray[i].re())/second.graph_z)), paint);

                //canvas.drawLine(width/16+second.final_width+(width/8)+(width/100),height/2-(height/4),width/16+second.final_width+(width/8)+(width/100),(float) (height/2-(height/4)-(height/2*(second.audioData[i]-1200)/second.graph_y)), paint);
                //canvas.drawLine(width/16+second.final_width+(width/8)+(width/100),height/2+(height/4),width/16+second.final_width+(width/8)+(width/100),(float) (height/2+(height/4)-(height/2*(second.fftArray[i].re())/second.graph_z)), paint);
            }
                second.final_width=second.final_width+1;
        }
        second.final_width=0;

    }