FileDocCategorySizeDatePackage
TableChart.javaAPI DocExample4017Mon Nov 09 12:45:50 GMT 1998None

TableChart

public class TableChart extends JComponent implements TableModelListener

Fields Summary
protected TableModel
model
protected ChartPainter
cp
protected double[]
percentages
protected String[]
labels
protected String[]
tips
protected NumberFormat
formatter
Constructors Summary
public TableChart(TableModel tm)


     
    setUI(cp = new PieChartPainter());
    setModel(tm);
  
Methods Summary
protected voidcalculatePercentages()

    double runningTotal = 0.0;
    for (int i = model.getRowCount() - 1; i >= 0; i--) {
      percentages[i] = 0.0;
      for (int j = model.getColumnCount() - 1; j >=0; j--) {
        
        // First try the cell as a Number object
        try {
          percentages[i] += ((Number)model.getValueAt(i, j)).doubleValue();
        }
        catch(ClassCastException cce) {  // oops, it wasn't numeric...
          // Ok, so try it as a string
          try {
            percentages[i]+=Double.valueOf(model.getValueAt(i,j).toString())
                                    .doubleValue();
          }
          catch (Exception e) { // give up. 
          }
        }
      }
      runningTotal += percentages[i];
    }

    // make each entry a percentage of the total
    for (int i = model.getRowCount() - 1; i >= 0; i--) {
      percentages[i] /= runningTotal;
    }
  
protected voidcreateLabelsAndTips()

    for (int i = model.getRowCount() - 1; i >= 0; i--) {
      labels[i] = (String)model.getValueAt(i, 0);
      tips[i] = formatter.format(percentages[i]);
    }
  
public java.awt.Color[]getColor()

 return cp.getColor(); 
public java.awt.ColorgetColor(int index)

 return cp.getColor(index); 
public javax.swing.table.TableModelgetModel()

 return model; 
public java.awt.ColorgetTextColor()

 return cp.getTextColor(); 
public java.awt.FontgetTextFont()

 return cp.getTextFont(); 
public java.lang.StringgetToolTipText(java.awt.event.MouseEvent me)

    if (tips != null) {
      int whichTip = cp.indexOfEntryAt(me);
      if (whichTip != -1) {
        return tips[whichTip];
      }
    }
    return null;
  
public voidsetColor(java.awt.Color[] clist)

 cp.setColor(clist); 
public voidsetColor(int index, java.awt.Color c)

 cp.setColor(index, c); 
public voidsetModel(javax.swing.table.TableModel tm)

    // get listener code correct.
    if (tm != model) {
      if (model != null) {
	model.removeTableModelListener(this);
      }
      model = tm;
      model.addTableModelListener(this);
      updateLocalValues(true);
    }
  
public voidsetTextColor(java.awt.Color c)

 cp.setTextColor(c); 
public voidsetTextFont(java.awt.Font f)

 cp.setTextFont(f); 
public voidtableChanged(javax.swing.event.TableModelEvent tme)

    // only rebuild the arrays if the structure changed.
    updateLocalValues(tme.getType() != TableModelEvent.UPDATE);
  
protected voidupdateLocalValues(boolean freshStart)

    if (freshStart) {
      int count = model.getRowCount();
      if ((tips == null) || (count != tips.length)) {
        percentages = new double[count];
        labels = new String[count];
        tips = new String[count];
      }
    }
    calculatePercentages();
    createLabelsAndTips();

    // Now that everything's up-to-date, reset the chart painter with the new
    // values
    cp.setValues(percentages);
    cp.setLabels(labels);

    // and finally, repaint the chart.
    repaint();