Example usage for org.jfree.chart.plot IntervalMarker IntervalMarker

List of usage examples for org.jfree.chart.plot IntervalMarker IntervalMarker

Introduction

In this page you can find the example usage for org.jfree.chart.plot IntervalMarker IntervalMarker.

Prototype

public IntervalMarker(double start, double end, Paint paint) 

Source Link

Document

Creates a new interval marker with the specified range and fill paint.

Usage

From source file:it.eng.spagobi.engines.kpi.bo.charttypes.dialcharts.BulletGraph.java

public JFreeChart createChart() {

    logger.debug("IN");
    Number value = null;/*from  ww  w .  j  a v  a2s  .co m*/

    if (dataset == null) {
        logger.debug("The dataset to be represented is null");
        value = new Double(0);
    } else {
        value = dataset.getValue();
    }

    DefaultCategoryDataset datasetC = new DefaultCategoryDataset();
    datasetC.addValue(value, "", "");

    // customize a bar chart 
    JFreeChart chart = ChartFactory.createBarChart(null, null, null, datasetC, PlotOrientation.HORIZONTAL,
            false, false, false);
    chart.setBorderVisible(false);

    CategoryPlot plot = chart.getCategoryPlot();
    plot.setOutlineVisible(true);
    plot.setOutlinePaint(Color.BLACK);

    plot.setInsets(new RectangleInsets(0.0, 0.0, 0.0, 0.0));
    plot.setBackgroundPaint(null);
    plot.setDomainGridlinesVisible(false);
    plot.setRangeGridlinesVisible(false);
    plot.setRangeCrosshairVisible(false);
    plot.setAnchorValue(value.doubleValue());

    // add the target marker 
    if (target != null) {
        ValueMarker marker = new ValueMarker(target.doubleValue(), Color.BLACK, new BasicStroke(2.0f));
        plot.addRangeMarker(marker, Layer.FOREGROUND);
    }

    //sets different marks
    for (Iterator iterator = intervals.iterator(); iterator.hasNext();) {
        KpiInterval interval = (KpiInterval) iterator.next();
        // add the marks 
        IntervalMarker marker = new IntervalMarker(interval.getMin(), interval.getMax(), interval.getColor());
        plot.addRangeMarker(marker, Layer.BACKGROUND);
        logger.debug("Added new interval to the plot");
    }

    // customize axes 
    CategoryAxis domainAxis = plot.getDomainAxis();
    domainAxis.setVisible(false);

    NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setVisible(show_axis);
    rangeAxis.setLabelFont(new Font("Arial", Font.PLAIN, 4));
    // calculate the upper limit 
    //double upperBound = target * upperFactor; 
    rangeAxis.setRange(new Range(lower, upper));
    plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);

    // customize renderer 
    BarRenderer renderer = (BarRenderer) plot.getRenderer();
    renderer.setMaximumBarWidth(0.18);
    renderer.setSeriesPaint(0, Color.BLACK);
    /*BasicStroke d = new BasicStroke(3f,BasicStroke.CAP_ROUND ,BasicStroke.JOIN_ROUND);
    renderer.setSeriesOutlineStroke(0, d);
    renderer.setSeriesStroke(0, d);
            
    renderer.setStroke(d);*/

    return chart;
}

From source file:it.eng.spagobi.engines.chart.bo.charttypes.dialcharts.BulletGraph.java

