Example usage for org.jfree.chart.axis NumberAxis setAutoRangeIncludesZero

List of usage examples for org.jfree.chart.axis NumberAxis setAutoRangeIncludesZero

Introduction

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

Prototype

public void setAutoRangeIncludesZero(boolean flag) 

Source Link

Document

Sets the flag that indicates whether or not the axis range, if automatically calculated, is forced to include zero.

Usage

From source file:charts.Chart.java

public static void MultipleLineChart(XYSeriesCollection datasets, String title, String x_axis_label,
        String y_axis_label) {/* w w  w. j av  a2  s .c om*/
    JFrame chartwindow = new JFrame(title);
    JFreeChart jfreechart = ChartFactory.createXYLineChart(title, x_axis_label, y_axis_label, datasets,
            PlotOrientation.VERTICAL, true, true, true);

    XYPlot xyplot = (XYPlot) jfreechart.getPlot();
    xyplot.setBackgroundPaint(Color.white);
    xyplot.setRangeGridlinePaint(Color.black);

    NumberAxis rangeAxis = (NumberAxis) xyplot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    rangeAxis.setAutoRangeIncludesZero(true);

    XYLineAndShapeRenderer lineandshaperenderer = (XYLineAndShapeRenderer) xyplot.getRenderer();
    lineandshaperenderer.setShapesVisible(true);
    lineandshaperenderer.setDrawOutlines(true);
    lineandshaperenderer.setUseFillPaint(true);
    lineandshaperenderer.setFillPaint(Color.white);

    JPanel jpanel = new ChartPanel(jfreechart);
    jpanel.setPreferredSize(new Dimension(defaultwidth, defaultheight));
    chartwindow.setContentPane(jpanel);
    chartwindow.pack();
    RefineryUtilities.centerFrameOnScreen(chartwindow);
    chartwindow.setVisible(true);
}

From source file:com.charts.ThreeMonthChart.java

public ThreeMonthChart(YStockQuote currentStock) throws ParseException {

    DateAxis domainAxis = new DateAxis("Date");
    NumberAxis rangeAxis = new NumberAxis("Price");
    CandlestickRenderer renderer = new CandlestickRenderer();
    XYDataset dataset = getDataSet(currentStock);

    XYPlot mainPlot = new XYPlot(dataset, domainAxis, rangeAxis, renderer);

    //Do some setting up, see the API Doc
    renderer.setSeriesPaint(0, Color.BLACK);
    renderer.setDrawVolume(false);/*from www . j  a va  2 s  .  c  o  m*/
    rangeAxis.setAutoRangeIncludesZero(false);

    domainAxis.setDateFormatOverride(new SimpleDateFormat("dd-MM-yy"));
    domainAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);

    //Now create the chart and chart panel
    JFreeChart chart = new JFreeChart(currentStock.get_name(), null, mainPlot, false);
    chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new Dimension(600, 300));

    XYPlot plot = (XYPlot) chart.getPlot();
    LegendTitle legend = new LegendTitle(plot);
    chart.addLegend(legend);
    chart.getLegend().setVisible(true);
    chart.getLegend().setPosition(RectangleEdge.BOTTOM);
    ValueAxis yAxis = (ValueAxis) plot.getRangeAxis();
    DateAxis xAxis = (DateAxis) plot.getDomainAxis();
    Date now = new Date();
    SegmentedTimeline segmentedTimeline = SegmentedTimeline.newMondayThroughFridayTimeline();
    Calendar[][] holidays = DayRange.getHolidayDates();
    int len = holidays.length;

    for (int i = 0; i < holidays[0].length; i++) {
        Calendar day = Calendar.getInstance(TimeZone.getTimeZone("EST"));
        day.set(Calendar.YEAR, holidays[0][i].get(Calendar.YEAR));
        day.set(Calendar.MONTH, holidays[0][i].get(Calendar.MONTH));
        day.set(Calendar.DAY_OF_MONTH, holidays[0][i].get(Calendar.DAY_OF_MONTH));
        day.set(Calendar.HOUR_OF_DAY, 9);
        segmentedTimeline.addException(day.getTime());

    }

    xAxis.setTimeline(segmentedTimeline);
    //xAxis.setVerticalTickLabels(true);
    xAxis.setDateFormatOverride(new SimpleDateFormat("MMM y"));
    xAxis.setAutoTickUnitSelection(true);
    xAxis.setAutoRange(true);
    renderer.setAutoWidthFactor(0.5);
    renderer.setUpPaint(Color.green);
    renderer.setDownPaint(new Color(0xc0, 0x00, 0x00));
    renderer.setSeriesPaint(0, Color.black);

    StandardXYItemRenderer renderer1 = new StandardXYItemRenderer();
    renderer1.setSeriesPaint(0, Color.BLUE);
    TimeSeries movingAverage30 = MovingAverage.createMovingAverage(close, "MA(30)", 30, 0);
    Double currMA30 = (Double) movingAverage30.getDataItem(movingAverage30.getItemCount() - 1).getValue();
    currMA30 = Math.round(currMA30 * 100.0) / 100.0;
    movingAverage30.setKey("MA(30): " + currMA30);
    TimeSeriesCollection collection = new TimeSeriesCollection();
    collection.addSeries(movingAverage30);
    plot.setDataset(1, collection);
    plot.setRenderer(1, renderer1);

    chartPanel.revalidate();
    chartPanel.repaint();
    chartPanel.revalidate();
    chartPanel.repaint();
}

