⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dateplot.java

📁 EclipseTrader is a stock exchange analysis system, featuring shares pricing watch, intraday and hi
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (c) 2004-2007 Marco Maccaferri and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: *     Marco Maccaferri - initial API and implementation */package net.sourceforge.eclipsetrader.charts;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Calendar;import java.util.Collections;import java.util.Comparator;import java.util.Date;import java.util.Iterator;import java.util.List;import net.sourceforge.eclipsetrader.core.db.Bar;import net.sourceforge.eclipsetrader.core.db.BarData;import org.eclipse.swt.SWT;import org.eclipse.swt.events.PaintEvent;import org.eclipse.swt.graphics.Color;import org.eclipse.swt.graphics.GC;import org.eclipse.swt.graphics.Point;import org.eclipse.swt.graphics.Rectangle;import org.eclipse.swt.layout.GridData;import org.eclipse.swt.layout.GridLayout;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Label;/** */public class DatePlot extends Composite{    private IndicatorPlot indicatorPlot;    private ScalePlot scalePlot;    private Color hilight = new Color(null, 255, 0, 0);    private int scaleWidth = 75;    private int interval = BarData.INTERVAL_DAILY;    private List dateList = new ArrayList();    private BarData barData;    private Label label;    private Color labelColor = new Color(null, 255, 255, 0);    private Color background;    private Color foreground;    private SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy"); //$NON-NLS-1$    private SimpleDateFormat tf = new SimpleDateFormat("HH:mm"); //$NON-NLS-1$    private int extendPeriod = 0;    private Comparator dateSearchComparator = new Comparator() {        public int compare(Object o1, Object o2)        {            Date d1 = (o1 instanceof TickItem) ? ((TickItem)o1).date : (Date)o1;            Date d2 = (o2 instanceof TickItem) ? ((TickItem)o2).date : (Date)o2;            if (d1.after(d2) == true)                return 1;            else if (d1.before(d2) == true)                return -1;            return 0;        }    };        private class TickItem    {        public boolean flag = false;        public boolean tick = false;        public String text = ""; //$NON-NLS-1$        public Date date;                public TickItem(Date date)        {            this.date = date;        }    }    public DatePlot(Composite parent, int style)    {        super(parent, style);        GridLayout gridLayout = new GridLayout();        gridLayout.numColumns = 2;        gridLayout.marginWidth = gridLayout.marginHeight = 1;        gridLayout.horizontalSpacing = gridLayout.verticalSpacing = 0;        setLayout(gridLayout);                indicatorPlot = new IndicatorPlot(this, SWT.NONE) {            public void paintControl(PaintEvent e)            {                if (image != null && !image.isDisposed())                {                    if (foreground != null)                        e.gc.setForeground(foreground);                                        Point plotLocation = getPlotLocation();                    e.gc.drawImage(image, plotLocation.x, plotLocation.y);                    e.gc.drawLine(0, 0, getBounds().width, 0);                }            }                        public void draw(GC gc)            {                if (background != null)                    gc.setBackground(background);                if (foreground != null)                    gc.setForeground(foreground);                gc.fillRectangle(image.getBounds());                int x = getMarginWidth() + getGridWidth() / 2;                for(Iterator iter = dateList.iterator(); iter.hasNext(); )                {                    TickItem item = (TickItem)iter.next();                    if (item.tick)                    {                        if (item.flag && hilight != null)                            gc.setForeground(hilight);                        gc.drawLine (x, 1, x, 6);                        gc.drawString(item.text, x - 1, 7, true);                        if (item.flag && hilight != null && foreground != null)                            gc.setForeground(foreground);                    }                    else                        gc.drawLine (x, 1, x, 3);                                        x += getGridWidth();                }            }        };        indicatorPlot.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true, 1, 1));        indicatorPlot.setPlotSize(0, indicatorPlot.getPlotSize().y);        scalePlot = new ScalePlot(this, SWT.NONE) {            public void draw(GC gc)            {                Rectangle bounds = getBounds();                 Color background = getBackground();                Color foreground = getForeground();                                if (background != null)                    gc.setBackground(background);                if (foreground != null)                    gc.setForeground(foreground);                gc.fillRectangle(bounds);                gc.drawLine(0, 0, bounds.width, 0);//                gc.drawLine(0, 0, 0, bounds.height);                                if (background != null)                    background.dispose();                if (foreground != null)                    foreground.dispose();            }        };        GridData gridData = new GridData();        gridData.widthHint = scaleWidth;        gridData.grabExcessVerticalSpace = true;        gridData.horizontalAlignment = GridData.FILL;        gridData.verticalAlignment = GridData.FILL;        scalePlot.setLayoutData(gridData);                label = new Label(indicatorPlot, SWT.CENTER);        label.setBackground(labelColor);        label.setBounds(0, 0, 0, 0);        pack();    }    public int getInterval()    {        return interval;    }    public void setInterval(int interval)    {        this.interval = interval;    }    public IndicatorPlot getIndicatorPlot()    {        return indicatorPlot;    }    public ScalePlot getScalePlot()    {        return scalePlot;    }    public void setScaleWidth(int scaleWidth)    {        this.scaleWidth = scaleWidth;        ((GridData)scalePlot.getLayoutData()).widthHint = this.scaleWidth;        pack();    }    public void setBackground(Color color)    {        if (background != null)            background.dispose();        background = color == null ? null : new Color(getDisplay(), color.getRGB());    }    public void setForeground(Color color)    {        if (foreground != null)            foreground.dispose();        foreground = color == null ? null : new Color(getDisplay(), color.getRGB());    }    public void setHilight(Color hilightColor)    {        if (hilight != null)            hilight.dispose();        hilight = hilightColor == null ? null : new Color(getDisplay(), hilightColor.getRGB());    }    public void setExtendPeriod(int extendPeriod)    {        this.extendPeriod = extendPeriod;    }    public int mapToScreen(Date date)    {        if (date == null)            return -1;                int index = Collections.binarySearch(dateList, date, dateSearchComparator);        if (index >= 0 && index < dateList.size())            return indicatorPlot.getMarginWidth() + indicatorPlot.getGridWidth() / 2 + index * indicatorPlot.getGridWidth();        return -1;    }    public Date mapToDate(int x)    {        int index = (x - indicatorPlot.getMarginWidth()) / indicatorPlot.getGridWidth();        if (index < 0)        {            if (dateList.size() > 0)                return ((TickItem)dateList.get(0)).date;            return null;        }        if (index >= dateList.size())        {            if (dateList.size() > 0)                return ((TickItem)dateList.get(dateList.size() - 1)).date;            return null;        }        return ((TickItem)dateList.get(index)).date;    }        public BarData getBarData()    {        return barData;    }        public void setBarData(BarData barData)    {        if (barData == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);        this.barData = barData;        dateList.clear();        switch (interval)        {            case BarData.INTERVAL_MINUTE1:            case BarData.INTERVAL_MINUTE2:            case BarData.INTERVAL_MINUTE5:            case BarData.INTERVAL_MINUTE10:            case BarData.INTERVAL_MINUTE15:            case BarData.INTERVAL_MINUTE30:            case BarData.INTERVAL_MINUTE60:                getMinuteDate();                break;            case BarData.INTERVAL_WEEKLY:                getWeeklyDate();                break;            case BarData.INTERVAL_MONTHLY:                getMonthlyDate();                break;            default:                getDailyDate();                break;        }        if (dateList.size() != 0)            indicatorPlot.setPlotSize(dateList.size() * indicatorPlot.getGridWidth() + indicatorPlot.getMarginWidth() * 2, indicatorPlot.getPlotSize().y);        else            indicatorPlot.setPlotSize(0, indicatorPlot.getPlotSize().y);                redraw();    }        private void getDailyDate()    {        SimpleDateFormat monthYearFormatter = new SimpleDateFormat("MMM, yyyy"); //$NON-NLS-1$        Calendar oldDate = null;        Calendar currentDate = Calendar.getInstance();        int x = indicatorPlot.getMarginWidth() + indicatorPlot.getGridWidth() / 2;                for (Iterator iter = barData.iterator(); iter.hasNext(); )        {            Bar bar = (Bar)iter.next();            currentDate.setTime(bar.getDate());            TickItem item = new TickItem(bar.getDate());            if (oldDate == null || currentDate.get(Calendar.DAY_OF_YEAR) != oldDate.get(Calendar.DAY_OF_YEAR))            {                if (oldDate == null)                {                    oldDate = Calendar.getInstance();                    oldDate.setTime(bar.getDate());                }                                if (currentDate.get(Calendar.MONTH) != oldDate.get(Calendar.MONTH))                    item.tick = true;                if (currentDate.get(Calendar.YEAR) != oldDate.get(Calendar.YEAR))                    item.flag = true;                item.text = monthYearFormatter.format(currentDate.getTime());                oldDate.setTime(bar.getDate());            }            dateList.add(item);            x += indicatorPlot.getGridWidth();        }                currentDate = Calendar.getInstance();        currentDate.set(Calendar.HOUR, 0);        currentDate.set(Calendar.MINUTE, 0);        currentDate.set(Calendar.SECOND, 0);        currentDate.set(Calendar.MILLISECOND, 0);        if (dateList.size() != 0)        {            currentDate.setTime(((TickItem)dateList.get(dateList.size() - 1)).date);            currentDate.add(Calendar.DATE, 1);                        for (int i = 0; i < extendPeriod; i++)            {                if (currentDate.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY && currentDate.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY)                {                    TickItem item = new TickItem(currentDate.getTime());                    if (oldDate == null || currentDate.get(Calendar.DAY_OF_YEAR) != oldDate.get(Calendar.DAY_OF_YEAR))                    {                        if (oldDate == null)                        {                            oldDate = Calendar.getInstance();                            oldDate.setTime(currentDate.getTime());                        }                                                if (currentDate.get(Calendar.MONTH) != oldDate.get(Calendar.MONTH))                            item.tick = true;                        if (currentDate.get(Calendar.YEAR) != oldDate.get(Calendar.YEAR))                            item.flag = true;                        item.text = monthYearFormatter.format(currentDate.getTime());                        oldDate.setTime(currentDate.getTime());                    }                                        dateList.add(item);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -