teiiluj
teiiluj

Reputation: 241

How to change text colour for AndroidPlot domain and range labels?

Hi all I am just trying out a simple XY Plot with AndroidPlot. I am having trouble figuring out how to change the label colours for the domain and range. Examples online say to use:

plot.getGraphWidget().getDomainLabelPaint().setColor(my_colour); plot.getDomainLabelWidget().getLabelPaint().setColor(my_colour);

However this does not compile for me. Can anyone suggest what I need to use? (I have also tried using getGraph() instead of getGraphWidget() )

I'm also trying to set margin space between the graph and domain labels, and also space between graph and range labels if anyone has a suggestion for that.

    plot = (XYPlot) findViewById(R.id.plot);
    plot.setPlotMargins(0, 0, 0, 0);
    plot.getBackgroundPaint().setColor(Color.WHITE);
    plot.setBorderStyle(XYPlot.BorderStyle.NONE, null, null);
    plot.getGraph().getBackgroundPaint().setColor(Color.WHITE);
    plot.getGraph().getGridBackgroundPaint().setColor(Color.WHITE);
    plot.getGraph().getDomainOriginLinePaint().setColor(Color.TRANSPARENT);
    plot.getGraph().getRangeOriginLinePaint().setColor(Color.TRANSPARENT);


    // create a couple arrays of y-values to plot:
    final Number[] domainLabels = {1, 10, 20, 6, 7, 8, 9, 10, 13, 14};
    Number[] series1Numbers = {1, 4, 2, 8, 4, 16, 8, 32, 16, 64};

    // turn the above arrays into XYSeries':
    // (Y_VALS_ONLY means use the element index as the x value)
    XYSeries series1 = new SimpleXYSeries(
            Arrays.asList(series1Numbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Series1");


    // create formatters to use for drawing a series using LineAndPointRenderer
    // and configure them from xml:
    LineAndPointFormatter series1Format = new LineAndPointFormatter();
    series1Format.setPointLabelFormatter(new PointLabelFormatter());
    series1Format.configure(getApplicationContext(),
            R.xml.line_point_formatter_with_labels);

    LineAndPointFormatter series2Format = new LineAndPointFormatter();
    series2Format.setPointLabelFormatter(new PointLabelFormatter());
    series2Format.configure(getApplicationContext(),
            R.xml.line_point_formatter_with_labels_2);

    // add an "dash" effect to the series2 line:
    series2Format.getLinePaint().setPathEffect(
            new DashPathEffect(new float[]{

                    // always use DP when specifying pixel sizes, to keep things consistent across devices:
                    PixelUtils.dpToPix(20),
                    PixelUtils.dpToPix(15)}, 0));

    plot.addSeries(series1, series1Format);
    plot.getGraph().getLineLabelStyle(XYGraphWidget.Edge.BOTTOM).setFormat(new Format() {
        @Override
        public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
            int i = Math.round(((Number) obj).floatValue());
            return toAppendTo.append(domainLabels[i]);
        }

        @Override
        public Object parseObject(String source, ParsePosition pos) {
            return null;
        }
    });

Upvotes: 1

Views: 810

Answers (1)

Nick
Nick

Reputation: 8317

Looks like you found the answer to your text color problem.

As far as adjusting the spacing between your graph and the domain / range labels, as of Androidplot 1.x that's controlled by the graph's line label insets. For example, to adjust the relative positioning of range labels on the left edge of the graph:

programmatically:

// move line labels away from the graph by 5dp:
plot.getGraph().getLineLabelInsets().setLeft(PixelUtils.dpToPix(-5));

xml param:

ap:lineLabelInsetLeft="-5dp"

Upvotes: 1

Related Questions