com.decypher.threadsclient.ChartProvider.java Source code

Java tutorial

Introduction

Here is the source code for com.decypher.threadsclient.ChartProvider.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.decypher.threadsclient;

import com.decypher.clientclasses.ProjectClient;
import com.decypher.clientclasses.ProjectMemberClient;
import com.decypher.clientclasses.StepClient;
import com.decypher.clientclasses.TaskClient;
import com.decypher.threadsmodel.DateCUtil;
import com.decypher.threadsmodel.Project;
import com.decypher.threadsmodel.ProjectMember;
import com.decypher.threadsmodel.Step;
import com.decypher.threadsmodel.SubTask;
import com.decypher.threadsmodel.Task;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.SystemColor;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Map;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.renderer.category.StandardBarPainter;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.util.RelativeDateFormat;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 *
 * @author JHK
 */
public class ChartProvider {

    private static int maxHeight;

    private ChartProvider() {
    }

    public ChartProvider(int maxH) {
        maxHeight = maxH - 30; //For Scrollbar 20, and Compound border top padding 4
    }

    public static int getMaxWidth() {
        int w = 680;
        int h = 420;
        int maxWidth = (int) (w * maxHeight / h);
        return maxWidth;
    }

    public class ProjectCharts {

        private ProjectCharts() {
        }

        private Project proj;
        DefaultCategoryDataset accomp = new DefaultCategoryDataset();
        XYSeriesCollection deadlining = new XYSeriesCollection();

        public ProjectCharts(Project project) {
            proj = project;
            ArrayList<Step> steps = new ArrayList<>();
            steps.addAll(new ProjectClient(proj).getChildTasks());
            String accompLegend = "Weight Completed";

            XYSeries deadline = new XYSeries("Deadline");
            XYSeries completed = new XYSeries("Completed On");
            XYSeries started = new XYSeries("Started");

            int i = 1;
            for (Step step : steps) {
                //Adding to accomp
                float getCompletedPrcntg = (step.doneWeight * 100) / step.allWeight;
                String name = i + " " + step.name;
                if (step.doneWeight >= step.allWeight) {
                    accomp.addValue(1, accompLegend, name);
                } else if (step.doneWeight == 0) {
                    accomp.addValue(null, accompLegend, name);
                } else {
                    accomp.addValue(getCompletedPrcntg, accompLegend, name);
                }
                long comp = DateCUtil.getDate(proj.startDate).getTime();
                if (step.completedDate != null && !step.completedDate.isEmpty() && !step.completedDate.equals("")) {
                    comp = DateCUtil.getDate(step.completedDate).getTime();
                }
                //Adding to deadlining
                deadline.add(i, DateCUtil.getDate(step.dueDate).getTime());
                completed.add(i, comp);
                started.add(i, DateCUtil.getDate(step.startDate).getTime());
                i++;
            }

            deadlining.addSeries(started);
            deadlining.addSeries(deadline);
            deadlining.addSeries(completed);

        }

        public JPanelChart projectProductivityChart() {
            //Initial Requirement of legend to hour ratio, Previous requirement, Needed requirement
            String legend = "Speed";
            final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

            float initial = (proj.allWeight / proj.getDuration().miliseconds);
            float current = ((proj.doneWeight / proj.getOverTime().miliseconds)) / initial;
            float needed = (((proj.allWeight - proj.doneWeight) / proj.getLeftOverTime().miliseconds)) / initial;

            dataset.addValue(1, legend, "Initial");
            dataset.addValue(current, legend, "Current");
            dataset.addValue(needed, legend, "Needed");

            JFreeChart chart = ChartFactory.createBarChart("Project Productivity", "Productivity Requirement",
                    "Weight/Hours", dataset, PlotOrientation.VERTICAL, false, false, false);

            CategoryPlot plot = (CategoryPlot) chart.getPlot();

            ((BarRenderer) plot.getRenderer()).setBarPainter(new StandardBarPainter());
            BarRenderer renderer = (BarRenderer) chart.getCategoryPlot().getRenderer();
            renderer.setSeriesPaint(0, Color.BLUE);
            return addChart(chart, "Project Productivity Chart");
        }

        public JPanelChart projectAccomplishmentChart() {
            //Each child task the work that has been completed

            JFreeChart chart = ChartFactory.createLineChart("Work Completion", "Step", "Completed %", accomp,
                    PlotOrientation.VERTICAL, false, false, false);

            CategoryPlot plot = (CategoryPlot) chart.getPlot();
            plot.getRangeAxis().setUpperBound(20.0);

            ((LineAndShapeRenderer) plot.getRenderer()).setSeriesPaint(0, Color.BLUE);

            return addChart(chart, "Project Accomplishment Chart");
        }

        public JPanelChart projectProgressionChart() {
            //Deadline lines and the actual completed date lines

            JFreeChart chart = ChartFactory.createXYLineChart("Project Progression Chart", "Date Parameter", "Step",
                    deadlining, PlotOrientation.VERTICAL, true, true, false);

            XYPlot plot = chart.getXYPlot();
            XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
            renderer.setSeriesPaint(0, Color.BLACK);
            renderer.setSeriesPaint(1, Color.BLUE);
            renderer.setSeriesPaint(2, Color.GREEN);
            renderer.setSeriesStroke(0, new BasicStroke(1.0f));
            renderer.setSeriesStroke(1, new BasicStroke(1.0f));
            renderer.setSeriesStroke(2, new BasicStroke(1.0f));
            renderer.setDrawSeriesLineAsPath(true);
            plot.setRenderer(renderer);

            //New //
            ValueAxis valAxis = new DateAxis();
            RelativeDateFormat rdf = new RelativeDateFormat();
            rdf.setShowZeroDays(false);
            rdf.setSecondFormatter(new DecimalFormat("00"));
            plot.setRangeAxis(valAxis);

            NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
            domainAxis.setTickUnit(new NumberTickUnit(1));
            plot.setDomainAxis(domainAxis);
            return addChart(chart, "Project Progression Chart");
        }
    }

    public class StepCharts {

        private StepCharts() {
        }

        private Step step;
        DefaultCategoryDataset accomp = new DefaultCategoryDataset();
        XYSeriesCollection deadlining = new XYSeriesCollection();

        public StepCharts(Step stp) {
            this.step = stp;
            ArrayList<Task> tasks = new ArrayList<>();
            tasks.addAll(new StepClient(this.step).getChildTasks());
            String accompLegend = "Weight Completed";

            XYSeries deadline = new XYSeries("Deadline");
            XYSeries completed = new XYSeries("Completed On");
            XYSeries started = new XYSeries("Started");

            int i = 1;
            for (Task task : tasks) {
                //Adding to accomp
                float getCompletedPrcntg = (task.doneWeight * 100) / task.allWeight;
                String name = i + " " + task.name;
                if (task.doneWeight >= task.allWeight) {
                    accomp.addValue(1, accompLegend, name);
                } else if (task.doneWeight == 0) {
                    accomp.addValue(null, accompLegend, name);
                } else {
                    accomp.addValue(getCompletedPrcntg, accompLegend, name);
                }
                long comp = DateCUtil.getDate(this.step.startDate).getTime();
                if (task.completedDate != null && !task.completedDate.isEmpty() && !task.completedDate.equals("")) {
                    comp = DateCUtil.getDate(task.completedDate).getTime();
                }
                //Adding to deadlining
                deadline.add(i, DateCUtil.getDate(task.dueDate).getTime());
                completed.add(i, comp);
                started.add(i, DateCUtil.getDate(task.startDate).getTime());
                i++;
            }

            deadlining.addSeries(started);
            deadlining.addSeries(deadline);
            deadlining.addSeries(completed);

        }

        public JPanelChart stepProductivityChart() {
            //Initial Requirement of legend to hour ratio, Previous requirement, Needed requirement
            String legend = "Speed";
            final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

            float initial = (step.allWeight / step.getDuration().miliseconds);
            float current = ((step.doneWeight / step.getOverTime().miliseconds)) / initial;
            float needed = (((step.allWeight - step.doneWeight) / step.getLeftOverTime().miliseconds)) / initial;

            dataset.addValue(1, legend, "Initial");
            dataset.addValue(current, legend, "Current");
            dataset.addValue(needed, legend, "Needed");

            JFreeChart chart = ChartFactory.createBarChart("Step Productivity", "Productivity Requirement",
                    "Weight/Hours", dataset, PlotOrientation.VERTICAL, false, false, false);

            CategoryPlot plot = (CategoryPlot) chart.getPlot();

            ((BarRenderer) plot.getRenderer()).setBarPainter(new StandardBarPainter());
            BarRenderer renderer = (BarRenderer) chart.getCategoryPlot().getRenderer();
            renderer.setSeriesPaint(0, Color.BLUE);
            return addChart(chart, "Step Productivity Chart");
        }

        public JPanelChart stepAccomplishmentChart() {
            //Each child task the work that has been completed

            JFreeChart chart = ChartFactory.createLineChart("Work Completion", "Task", "Completed %", accomp,
                    PlotOrientation.VERTICAL, false, false, false);

            CategoryPlot plot = (CategoryPlot) chart.getPlot();
            plot.getRangeAxis().setUpperBound(20.0);

            ((LineAndShapeRenderer) plot.getRenderer()).setSeriesPaint(0, Color.BLUE);

            return addChart(chart, "Step Accomplishment Chart");
        }

        public JPanelChart stepProgressionChart() {
            //Deadline lines and the actual completed date lines

            JFreeChart chart = ChartFactory.createXYLineChart("Step Progression Chart", "Date Parameter", "Task",
                    deadlining, PlotOrientation.VERTICAL, true, true, false);

            XYPlot plot = chart.getXYPlot();
            XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
            renderer.setSeriesPaint(0, Color.BLACK);
            renderer.setSeriesPaint(1, Color.BLUE);
            renderer.setSeriesPaint(2, Color.GREEN);
            renderer.setSeriesStroke(0, new BasicStroke(1.0f));
            renderer.setSeriesStroke(1, new BasicStroke(1.0f));
            renderer.setSeriesStroke(2, new BasicStroke(1.0f));
            renderer.setDrawSeriesLineAsPath(true);
            plot.setRenderer(renderer);

            //New //
            ValueAxis valAxis = new DateAxis();
            RelativeDateFormat rdf = new RelativeDateFormat();
            rdf.setShowZeroDays(false);
            rdf.setSecondFormatter(new DecimalFormat("00"));
            plot.setRangeAxis(valAxis);

            NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
            domainAxis.setTickUnit(new NumberTickUnit(1));
            plot.setDomainAxis(domainAxis);
            return addChart(chart, "Step Progression Chart");
        }
    }

    public class TaskCharts {

        private TaskCharts() {
        }

        private Task task;
        XYSeriesCollection deadlining = new XYSeriesCollection();

        public TaskCharts(Task tsk) {
            this.task = tsk;
            ArrayList<SubTask> tasks = new ArrayList<>();
            tasks.addAll(new TaskClient(this.task).getChildTasks());
            XYSeries deadline = new XYSeries("Deadline");
            XYSeries completed = new XYSeries("Completed On");

            int i = 1;
            for (SubTask subtask : tasks) {
                //Adding to accomp
                subtask.startDate = task.startDate;

                long comp = DateCUtil.getDate(this.task.startDate).getTime();
                if (subtask.completedDate != null && !subtask.completedDate.isEmpty()
                        && !subtask.completedDate.equals("")) {
                    comp = DateCUtil.getDate(subtask.completedDate).getTime();
                }
                //Adding to deadlining
                deadline.add(i, DateCUtil.getDate(subtask.dueDate).getTime());
                completed.add(i, comp);
                i++;
            }

            deadlining.addSeries(deadline);
            deadlining.addSeries(completed);

        }

        public JPanelChart taskProductivityChart() {
            //Initial Requirement of legend to hour ratio, Previous requirement, Needed requirement
            String legend = "Speed";
            final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

            float initial = (task.allWeight / task.getDuration().miliseconds);
            float current = ((task.doneWeight / task.getOverTime().miliseconds)) / initial;
            float needed = (((task.allWeight - task.doneWeight) / task.getLeftOverTime().miliseconds)) / initial;

            dataset.addValue(1, legend, "Initial");
            dataset.addValue(current, legend, "Current");
            dataset.addValue(needed, legend, "Needed");

            JFreeChart chart = ChartFactory.createBarChart("Task Productivity", "Productivity Requirement",
                    "Weight/Hours", dataset, PlotOrientation.VERTICAL, false, false, false);

            CategoryPlot plot = (CategoryPlot) chart.getPlot();

            ((BarRenderer) plot.getRenderer()).setBarPainter(new StandardBarPainter());
            BarRenderer renderer = (BarRenderer) chart.getCategoryPlot().getRenderer();
            renderer.setSeriesPaint(0, Color.BLUE);
            return addChart(chart, "Task Productivity Chart");
        }

