Java Code Examples for org.jfree.data.DefaultKeyedValues#addValue()

The following examples show how to use org.jfree.data.DefaultKeyedValues#addValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultKeyedValuesTests.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Some checks for the getKeys() method.
 */
public void testGetKeys() {
    DefaultKeyedValues d = new DefaultKeyedValues();
    List keys = d.getKeys();
    assertTrue(keys.isEmpty());
    d.addValue("A", 1.0);
    keys = d.getKeys();
    assertEquals(1, keys.size());
    assertTrue(keys.contains("A"));
    d.addValue("B", 2.0);
    keys = d.getKeys();
    assertEquals(2, keys.size());
    assertTrue(keys.contains("A"));
    assertTrue(keys.contains("B"));
    d.clear();
    keys = d.getKeys();
    assertEquals(0, keys.size());        
}
 
Example 2
Source File: DefaultKeyedValuesTests.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Some checks for the getKeys() method.
 */
public void testGetKeys() {
    DefaultKeyedValues d = new DefaultKeyedValues();
    List keys = d.getKeys();
    assertTrue(keys.isEmpty());
    d.addValue("A", 1.0);
    keys = d.getKeys();
    assertEquals(1, keys.size());
    assertTrue(keys.contains("A"));
    d.addValue("B", 2.0);
    keys = d.getKeys();
    assertEquals(2, keys.size());
    assertTrue(keys.contains("A"));
    assertTrue(keys.contains("B"));
    d.clear();
    keys = d.getKeys();
    assertEquals(0, keys.size());
}
 
Example 3
Source File: DefaultKeyedValuesTests.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Some checks for the getIndex() methods.
 */
public void testGetIndex() {
    DefaultKeyedValues v1 = new DefaultKeyedValues();
    assertEquals(-1, v1.getIndex("K1"));

    DefaultKeyedValues v2 = new DefaultKeyedValues();
    v2.addValue("K1", new Integer(1));
    v2.addValue("K2", new Integer(2));
    v2.addValue("K3", new Integer(3));
    assertEquals(2, v2.getIndex("K3"));
    
    // try null
    boolean pass = false;
    try {
        v2.getIndex(null);
    }
    catch (IllegalArgumentException e) {
        pass = true;
    }
    assertTrue(pass);
}
 
Example 4
Source File: DefaultKeyedValuesTests.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Some checks for the getIndex() methods.
 */
public void testGetIndex() {
    DefaultKeyedValues v1 = new DefaultKeyedValues();
    assertEquals(-1, v1.getIndex("K1"));

    DefaultKeyedValues v2 = new DefaultKeyedValues();
    v2.addValue("K1", new Integer(1));
    v2.addValue("K2", new Integer(2));
    v2.addValue("K3", new Integer(3));
    assertEquals(2, v2.getIndex("K3"));

    // try null
    boolean pass = false;
    try {
        v2.getIndex(null);
    }
    catch (IllegalArgumentException e) {
        pass = true;
    }
    assertTrue(pass);
}
 
Example 5
Source File: DefaultKeyedValuesTests.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Check that inserting and retrieving values works as expected.
 */
public void testInsertAndRetrieve() {
    DefaultKeyedValues data = new DefaultKeyedValues();
    data.addValue("A", new Double(1.0));
    data.addValue("B", new Double(2.0));
    data.addValue("C", new Double(3.0));
    data.addValue("D", null);

    // check key order
    assertEquals(data.getKey(0), "A");
    assertEquals(data.getKey(1), "B");
    assertEquals(data.getKey(2), "C");
    assertEquals(data.getKey(3), "D");

    // check retrieve value by key
    assertEquals(data.getValue("A"), new Double(1.0));
    assertEquals(data.getValue("B"), new Double(2.0));
    assertEquals(data.getValue("C"), new Double(3.0));
    assertEquals(data.getValue("D"), null);

    // check retrieve value by index
    assertEquals(data.getValue(0), new Double(1.0));
    assertEquals(data.getValue(1), new Double(2.0));
    assertEquals(data.getValue(2), new Double(3.0));
    assertEquals(data.getValue(3), null);
}
 
