Created
February 26, 2012 19:45
-
-
Save kishida/1918612 to your computer and use it in GitHub Desktop.
取得したAmazonランクを表示する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Amazonランキングのグラフを表示する | |
* JFreeChartが必要。コンパイルはJavaSE7で。 | |
*/ | |
package amazonrank; | |
import java.awt.Font; | |
import java.awt.image.BufferedImage; | |
import java.io.*; | |
import java.net.URL; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.*; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import javax.imageio.ImageIO; | |
import org.jfree.chart.ChartFactory; | |
import org.jfree.chart.ChartPanel; | |
import org.jfree.chart.JFreeChart; | |
import org.jfree.chart.plot.XYPlot; | |
import org.jfree.data.general.SeriesException; | |
import org.jfree.data.time.Hour; | |
import org.jfree.data.time.TimeSeries; | |
import org.jfree.data.time.TimeSeriesCollection; | |
/** | |
* Amazonランキングのグラフを表示する | |
* @author naoki | |
*/ | |
public class AmazonRankFrame extends javax.swing.JFrame { | |
String dataFile = "amazon.txt"; | |
String pngFile = "amazon.png"; | |
/** | |
* Creates new form AmazonRankFrame | |
*/ | |
public AmazonRankFrame() { | |
try { | |
initComponents(); | |
List<String> lines = Files.readAllLines( | |
Paths.get(dataFile), | |
Charset.defaultCharset()); | |
Map<String, TimeSeries> tss = new HashMap<>(); | |
for(String line : lines){ | |
String[] items = line.split(" ", 5); | |
Date d = parseDate(items[0], items[1]); | |
String url = items[2]; | |
String rankStr = items[3]; | |
int rank = Integer.parseInt(rankStr.replace(",", "")); | |
//系列に追加 | |
TimeSeries t = tss.get(url); | |
if(t == null){ | |
String title = items[4]; | |
t = new TimeSeries(title); | |
tss.put(url, t); | |
} | |
try{ | |
t.add(new Hour(d), rank); | |
}catch(SeriesException ex){ | |
System.out.println("error:" + line); | |
} | |
} | |
//系列をグラフに追加 | |
TimeSeriesCollection data = new TimeSeriesCollection(); | |
for(TimeSeries ts : tss.values()){ | |
data.addSeries(ts); | |
} | |
//グラフの準備 | |
JFreeChart chart = ChartFactory.createTimeSeriesChart( | |
"Amazon順位", "日時", "順位", data, true, true, true); | |
XYPlot xyPlot = chart.getXYPlot(); | |
Font f = new Font(Font.SANS_SERIF, Font.PLAIN, 14); | |
xyPlot.getDomainAxis().setLabelFont(f); | |
xyPlot.getRangeAxis().setLabelFont(f); | |
//xyPlot.getRenderer().setSeriesVisible(0, false); | |
chart.getLegend().setItemFont(f.deriveFont((float)10)); | |
chart.getTitle().setFont(f.deriveFont(Font.BOLD, 16)); | |
//画像ファイルを作成 | |
BufferedImage img = chart.createBufferedImage(450, 400); | |
ImageIO.write(img, "png", new File(pngFile)); | |
//画面に配置 | |
jPanel1.add(new ChartPanel(chart)); | |
} catch (ParseException | IOException ex) { | |
Logger.getLogger(AmazonRankFrame.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
public static Date parseDate(String day, String time) throws ParseException{ | |
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MMM/dd HH:mm:ss", Locale.US); | |
sdf.setTimeZone(TimeZone.getTimeZone("PDT")); | |
Date d = sdf.parse(day + " " + time); | |
return d; | |
} | |
/** | |
* This method is called from within the constructor to initialize the form. | |
* WARNING: Do NOT modify this code. The content of this method is always | |
* regenerated by the Form Editor. | |
*/ | |
@SuppressWarnings("unchecked") | |
// <editor-fold defaultstate="collapsed" desc="Generated Code"> | |
private void initComponents() { | |
jPanel1 = new javax.swing.JPanel(); | |
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); | |
jPanel1.setLayout(new java.awt.BorderLayout()); | |
getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER); | |
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); | |
setBounds((screenSize.width-402)/2, (screenSize.height-351)/2, 402, 351); | |
}// </editor-fold> | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String args[]) { | |
/* | |
* Create and display the form | |
*/ | |
java.awt.EventQueue.invokeLater(new Runnable() { | |
public void run() { | |
new AmazonRankFrame().setVisible(true); | |
} | |
}); | |
} | |
// Variables declaration - do not modify | |
private javax.swing.JPanel jPanel1; | |
// End of variables declaration | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment