java/cewolf-1.0/src/main/java/de/laures/cewolf/cpp/RotatedAxisLabels.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 28 Feb 2009 21:31:02 +0100
changeset 1 639991d0808a
permissions -rw-r--r--
Rozbalená knihovna verze 1.0
franta-hg@1
     1
package de.laures.cewolf.cpp;
franta-hg@1
     2
franta-hg@1
     3
import java.util.Map;
franta-hg@1
     4
franta-hg@1
     5
import org.jfree.chart.JFreeChart;
franta-hg@1
     6
import org.jfree.chart.axis.CategoryAxis;
franta-hg@1
     7
import org.jfree.chart.axis.CategoryLabelPositions;
franta-hg@1
     8
import org.jfree.chart.plot.CategoryPlot;
franta-hg@1
     9
import org.jfree.data.category.CategoryDataset;
franta-hg@1
    10
franta-hg@1
    11
import de.laures.cewolf.ChartPostProcessor;
franta-hg@1
    12
franta-hg@1
    13
/**
franta-hg@1
    14
* A cewolf post-processor for rotating and/or removing the labels on the X-Axis
franta-hg@1
    15
* parameters:
franta-hg@1
    16
* rotate_at: make the labels vertical
franta-hg@1
    17
* skip_at: print only some of the labels (so they don't overlap)
franta-hg@1
    18
* remove_at: don't print any labels
franta-hg@1
    19
*
franta-hg@1
    20
* Usage:
franta-hg@1
    21
* <chart:chartpostprocessor id="labelRotation">
franta-hg@1
    22
* <chart:param name="rotate_at" value='<%= new Integer(10) %>'/>
franta-hg@1
    23
* <chart:param name="skip_at" value='<%= new Integer(50) %>'/>
franta-hg@1
    24
* <chart:param name="remove_at" value='<%= new Integer(100) %>'/>
franta-hg@1
    25
* </chart:chartpostprocessor>
franta-hg@1
    26
*
franta-hg@1
    27
*
franta-hg@1
    28
* @author Rich Unger
franta-hg@1
    29
*/
franta-hg@1
    30
franta-hg@1
    31
public class RotatedAxisLabels implements ChartPostProcessor {
franta-hg@1
    32
	
franta-hg@1
    33
public void processChart(Object chart, Map params) {
franta-hg@1
    34
		CategoryPlot plot = (CategoryPlot) ((JFreeChart) chart).getPlot();
franta-hg@1
    35
franta-hg@1
    36
		CategoryAxis axis = plot.getDomainAxis();
franta-hg@1
    37
franta-hg@1
    38
		Number rotateThreshold = (Number) params.get("rotate_at");
franta-hg@1
    39
		Number skipThreshold = (Number) params.get("skip_at");
franta-hg@1
    40
		Number removeThreshold = (Number) params.get("remove_at");
franta-hg@1
    41
franta-hg@1
    42
		CategoryDataset dataset = plot.getDataset();
franta-hg@1
    43
		int iCategoryCount = dataset.getRowCount();
franta-hg@1
    44
franta-hg@1
    45
		if (rotateThreshold != null) 
franta-hg@1
    46
    {
franta-hg@1
    47
      if (iCategoryCount >= rotateThreshold.intValue()) 
franta-hg@1
    48
      {
franta-hg@1
    49
        axis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
franta-hg@1
    50
      }
franta-hg@1
    51
      else 
franta-hg@1
    52
      {
franta-hg@1
    53
        axis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD);
franta-hg@1
    54
      }
franta-hg@1
    55
franta-hg@1
    56
    }
franta-hg@1
    57
    
franta-hg@1
    58
		if (skipThreshold != null) 
franta-hg@1
    59
    {
franta-hg@1
    60
      //this method does nothing in jfreechart .9.18
franta-hg@1
    61
			//axis.setSkipCategoryLabelsToFit(iCategoryCount >= skipThreshold.intValue());
franta-hg@1
    62
		}
franta-hg@1
    63
		
franta-hg@1
    64
    if (removeThreshold != null) 
franta-hg@1
    65
    {
franta-hg@1
    66
			axis.setTickLabelsVisible(iCategoryCount < removeThreshold.intValue());
franta-hg@1
    67
		}
franta-hg@1
    68
	}
franta-hg@1
    69
	
franta-hg@1
    70
}