public JFreeChart createChart(DatasetMap datasets) {

    logger.debug("IN");
    Dataset dataset = (Dataset) datasets.getDatasets().get("1");
    ValueDataset valDataSet = (ValueDataset) dataset;

    Number value = valDataSet.getValue();

    DefaultCategoryDataset datasetC = new DefaultCategoryDataset();
    datasetC.addValue(value, "", "");

    // customize a bar chart 
    JFreeChart chart = ChartFactory.createBarChart(null, null, null, datasetC, PlotOrientation.HORIZONTAL,
            false, false, false);//from   w  w  w  .  j a  va 2s.co m
    chart.setBorderVisible(false);

    chart.setBackgroundPaint(color);

    TextTitle title = setStyleTitle(name, styleTitle);
    chart.setTitle(title);
    if (subName != null && !subName.equals("")) {
        TextTitle subTitle = setStyleTitle(subName, styleSubTitle);
        chart.addSubtitle(subTitle);
    }

    CategoryPlot plot = chart.getCategoryPlot();
    plot.setOutlineVisible(true);
    plot.setOutlinePaint(Color.BLACK);
    plot.setInsets(new RectangleInsets(0.0, 0.0, 0.0, 0.0));
    plot.setBackgroundPaint(null);
    plot.setDomainGridlinesVisible(false);
    plot.setRangeGridlinesVisible(false);
    plot.setRangeCrosshairVisible(false);
    plot.setAnchorValue(value.doubleValue());

    // add the target marker 
    if (target != null) {
        ValueMarker marker = new ValueMarker(target.doubleValue(), Color.BLACK, new BasicStroke(2.0f));
        plot.addRangeMarker(marker, Layer.FOREGROUND);
    }

    //sets different marks
    for (Iterator iterator = intervals.iterator(); iterator.hasNext();) {
        KpiInterval interval = (KpiInterval) iterator.next();
        // add the marks 
        IntervalMarker marker = new IntervalMarker(interval.getMin(), interval.getMax(), interval.getColor());
        plot.addRangeMarker(marker, Layer.BACKGROUND);
        logger.debug("Added new interval to the plot");
    }

    // customize axes 
    CategoryAxis domainAxis = plot.getDomainAxis();
    domainAxis.setVisible(false);

    NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setVisible(true);
    // calculate the upper limit 
    //double upperBound = target * upperFactor; 
    rangeAxis.setRange(new Range(lower, upper));
    plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);

    // customize renderer 
    BarRenderer renderer = (BarRenderer) plot.getRenderer();
    renderer.setMaximumBarWidth(0.18);
    renderer.setSeriesPaint(0, Color.BLACK);

    return chart;
}

From source file:org.openmrs.web.servlet.ShowGraphServlet.java

/**
 * The main method for this class. It will create a JFreeChart object to be written to the
 * response./*from  w  w w .j  ava2s  .  c o  m*/
 *
 * @param request the current request will all the parameters needed
 * @return JFreeChart object to be rendered
 * @should set value axis label to given units
 * @should set value axis label to concept numeric units if given units is null
 */