Example 6
Source File: DefaultKeyedValuesTests.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check that inserting and retrieving values works as expected.
 */
public void testInsertAndRetrieve() {

    DefaultKeyedValues data = new DefaultKeyedValues();
    data.addValue("A", new Double(1.0));
    data.addValue("B", new Double(2.0));
    data.addValue("C", new Double(3.0));
    data.addValue("D", null);

    // check key order
    assertEquals(data.getKey(0), "A");
    assertEquals(data.getKey(1), "B");
    assertEquals(data.getKey(2), "C");
    assertEquals(data.getKey(3), "D");

    // check retrieve value by key
    assertEquals(data.getValue("A"), new Double(1.0));
    assertEquals(data.getValue("B"), new Double(2.0));
    assertEquals(data.getValue("C"), new Double(3.0));
    assertEquals(data.getValue("D"), null);

    // check retrieve value by index
    assertEquals(data.getValue(0), new Double(1.0));
    assertEquals(data.getValue(1), new Double(2.0));
    assertEquals(data.getValue(2), new Double(3.0));
    assertEquals(data.getValue(3), null);

}
 
Example 7
Source File: DefaultKeyedValuesTests.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests sorting of data by value (ascending).
 */
public void testSortByValueAscending() {

    DefaultKeyedValues data = new DefaultKeyedValues();
    data.addValue("C", new Double(1.0));
    data.addValue("B", null);
    data.addValue("D", new Double(3.0));
    data.addValue("A", new Double(2.0));

    data.sortByValues(SortOrder.ASCENDING);

    // check key order
    assertEquals(data.getKey(0), "C");
    assertEquals(data.getKey(1), "A");
    assertEquals(data.getKey(2), "D");
    assertEquals(data.getKey(3), "B");

    // check retrieve value by key
    assertEquals(data.getValue("A"), new Double(2.0));
    assertEquals(data.getValue("B"), null);
    assertEquals(data.getValue("C"), new Double(1.0));
    assertEquals(data.getValue("D"), new Double(3.0));

    // check retrieve value by index
    assertEquals(data.getValue(0), new Double(1.0));
    assertEquals(data.getValue(1), new Double(2.0));
    assertEquals(data.getValue(2), new Double(3.0));
    assertEquals(data.getValue(3), null);

}
 
Example 8
Source File: DefaultKeyedValuesTests.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Some checks for the getItemCount() method.
 */
public void testGetItemCount() {
    DefaultKeyedValues d = new DefaultKeyedValues();
    assertEquals(0, d.getItemCount());
    d.addValue("A", 1.0);
    assertEquals(1, d.getItemCount());
    d.addValue("B", 2.0);
    assertEquals(2, d.getItemCount());
    d.clear();
    assertEquals(0, d.getItemCount());        
}
 
Example 9
Source File: DefaultKeyedValuesTests.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests sorting of data by key (ascending).
 */
public void testSortByKeyAscending() {
    DefaultKeyedValues data = new DefaultKeyedValues();
    data.addValue("C", new Double(1.0));
    data.addValue("B", null);
    data.addValue("D", new Double(3.0));
    data.addValue("A", new Double(2.0));

    data.sortByKeys(SortOrder.ASCENDING);

    // check key order
    assertEquals(data.getKey(0), "A");
    assertEquals(data.getKey(1), "B");
    assertEquals(data.getKey(2), "C");
    assertEquals(data.getKey(3), "D");

    // check retrieve value by key
    assertEquals(data.getValue("A"), new Double(2.0));
    assertEquals(data.getValue("B"), null);
    assertEquals(data.getValue("C"), new Double(1.0));
    assertEquals(data.getValue("D"), new Double(3.0));

    // check retrieve value by index
    assertEquals(data.getValue(0), new Double(2.0));
    assertEquals(data.getValue(1), null);
    assertEquals(data.getValue(2), new Double(1.0));
    assertEquals(data.getValue(3), new Double(3.0));
}
 