From source file:playground.artemc.analysis.AnalysisControlerListener.java

private void writeGraph(String name, String yLabel, Map<Integer, Double> it2Double) {

    XYLineChart chart = new XYLineChart(name, "Iteration", yLabel);

    double[] xValues = new double[it2Double.size()];
    double[] yValues = new double[it2Double.size()];
    int counter = 0;
    for (Integer iteration : it2Double.keySet()) {
        xValues[counter] = iteration.doubleValue();
        yValues[counter] = it2Double.get(iteration);
        counter++;/*w  ww .  j a v a2  s  .  c  om*/
    }

    chart.addSeries(name, xValues, yValues);

    XYPlot plot = chart.getChart().getXYPlot();
    NumberAxis axis = (NumberAxis) plot.getRangeAxis();
    axis.setAutoRange(true);
    axis.setAutoRangeIncludesZero(false);

    String outputFile = this.scenario.getConfig().controler().getOutputDirectory() + "/" + name + ".png";
    chart.saveAsPng(outputFile, 1000, 800); // File Export
}

From source file:com.charts.OneMonthChart.java

public OneMonthChart(YStockQuote currentStock) throws ParseException {

    DateAxis domainAxis = new DateAxis("Date");
    NumberAxis rangeAxis = new NumberAxis("Price");
    CandlestickRenderer renderer = new CandlestickRenderer();
    XYDataset dataset = getDataSet(currentStock);

    XYPlot mainPlot = new XYPlot(dataset, domainAxis, rangeAxis, renderer);

    //Do some setting up, see the API Doc
    renderer.setSeriesPaint(0, Color.BLACK);
    renderer.setDrawVolume(false);// w  ww . j a  v  a  2s  .  co  m
    rangeAxis.setAutoRangeIncludesZero(false);

    domainAxis.setVerticalTickLabels(true);
    domainAxis.setDateFormatOverride(new SimpleDateFormat("dd-MM-yy"));
    domainAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);

    //Now create the chart and chart panel
    JFreeChart chart = new JFreeChart(currentStock.get_name(), null, mainPlot, false);
    chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new Dimension(600, 300));

    XYPlot plot = (XYPlot) chart.getPlot();
    LegendTitle legend = new LegendTitle(plot);
    chart.addLegend(legend);
    chart.getLegend().setVisible(true);
    chart.getLegend().setPosition(RectangleEdge.BOTTOM);

    ValueAxis yAxis = (ValueAxis) plot.getRangeAxis();
    DateAxis xAxis = (DateAxis) plot.getDomainAxis();
    Date now = new Date();
    SegmentedTimeline segmentedTimeline = SegmentedTimeline.newMondayThroughFridayTimeline();
    Calendar[][] holidays = DayRange.getHolidayDates();
    int len = holidays.length;

    for (int i = 0; i < holidays[0].length; i++) {
        Calendar day = Calendar.getInstance(TimeZone.getTimeZone("EST"));
        day.set(Calendar.YEAR, holidays[0][i].get(Calendar.YEAR));
        day.set(Calendar.MONTH, holidays[0][i].get(Calendar.MONTH));
        day.set(Calendar.DAY_OF_MONTH, holidays[0][i].get(Calendar.DAY_OF_MONTH));
        day.set(Calendar.HOUR_OF_DAY, 9);
        segmentedTimeline.addException(day.getTime());

    }

    xAxis.setTimeline(segmentedTimeline);
    //xAxis.setVerticalTickLabels(true);
    xAxis.setDateFormatOverride(new SimpleDateFormat("MMM-dd"));
    xAxis.setAutoTickUnitSelection(false);
    xAxis.setAutoRange(false);
    renderer.setAutoWidthFactor(0.5);
    renderer.setUpPaint(Color.green);
    renderer.setDownPaint(new Color(0xc0, 0x00, 0x00));
    renderer.setSeriesPaint(0, Color.black);

    StandardXYItemRenderer renderer1 = new StandardXYItemRenderer();
    renderer1.setSeriesPaint(0, Color.BLUE);
    TimeSeries movingAverage30 = MovingAverage.createMovingAverage(close, "MA(30)", 30, 0);
    Double currMA30 = (Double) movingAverage30.getDataItem(movingAverage30.getItemCount() - 1).getValue();
    currMA30 = Math.round(currMA30 * 100.0) / 100.0;
    movingAverage30.setKey("MA(30): " + currMA30);
    TimeSeriesCollection collection = new TimeSeriesCollection();
    collection.addSeries(movingAverage30);
    plot.setDataset(1, collection);
    plot.setRenderer(1, renderer1);

    chartPanel.revalidate();
    chartPanel.repaint();
    chartPanel.revalidate();
    chartPanel.repaint();
}

From source file:playground.artemc.analysis.AnalysisControlerListener.java

private void writeGraphSum(String name, String yLabel, Map<Integer, Double> it2Double1,
        Map<Integer, Double> it2Double2) {

    XYLineChart chart = new XYLineChart(name, "Iteration", yLabel);

    double[] xValues = new double[it2Double1.size()];
    double[] yValues = new double[it2Double1.size()];
    int counter = 0;
    for (Integer iteration : it2Double1.keySet()) {
        xValues[counter] = iteration.doubleValue();
        yValues[counter] = (it2Double1.get(iteration)) + (it2Double2.get(iteration));
        counter++;/*  w ww . j  a v  a 2s .c  om*/
    }

    chart.addSeries(name, xValues, yValues);

    XYPlot plot = chart.getChart().getXYPlot();
    NumberAxis axis = (NumberAxis) plot.getRangeAxis();
    axis.setAutoRange(true);
    axis.setAutoRangeIncludesZero(false);

    String outputFile = this.scenario.getConfig().controler().getOutputDirectory() + "/" + name + ".png";
    chart.saveAsPng(outputFile, 1000, 800); // File Export
}

From source file:playground.artemc.analysis.AnalysisControlerListener.java

private void writeGraphDiv(String name, String yLabel, Map<Integer, Double> it2Double1,
        Map<Integer, Double> it2Double2) {

    XYLineChart chart = new XYLineChart(name, "Iteration", yLabel);

    double[] xValues = new double[it2Double1.size()];
    double[] yValues = new double[it2Double1.size()];
    int counter = 0;
    for (Integer iteration : it2Double1.keySet()) {
        xValues[counter] = iteration.doubleValue();
        yValues[counter] = (it2Double1.get(iteration)) / (it2Double2.get(iteration));
        counter++;//w w w .  j  av  a  2 s.c  om
    }

    chart.addSeries(name, xValues, yValues);

    XYPlot plot = chart.getChart().getXYPlot();
    NumberAxis axis = (NumberAxis) plot.getRangeAxis();
    axis.setAutoRange(true);
    axis.setAutoRangeIncludesZero(false);

    String outputFile = this.scenario.getConfig().controler().getOutputDirectory() + "/" + name + ".png";
    chart.saveAsPng(outputFile, 1000, 800); // File Export
}

