2

如何正确制作 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;

这就是我得到的:

在此处输入图像描述

如您所见,它们的间距不正确,它们位于 x 轴线上并且具有不需要的值,例如 10、15、20 .. 我该如何纠正这个问题?谢谢你。

4

2 回答 2

3

乍一看,您应该删除该行

mRenderer.setXLabels(10);

如您所愿,setXLabels(0)您已在另一行中。我认为该代码告诉 AChartEngine 尝试打印 10 个标签以及您的文本标签。

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

于 2012-10-22T15:14:06.140 回答
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;

    }
于 2012-10-22T12:02:33.777 回答