Example 10
Source File: DefaultKeyedValuesTests.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Some checks for the getItemCount() method.
 */
public void testGetItemCount() {
    DefaultKeyedValues d = new DefaultKeyedValues();
    assertEquals(0, d.getItemCount());
    d.addValue("A", 1.0);
    assertEquals(1, d.getItemCount());
    d.addValue("B", 2.0);
    assertEquals(2, d.getItemCount());
    d.clear();
    assertEquals(0, d.getItemCount());
}
 
Example 11
Source File: DefaultKeyedValuesTests.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Another check for the getIndex(Comparable) method.
 */
public void testGetIndex2() {
	DefaultKeyedValues v = new DefaultKeyedValues();
	assertEquals(-1, v.getIndex("K1"));
	v.addValue("K1", 1.0);
	assertEquals(0, v.getIndex("K1"));
	v.removeValue("K1");
	assertEquals(-1, v.getIndex("K1"));
}
 
Example 12
Source File: DefaultKeyedValuesTests.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests sorting of data by key (ascending).
 */
public void testSortByKeyAscending() {

    DefaultKeyedValues data = new DefaultKeyedValues();
    data.addValue("C", new Double(1.0));
    data.addValue("B", null);
    data.addValue("D", new Double(3.0));
    data.addValue("A", new Double(2.0));

    data.sortByKeys(SortOrder.ASCENDING);

    // check key order
    assertEquals(data.getKey(0), "A");
    assertEquals(data.getKey(1), "B");
    assertEquals(data.getKey(2), "C");
    assertEquals(data.getKey(3), "D");

    // check retrieve value by key
    assertEquals(data.getValue("A"), new Double(2.0));
    assertEquals(data.getValue("B"), null);
    assertEquals(data.getValue("C"), new Double(1.0));
    assertEquals(data.getValue("D"), new Double(3.0));

    // check retrieve value by index
    assertEquals(data.getValue(0), new Double(2.0));
    assertEquals(data.getValue(1), null);
    assertEquals(data.getValue(2), new Double(1.0));
    assertEquals(data.getValue(3), new Double(3.0));

}
 
Example 13
Source File: DefaultKeyedValuesTests.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests sorting of data by key (descending).
 */
public void testSortByValueDescending() {

    DefaultKeyedValues data = new DefaultKeyedValues();
    data.addValue("C", new Double(1.0));
    data.addValue("B", null);
    data.addValue("D", new Double(3.0));
    data.addValue("A", new Double(2.0));

    data.sortByValues(SortOrder.DESCENDING);

    // check key order
    assertEquals(data.getKey(0), "D");
    assertEquals(data.getKey(1), "A");
    assertEquals(data.getKey(2), "C");
    assertEquals(data.getKey(3), "B");

    // check retrieve value by key
    assertEquals(data.getValue("A"), new Double(2.0));
    assertEquals(data.getValue("B"), null);
    assertEquals(data.getValue("C"), new Double(1.0));
    assertEquals(data.getValue("D"), new Double(3.0));

    // check retrieve value by index
    assertEquals(data.getValue(0), new Double(3.0));
    assertEquals(data.getValue(1), new Double(2.0));
    assertEquals(data.getValue(2), new Double(1.0));
    assertEquals(data.getValue(3), null);

}
 
Example 14
Source File: PiePlot.java    From buffer_bci with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Draws the labels for the pie sections.
 *
 * @param g2  the graphics device.
 * @param keys  the keys.
 * @param totalValue  the total value.
 * @param plotArea  the plot area.
 * @param linkArea  the link area.
 * @param state  the state.
 */