From source file:gov.llnl.lc.infiniband.opensm.plugin.gui.chart.PortCounterPlotWorker.java

@Override
protected Void doInBackground() throws Exception {
    // 1.  counter value (already done, just add following to this)
    // 2.  delta/period
    ////from  w  w w .j  av a 2 s  . com
    //    -- if error counter --
    // 3.  include xmit and rcv traffic deltas? (own scale)
    //
    //
    //    -- if traffic counter --
    // 3.  include rate and utilization values?
    //

    // this is a SwingWorker thread from its pool, give it a recognizable name
    Thread.currentThread().setName("PortCounterPlotWorker");

    logger.info("Worker Building Plot");
    SMT_UpdateService updateService = SMT_UpdateService.getInstance();
    OMS_Collection history = updateService.getCollection();
    OSM_FabricDeltaCollection deltaHistory = history.getOSM_FabricDeltaCollection();

    XYPlot plot = (XYPlot) Chart.getPlot();

    // AXIS 2 - the change, or delta value of the desired counter
    NumberAxis axis2 = new NumberAxis(PortCounterAxisLabel.DELTA.getName());
    axis2.setFixedDimension(10.0);
    axis2.setAutoRangeIncludesZero(false);
    plot.setRangeAxis(1, axis2);

    XYDataset dataset2 = createDeltaDataset(deltaHistory, PortCounter, PortCounterAxisLabel.DELTA.getName());
    plot.setDataset(1, dataset2);
    plot.mapDatasetToRangeAxis(1, 1);
    XYItemRenderer renderer2 = new StandardXYItemRenderer();
    plot.setRenderer(1, renderer2);

    // the other two axis are optional, and vary depending on the
    // type of counter
    NumberAxis axis3 = null;
    XYDataset dataset3 = null;
    XYItemRenderer renderer3 = null;
    NumberAxis axis4 = null;
    XYDataset dataset4 = null;
    XYItemRenderer renderer4 = null;

    if (AddExtra) {
        if (isError) {

            // add rcv deltas
            PortCounterName pcr = PortCounterName.rcv_data;
            axis3 = new NumberAxis(PortCounterAxisLabel.RCV_DELTA.getName());
            axis3.setFixedDimension(10.0);
            axis3.setAutoRangeIncludesZero(false);
            plot.setRangeAxis(2, axis3);

            dataset3 = createDeltaDataset(deltaHistory, pcr, pcr.getName());
            plot.setDataset(2, dataset3);
            plot.mapDatasetToRangeAxis(2, 2);
            renderer3 = new StandardXYItemRenderer();
            plot.setRenderer(2, renderer3);

            // add xmit deltas
            pcr = PortCounterName.xmit_data;
            axis4 = new NumberAxis(PortCounterAxisLabel.XMT_DELTA.getName());
            axis4.setFixedDimension(10.0);
            axis4.setAutoRangeIncludesZero(false);
            plot.setRangeAxis(3, axis4);

            dataset4 = createDeltaDataset(deltaHistory, pcr, pcr.getName());
            plot.setDataset(3, dataset4);
            plot.mapDatasetToRangeAxis(3, 3);
            renderer4 = new StandardXYItemRenderer();
            plot.setRenderer(3, renderer4);

            // use a common scale for both xmit and rcv counters
            double minRange = axis3.getLowerBound() < axis4.getLowerBound() ? axis3.getLowerBound()
                    : axis4.getLowerBound();
            double maxRange = axis3.getUpperBound() < axis4.getUpperBound() ? axis4.getUpperBound()
                    : axis3.getUpperBound();
            axis3.setAutoRange(false);
            axis4.setAutoRange(false);
            axis3.setRange(minRange, maxRange);
            axis4.setRange(minRange, maxRange);
        } else {
            // add rate
            PortCounterName pcr = PortCounter;
            axis3 = new NumberAxis(pcr.getName() + " " + PortCounterAxisLabel.RATE.getUnits());
            axis3.setFixedDimension(10.0);
            axis3.setAutoRangeIncludesZero(true);
            plot.setRangeAxis(2, axis3);

            dataset3 = createRateDataset(deltaHistory, pcr, PortCounterAxisLabel.RATE.getName());
            plot.setDataset(2, dataset3);
            plot.mapDatasetToRangeAxis(2, 2);
            renderer3 = new StandardXYItemRenderer();
            plot.setRenderer(2, renderer3);

            // add utilization
            axis4 = new NumberAxis(pcr.getName() + " " + PortCounterAxisLabel.UTILIZATION.getUnits());
            axis4.setFixedDimension(10.0);
            //         axis4.setAutoRangeIncludesZero(true);
            axis4.setRange(0.0, 100.0);
            plot.setRangeAxis(3, axis4);

            dataset4 = createUtilizationDataset(deltaHistory, pcr, PortCounterAxisLabel.UTILIZATION.getName());
            plot.setDataset(3, dataset4);
            plot.mapDatasetToRangeAxis(3, 3);
            renderer4 = new StandardXYItemRenderer();
            plot.setRenderer(3, renderer4);
        }
    }
    ChartUtilities.applyCurrentTheme(Chart);

    Color c1 = Color.black;
    Color c2 = Color.blue;

    Color c3 = Color.green;
    Color c4 = Color.magenta;

    Color ce = Color.red;

    if (isError)
        c2 = ce;

    // change the series and axis colours after the theme has
    // been applied...
    plot.getRenderer().setSeriesPaint(0, c1);

    renderer2.setSeriesPaint(0, c2);
    axis2.setLabelPaint(c2);
    axis2.setTickLabelPaint(c2);

    if (AddExtra) {
        renderer3.setSeriesPaint(0, c3);
        axis3.setLabelPaint(c3);
        axis3.setTickLabelPaint(c3);

        renderer4.setSeriesPaint(0, c4);
        axis4.setLabelPaint(c4);
        axis4.setTickLabelPaint(c4);
    }

    return null;
}

