- final int ROW_HEIGHT = 30;
- final int COL_WIDTH = 200;
- final int COLOR_BOX_SIZE = 20;
- final int TEXT_OFFSET_X = 5;
- final int TEXT_OFFSET_Y = -10;
- final int LINE_OFFSET = 16;
- final int COLOR_COLUMN_OFFSET = 150;
- String[] getSectors() {
- return new String[] {
- "Health",
- "Education",
- "Public order and safety",
- "Social security and welfare",
- "Housing and community amenities",
- "Transport and communications",
- "Recreation and culture",
- "General public services",
- "Agriculture, forestry, fishing and hunting",
- "Fuel and energy",
- "Mining and mineral resources other than fuels",
- "Other economic affairs",
- "Other purposes",
- "Total"
- };
- }
- int[] getBudgets() {
- return new int[] {
- 5618, 4364, 1752, 1437, 1275, 1219, 421,
- 408, 213, 74, 73, 390, 766, sumArray(new int[]{5618, 4364, 1752, 1437, 1275, 1219, 421, 408, 213, 74, 73, 390, 766})
- };
- }
- color[] getColors() {
- return new color[] {
- color(255, 0, 0),
- color(0, 255, 0),
- color(0, 0, 255),
- color(255, 255, 0),
- color(255, 0, 255),
- color(0, 255, 255),
- color(128, 0, 0),
- color(128, 128, 0),
- color(0, 128, 0),
- color(128, 0, 128),
- color(0, 128, 128),
- color(0, 0, 128),
- color(128, 128, 128)
- };
- }
- int sumArray(int[] values) {
- int sum = 0;
- for (int value : values) {
- sum += value;
- }
- return sum;
- }
- void setup() {
- size(800, 800);
- textSize(10);
- }
- void drawPieChart(int[] budgets, color[] colors, float centerX, float centerY, float diameter) {
- int totalBudget = sumArray(budgets);
- float lastAngle = 0;
- for (int i = 0; i < budgets.length - 1; i++) {
- float proporti / totalBudget;
- float angle = proportion * TWO_PI * 2;
- fill(colors[i]);
- arc(centerX, centerY, diameter, diameter, lastAngle, lastAngle + angle);
- lastAngle += angle;
- }
- save("Problem_Data_visualisation.png");
- }
- void draw() {
- background(255);
- drawTable();
- drawPieChart(getBudgets(), getColors(), width/1.5 , height / 4, 300);
- }
- void drawTable() {
- String[] sectors = getSectors();
- int[] budgets = getBudgets();
- color[] colors = getColors();
- int x = 10;
- int y = 40;
- drawColumnTitles(x, y);
- for (int i = 0; i < sectors.length; i++) {
- int currentY = y + i * ROW_HEIGHT;
- drawText(sectors[i], x + TEXT_OFFSET_X, currentY);
- drawText(str(budgets[i]), x + COL_WIDTH + TEXT_OFFSET_X, currentY);
- if (i < sectors.length-1 ) {
- drawColorBox(colors[i], x + 2 * COL_WIDTH - COLOR_COLUMN_OFFSET + TEXT_OFFSET_X, currentY + TEXT_OFFSET_Y);
- }
- drawHorizontalLine(x, currentY + ROW_HEIGHT / 2, 3 * COL_WIDTH - COLOR_COLUMN_OFFSET);
- }
- drawVerticalLines(x, y, sectors.length);
- }
- void drawText(String text, int x, int y) {
- fill(0);
- text(text, x, y);
- }
- void drawColorBox(color c, int x, int y) {
- fill(c);
- rect(x, y, COLOR_BOX_SIZE, COLOR_BOX_SIZE);
- }
- void drawHorizontalLine(int x, int y, int length) {
- line(x, y, x + length-150, y);
- }
- void drawVerticalLines(int x, int y, int numRows) {
- int endY = y + numRows * ROW_HEIGHT - LINE_OFFSET;
- line(x, y - LINE_OFFSET - ROW_HEIGHT, x, endY);
- line(x + COL_WIDTH, y - LINE_OFFSET - ROW_HEIGHT, x + COL_WIDTH, endY);
- line(x + 2 * COL_WIDTH - COLOR_COLUMN_OFFSET, y - LINE_OFFSET - ROW_HEIGHT, x + 2 * COL_WIDTH - COLOR_COLUMN_OFFSET, endY);
- line(x + 3 * COL_WIDTH - COLOR_COLUMN_OFFSET * 2, y - LINE_OFFSET - ROW_HEIGHT, x + 3 * COL_WIDTH - COLOR_COLUMN_OFFSET * 2, endY);
- }
- void drawColumnTitles(int x, int y) {
- drawText("Sector", x + 1, y - ROW_HEIGHT);
- drawText("Budget", x + COL_WIDTH + 1, y - ROW_HEIGHT);
- drawText("Color", x + 2 * COL_WIDTH + 1 - COLOR_COLUMN_OFFSET, y - ROW_HEIGHT);
- }