Skip to content

Commit

Permalink
chart
Browse files Browse the repository at this point in the history
  • Loading branch information
teachingprogramming committed Jul 10, 2018
1 parent 6eef39f commit 045c555
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,7 @@ dependencies {
compile('com.h2database:h2')
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.boot:spring-boot-starter-mail')

// https://mvnrepository.com/artifact/org.jfree/jfreechart
compile group: 'org.jfree', name: 'jfreechart', version: '1.5.0'
}
65 changes: 65 additions & 0 deletions src/main/java/d00000/webapp/ChartController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package d00000.webapp;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.data.category.DefaultCategoryDataset;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;

/**
* チャート
*/
@Controller
@RequestMapping("/chart")
public class ChartController {

/** 入力(フォーム) */
@GetMapping("input")
public String input() {
return "chart/input";
}

/** 処理(フォームから受け取り表示) */
@PostMapping("process")
public String process(@RequestParam("data1") String data1, @RequestParam("data2") String data2, ModelMap modelMap) {
// データセットを作成
DefaultCategoryDataset dataset = new DefaultCategoryDataset();

// data1をデータセットに追加
String[] valueArray1 = data1.split("\n");
for (int i=0; i<valueArray1.length; i++) {
double value = Double.valueOf(valueArray1[i]);
dataset.addValue(value, "data1", String.valueOf(i));
}

// data2をデータセットに追加
String[] valueArray2 = data2.split("\n");
for (int i=0; i<valueArray2.length; i++) {
double value = Double.valueOf(valueArray2[i]);
dataset.addValue(value, "data2", String.valueOf(i));
}

// チャートを作成
JFreeChart chart = ChartFactory.createLineChart("タイトル", "カテゴリー", "値", dataset);

try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); // 画像の出力先
ChartUtils.writeChartAsPNG(byteArrayOutputStream, chart, 600, 400); // チャートをPNG画像として出力
String base64string = Base64.getEncoder().encodeToString(byteArrayOutputStream.toByteArray()); // 画像をBase64でエンコード
String dataUri = "data:image/png;base64," + base64string; // data URIの文字列を作成
modelMap.addAttribute("dataUri", dataUri);
} catch (IOException e) {
e.printStackTrace();
}
return "chart/process";
}
}
24 changes: 24 additions & 0 deletions src/main/resources/templates/chart/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>チャート生成サンプル</title>
</head>
<body>

<h1>チャート生成サンプル</h1>

<h2>入力(フォーム)</h2>

改行区切りで数値を入力

<form th:action="@{/chart/process}" method="post">
data1:<br/>
<textarea name="data1" rows="10"></textarea><br/>
data2:<br/>
<textarea name="data2" rows="10"></textarea><br/>
<button type="submit">送信</button>
</form>

</body>
</html>
16 changes: 16 additions & 0 deletions src/main/resources/templates/chart/process.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>チャート生成サンプル</title>
</head>
<body>

<h1>チャート生成サンプル</h1>

<h2>処理(フォームから受け取り表示)</h2>

<img th:src="${dataUri}">

</body>
</html>

0 comments on commit 045c555

Please sign in to comment.