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

时间:2016-09-13 04:23:09

标签: android androidplot

大家好我只想用AndroidPlot尝试一个简单的XY图。我无法弄清楚如何更改域和范围的标签颜色。在线示例说使用:

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

然而,这不适合我。任何人都可以建议我需要使用什么? (我也尝试使用getGraph()而不是getGraphWidget())

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

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

1 个答案:

答案 0 :(得分:1)

您的文字颜色问题似乎是found the answer

至于调整图形与域/范围标签之间的间距,从Android图1.x开始,由图形的线标签插入控制。例如,要调整图表左边缘上范围标签的相对位置:

<强>编程:

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

xml param:

ap:lineLabelInsetLeft="-5dp"