问题
How can i make the XAxis in achartEngine format correctly? I would like it to have only the date value spaced correctly in the X-Axis with date values but i can't seem to get it work correctly.
this is what i have tried:
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;
this is what i get:

as you can see, They are not spaced correctly, they are on the x-axis line and have unwanted values e.g 10, 15, 20.. How can i correct this? Thank you.
回答1:
At first glance you should remove the line
mRenderer.setXLabels(10);
as you want to setXLabels(0)
like you have in another line. I think that code tells AChartEngine to try and print 10 labels as well as your text labels.
Edit: You also need to call addXTextLabel()
with the parameter of the x position you would like the label to be at. So renderer.addXTextLabel(date_value_that_should_have_a_string, date_value_string)
回答2:
following code is display recording chart in positive and negative value of recording (PCM 16bit)data.....
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;
}
来源:https://stackoverflow.com/questions/13010687/how-to-format-xaxis-values-correctly-in-line-chart-in-achartengine-in-android