From source file:com.charts.SixMonthChart.java

public SixMonthChart(YStockQuote currentStock) throws ParseException {

    DateAxis domainAxis = new DateAxis("Date");
    NumberAxis rangeAxis = new NumberAxis("Price");
    CandlestickRenderer renderer = new CandlestickRenderer();
    XYDataset dataset = getDataSet(currentStock);

    XYPlot mainPlot = new XYPlot(dataset, domainAxis, rangeAxis, renderer);

    //Do some setting up, see the API Doc
    renderer.setSeriesPaint(0, Color.BLACK);
    renderer.setDrawVolume(false);/*w  w w . j a  va 2 s . c om*/
    rangeAxis.setAutoRangeIncludesZero(false);

    domainAxis.setDateFormatOverride(new SimpleDateFormat("dd-MM-yy"));
    domainAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);

    //Now create the chart and chart panel
    JFreeChart chart = new JFreeChart(currentStock.get_name(), null, mainPlot, false);
    chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new Dimension(600, 300));

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

    LegendTitle legend = new LegendTitle(plot);

    chart.addLegend(legend);
    chart.getLegend().setVisible(true);
    chart.getLegend().setPosition(RectangleEdge.BOTTOM);

    ValueAxis yAxis = (ValueAxis) plot.getRangeAxis();
    DateAxis xAxis = (DateAxis) plot.getDomainAxis();
    Date now = new Date();
    SegmentedTimeline segmentedTimeline = SegmentedTimeline.newMondayThroughFridayTimeline();
    Calendar[][] holidays = DayRange.getHolidayDates();
    int len = holidays.length;

    for (int i = 0; i < holidays[0].length; i++) {
        Calendar day = Calendar.getInstance(TimeZone.getTimeZone("EST"));
        day.set(Calendar.YEAR, holidays[0][i].get(Calendar.YEAR));
        day.set(Calendar.MONTH, holidays[0][i].get(Calendar.MONTH));
        day.set(Calendar.DAY_OF_MONTH, holidays[0][i].get(Calendar.DAY_OF_MONTH));
        day.set(Calendar.HOUR_OF_DAY, 9);
        segmentedTimeline.addException(day.getTime());

    }

    xAxis.setTimeline(segmentedTimeline);
    //xAxis.setVerticalTickLabels(true);
    xAxis.setDateFormatOverride(new SimpleDateFormat("MMM y"));
    xAxis.setAutoTickUnitSelection(true);
    xAxis.setAutoRange(true);
    renderer.setAutoWidthFactor(0.5);
    renderer.setUpPaint(Color.green);
    renderer.setDownPaint(new Color(0xc0, 0x00, 0x00));
    renderer.setSeriesPaint(0, Color.black);

    StandardXYItemRenderer renderer1 = new StandardXYItemRenderer();
    renderer1.setSeriesPaint(0, Color.BLUE);
    TimeSeries movingAverage30 = MovingAverage.createMovingAverage(close, "MA(30)", 30, 0);
    Double currMA30 = (Double) movingAverage30.getDataItem(movingAverage30.getItemCount() - 1).getValue();
    currMA30 = Math.round(currMA30 * 100.0) / 100.0;
    movingAverage30.setKey("MA(30): " + currMA30);
    TimeSeriesCollection collection = new TimeSeriesCollection();
    collection.addSeries(movingAverage30);
    plot.setDataset(1, collection);
    plot.setRenderer(1, renderer1);

    chartPanel.revalidate();
    chartPanel.repaint();
    chartPanel.revalidate();
    chartPanel.repaint();
}