protected void drawLabels(Graphics2D g2, List keys, double totalValue,
                          Rectangle2D plotArea, Rectangle2D linkArea,
                          PiePlotState state) {

    Composite originalComposite = g2.getComposite();
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
            1.0f));

    // classify the keys according to which side the label will appear...
    DefaultKeyedValues leftKeys = new DefaultKeyedValues();
    DefaultKeyedValues rightKeys = new DefaultKeyedValues();

    double runningTotal = 0.0;
    Iterator iterator = keys.iterator();
    while (iterator.hasNext()) {
        Comparable key = (Comparable) iterator.next();
        boolean include;
        double v = 0.0;
        Number n = this.dataset.getValue(key);
        if (n == null) {
            include = !this.ignoreNullValues;
        }
        else {
            v = n.doubleValue();
            include = this.ignoreZeroValues ? v > 0.0 : v >= 0.0;
        }

        if (include) {
            runningTotal = runningTotal + v;
            // work out the mid angle (0 - 90 and 270 - 360) = right,
            // otherwise left
            double mid = this.startAngle + (this.direction.getFactor()
                    * ((runningTotal - v / 2.0) * 360) / totalValue);
            if (Math.cos(Math.toRadians(mid)) < 0.0) {
                leftKeys.addValue(key, new Double(mid));
            }
            else {
                rightKeys.addValue(key, new Double(mid));
            }
        }
    }

    g2.setFont(getLabelFont());

    // calculate the max label width from the plot dimensions, because
    // a circular pie can leave a lot more room for labels...
    double marginX = plotArea.getX();
    double gap = plotArea.getWidth() * this.labelGap;
    double ww = linkArea.getX() - gap - marginX;
    float labelWidth = (float) this.labelPadding.trimWidth(ww);

    // draw the labels...
    if (this.labelGenerator != null) {
        drawLeftLabels(leftKeys, g2, plotArea, linkArea, labelWidth,
                state);
        drawRightLabels(rightKeys, g2, plotArea, linkArea, labelWidth,
                state);
    }
    g2.setComposite(originalComposite);

}
 
Example 15
Source File: PiePlot.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Draws the labels for the pie sections.
 * 
 * @param g2  the graphics device.
 * @param keys  the keys.
 * @param totalValue  the total value.
 * @param plotArea  the plot area.
 * @param linkArea  the link area.
 * @param state  the state.
 */
protected void drawLabels(Graphics2D g2, List keys, double totalValue, 
                          Rectangle2D plotArea, Rectangle2D linkArea, 
                          PiePlotState state) {   

    Composite originalComposite = g2.getComposite();
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 
            1.0f));

    // classify the keys according to which side the label will appear...
    DefaultKeyedValues leftKeys = new DefaultKeyedValues();
    DefaultKeyedValues rightKeys = new DefaultKeyedValues();
   
    double runningTotal = 0.0;
    Iterator iterator = keys.iterator();
    while (iterator.hasNext()) {
        Comparable key = (Comparable) iterator.next();
        boolean include = true;
        double v = 0.0;
        Number n = this.dataset.getValue(key);
        if (n == null) {
            include = !this.ignoreNullValues;
        }
        else {
            v = n.doubleValue();
            include = this.ignoreZeroValues ? v > 0.0 : v >= 0.0;
        }

        if (include) {
            runningTotal = runningTotal + v;
            // work out the mid angle (0 - 90 and 270 - 360) = right, 
            // otherwise left
            double mid = this.startAngle + (this.direction.getFactor()
                    * ((runningTotal - v / 2.0) * 360) / totalValue);
            if (Math.cos(Math.toRadians(mid)) < 0.0) {
                leftKeys.addValue(key, new Double(mid));
            }
            else {
                rightKeys.addValue(key, new Double(mid));
            }
        }
    }
   
    g2.setFont(getLabelFont());
    
    // calculate the max label width from the plot dimensions, because
    // a circular pie can leave a lot more room for labels...
    double marginX = plotArea.getX() + this.interiorGap 
            * plotArea.getWidth();
    double gap = plotArea.getWidth() * this.labelGap;
    double ww = linkArea.getX() - gap - marginX;
    float labelWidth = (float) this.labelPadding.trimWidth(ww);
    
    // draw the labels...
    if (this.labelGenerator != null) {
        drawLeftLabels(leftKeys, g2, plotArea, linkArea, labelWidth, 
                state);
        drawRightLabels(rightKeys, g2, plotArea, linkArea, labelWidth, 
                state);
    }
    g2.setComposite(originalComposite);

}
 
