0

How can the colour of the Axis in JChart2D be changed.

So far the closest thing I have found is something like:

chart.setForeground(Color.BLUE);

unfortunately this changes everything, both Axis and Grid. What I am looking for is having multiple yAxis with different colours.

How this can be achieved?

Camilo Guevara
  • 165
  • 1
  • 12

2 Answers2

2

I modified and recompiled the library...

To do so, in IAxis.java the following code was added

 /**
   * The property key defining the <code>color</code> property. Use in
   * combination with
   * {@link #addPropertyChangeListener(String, PropertyChangeListener)}.
   */
  public static final String PROPERTY_COLOR = "IAxis.PROPERTY_COLOR";  

  /**
   * Returns the color of the axis
   * @return The chosen java.awt.Color or null if the decision for the color
   *         should be made by the corresponding <code>Chart2D</code>.
   */
  public Color getColor();

  /**
   * Set a <code>java.awt.Color</code> for this axis.
   * <p>
   * 
   * @param color
   *          the <tt>Color</tt> to set.
   */
  public abstract void setColor(Color color);

In AAxis.java the following code was added:

  /** The color property. */
  private Color m_color = Color.black;

  /**
    * Get the <code>Color</code> this color will be painted with.
    * <p>
    * 
    * @return the <code>Color</code> of this instance
    */
  public final Color getColor() {
    return this.m_color;
  }

  /**
   * <p>
   * Set the <code>Color</code> this axis will be painted with.
   * </p>
   * 
   * @param color
   *          the <code>Color</code> this trace will be painted with.
   */
  public final void setColor(final Color color) {
    final Color oldValue = this.m_color;
    this.m_color = color;
    if (!this.m_color.equals(oldValue)) {
      this.m_propertyChangeSupport.firePropertyChange(IAxis.PROPERTY_COLOR, oldValue, this.m_color);
    }
  }

Finally two methods were modified in paintAxisYLeft and paintAxisYRight

add

g2d.setColor(this.getColor());

before the line:

g2d.drawLine(xAxisLine, yAxisStart, xAxisLine, yAxisEnd);

and add

g2d.setColor(this.getColor());

before the line:

tickPainter.paintYTick(xAxisLine, tmp, label.isMajorTick(), true, g2d);

Hope this can be added to the next release...

Camilo Guevara
  • 165
  • 1
  • 12
0

this does not work by now. There has been a feature request long ago: http://sourceforge.net/p/jchart2d/feature-requests/51/

Feel free to vote or even to contribute a patch.

kind regards, Achim

  • Thanks Achim, I did not find how to vote but I will try to create a patch for it. If I am successful I will post the code so it can go in the next release. – Camilo Guevara Jan 26 '15 at 13:05