From source file:edu.ucla.stat.SOCR.chart.demo.DotChart.java

/**
 * Creates a chart.//  w  w w  .j a  v a  2 s . c o  m
 * 
 * @param dataset  the data for the chart.
 * 
 * @return a chart.
 */
protected JFreeChart createChart1(XYDataset dataset) {
    //System.out.println("createChart1 called");
    //   create the chart...
    JFreeChart chart = ChartFactory.createXYLineChart(chartTitle, // chart title
            "", // x axis label domain
            rangeLabel, // y axis label  range
            dataset, // data
            PlotOrientation.HORIZONTAL, !legendPanelOn, // include legend
            true, // tooltips
            false // urls
    );

    // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
    chart.setBackgroundPaint(Color.white);

    // get a reference to the plot for further customisation...
    XYPlot plot = (XYPlot) chart.getPlot();

    plot.setBackgroundPaint(Color.white);
    plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
    plot.setDomainGridlinePaint(Color.lightGray);
    plot.setRangeGridlinePaint(Color.lightGray);

    XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
    //  renderer.setSeriesShape(0, java.awt.Shape.round);
    renderer.setBaseShapesVisible(true);
    renderer.setBaseShapesFilled(true);
    renderer.setBaseLinesVisible(false);

    renderer.setLegendItemLabelGenerator(new SOCRXYSeriesLabelGenerator());

    //change the auto tick unit selection to integer units only...
    NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    rangeAxis.setAutoRangeIncludesZero(true);
    rangeAxis.setUpperMargin(0.01);
    rangeAxis.setLowerMargin(0.01);

    NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
    //domainAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    domainAxis.setAutoRangeIncludesZero(true);
    domainAxis.setTickLabelsVisible(false);
    domainAxis.setTickMarksVisible(false);
    domainAxis.setUpperMargin(5);
    domainAxis.setLowerMargin(0.01);

    // OPTIONAL CUSTOMISATION COMPLETED.
    setYSummary(dataset);

    try {
        //   System.out.println("setting the common RangeAxis to null");
        common_rangeAxis = null;
        common_rangeAxis = (NumberAxis) rangeAxis.clone();
        //   System.out.println("creating the common RangeAxis");
    } catch (CloneNotSupportedException e) {
        System.out.println("CloneNotSupportedException!, exception caught");
    }

    return chart;
}