Example 16
Source File: PiePlot.java    From ECG-Viewer with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Draws the labels for the pie sections.
 *
 * @param g2  the graphics device.
 * @param keys  the keys.
 * @param totalValue  the total value.
 * @param plotArea  the plot area.
 * @param linkArea  the link area.
 * @param state  the state.
 */
protected void drawLabels(Graphics2D g2, List keys, double totalValue,
                          Rectangle2D plotArea, Rectangle2D linkArea,
                          PiePlotState state) {

    Composite originalComposite = g2.getComposite();
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
            1.0f));

    // classify the keys according to which side the label will appear...
    DefaultKeyedValues leftKeys = new DefaultKeyedValues();
    DefaultKeyedValues rightKeys = new DefaultKeyedValues();

    double runningTotal = 0.0;
    Iterator iterator = keys.iterator();
    while (iterator.hasNext()) {
        Comparable key = (Comparable) iterator.next();
        boolean include;
        double v = 0.0;
        Number n = this.dataset.getValue(key);
        if (n == null) {
            include = !this.ignoreNullValues;
        }
        else {
            v = n.doubleValue();
            include = this.ignoreZeroValues ? v > 0.0 : v >= 0.0;
        }

        if (include) {
            runningTotal = runningTotal + v;
            // work out the mid angle (0 - 90 and 270 - 360) = right,
            // otherwise left
            double mid = this.startAngle + (this.direction.getFactor()
                    * ((runningTotal - v / 2.0) * 360) / totalValue);
            if (Math.cos(Math.toRadians(mid)) < 0.0) {
                leftKeys.addValue(key, new Double(mid));
            }
            else {
                rightKeys.addValue(key, new Double(mid));
            }
        }
    }

    g2.setFont(getLabelFont());

    // calculate the max label width from the plot dimensions, because
    // a circular pie can leave a lot more room for labels...
    double marginX = plotArea.getX();
    double gap = plotArea.getWidth() * this.labelGap;
    double ww = linkArea.getX() - gap - marginX;
    float labelWidth = (float) this.labelPadding.trimWidth(ww);

    // draw the labels...
    if (this.labelGenerator != null) {
        drawLeftLabels(leftKeys, g2, plotArea, linkArea, labelWidth,
                state);
        drawRightLabels(rightKeys, g2, plotArea, linkArea, labelWidth,
                state);
    }
    g2.setComposite(originalComposite);

}
 
Example 17
Source File: Chart_15_PiePlot_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Draws the labels for the pie sections.
 * 
 * @param g2  the graphics device.
 * @param keys  the keys.
 * @param totalValue  the total value.
 * @param plotArea  the plot area.
 * @param linkArea  the link area.
 * @param state  the state.
 */
