AndroidPlot: domain labels from 1 to 11

戏子无情 提交于 2020-01-04 05:25:10

问题


I've implemented AndroidPlot in my app and it works fine, apart from the X-axis labels, which they go from 0 to ten. I'd like to display 1 to eleven. Besides, the labels on the Y-axis do not appear.

Code I'm using:

import java.text.DecimalFormat;
import java.util.Arrays;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.androidplot.series.XYSeries;
import com.androidplot.xy.LineAndPointFormatter;
import com.androidplot.xy.SimpleXYSeries;
import com.androidplot.xy.XYPlot;
import com.androidplot.xy.XYStepMode;

public class Scatter extends Activity {
    private XYPlot mySimpleXYPlot;

    //declare new arrays

   float[] one;
   float[] two;
   float[] three;
   Number[] series1Numbers; 
   Number[] series2Numbers;
   Number[] series3Numbers;
   String chainringCount;


    /** Called when the activity is first created. */
    @SuppressWarnings("deprecation")

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scatter);


        Bundle bundle = this.getIntent().getExtras();
        one = bundle.getFloatArray("one");
        two = bundle.getFloatArray("two");
        three = bundle.getFloatArray("three");
        chainringCount=bundle.getString("CC");



        if (Integer.parseInt(chainringCount)==1){
            series1Numbers = new Number[one.length];

            for (int a=0; a<one.length; a++){
                series1Numbers[a]=one[a];
            }

            mySimpleXYPlot = (XYPlot) findViewById(R.id.mySimpleXYPlot);
            mySimpleXYPlot.setDomainStep(XYStepMode.SUBDIVIDE,11);
            mySimpleXYPlot.setDomainStep(XYStepMode.INCREMENT_BY_VAL,1);
            mySimpleXYPlot.setDomainValueFormat(new DecimalFormat("#"));
            mySimpleXYPlot.setTicksPerRangeLabel(13);
            mySimpleXYPlot.disableAllMarkup();
            mySimpleXYPlot.getBackgroundPaint().setAlpha(0);
            mySimpleXYPlot.getGraphWidget().getBackgroundPaint().setAlpha(0);
            mySimpleXYPlot.getGraphWidget().getGridBackgroundPaint().setAlpha(0);
            mySimpleXYPlot.setDomainLabel(getString(R.string.domainName));
            mySimpleXYPlot.setRangeLabel(getString(R.string.rangeName));

            XYSeries series1 = new SimpleXYSeries(Arrays.asList(series1Numbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "#1"); 
            LineAndPointFormatter series1Format = new LineAndPointFormatter(Color.rgb(0, 200, 0), Color.rgb(0, 100, 0),null);                                  // fill color (none)
            mySimpleXYPlot.addSeries(series1, series1Format);

        }...


回答1:


You can apply a custom formatter to make the labels print whatever you want. In your case, I believe something like this should work:

plot.setDomainValueFormat(new NumberFormat() {

            @Override
            public StringBuffer format(double d, StringBuffer sb, FieldPosition fp) {
                return sb.append((d + 1) + ""); // shortcut to convert d+1 into a String
            }

            // unused
            @Override
            public StringBuffer format(long l, StringBuffer stringBuffer, FieldPosition fieldPosition) { return null;}

            // unused
            @Override
            public Number parse(String s, ParsePosition parsePosition) { return null;}
});


来源:https://stackoverflow.com/questions/16057175/androidplot-domain-labels-from-1-to-11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!