protected JFreeChart getChart(HttpServletRequest request) {
    // All available GET parameters
    String patientId = request.getParameter("patientId"); // required
    String conceptId1 = request.getParameter("conceptId"); // required
    String conceptId2 = request.getParameter("conceptId2");
    String chartTitle = request.getParameter("chartTitle");
    String units = request.getParameter("units");

    String minRangeString = request.getParameter("minRange");
    String maxRangeString = request.getParameter("maxRange");

    String hideDate = request.getParameter("hideDate");

    Patient patient = Context.getPatientService().getPatient(Integer.parseInt(patientId));

    // Set date range to passed values, otherwise set a default date range to the last 12 months
    Calendar cal = Calendar.getInstance();
    Date fromDate = getFromDate(request.getParameter("fromDate"));
    Date toDate = getToDate(request.getParameter("toDate"));

    // Swap if fromDate is after toDate
    if (fromDate.getTime() > toDate.getTime()) {
        Long temp = fromDate.getTime();
        fromDate.setTime(toDate.getTime());
        toDate.setTime(temp);
    }

    // Graph parameters
    Double minRange = null;
    Double maxRange = null;
    Double normalLow = null;
    Double normalHigh = null;
    Double criticalLow = null;
    Double criticalHigh = null;
    String timeAxisTitle = null;
    String rangeAxisTitle = null;
    boolean userSpecifiedMaxRange = false;
    boolean userSpecifiedMinRange = false;

    // Fetching obs
    List<Obs> observations1 = new ArrayList<Obs>();
    List<Obs> observations2 = new ArrayList<Obs>();
    Concept concept1 = null, concept2 = null;
    if (conceptId1 != null) {
        concept1 = Context.getConceptService().getConcept(Integer.parseInt(conceptId1));
    }
    if (conceptId2 != null) {
        concept2 = Context.getConceptService().getConcept(Integer.parseInt(conceptId2));
    }
    if (concept1 != null) {
        observations1 = Context.getObsService().getObservationsByPersonAndConcept(patient, concept1);
        chartTitle = concept1.getName().getName();
        rangeAxisTitle = ((ConceptNumeric) concept1).getUnits();
        minRange = ((ConceptNumeric) concept1).getLowAbsolute();
        maxRange = ((ConceptNumeric) concept1).getHiAbsolute();
        normalLow = ((ConceptNumeric) concept1).getLowNormal();
        normalHigh = ((ConceptNumeric) concept1).getHiNormal();
        criticalLow = ((ConceptNumeric) concept1).getLowCritical();
        criticalHigh = ((ConceptNumeric) concept1).getHiCritical();

        // Only get observations2 if both concepts share the same units; update chart title and ranges
        if (concept2 != null) {
            String concept2Units = ((ConceptNumeric) concept2).getUnits();
            if (concept2Units != null && concept2Units.equals(rangeAxisTitle)) {
                observations2 = Context.getObsService().getObservationsByPersonAndConcept(patient, concept2);
                chartTitle += " + " + concept2.getName().getName();
                if (((ConceptNumeric) concept2).getHiAbsolute() != null
                        && ((ConceptNumeric) concept2).getHiAbsolute() > maxRange) {
                    maxRange = ((ConceptNumeric) concept2).getHiAbsolute();
                }
                if (((ConceptNumeric) concept2).getLowAbsolute() != null
                        && ((ConceptNumeric) concept2).getLowAbsolute() < minRange) {
                    minRange = ((ConceptNumeric) concept2).getLowAbsolute();
                }
            } else {
                log.warn("Units for concept id: " + conceptId2 + " don't match units for concept id: "
                        + conceptId1 + ". Only displaying " + conceptId1);
                concept2 = null; // nullify concept2 so that the legend isn't shown later
            }
        }
    } else {
        chartTitle = "Concept " + conceptId1 + " not found";
        rangeAxisTitle = "Value";
    }

    // Overwrite with user-specified values, otherwise use default values
    if (units != null && units.length() > 0) {
        rangeAxisTitle = units;
    }
    if (minRangeString != null) {
        minRange = Double.parseDouble(minRangeString);
        userSpecifiedMinRange = true;
    }
    if (maxRangeString != null) {
        maxRange = Double.parseDouble(maxRangeString);
        userSpecifiedMaxRange = true;
    }
    if (chartTitle == null) {
        chartTitle = "";
    }
    if (rangeAxisTitle == null) {
        rangeAxisTitle = "";
    }
    if (minRange == null) {
        minRange = 0.0;
    }
    if (maxRange == null) {
        maxRange = 200.0;
    }

    // Create data set
    TimeSeriesCollection dataset = new TimeSeriesCollection();
    TimeSeries series1, series2;

    // Interval-dependent units
    Class<? extends RegularTimePeriod> timeScale = null;
    if (toDate.getTime() - fromDate.getTime() <= 86400000) {
        // Interval <= 1 day: minutely
        timeScale = Minute.class;
        timeAxisTitle = "Time";
    } else if (toDate.getTime() - fromDate.getTime() <= 259200000) {
        // Interval <= 3 days: hourly
        timeScale = Hour.class;
        timeAxisTitle = "Time";
    } else {
        timeScale = Day.class;
        timeAxisTitle = "Date";
    }
    if (concept1 == null) {
        series1 = new TimeSeries("NULL", Hour.class);
    } else {
        series1 = new TimeSeries(concept1.getName().getName(), timeScale);
    }
    if (concept2 == null) {
        series2 = new TimeSeries("NULL", Hour.class);
    } else {
        series2 = new TimeSeries(concept2.getName().getName(), timeScale);
    }

    // Add data points for concept1
    for (Obs obs : observations1) {
        if (obs.getValueNumeric() != null && obs.getObsDatetime().getTime() >= fromDate.getTime()
                && obs.getObsDatetime().getTime() < toDate.getTime()) {
            cal.setTime(obs.getObsDatetime());
            if (timeScale == Minute.class) {
                Minute min = new Minute(cal.get(Calendar.MINUTE), cal.get(Calendar.HOUR_OF_DAY),
                        cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.YEAR));
                series1.addOrUpdate(min, obs.getValueNumeric());
            } else if (timeScale == Hour.class) {
                Hour hour = new Hour(cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.DAY_OF_MONTH),
                        cal.get(Calendar.MONTH) + 1, cal.get(Calendar.YEAR));
                series1.addOrUpdate(hour, obs.getValueNumeric());
            } else {
                Day day = new Day(cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.MONTH) + 1,
                        cal.get(Calendar.YEAR));
                series1.addOrUpdate(day, obs.getValueNumeric());
            }
        }
    }

    // Add data points for concept2
    for (Obs obs : observations2) {
        if (obs.getValueNumeric() != null && obs.getObsDatetime().getTime() >= fromDate.getTime()
                && obs.getObsDatetime().getTime() < toDate.getTime()) {
            cal.setTime(obs.getObsDatetime());
            if (timeScale == Minute.class) {
                Minute min = new Minute(cal.get(Calendar.MINUTE), cal.get(Calendar.HOUR_OF_DAY),
                        cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.YEAR));
                series2.addOrUpdate(min, obs.getValueNumeric());
            } else if (timeScale == Hour.class) {
                Hour hour = new Hour(cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.DAY_OF_MONTH),
                        cal.get(Calendar.MONTH) + 1, cal.get(Calendar.YEAR));
                series2.addOrUpdate(hour, obs.getValueNumeric());
            } else {
                Day day = new Day(cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.MONTH) + 1,
                        cal.get(Calendar.YEAR));
                series2.addOrUpdate(day, obs.getValueNumeric());
            }
        }
    }

    // Add series to dataset
    dataset.addSeries(series1);
    if (!series2.isEmpty()) {
        dataset.addSeries(series2);
    }

    // As of JFreeChart 1.0.11 the default background color is dark grey instead of white.
    // This line restores the original white background.
    ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());

    JFreeChart chart = null;

    // Show legend only if more than one series
    if (concept2 == null) {
        chart = ChartFactory.createTimeSeriesChart(chartTitle, timeAxisTitle, rangeAxisTitle, dataset, false,
                false, false);
    } else {
        chart = ChartFactory.createTimeSeriesChart(chartTitle, timeAxisTitle, rangeAxisTitle, dataset, true,
                false, false);
    }

    // Customize title font
    Font font = new Font("Arial", Font.BOLD, 12);
    TextTitle title = chart.getTitle();
    title.setFont(font);
    chart.setTitle(title);

    // Add subtitle, unless 'hideDate' has been passed
    if (hideDate == null) {
        TextTitle subtitle = new TextTitle(fromDate.toString() + " - " + toDate.toString());
        subtitle.setFont(font);
        chart.addSubtitle(subtitle);
    }

    XYPlot plot = (XYPlot) chart.getPlot();
    plot.setNoDataMessage("No Data Available");

    // Add abnormal/critical range background color (only for single-concept graphs)
    if (concept2 == null) {
        IntervalMarker abnormalLow, abnormalHigh, critical;
        if (normalHigh != null) {
            abnormalHigh = new IntervalMarker(normalHigh, maxRange, COLOR_ABNORMAL);
            plot.addRangeMarker(abnormalHigh);
        }
        if (normalLow != null) {
            abnormalLow = new IntervalMarker(minRange, normalLow, COLOR_ABNORMAL);
            plot.addRangeMarker(abnormalLow);
        }
        if (criticalHigh != null) {
            critical = new IntervalMarker(criticalHigh, maxRange, COLOR_CRITICAL);
            plot.addRangeMarker(critical);
        }
        if (criticalLow != null) {
            critical = new IntervalMarker(minRange, criticalLow, COLOR_CRITICAL);
            plot.addRangeMarker(critical);
        }

        // there is data outside of the absolute lower limits for this concept (or of what the user specified as minrange)
        if (plot.getRangeAxis().getLowerBound() < minRange) {
            IntervalMarker error = new IntervalMarker(plot.getRangeAxis().getLowerBound(), minRange,
                    COLOR_ERROR);
            plot.addRangeMarker(error);
        }

        if (plot.getRangeAxis().getUpperBound() > maxRange) {
            IntervalMarker error = new IntervalMarker(maxRange, plot.getRangeAxis().getUpperBound(),
                    COLOR_ERROR);
            plot.addRangeMarker(error);
        }

    }

    // Visuals
    XYItemRenderer r = plot.getRenderer();
    if (r instanceof XYLineAndShapeRenderer) {
        XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
        renderer.setBaseShapesFilled(true);
        renderer.setBaseShapesVisible(true);
    }

    // Customize the plot (range and domain axes)

    // Modify x-axis (datetime)
    DateAxis timeAxis = (DateAxis) plot.getDomainAxis();
    if (timeScale == Day.class) {
        timeAxis.setDateFormatOverride(new SimpleDateFormat("dd-MMM-yyyy"));
    }

    timeAxis.setRange(fromDate, toDate);

    // Set y-axis range (values)
    NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

    if (userSpecifiedMinRange) {
        minRange = (rangeAxis.getLowerBound() < minRange) ? rangeAxis.getLowerBound() : minRange;
    }

    if (userSpecifiedMaxRange) {
        // otherwise we just use default range
        maxRange = (rangeAxis.getUpperBound() > maxRange) ? rangeAxis.getUpperBound() : maxRange;
    }

    rangeAxis.setRange(minRange, maxRange);

    return chart;
}

