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 BubblyBubblesDemo 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 BubblyBubblesDemo(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, "X", "Y", dataset, 90 PlotOrientation.VERTICAL, 91 true, 92 true, false); 93 94 chart.setBackgroundPaint(new GradientPaint (0, 0, Color.white, 0, 95 1000, Color.blue)); 96 97 XYPlot plot = chart.getXYPlot(); 98 plot.setForegroundAlpha(0.5f); 99 100 NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis(); 101 domainAxis.setLowerBound(-0.5); 102 103 NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); 104 105 rangeAxis.setLowerBound(-0.5); 107 108 ChartPanel chartPanel = new ChartPanel(chart); 109 chartPanel.setVerticalZoom(true); 110 chartPanel.setHorizontalZoom(true); 111 setContentPane(chartPanel); 112 } 113 114 119 public static void main(String [] args) { 120 BubblyBubblesDemo demo = new BubblyBubblesDemo(TITLE); 121 demo.pack(); 122 demo.setSize(800, 600); 123 RefineryUtilities.centerFrameOnScreen(demo); 124 demo.setVisible(true); 125 126 Thread updater = demo.new UpdaterThread(); 127 updater.setDaemon(true); 128 updater.start(); 129 } 130 131 142 147 private NormalizedMatrixSeries createInitialSeries() { 148 NormalizedMatrixSeries series = 149 new NormalizedMatrixSeries("Sample Grid 1", SIZE, SIZE); 150 151 for (int count = 0; count < SIZE; count++) { 153 int i = (int) (Math.random() * SIZE); 154 int j = (int) (Math.random() * SIZE); 155 156 int mij = (int) (Math.random() * SIZE); 157 series.update(i, j, mij); 158 } 159 160 series.setScaleFactor(series.getItemCount()); 161 162 return series; 163 } 164 165 168 private class UpdaterThread extends Thread { 169 172 public void run() { 173 setPriority(MIN_PRIORITY); 175 while (true) { 176 int i = (int) (Math.random() * SIZE); 177 int j = (int) (Math.random() * SIZE); 178 179 m_series.update(i, j, m_series.get(i, j) + 1); 180 181 try { 182 sleep(50); 183 } 184 catch (InterruptedException e) { 185 } 186 } 187 } 188 } 189 } 190 191 192 193 | Popular Tags |