1 37 38 package org.jfree.chart.demo; 39 40 import java.awt.Color ; 41 import java.awt.GradientPaint ; 42 43 import org.jfree.chart.ChartFactory; 44 import org.jfree.chart.ChartPanel; 45 import org.jfree.chart.JFreeChart; 46 import org.jfree.chart.axis.NumberAxis; 47 import org.jfree.chart.plot.PlotOrientation; 48 import org.jfree.chart.plot.XYPlot; 49 import org.jfree.data.MatrixSeriesCollection; 50 import org.jfree.data.NormalizedMatrixSeries; 51 import org.jfree.ui.ApplicationFrame; 52 import org.jfree.ui.RefineryUtilities; 53 54 62 public class BubblyBubblesDemo2 extends ApplicationFrame { 63 64 65 private static final int SIZE = 10; 66 67 68 private static final String TITLE = "Population count at grid locations"; 69 70 74 private NormalizedMatrixSeries m_series; 75 76 81 public BubblyBubblesDemo2(String title) { 82 super(title); 83 84 m_series = createInitialSeries(); 85 86 MatrixSeriesCollection dataset = new MatrixSeriesCollection(m_series); 87 88 JFreeChart chart = ChartFactory.createBubbleChart( 89 TITLE, 90 "X", 91 "Y", 92 dataset, 93 PlotOrientation.VERTICAL, 94 true, 95 true, 96 false 97 ); 98 99 chart.setBackgroundPaint(new GradientPaint (0, 0, Color.white, 0, 1000, Color.yellow)); 100 101 XYPlot plot = chart.getXYPlot(); 102 plot.setForegroundAlpha(0.5f); 103 104 NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis(); 105 domainAxis.setLowerBound(-0.5); 106 107 NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); 108 109 rangeAxis.setLowerBound(-0.5); 111 112 ChartPanel chartPanel = new ChartPanel(chart); 113 chartPanel.setVerticalZoom(true); 114 chartPanel.setHorizontalZoom(true); 115 setContentPane(chartPanel); 116 } 117 118 129 134 public static void main(String [] args) { 135 BubblyBubblesDemo2 demo = new BubblyBubblesDemo2(TITLE); 136 demo.pack(); 137 demo.setSize(800, 600); 138 RefineryUtilities.centerFrameOnScreen(demo); 139 demo.setVisible(true); 140 141 Thread updater = demo.new UpdaterThread(); 142 updater.setDaemon(true); 143 updater.start(); 144 } 145 146 147 152 private NormalizedMatrixSeries createInitialSeries() { 153 NormalizedMatrixSeries series = new NormalizedMatrixSeries("Sample Grid 1", SIZE, SIZE); 154 155 for (int count = 0; count < SIZE * 3; count++) { 157 int i = (int) (Math.random() * SIZE); 158 int j = (int) (Math.random() * SIZE); 159 160 double mij = Math.random() * 1; 161 series.update(i, j, mij); 162 } 163 164 series.setScaleFactor(series.getItemCount()); 165 166 return series; 167 } 168 169 172 private class UpdaterThread extends Thread { 173 176 public void run() { 177 setPriority(MIN_PRIORITY); 179 while (true) { 180 int i = (int) (Math.random() * SIZE); 181 int j = (int) (Math.random() * SIZE); 182 183 double change = Math.random() * 3.0 - 1.0; 184 185 m_series.update(i, j, m_series.get(i, j) + change); 186 187 try { 188 sleep(50); 189 } 190 catch (InterruptedException e) { 191 } 192 } 193 } 194 } 195 } 196 | Popular Tags |