From source file:com.intel.stl.ui.common.view.ComponentFactory.java

public static JFreeChart createBulletChart(CategoryDataset dataset, double[] thresholds, Color[] colors) {
    if (thresholds.length != colors.length) {
        throw new IllegalArgumentException(
                "Inconsistant array sizes: thresholds=" + thresholds.length + " colors=" + colors.length);
    }//from  w w w.  j av a 2 s. c o  m

    JFreeChart jfreechart = ChartFactory.createBarChart(null, null, null, dataset, PlotOrientation.HORIZONTAL,
            false, true, false);
    CategoryPlot categoryplot = jfreechart.getCategoryPlot();
    categoryplot.setBackgroundPaint(null);
    categoryplot.setOutlinePaint(null);

    categoryplot.getDomainAxis().setVisible(false);

    NumberAxis rangeAxis = (NumberAxis) categoryplot.getRangeAxis();
    rangeAxis.setVisible(false);
    rangeAxis.setRange(new Range(0, 1.0));

    double last = 0.0;
    for (int i = 0; i < thresholds.length; i++) {
        IntervalMarker marker = new IntervalMarker(last, thresholds[i], colors[i]);
        categoryplot.addRangeMarker(marker, Layer.BACKGROUND);
        last = thresholds[i];
    }

    BarRenderer renderer = (BarRenderer) categoryplot.getRenderer();
    renderer.setShadowVisible(false);
    renderer.setMaximumBarWidth(0.33);
    renderer.setSeriesPaint(0, UIConstants.INTEL_DARK_GRAY);

    return jfreechart;
}