com.ivli.roim.controls.DomainMarker.java Source code

Java tutorial

Introduction

Here is the source code for com.ivli.roim.controls.DomainMarker.java

Source

/*
 * Copyright (C) 2016 likhachev
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package com.ivli.roim.controls;

import java.awt.BasicStroke;
import java.awt.Color;
import org.jfree.chart.plot.ValueMarker;
import org.jfree.data.general.SeriesChangeEvent;
import org.jfree.data.general.SeriesChangeListener;
import org.jfree.data.xy.XYSeries;
import org.jfree.ui.RectangleAnchor;
import org.jfree.ui.RectangleInsets;

/**
 *
 * @author likhachev
 */
class DomainMarker extends ValueMarker implements SeriesChangeListener {
    private final XYSeries iSeries;

    private ValueMarker iLink;

    private static final String LABEL_FORMAT = "%.2f"; //NOI18N

    public DomainMarker(XYSeries aSet) {
        this((aSet.getMaxX() - aSet.getMinX()) / 2., aSet);
    }

    public DomainMarker(double aV, XYSeries aS) {
        super(aV);
        iSeries = aS;
        iSeries.addChangeListener(this);
        setLabel(String.format(LABEL_FORMAT, aV));
        setLabelAnchor(RectangleAnchor.CENTER);
        setLabelOffset(RectangleInsets.ZERO_INSETS);

        setAlpha(1.0f);
        setPaint(Color.BLACK);
        setStroke(new BasicStroke(1.0f));

        setOutlinePaint(Color.CYAN);
        setOutlineStroke(new BasicStroke(.0f));
    }

    public XYSeries getSeries() {
        return iSeries;
    }

    public void setLinkedMarker(ValueMarker aM) {
        iLink = aM;
        iLink.setValue(XYSeriesUtilities.getNearestY(iSeries, getValue()));
        iLink.setLabelAnchor(RectangleAnchor.BOTTOM);
        iLink.setLabelOffset(RectangleInsets.ZERO_INSETS);
    }

    public ValueMarker getLinkedMarker() {
        return iLink;
    }

    @Override
    public void seriesChanged(SeriesChangeEvent sce) {
        setValue(getValue());
    }

    @Override
    public void setValue(double aVal) {
        super.setValue(aVal);

        if (null != iSeries) {
            final Double newY = XYSeriesUtilities.getNearestY(iSeries, aVal);
            if (null != iLink) {
                iLink.setValue(newY);
                iLink.setLabel(String.format(LABEL_FORMAT, newY));
            }
            setLabel(String.format(LABEL_FORMAT, aVal));
        } else {
            setLabel(String.format(LABEL_FORMAT, aVal)); //NOI18N
        }
    }

    private static final org.apache.logging.log4j.Logger LOG = org.apache.logging.log4j.LogManager.getLogger();
}