protected void drawLabels(Graphics2D g2, List keys, double totalValue, 
                          Rectangle2D plotArea, Rectangle2D linkArea, 
                          PiePlotState state) {   

    Composite originalComposite = g2.getComposite();
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 
            1.0f));

    // classify the keys according to which side the label will appear...
    DefaultKeyedValues leftKeys = new DefaultKeyedValues();
    DefaultKeyedValues rightKeys = new DefaultKeyedValues();
   
    double runningTotal = 0.0;
    Iterator iterator = keys.iterator();
    while (iterator.hasNext()) {
        Comparable key = (Comparable) iterator.next();
        boolean include = true;
        double v = 0.0;
        Number n = this.dataset.getValue(key);
        if (n == null) {
            include = !this.ignoreNullValues;
        }
        else {
            v = n.doubleValue();
            include = this.ignoreZeroValues ? v > 0.0 : v >= 0.0;
        }

        if (include) {
            runningTotal = runningTotal + v;
            // work out the mid angle (0 - 90 and 270 - 360) = right, 
            // otherwise left
            double mid = this.startAngle + (this.direction.getFactor()
                    * ((runningTotal - v / 2.0) * 360) / totalValue);
            if (Math.cos(Math.toRadians(mid)) < 0.0) {
                leftKeys.addValue(key, new Double(mid));
            }
            else {
                rightKeys.addValue(key, new Double(mid));
            }
        }
    }
   
    g2.setFont(getLabelFont());
    
    // calculate the max label width from the plot dimensions, because
    // a circular pie can leave a lot more room for labels...
    double marginX = plotArea.getX() + this.interiorGap * plotArea.getWidth();
    double gap = plotArea.getWidth() * this.labelGap;
    double ww = linkArea.getX() - gap - marginX;
    float labelWidth = (float) this.labelPadding.trimWidth(ww);
    
    // draw the labels...
    if (this.labelGenerator != null) {
        drawLeftLabels(leftKeys, g2, plotArea, linkArea, labelWidth, 
                state);
        drawRightLabels(rightKeys, g2, plotArea, linkArea, labelWidth, 
                state);
    }
    g2.setComposite(originalComposite);

}
 
Example 18
Source File: Chart_15_PiePlot_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Draws the labels for the pie sections.
 * 
 * @param g2  the graphics device.
 * @param keys  the keys.
 * @param totalValue  the total value.
 * @param plotArea  the plot area.
 * @param linkArea  the link area.
 * @param state  the state.
 */
protected void drawLabels(Graphics2D g2, List keys, double totalValue, 
                          Rectangle2D plotArea, Rectangle2D linkArea, 
                          PiePlotState state) {   

    Composite originalComposite = g2.getComposite();
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 
            1.0f));

    // classify the keys according to which side the label will appear...
    DefaultKeyedValues leftKeys = new DefaultKeyedValues();
    DefaultKeyedValues rightKeys = new DefaultKeyedValues();
   
    double runningTotal = 0.0;
    Iterator iterator = keys.iterator();
    while (iterator.hasNext()) {
        Comparable key = (Comparable) iterator.next();
        boolean include = true;
        double v = 0.0;
        Number n = this.dataset.getValue(key);
        if (n == null) {
            include = !this.ignoreNullValues;
        }
        else {
            v = n.doubleValue();
            include = this.ignoreZeroValues ? v > 0.0 : v >= 0.0;
        }

        if (include) {
            runningTotal = runningTotal + v;
            // work out the mid angle (0 - 90 and 270 - 360) = right, 
            // otherwise left
            double mid = this.startAngle + (this.direction.getFactor()
                    * ((runningTotal - v / 2.0) * 360) / totalValue);
            if (Math.cos(Math.toRadians(mid)) < 0.0) {
                leftKeys.addValue(key, new Double(mid));
            }
            else {
                rightKeys.addValue(key, new Double(mid));
            }
        }
    }
   
    g2.setFont(getLabelFont());
    
    // calculate the max label width from the plot dimensions, because
    // a circular pie can leave a lot more room for labels...
    double marginX = plotArea.getX() + this.interiorGap * plotArea.getWidth();
    double gap = plotArea.getWidth() * this.labelGap;
    double ww = linkArea.getX() - gap - marginX;
    float labelWidth = (float) this.labelPadding.trimWidth(ww);
    
    // draw the labels...
    if (this.labelGenerator != null) {
        drawLeftLabels(leftKeys, g2, plotArea, linkArea, labelWidth, 
                state);
        drawRightLabels(rightKeys, g2, plotArea, linkArea, labelWidth, 
                state);
    }
    g2.setComposite(originalComposite);

}
 
Example 19
Source File: PiePlot.java    From SIMVA-SoS with Apache License 2.0 4 votes vote down vote up
/**
 * Draws the labels for the pie sections.
 *
 * @param g2  the graphics device.
 * @param keys  the keys.
 * @param totalValue  the total value.
 * @param plotArea  the plot area.
 * @param linkArea  the link area.
 * @param state  the state.
 */
