#18 -- fix percents

This commit is contained in:
Anton Romanov 2021-04-05 12:31:36 +04:00
parent eb59d3684d
commit fa3d285d83
2 changed files with 20 additions and 40 deletions

View File

@ -1,3 +1,8 @@
/*
* Copyright (C) 2021 Anton Romanov - All Rights Reserved
* You may use, distribute and modify this code, please write to: romanov73@gmail.com.
*/
package ru.ulstu.extractor.controller; package ru.ulstu.extractor.controller;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
@ -24,28 +29,11 @@ public class StatisticController {
"Europe", "North America", "Sourth America" "Europe", "North America", "Sourth America"
}; };
model.addAttribute("continents", continents); model.addAttribute("continents", continents);
model.addAttribute("array", new int[]{1, 2, 3}); List<Object[]> authorCommits = commitRepository.getCommitAuthorStatistic().stream()
List<String[]> listArr = commitRepository.getCommitAuthorStatistic().stream() .map(stat -> new Object[]{stat.getAuthor(), stat.getCountCommit()})
.map(stat -> new String[]{stat.getAuthor(), stat.getCountCommit().toString()})
.collect(Collectors.toList()); .collect(Collectors.toList());
String[][] commitAuthorData = listArr.toArray(String[][]::new); model.addAttribute("commitAuthorData", authorCommits);
model.addAttribute("commitAuthorData", findPercent(commitAuthorData));
return "statistic"; return "statistic";
} }
public String[][] findPercent(String[][] commitAuthorData) {
long sum = 0;
for (int i = 0; i < commitAuthorData.length; i++) {
sum = sum + Integer.parseInt(commitAuthorData[i][1]);
}
System.out.println(sum);
long percent = 0;
for (int i = 0; i < commitAuthorData.length; i++) {
percent = Integer.parseInt(commitAuthorData[i][1]) * 100 / sum;
commitAuthorData[i][1] = "";
}
return commitAuthorData;
}
} }

View File

@ -1,3 +1,8 @@
<!--
~ Copyright (C) 2021 Anton Romanov - All Rights Reserved
~ You may use, distribute and modify this code, please write to: romanov73@gmail.com.
-->
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd"> <!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html <html
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.w3.org/1999/xhtml" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.w3.org/1999/xhtml"
@ -8,33 +13,20 @@
</head> </head>
<div class="container" layout:fragment="content"> <div class="container" layout:fragment="content">
<script src="https://www.google.com/jsapi"></script> <script src="https://www.google.com/jsapi"></script>
<script type="text/javascript" >
var items = [];
/*[# th:each="n : ${items}"]*/
items.push("[(${n})]");
/*[/]*/
console.log(items);
</script>
<script th:inline="javascript">
var username = [[${array}]];
console.log(username);
</script>
<script th:inline="javascript"> <script th:inline="javascript">
google.load("visualization", "1", {packages: ["corechart"]}); google.load("visualization", "1", {packages: ["corechart"]});
google.setOnLoadCallback(drawChart); google.setOnLoadCallback(drawChart);
function drawChart() { function drawChart() {
var arr = [[${commitAuthorData}]]; var onlyCommitData = [[${commitAuthorData}]];
var newArray = [ var commitAuthorData = [
['Author','Commit'] ['Автор','% коммитов']
].concat(arr); ].concat(onlyCommitData);
var data = google.visualization.arrayToDataTable(newArray); var data = google.visualization.arrayToDataTable(commitAuthorData);
var options = { var options = {
title: 'Состав воздуха', title: '% коммитов авторов',
is3D: true, is3D: true,
pieResidueSliceLabel: 'Остальное' pieResidueSliceLabel: 'Остальные'
}; };
var chart = new google.visualization.PieChart(document.getElementById('air')); var chart = new google.visualization.PieChart(document.getElementById('air'));
chart.draw(data, options); chart.draw(data, options);