How can I get series by name

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
sesar
Posts: 12
Joined: Fri Jul 13, 2007 10:57 am

How can I get series by name

Post by sesar » Sun Dec 02, 2007 1:51 pm

Hi, I use XYSeriesCollection and how can I get a serie from dataset by serie's name ???

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Mon Dec 03, 2007 5:11 pm

It looks as though you can only iterate through the collection, and call getSeriesKey(int) for each series, then if you get a matching key call getSeries(int).

I propose the following method (to add in XYSeriesCollection) to fill that gap:

Code: Select all

    /**
     * Returns a series from the collection.
     * 
     * @param key  the key (<code>null</code> not permitted).
     * 
     * @return The series with the specified key.
     * 
     * @throws UnknownKeyException if <code>key</code> is not found in the
     *         collection.
     * 
     * @since 1.0.9
     */
    public XYSeries getSeries(Comparable key) {
        if (key == null) {
            throw new IllegalArgumentException("Null 'key' argument.");
        }
        Iterator iterator = this.data.iterator();
        while (iterator.hasNext()) {
            XYSeries series = (XYSeries) iterator.next();
            if (key.equals(series.getKey())) {
                return series;
            }
        }
        throw new UnknownKeyException("Key not found: " + key);
    }
Let me know if you spot a problem with it.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Locked