/**
  * Draws the Legend for the scatter plot
  *
  * @param g {@link Graphics}
  */
 private void drawLegend(Graphics g) {
   // set the color of the legend rectangle
   g.setColor(LEGEND_BACKGROUND);
   int lineHeight = g.getFontMetrics().getHeight() + 2; // height of a legend line
   // draw the legend rectangle
   g.fillRect(
       getWidth() - legendWidth - LENGEND_PAD - (LEGEND_INSET * 2),
       LENGEND_PAD,
       legendWidth + (LEGEND_INSET * 2),
       ((data.size() + 2) * lineHeight) + (LEGEND_INSET * 2));
   g.setColor(Colors.BLACK);
   g.drawRect(
       getWidth() - legendWidth - LENGEND_PAD - (LEGEND_INSET * 2),
       LENGEND_PAD,
       legendWidth + (LEGEND_INSET * 2),
       ((data.size() + 2) * lineHeight) + (LEGEND_INSET * 2));
   // search the position where to start the text of the legend
   Point p =
       new Point(
           getWidth() - legendWidth - LENGEND_PAD - LEGEND_INSET,
           LENGEND_PAD + LEGEND_INSET + g.getFontMetrics().getHeight());
   // draw the axis names
   g.drawString(X_AXIS_PREFIX + xAxis.getName(), p.x, p.y); // draw X-Axis legend
   g.drawString(Y_AXIS_PREFIX + yAxis.getName(), p.x, p.y + lineHeight); // draw Y-Axis legend
   // draw graph name legend
   for (int i = 0; i < data.size(); i++) {
     Color graphColor = data.get(i).getColor();
     String graphName = data.get(i).getName();
     g.setColor(graphColor);
     int yLine = p.y + ((i + 2) * lineHeight); // y position of the current line
     g.drawLine(p.x, yLine - 5, p.x + 25, yLine - 5); // draw a line with the color of the graph
     g.setColor(Colors.BLACK);
     g.drawString(graphName, p.x + 30, yLine); // draw the name of the graph
   }
 }
 /** @return the width of the legend text in pixels */
 private int computeLegendWidth() {
   FontMetrics fm = getFontMetrics(getFont());
   int legendWidth = fm.stringWidth(X_AXIS_PREFIX + xAxis.getName());
   legendWidth = Math.max(legendWidth, fm.stringWidth(Y_AXIS_PREFIX + yAxis.getName()));
   for (ScatterPlotData currentPlot : data) {
     if (data != null) {
       legendWidth = Math.max(legendWidth, fm.stringWidth(currentPlot.getName()) + 30);
     }
   }
   return legendWidth;
 }