Search code examples
androidandroidplot

Remove a lengend from an XYPlot


Am using AndroidPlot on my project and it's just great so far. This is what i got.

plot with androidplot showing 4 series and a legend

Now am trying to remove one of the series indicators from the legend. Actually i want to keep only the first one (the blue one) cause the others are self explanatory.

Am not asking how to remove the legend it self (see https://stackoverflow.com/a/13413758/665823 for that issue).

If someone can help me i'll be great. Thanks in advance.


Solution

  • I finally found a tweek that allows me to do what i need. To achieve this the series that i need to be shown must be added to the plot first over the ones i need to hide :

    // I add the blue serie first
    LineAndPointFormatter mFormat = new LineAndPointFormatter();
    mFormat.configure(context, R.xml.plot_blue);
    graphView.addSeries(serieMesures, mFormat);
    
    // I add the other colors after
    for (XYSeries serie : seriesSeuils) {
         LineAndPointFormatter sFormat = new LineAndPointFormatter();
         if (serie.getTitle().equals("RED") {
            sFormat.configure(context, R.xml.plot_red);
         } 
         else if (serie.getTitle().equals("ORANGE")) {
            sFormat.configure(context, R.xml.plot_orange);
         }
         else if (serie.getTitle().equals("YELLOW")) {
            sFormat.configure(context, R.xml.plot_yellow);
         }
         graphView.addSeries(serie, sFormat);
    }
    

    Then i just created a legend where there is only enough space to show the series i need (in my case just the first one).

    // This is what does the trick! A Table with 1 line and 1 column 
    // so the other series can't be rendered in the LegendWidget
    graphView.getLegendWidget().setTableModel(new DynamicTableModel(1, 1));
    
    // Other customizations (size and position)
    graphView.getLegendWidget().setSize(new SizeMetrics(55, SizeLayoutType.ABSOLUTE, legendWith, SizeLayoutType.ABSOLUTE));
    graphView.getLegendWidget().setPositionMetrics(
         new PositionMetrics(20,
                             XLayoutStyle.ABSOLUTE_FROM_RIGHT,
                             20,
                             YLayoutStyle.ABSOLUTE_FROM_TOP,
                             AnchorPosition.RIGHT_TOP));
    graphView.getLegendWidget().setBackgroundPaint(bgPaint);
    graphView.getLegendWidget().setTextPaint(txPaint);
    graphView.getLegendWidget().setBorderPaint(brPaint);
    graphView.getLegendWidget().setPadding(10, 1, 10, 1);
    

    And voila!

    androidplot showing 4 series and just one in the legendwidget