protected void drawLabels(Graphics2D g2, List keys, double totalValue,
                          Rectangle2D plotArea, Rectangle2D linkArea,
                          PiePlotState state) {

    Composite originalComposite = g2.getComposite();
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
            1.0f));

    // classify the keys according to which side the label will appear...
    DefaultKeyedValues leftKeys = new DefaultKeyedValues();
    DefaultKeyedValues rightKeys = new DefaultKeyedValues();

    double runningTotal = 0.0;
    Iterator iterator = keys.iterator();
    while (iterator.hasNext()) {
        Comparable key = (Comparable) iterator.next();
        boolean include;
        double v = 0.0;
        Number n = this.dataset.getValue(key);
        if (n == null) {
            include = !this.ignoreNullValues;
        }
        else {
            v = n.doubleValue();
            include = this.ignoreZeroValues ? v > 0.0 : v >= 0.0;
        }

        if (include) {
            runningTotal = runningTotal + v;
            // work out the mid angle (0 - 90 and 270 - 360) = right,
            // otherwise left
            double mid = this.startAngle + (this.direction.getFactor()
                    * ((runningTotal - v / 2.0) * 360) / totalValue);
            if (Math.cos(Math.toRadians(mid)) < 0.0) {
                leftKeys.addValue(key, new Double(mid));
            }
            else {
                rightKeys.addValue(key, new Double(mid));
            }
        }
    }

    g2.setFont(getLabelFont());

    // calculate the max label width from the plot dimensions, because
    // a circular pie can leave a lot more room for labels...
    double marginX = plotArea.getX();
    double gap = plotArea.getWidth() * this.labelGap;
    double ww = linkArea.getX() - gap - marginX;
    float labelWidth = (float) this.labelPadding.trimWidth(ww);

    // draw the labels...
    if (this.labelGenerator != null) {
        drawLeftLabels(leftKeys, g2, plotArea, linkArea, labelWidth,
                state);
        drawRightLabels(rightKeys, g2, plotArea, linkArea, labelWidth,
                state);
    }
    g2.setComposite(originalComposite);

}
 
Example 20
Source File: CumulativeCurveChart.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
public DatasetMap calculateValue() throws Exception {

		logger.debug("IN");
		String res=DataSetAccessFunctions.getDataSetResultFromId(profile, getData(),parametersObject);

		SourceBean sbRows=SourceBean.fromXMLString(res);
		SourceBean sbRow=(SourceBean)sbRows.getAttribute("ROW");
		List listAtts=sbRow.getContainedAttributes();

		DefaultKeyedValues keyedValues=new DefaultKeyedValues();



		for (Iterator iterator = listAtts.iterator(); iterator.hasNext();) {
			SourceBeanAttribute att = (SourceBeanAttribute) iterator.next();
			String name=att.getKey();
			String valueS=(String)att.getValue();

			//try Double and Integer Conversion

			Double valueD=null;
			try{
				valueD=Double.valueOf(valueS);
			}
			catch (Exception e) {}

			Integer valueI=null;
			if(valueD==null){
				valueI=Integer.valueOf(valueS);
			}

			if(name!=null && valueD!=null){
				keyedValues.addValue(name, valueD);
			}
			else if(name!=null && valueI!=null){
				keyedValues.addValue(name, valueI);
			}
		}
		keyedValues.sortByValues(sortOrder);  //let user choose


		KeyedValues cumulative = DataUtilities.getCumulativePercentages(keyedValues);

		CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
				"Languages", keyedValues);
		CategoryDataset dataset2 = DatasetUtilities.createCategoryDataset(
				"Cumulative", cumulative);


		logger.debug("OUT");
		DatasetMap datasets=new DatasetMap();
		datasets.addDataset("1",dataset);
		datasets.addDataset("2",dataset2);

		return datasets;

	}