All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.tundeadetunji.Charts Maven / Gradle / Ivy

There is a newer version: 2.2.5
Show newest version
package io.github.tundeadetunji;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Methods to construct charts (HTML/CSS/JS snippet) dynamically. Pass in appropriate parameters,
 * or combine with MySQL.count() for example, to generate a pie chart, then
 * put the resulting string in HTML div at runtime.
 * 

* The methods assume you are using Shoppy's template described in General.previewHtml(). * For it to work, the CSS and JS and other related assets must be in place. You can pass * the result of desired method as content (parameter) to General.previewHTML(), for instance. */ public final class Charts { public static String pie(List values) { return Chart_Pie_String(values, null, 470, 300); } public static String pie(Map values) { return Chart_Pie_String(General.mapToList(values, General.SideToReturn.Right), null, 470, 300); } public static String pie(List values, List colors) { return Chart_Pie_String(values, colors, 470, 300); } public static String pie(Map values, List colors) { return Chart_Pie_String(General.mapToList(values, General.SideToReturn.Right), colors, 470, 300); } public static String pie(List values, List colors, int width, int height) { return Chart_Pie_String(values, colors, width, height); } public static String pie(Map values, List colors, int width, int height) { return Chart_Pie_String(General.mapToList(values, General.SideToReturn.Right), colors, width, height); } public static String pie(List values, int width, int height) { return Chart_Pie_String(values, null, width, height); } public static String pie(Map values, int width, int height) { return Chart_Pie_String(General.mapToList(values, General.SideToReturn.Right), null, width, height); } public static String doughnut(List values) { return Chart_Doughnut_String(values, null, 470, 300); } public static String doughnut(Map values) { return Chart_Doughnut_String(General.mapToList(values, General.SideToReturn.Right), null, 470, 300); } public static String doughnut(List values, List colors) { return Chart_Doughnut_String(values, colors, 470, 300); } public static String doughnut(Map values, List colors) { return Chart_Doughnut_String(General.mapToList(values, General.SideToReturn.Right), colors, 470, 300); } public static String doughnut(List values, List colors, int width, int height) { return Chart_Doughnut_String(values, colors, width, height); } public static String doughnut(Map values, List colors, int width, int height) { return Chart_Doughnut_String(General.mapToList(values, General.SideToReturn.Right), colors, width, height); } public static String doughnut(List values, int width, int height) { return Chart_Doughnut_String(values, null, width, height); } public static String doughnut(Map values, int width, int height) { return Chart_Doughnut_String(General.mapToList(values, General.SideToReturn.Right), null, width, height); } public static String line(List labels, List values) { return Chart_Line_String(labels, values, 400, 300); } public static String line(Map labelsValues) { return Chart_Line_String(General.mapToList(labelsValues, General.SideToReturn.Left), General.mapToList(labelsValues, General.SideToReturn.Right), 400, 300); } public static String line(List labels, List values, int width, int height) { return Chart_Line_String(labels, values, width, height); } public static String line(Map labelsValues, int width, int height) { return Chart_Line_String(General.mapToList(labelsValues, General.SideToReturn.Left), General.mapToList(labelsValues, General.SideToReturn.Right), width, height); } public static String bar(List labels, List values) { return BarChart_String(labels, values, 400, 300); } public static String bar(Map labelsValues) { return BarChart_String(General.mapToList(labelsValues, General.SideToReturn.Left) , General.mapToList(labelsValues, General.SideToReturn.Right), 400, 300); } public static String bar(List labels, List values, int width, int height) { return BarChart_String(labels, values, width, height); } public static String bar(Map labelsValues, int width, int height) { return BarChart_String(General.mapToList(labelsValues, General.SideToReturn.Left) , General.mapToList(labelsValues, General.SideToReturn.Right), width, height); } private static String BarChart_String(List labels, List values, int width, int height) { String id = General.newGUID(General.IDPattern.Short); return new StringBuilder().append("" + "\r\n" + " ").toString(); } private static String bar_labels_string(List labels) { StringBuilder result = new StringBuilder(); for (int i = 0; i < labels.size(); i++) { result.append("\"" + labels.get(i) + "\"" + ((i < labels.size() - 1) ? ", " : "")); } return result.toString(); } private static String bar_dataset_string(List dataset) { StringBuilder result = new StringBuilder(); for (int i = 0; i < dataset.size(); i++) { result.append(dataset.get(i)).append(((i < dataset.size() - 1) ? ", " : "")); } return result.toString(); } private static String Chart_Line_String(List labels, List values, int width, int height) { var id = io.github.tundeadetunji.General.newGUID(General.IDPattern.Short); return new StringBuilder().append("" + "\r\n" + " ").toString(); } private static String Chart_Line_Variable(List labels, List values) { StringBuilder l_string = new StringBuilder().append("labels : ["); for (var i = 0; i < labels.size(); i++) { l_string.append("\"" + labels.get(i) + "\"") .append(i != labels.size() - 1 ? "," : ""); } String fc = randomColor(new ArrayList()); String c = randomColor(new ArrayList()); l_string.append("],"); StringBuilder v_string = new StringBuilder().append("datasets: [" + "\r\n" + " {" + "\r\n" + " fillColor: \"" + fc.toString() + "\"," + "\r\n" + " strokeColor: \"" + c.toString() + "\"," + "\r\n" + " pointColor: \"" + c.toString() + "\"," + "\r\n" + " pointStrokeColor: \"#fff\"," + "\r\n" + " data: ["); for (var j = 0; j < values.size(); j++) { v_string.append(values.get(j)) .append(j != values.size() - 1 ? "," : ""); } v_string.append("]}]};"); return l_string.append(v_string).toString(); } private static String Chart_Pie_String(List values, List colors, int width, int height) { String id = io.github.tundeadetunji.General.newGUID(General.IDPattern.Short); return new StringBuilder().append("" + "\r\n" + " ") .toString(); } private static String Chart_Pie_Variable(List values, List colorsList) { StringBuilder result = new StringBuilder(); for (int i = 0; i < values.size(); i++) { String colors = colorsList != null ? colorsList.get(i) : randomColor(new ArrayList()); result.append("{" + "\r\n" + " value: " + values.get(i) + "," + "\r\n" + " color: \"" + colors + "\"" + "\r\n" + " }").append(i < values.size() - 1 ? "," : ""); } return result.toString(); } private static String Chart_Doughnut_String(List values, List colors, int width, int height) { String id = io.github.tundeadetunji.General.newGUID(General.IDPattern.Short); return new StringBuilder().append("" + "\r\n" + " ").toString(); } private static String Chart_Doughnut_Variable(List values, List colorsList) { StringBuilder result = new StringBuilder(); for (int i = 0; i < values.size(); i++) { String colors = colorsList != null ? colorsList.get(i) : randomColor(new ArrayList<>()); result.append("{" + "\r\n" + " value: " + values.get(i) + "," + "\r\n" + " color: \"" + colors + "\"" + "\r\n" + " }").append(i < values.size() - 1 ? "," : ""); } return result.toString(); } private static String randomColor(List integers) { return "rgb(" + randomList(0, 256, integers) + ", " + randomList(0, 256, integers) + ", " + randomList(0, 256, integers) + ")"; } private static int randomList(int min, int max, List already) { if (already.size() >= max) already.clear(); int random = (int) (Math.random() * (max - min + 1) + min); while (already.size() > 0 && already.contains(random)) { random = (int) Math.random(); } already.add(random); return random; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy