Example usage for org.jfree.chart.axis NumberTickUnitSource NumberTickUnitSource

List of usage examples for org.jfree.chart.axis NumberTickUnitSource NumberTickUnitSource

Introduction

In this page you can find the example usage for org.jfree.chart.axis NumberTickUnitSource NumberTickUnitSource.

Prototype

public NumberTickUnitSource(boolean integers) 

Source Link

Document

Creates a new instance.

Usage

From source file:org.sakaiproject.gradebookng.tool.panels.AssignmentStatisticsPanel.java

@Override
public void onInitialize() {
    super.onInitialize();

    final Long assignmentId = ((Model<Long>) getDefaultModel()).getObject();

    final Assignment assignment = this.businessService.getAssignment(assignmentId.longValue());

    AssignmentStatisticsPanel.this.window.setTitle(
            (new StringResourceModel("label.statistics.title", null, new Object[] { assignment.getName() })
                    .getString()));/*w  w w .  ja v  a 2  s.c  o  m*/

    final List<GbStudentGradeInfo> gradeInfo = this.businessService.buildGradeMatrix(Arrays.asList(assignment));

    final List<Double> allGrades = new ArrayList<>();

    for (int i = 0; i < gradeInfo.size(); i++) {
        final GbStudentGradeInfo studentGradeInfo = gradeInfo.get(i);

        final Map<Long, GbGradeInfo> studentGrades = studentGradeInfo.getGrades();
        final GbGradeInfo grade = studentGrades.get(assignmentId);

        if (grade == null || grade.getGrade() == null) {
            // this is not the grade you are looking for
        } else {
            allGrades.add(Double.valueOf(grade.getGrade()));
        }
    }

    Collections.sort(allGrades);

    final DefaultCategoryDataset data = new DefaultCategoryDataset();

    final Map<String, Integer> counts = new TreeMap<>();
    Integer extraCredits = 0;

    // Start off with a 0-50% range
    counts.put(String.format("%d-%d", 0, 50), 0);

    final int range = 10;
    for (int start = 50; start < 100; start = start + range) {
        final String key = String.format("%d-%d", start, start + range);
        counts.put(key, 0);
    }

    for (final Double grade : allGrades) {
        if (isExtraCredit(grade, assignment)) {
            extraCredits = extraCredits + 1;
            continue;
        }

        final double percentage;
        if (GradingType.PERCENTAGE.equals(this.gradingType)) {
            percentage = grade;
        } else {
            percentage = grade / assignment.getPoints() * 100;
        }

        final int total = Double.valueOf(Math.ceil(percentage) / range).intValue();

        int start = total * range;

        if (start == 100) {
            start = start - range;
        }

        String key = String.format("%d-%d", start, start + range);

        if (start < 50) {
            key = String.format("%d-%d", 0, 50);
        }

        counts.put(key, counts.get(key) + 1);
    }

    for (final String label : counts.keySet()) {
        data.addValue(counts.get(label), "count", label);
    }
    if (extraCredits > 0) {
        data.addValue(extraCredits, "count", getString("label.statistics.chart.extracredit"));
    }

    final JFreeChart chart = ChartFactory.createBarChart(null, // the chart title
            getString("label.statistics.chart.xaxis"), // the label for the category axis
            getString("label.statistics.chart.yaxis"), // the label for the value axis
            data, // the dataset for the chart
            PlotOrientation.VERTICAL, // the plot orientation
            false, // show legend
            true, // show tooltips
            false); // show urls

    chart.setBorderVisible(false);

    chart.setAntiAlias(false);

    final CategoryPlot categoryPlot = chart.getCategoryPlot();
    final BarRenderer br = (BarRenderer) categoryPlot.getRenderer();

    br.setItemMargin(0);
    br.setMinimumBarLength(0.05);
    br.setMaximumBarWidth(0.1);
    br.setSeriesPaint(0, new Color(51, 122, 183));
    br.setBarPainter(new StandardBarPainter());
    br.setShadowPaint(new Color(220, 220, 220));
    BarRenderer.setDefaultShadowsVisible(true);

    br.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator(getString("label.statistics.chart.tooltip"),
            NumberFormat.getInstance()));

    categoryPlot.setRenderer(br);

    // show only integers in the count axis
    categoryPlot.getRangeAxis().setStandardTickUnits(new NumberTickUnitSource(true));
    categoryPlot.setBackgroundPaint(Color.white);

    add(new JFreeChartImageWithToolTip("chart", Model.of(chart), "tooltip", 540, 300));

    add(new Label("graded", String.valueOf(allGrades.size())));

    if (allGrades.size() > 0) {
        add(new Label("average", constructAverageLabel(allGrades, assignment)));
        add(new Label("median", constructMedianLabel(allGrades, assignment)));
        add(new Label("lowest", constructLowestLabel(allGrades, assignment)));
        add(new Label("highest", constructHighestLabel(allGrades, assignment)));
        add(new Label("deviation", constructStandardDeviationLabel(allGrades)));
    } else {
        add(new Label("average", "-"));
        add(new Label("median", "-"));
        add(new Label("lowest", "-"));
        add(new Label("highest", "-"));
        add(new Label("deviation", "-"));
    }

    add(new GbAjaxLink("done") {
        private static final long serialVersionUID = 1L;

        @Override
        public void onClick(final AjaxRequestTarget target) {
            AssignmentStatisticsPanel.this.window.close(target);
        }
    });
}

From source file:org.sakaiproject.gradebookng.tool.panels.SettingsGradingSchemaPanel.java

/**
 * Build the data for the chart//w  ww. ja va 2  s  .com
 * 
 * @return
 */
private JFreeChart getChartData() {

    // just need the list
    final List<CourseGrade> courseGrades = this.courseGradeMap.values().stream().collect(Collectors.toList());

    // get current grading schema (from model so that it reflects current state)
    final List<GbGradingSchemaEntry> gradingSchemaEntries = this.model.getObject().getGradingSchemaEntries();

    final DefaultCategoryDataset data = new DefaultCategoryDataset();
    final Map<String, Integer> counts = new LinkedHashMap<>(); // must retain order so graph can be printed correctly

    // add all schema entries (these will be sorted according to {@link LetterGradeComparator})
    gradingSchemaEntries.forEach(e -> {
        counts.put(e.getGrade(), 0);
    });

    // now add the count of each course grade for those schema entries
    this.total = 0;
    for (final CourseGrade g : courseGrades) {

        // course grade may not be released so we have to skip it
        if (StringUtils.isBlank(g.getMappedGrade())) {
            continue;
        }

        counts.put(g.getMappedGrade(), counts.get(g.getMappedGrade()) + 1);
        this.total++;
    }

    // build the data
    final ListIterator<String> iter = new ArrayList<>(counts.keySet()).listIterator(0);
    while (iter.hasNext()) {
        final String c = iter.next();
        data.addValue(counts.get(c), "count", c);
    }

    final JFreeChart chart = ChartFactory.createBarChart(null, // the chart title
            getString("settingspage.gradingschema.chart.xaxis"), // the label for the category (x) axis
            getString("label.statistics.chart.yaxis"), // the label for the value (y) axis
            data, // the dataset for the chart
            PlotOrientation.HORIZONTAL, // the plot orientation
            false, // show legend
            true, // show tooltips
            false); // show urls

    chart.getCategoryPlot().setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
    chart.setBorderVisible(false);
    chart.setAntiAlias(false);

    final CategoryPlot plot = chart.getCategoryPlot();
    final BarRenderer br = (BarRenderer) plot.getRenderer();

    br.setItemMargin(0);
    br.setMinimumBarLength(0.05);
    br.setMaximumBarWidth(0.1);
    br.setSeriesPaint(0, new Color(51, 122, 183));
    br.setBarPainter(new StandardBarPainter());
    br.setShadowPaint(new Color(220, 220, 220));
    BarRenderer.setDefaultShadowsVisible(true);

    br.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator(getString("label.statistics.chart.tooltip"),
            NumberFormat.getInstance()));

    plot.setRenderer(br);

    // show only integers in the count axis
    plot.getRangeAxis().setStandardTickUnits(new NumberTickUnitSource(true));

    // make x-axis wide enough so we don't get ... suffix
    plot.getDomainAxis().setMaximumCategoryLabelWidthRatio(2.0f);

    plot.setBackgroundPaint(Color.white);

    chart.setTitle(getString("settingspage.gradingschema.chart.heading"));

    return chart;
}