Search code examples
androidandroidplot

Add 2nd Y Axis Caption, Mark line + Add line Caption


I got the following Problem,

I would like to :

  • add a second Axis Caption to my Android Plot on the right side
  • same Labels like right.(maybe)
  • mark a line with a color and some text
  • the violet line should always be in front the yellow one

Is there any way to do this?

Because a picture says more than thousands of words.

enter image description here


Solution

  • There's not a built in second axis caption, but since they're just instances of TextLabelWidget its pretty easy to add your own. Here's an example that adds a label to the SimpleXYPlotActivity example

        TextLabelWidget textLabelWidget = new TextLabelWidget(
                plot.getLayoutManager(),
                "some text",
                null,  // TextLabelWidget instances "pack" to wrap the actual text size
                TextOrientation.VERTICAL_ASCENDING);
    
        textLabelWidget.getLabelPaint().setColor(Color.RED);
        textLabelWidget.getLabelPaint().setTextSize(PixelUtils.dpToPix(24));
    
        plot.getLayoutManager().add(textLabelWidget);
        textLabelWidget.position(
                // add a right margin of 4dp:
                PixelUtils.dpToPix(4), HorizontalPositioning.ABSOLUTE_FROM_RIGHT,
    
                // center the text with the plot space vertically:
                0, VerticalPositioning.ABSOLUTE_FROM_CENTER,
    
                // use the middle of the right edge of the text widget as the anchor:
                Anchor.RIGHT_MIDDLE);
    

    Which produces:

    enter image description here