From source file:edu.wisc.ssec.mcidasv.control.McIDASVHistogramWrapper.java

/**
 * Assumes that {@code data} has been validated and is okay to actually try
 * loading./*from   w ww. ja  v  a2  s. c om*/
 *
 * @param data Data to use in histogram. Cannot be {@code null} or all NaNs.
 *
 * @throws VisADException
 * @throws RemoteException
 */
private void reallyLoadData(FlatField data) throws VisADException, RemoteException {
    createChart();
    List dataChoiceWrappers = getDataChoiceWrappers();

    try {
        clearHistogram();

        Hashtable props = new Hashtable();
        ErrorEstimate[] errOut = new ErrorEstimate[1];
        for (int paramIdx = 0; paramIdx < dataChoiceWrappers.size(); paramIdx++) {
            DataChoiceWrapper wrapper = (DataChoiceWrapper) dataChoiceWrappers.get(paramIdx);

            DataChoice dataChoice = wrapper.getDataChoice();
            props = dataChoice.getProperties();
            Unit defaultUnit = ucar.visad.Util.getDefaultRangeUnits((FlatField) data)[0];
            Unit unit = ((DisplayControlImpl) imageControl).getDisplayUnit();
            double[][] samples = data.getValues(false);
            double[] actualValues = filterData(samples[0], getTimeValues(samples, data))[0];
            if ((defaultUnit != null) && !defaultUnit.equals(unit)) {
                actualValues = Unit.transformUnits(unit, errOut, defaultUnit, null, actualValues);
            }
            final NumberAxis domainAxis = new NumberAxis(wrapper.getLabel(unit));

            domainAxis.setAutoRangeIncludesZero(false);

            XYItemRenderer renderer;
            if (getStacked()) {
                renderer = new StackedXYBarRenderer();
            } else {
                renderer = new XYBarRenderer();
            }
            if ((plot == null) && (chartPanel != null)) {
                plot = chartPanel.getChart().getXYPlot();
            }
            plot.setRenderer(paramIdx, renderer);
            Color c = wrapper.getColor(paramIdx);
            domainAxis.setLabelPaint(c);
            renderer.setSeriesPaint(0, c);

            MyHistogramDataset dataset = new MyHistogramDataset();
            dataset.setType(HistogramType.FREQUENCY);
            dataset.addSeries(dataChoice.getName() + " [" + unit + ']', actualValues, getBins());
            samples = null;
            actualValues = null;
            plot.setDomainAxis(paramIdx, domainAxis, false);
            plot.mapDatasetToDomainAxis(paramIdx, paramIdx);
            plot.setDataset(paramIdx, dataset);

            domainAxis.addChangeListener(new AxisChangeListener() {
                public void axisChanged(AxisChangeEvent ae) {
                    if (!imageControl.isInitDone()) {
                        return;
                    }

                    Range range = domainAxis.getRange();
                    double newLow = Math.floor(range.getLowerBound() + 0.5);
                    double newHigh = Math.floor(range.getUpperBound() + 0.5);
                    double prevLow = getLow();
                    double prevHigh = getHigh();
                    try {
                        ucar.unidata.util.Range newRange;
                        if (prevLow > prevHigh) {
                            newRange = new ucar.unidata.util.Range(newHigh, newLow);
                        } else {
                            newRange = new ucar.unidata.util.Range(newLow, newHigh);
                        }
                        ((DisplayControlImpl) imageControl).setRange(newRange);
                    } catch (Exception e) {
                        logger.error("Cannot change range", e);
                    }
                }
            });

            Range range = domainAxis.getRange();
            low = range.getLowerBound();
            high = range.getUpperBound();
        }

    } catch (Exception exc) {
        System.out.println("Exception exc=" + exc);
        LogUtil.logException("Error creating data set", exc);
    }
}