        public JPanelChart taskProgressionChart() {
            //Deadline lines and the actual completed date lines

            JFreeChart chart = ChartFactory.createXYLineChart("Task Progression Chart", "Date Parameter", "SubTask",
                    deadlining, PlotOrientation.VERTICAL, true, true, false);

            XYPlot plot = chart.getXYPlot();
            XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
            renderer.setSeriesPaint(0, Color.BLACK);
            renderer.setSeriesPaint(1, Color.BLUE);
            renderer.setSeriesStroke(0, new BasicStroke(1.0f));
            renderer.setSeriesStroke(1, new BasicStroke(1.0f));
            renderer.setDrawSeriesLineAsPath(true);
            plot.setRenderer(renderer);

            //New //
            ValueAxis valAxis = new DateAxis();
            RelativeDateFormat rdf = new RelativeDateFormat();
            rdf.setShowZeroDays(false);
            rdf.setSecondFormatter(new DecimalFormat("00"));
            plot.setRangeAxis(valAxis);

            NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
            domainAxis.setTickUnit(new NumberTickUnit(1));
            plot.setDomainAxis(domainAxis);
            return addChart(chart, "Task Progression Chart");
        }
    }

    /**
     * ******** SubTask chart *******
     */
    /*
     public JPanelChart subTaskProductivityChart() {
     //Initial Requirement of legend to hour ratio, Previous requirement, Needed requirement
     String legend = "Speed";
     final DefaultCategoryDataset dataset
     = new DefaultCategoryDataset();
     dataset.addValue(2, legend, "Initial");
     dataset.addValue(4, legend, "Current");
     dataset.addValue(2, legend, "Needed");
        
     JFreeChart chart = ChartFactory.createBarChart(
     "",
     "Productivity Requirement",
     "Weight/Hours",
     dataset,
     PlotOrientation.VERTICAL,
     false, false, false
     );
        
     CategoryPlot plot = (CategoryPlot) chart.getPlot();
        
     ((BarRenderer) plot.getRenderer()).setBarPainter(new StandardBarPainter());
     BarRenderer renderer = (BarRenderer) chart.getCategoryPlot().getRenderer();
     renderer.setSeriesPaint(0, Color.BLUE);
     return addChart(chart, "SubTask Productivity Chart");
     }
     */
    /**
     * ****** end of sub Tasks *********
     */
    /**
     * ******* Dashboard Charts **************
     */
    /**
     * ** Employees ******
     */
    public JPanelChart memberContributionChart() {
        //Contribution to all projects each day of week

        String legend = "Weight";
        final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        int i = 1;
        for (Float fl : new ProjectMemberClient(StoredVariables.getOwner()).getEachDayContribution()) {
            String day = String.valueOf(i);
            if (fl > 0f) {
                dataset.addValue(fl, legend, day);
            } else {
                dataset.addValue(null, legend, day);
            }
            i++;
        }

        JFreeChart chart = ChartFactory.createBarChart("Contribution", "Days From Today", "Weight Chart", dataset,
                PlotOrientation.VERTICAL, false, false, false);

        CategoryPlot plot = (CategoryPlot) chart.getPlot();

        ((BarRenderer) plot.getRenderer()).setBarPainter(new StandardBarPainter());
        BarRenderer renderer = (BarRenderer) chart.getCategoryPlot().getRenderer();
        renderer.setSeriesPaint(0, Color.BLUE);
        return addChart(chart, "Member Contribution Chart");
    }

