如何更改 AndroidPlot 域和范围标签的文本颜色?

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

大家好,我正在尝试使用 AndroidPlot 绘制一个简单的 XY 图。我无法弄清楚如何更改域和范围的标签颜色。网上例子说用:

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

但是这不适合我。谁能建议我需要使用什么? (我也尝试过使用 getGraph() 而不是 getGraphWidget() )

我也在尝试在图表和域标签之间设置边距 space,如果有人对此有建议,还会在图表和范围标签之间设置 space。

    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;
        }
    });

看起来你 found the answer 你的文本颜色问题。

就调整图形与域/范围标签之间的间距而言,Androidplot 1.x 由图形的线标签插图控制。例如,要调整范围标签在图表左边缘的相对位置:

以编程方式:

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

xml 参数:

ap:lineLabelInsetLeft="-5dp"