1 37 38 39 package org.jfree.chart.demo; 40 import java.awt.BorderLayout ; 41 import java.awt.Color ; 42 import java.awt.Font ; 43 import java.awt.event.ActionEvent ; 44 import java.awt.event.ActionListener ; 45 46 import javax.swing.JCheckBox ; 47 import javax.swing.JLabel ; 48 import javax.swing.JPanel ; 49 import javax.swing.JTextField ; 50 51 import org.jfree.chart.ChartFactory; 52 import org.jfree.chart.ChartPanel; 53 import org.jfree.chart.JFreeChart; 54 import org.jfree.chart.Spacer; 55 import org.jfree.chart.axis.AxisLocation; 56 import org.jfree.chart.axis.DateAxis; 57 import org.jfree.chart.axis.NumberAxis; 58 import org.jfree.chart.axis.SymbolicAxis; 59 import org.jfree.chart.axis.ValueAxis; 60 import org.jfree.chart.plot.PlotOrientation; 61 import org.jfree.chart.plot.XYPlot; 62 import org.jfree.chart.renderer.StandardXYItemRenderer; 63 import org.jfree.data.XYSeries; 64 import org.jfree.data.XYSeriesCollection; 65 import org.jfree.ui.ApplicationFrame; 66 import org.jfree.ui.RefineryUtilities; 67 68 73 public class XYTickLabelDemo extends ApplicationFrame implements ActionListener { 74 75 private static int DEFAULT_FONT_SIZE = 13; 77 78 JFreeChart chart; 79 80 81 private JCheckBox verticalTickLabelsCheckBox; 82 83 84 private JCheckBox horizontalPlotCheckBox; 85 86 87 private JCheckBox symbolicAxesCheckBox; 88 89 90 private JTextField fontSizeTextField; 91 92 97 public XYTickLabelDemo(String title) { 98 99 super(title); 100 chart = createChart(); 101 ChartPanel chartPanel = new ChartPanel(chart); 102 chartPanel.setPreferredSize(new java.awt.Dimension (600, 270)); 103 104 JPanel mainPanel = new JPanel (new BorderLayout ()); 105 setContentPane(mainPanel); 106 mainPanel.add(chartPanel); 107 108 JPanel optionsPanel = new JPanel (); 109 mainPanel.add(optionsPanel, BorderLayout.SOUTH); 110 111 symbolicAxesCheckBox = new JCheckBox ("Symbolic axes"); 112 symbolicAxesCheckBox.addActionListener(this); 113 optionsPanel.add(symbolicAxesCheckBox); 114 115 verticalTickLabelsCheckBox = new JCheckBox ("Tick labels vertical"); 116 verticalTickLabelsCheckBox.addActionListener(this); 117 optionsPanel.add(verticalTickLabelsCheckBox); 118 119 fontSizeTextField = new JTextField (3); 120 fontSizeTextField.addActionListener(this); 121 optionsPanel.add(new JLabel ("Font size:")); 122 optionsPanel.add(fontSizeTextField); 123 ValueAxis axis = chart.getXYPlot().getDomainAxis(); 124 fontSizeTextField.setText(DEFAULT_FONT_SIZE+""); 125 126 XYPlot plot = chart.getXYPlot(); 127 Font ft = axis.getTickLabelFont(); 128 ft = ft.deriveFont((float) DEFAULT_FONT_SIZE); 129 plot.getDomainAxis().setTickLabelFont(ft); 130 plot.getRangeAxis().setTickLabelFont(ft); 131 plot.getSecondaryDomainAxis(0).setTickLabelFont(ft); 132 plot.getSecondaryRangeAxis(0).setTickLabelFont(ft); 133 134 135 horizontalPlotCheckBox = new JCheckBox ("Plot horizontal"); 136 horizontalPlotCheckBox.addActionListener(this); 137 optionsPanel.add(horizontalPlotCheckBox); 138 } 139 140 145 public void actionPerformed(ActionEvent evt) 146 { 147 ValueAxis[] axes = new ValueAxis[4]; 148 XYPlot plot = chart.getXYPlot(); 149 axes[0] = plot.getDomainAxis(); 150 axes[1] = plot.getRangeAxis(); 151 axes[2] = plot.getSecondaryDomainAxis(0); 152 axes[3] = plot.getSecondaryRangeAxis(0); 153 154 Object source = evt.getSource(); 155 156 if(source == symbolicAxesCheckBox) { 157 158 boolean val = symbolicAxesCheckBox.isSelected(); 159 160 for(int i = 0; i < axes.length; i++) 161 { 162 ValueAxis axis = axes[i]; 163 String label = axis.getLabel(); 164 int maxTick = (int) axis.getUpperBound(); 165 String [] tickLabels = new String [maxTick]; 166 Font ft = axis.getTickLabelFont(); 167 for(int itk = 0; itk < maxTick; itk++) 168 tickLabels[itk] = "Label " + itk; 169 axis = val ? new SymbolicAxis(label, tickLabels) : new NumberAxis(label); 170 axis.setTickLabelFont(ft); 171 axes[i] = axis; 172 } 173 plot.setDomainAxis(axes[0]); 174 plot.setRangeAxis(axes[1]); 175 plot.setSecondaryDomainAxis(0, axes[2]); 176 plot.setSecondaryRangeAxis(0, axes[3]); 177 178 } 179 180 if(source == symbolicAxesCheckBox || source == verticalTickLabelsCheckBox) { 181 boolean val = verticalTickLabelsCheckBox.isSelected(); 182 183 for(int i = 0; i < axes.length; i++) 184 axes[i].setVerticalTickLabels(val); 185 186 } else if (source == symbolicAxesCheckBox || source == horizontalPlotCheckBox) { 187 188 PlotOrientation val = horizontalPlotCheckBox.isSelected() ? 189 PlotOrientation.HORIZONTAL : PlotOrientation.VERTICAL; 190 chart.getXYPlot().setOrientation(val); 191 192 } else if (source == symbolicAxesCheckBox || source == fontSizeTextField) { 193 String s = fontSizeTextField.getText(); 194 if(s.length() > 0) { 195 float sz = Float.parseFloat(s); 196 for(int i = 0; i < axes.length; i++) { 197 ValueAxis axis = axes[i]; 198 Font ft = axis.getTickLabelFont(); 199 ft = ft.deriveFont(sz); 200 axis.setTickLabelFont(ft); 201 } 202 } 203 } 204 } 205 206 211 private JFreeChart createChart() { 212 213 215 XYSeries series1 = new XYSeries("Something"); 216 series1.add(0.0, 30.0); 217 series1.add(1.0, 10.0); 218 series1.add(2.0, 40.0); 219 series1.add(3.0, 30.0); 220 series1.add(4.0, 50.0); 221 series1.add(5.0, 50.0); 222 series1.add(6.0, 70.0); 223 series1.add(7.0, 70.0); 224 series1.add(8.0, 80.0); 225 226 XYSeriesCollection dataset1 = new XYSeriesCollection(); 227 dataset1.addSeries(series1); 228 229 XYSeries series2 = new XYSeries("Something else"); 230 series2.add(0.0, 5.0); 231 series2.add(1.0, 4.0); 232 series2.add(2.0, 1.0); 233 series2.add(3.0, 5.0); 234 series2.add(4.0, 0.0); 235 236 XYSeriesCollection dataset2 = new XYSeriesCollection(); 237 dataset2.addSeries(series2); 238 239 241 JFreeChart chart = ChartFactory.createXYLineChart( 242 "Tick Label Demo", 243 "Domain Axis 1", 244 "Range Axis 1", 245 dataset1, 246 PlotOrientation.VERTICAL, 247 false, 248 true, 249 false 250 ); 251 252 chart.setBackgroundPaint(Color.white); 253 XYPlot plot = chart.getXYPlot(); 254 plot.setOrientation(PlotOrientation.VERTICAL); 255 plot.setBackgroundPaint(Color.lightGray); 256 plot.setDomainGridlinePaint(Color.white); 257 plot.setRangeGridlinePaint(Color.white); 258 plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0)); 259 260 StandardXYItemRenderer renderer = (StandardXYItemRenderer) plot.getRenderer(); 261 renderer.setPaint(Color.black); 262 263 NumberAxis xAxis2 = new NumberAxis("Domain Axis 2"); 265 xAxis2.setAutoRangeIncludesZero(false); 266 plot.setSecondaryDomainAxis(0, xAxis2); 267 268 DateAxis yAxis1 = new DateAxis("Range Axis 1"); 270 plot.setRangeAxis(yAxis1); 271 272 DateAxis yAxis2 = new DateAxis("Range Axis 2"); 273 plot.setSecondaryRangeAxis(0, yAxis2); 274 plot.setSecondaryRangeAxisLocation(0, AxisLocation.BOTTOM_OR_RIGHT); 275 276 plot.setSecondaryDataset(0, dataset2); 277 plot.mapSecondaryDatasetToDomainAxis(0, new Integer (0)); 278 plot.mapSecondaryDatasetToRangeAxis(0, new Integer (0)); 279 280 return chart; 281 } 282 283 288 public static void main(String [] args) { 289 290 XYTickLabelDemo demo = new XYTickLabelDemo("Tick Label Demo"); 291 demo.pack(); 292 RefineryUtilities.centerFrameOnScreen(demo); 293 demo.setVisible(true); 294 295 } 296 } 297 298 | Popular Tags |