    private JPanelChart memberRequirementChart(ArrayList<Task> tasks) {
        //productivity requirement;

        String ini = "Initial";
        String curr = "Current";
        String need = "Needed";

        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        for (Task task : tasks) {
            float initial = (task.allWeight / task.getDuration().miliseconds);
            float current = ((task.doneWeight / task.getOverTime().miliseconds)) / initial;
            float needed = (((task.allWeight - task.doneWeight) / task.getLeftOverTime().miliseconds)) / initial;

            String tsk = String.valueOf(task.itemId);

            dataset.addValue(1, ini, tsk);
            dataset.addValue(current, curr, tsk);
            dataset.addValue(needed, need, tsk);

        }
        JFreeChart chart = ChartFactory.createBarChart("Task Productivity", "Productivity Requirement",
                "Weight/Hours", dataset, PlotOrientation.VERTICAL, false, false, false);

        CategoryPlot plot = (CategoryPlot) chart.getPlot();

        ((BarRenderer) plot.getRenderer()).setBarPainter(new StandardBarPainter());
        BarRenderer renderer = (BarRenderer) chart.getCategoryPlot().getRenderer();
        renderer.setSeriesPaint(0, Color.BLUE);
        return addChart(chart, "Task Productivity Chart");

    }

    /**
     * ** Manager ******
     */
    public JPanelChart employeeContributionChart() {
        //Employee Contribution to all projects
        String assigned = "Assigned Weight";
        String completed = "Completed Weight";
        final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        for (int pId : StoredVariables.projIds) {
            Project proj = StoredVariables.involvedProjs.get(pId);

            for (Map.Entry<Integer, String> pair : proj.members.entrySet()) {
                int memberId = pair.getKey();
                if (memberId != StoredVariables.getOwner().memberId) {
                    ProjectMember memb = new ProjectMember();
                    memb.memberId = memberId;

                    ProjectMemberClient cl = new ProjectMemberClient(memb);
                    float assignedVal = cl.getAssignedAmount();
                    float all = cl.getAllAssignedAmount();

                    dataset.addValue(assignedVal, assigned, pair.getValue());
                    dataset.addValue(all, completed, pair.getValue());
                }
            }

        }

        JFreeChart chart = ChartFactory.createBarChart("", "Member", "All Time Contribution", dataset,
                PlotOrientation.VERTICAL, true, false, false);

        CategoryPlot plot = (CategoryPlot) chart.getPlot();

        ((BarRenderer) plot.getRenderer()).setBarPainter(new StandardBarPainter());
        BarRenderer renderer = (BarRenderer) chart.getCategoryPlot().getRenderer();
        renderer.setSeriesPaint(0, Color.BLUE);
        renderer.setSeriesPaint(1, Color.BLACK);
        return addChart(chart, "Member Contribution Chart");
    }

    public JPanelChart overallCompletionChart() {
        //Completed amount of each project
        final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        for (int pId : StoredVariables.projIds) {
            Project p = StoredVariables.involvedProjs.get(pId);
            int precentage = (int) ((p.doneWeight * 100) / p.allWeight);
            dataset.addValue(precentage, "Completed Weight Precentage", p.projectKey);
        }

        JFreeChart chart = ChartFactory.createBarChart("", "Project", "Completed", dataset,
                PlotOrientation.VERTICAL, true, false, false);

        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.getRangeAxis().setUpperBound(100.0);

        ((BarRenderer) plot.getRenderer()).setBarPainter(new StandardBarPainter());
        BarRenderer renderer = (BarRenderer) chart.getCategoryPlot().getRenderer();
        renderer.setSeriesPaint(0, Color.BLUE);
        return addChart(chart, "Overall Completion Chart");
    }

    /**
     * ****** End of Dashboard Charts **********
     */
    public static JPanelChart addChart(JFreeChart chart, String title) {
        chart.getPlot().setBackgroundPaint(Color.LIGHT_GRAY);
        chart.setBackgroundPaint(SystemColor.control);
        ChartPanel cpnl = new ChartPanel(chart);
        JPanelChart pnl = new JPanelChart(chart);
        pnl.setLayout(new BorderLayout());
        pnl.setPreferredSize(new Dimension(getMaxWidth(), maxHeight));
        pnl.add(cpnl, BorderLayout.CENTER);
        pnl.setName(title);
        return pnl;
    }

}