Example usage for org.jfree.data.xy DefaultHighLowDataset getVolumeValue

List of usage examples for org.jfree.data.xy DefaultHighLowDataset getVolumeValue

Introduction

In this page you can find the example usage for org.jfree.data.xy DefaultHighLowDataset getVolumeValue.

Prototype

@Override
public double getVolumeValue(int series, int item) 

Source Link

Document

Returns the volume-value (as a double primitive) for an item within a series.

Usage

From source file:org.yccheok.jstock.gui.charting.ChartJDialog.java

/**
 * Zoom in to this chart with specific amount of time.
 * @param field the calendar field.//from   w ww. j a v  a2 s .  c o m
 * @param amount the amount of date or time to be added to the field.
 */
private void _zoom(int field, int amount) {
    this.chartPanel.restoreAutoBounds();

    final int itemCount = this.priceTimeSeries.getItemCount();
    final Day day = (Day) this.priceTimeSeries.getDataItem(itemCount - 1).getPeriod();
    // Candle stick takes up half day space.
    // Volume price chart's volume information takes up whole day space.
    final long end = day.getFirstMillisecond()
            + (this.getCurrentType() == Type.Candlestick ? (1000 * 60 * 60 * 12) : (1000 * 60 * 60 * 24 - 1));
    final Calendar calendar = Calendar.getInstance();
    // -1. Calendar's month is 0 based but JFreeChart's month is 1 based.
    calendar.set(day.getYear(), day.getMonth() - 1, day.getDayOfMonth(), 0, 0, 0);
    calendar.set(Calendar.MILLISECOND, 0);

    calendar.add(field, amount);
    // Candle stick takes up half day space.
    // Volume price chart's volume information does not take up any space.
    final long start = Math.max(0, calendar.getTimeInMillis()
            - (this.getCurrentType() == Type.Candlestick ? (1000 * 60 * 60 * 12) : 0));
    final ValueAxis valueAxis = this.getPlot().getDomainAxis();

    if (priceTimeSeries.getItemCount() > 0) {
        if (start < priceTimeSeries.getTimePeriod(0).getFirstMillisecond()) {
            // To prevent zoom-out too much.
            // This happens when user demands for 10 years zoom, where we
            // are only having 5 years data.
            return;
        }
    }

    valueAxis.setRange(start, end);

    double min = Double.MAX_VALUE;
    double max = Double.MIN_VALUE;
    double max_volume = Double.MIN_VALUE;
    final DefaultHighLowDataset defaultHighLowDataset = (DefaultHighLowDataset) this.priceOHLCDataset;

    for (int i = itemCount - 1; i >= 0; i--) {
        final TimeSeriesDataItem item = this.priceTimeSeries.getDataItem(i);
        final Day d = (Day) item.getPeriod();
        if (d.getFirstMillisecond() < start) {
            break;
        }

        final double high = defaultHighLowDataset.getHighValue(0, i);
        final double low = defaultHighLowDataset.getLowValue(0, i);
        final double volume = defaultHighLowDataset.getVolumeValue(0, i);

        if (max < high) {
            max = high;
        }
        if (min > low) {
            min = low;
        }
        if (max_volume < volume) {
            max_volume = volume;
        }
    }

    if (min > max) {
        return;
    }

    final ValueAxis rangeAxis = this.getPlot().getRangeAxis();
    final Range rangeAxisRange = rangeAxis.getRange();
    // Increase each side by 1%
    double tolerance = 0.01 * (max - min);
    // The tolerance must within range [0.01, 1.0]
    tolerance = Math.min(Math.max(0.01, tolerance), 1.0);
    // The range must within the original chart range.
    min = Math.max(rangeAxisRange.getLowerBound(), min - tolerance);
    max = Math.min(rangeAxisRange.getUpperBound(), max + tolerance);

    this.getPlot().getRangeAxis().setRange(min, max);

    if (this.getPlot().getRangeAxisCount() > 1) {
        final double volumeUpperBound = this.getPlot().getRangeAxis(1).getRange().getUpperBound();
        final double suggestedVolumneUpperBound = max_volume * 4;
        // To prevent over zoom-in.
        if (suggestedVolumneUpperBound < volumeUpperBound) {
            this.getPlot().getRangeAxis(1).setRange(0, suggestedVolumneUpperBound);
        }
    }
}