Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
chart
- Loading branch information
1 parent
6eef39f
commit 045c555
Showing
4 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
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"; | ||
} | ||
} |
This file contains 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
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> |
This file contains 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
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> |