ChartDirector 6.1 (Java Edition)

Finance Chart Track Line (Windows)




NOTE: This section describes Finance Chart Track Line for Windows applications only. For web applications, please refer to Finance Chart Track Line (Web).

This sample program demonstrates a track cursor for a finance chart programmed with the following features:

The code first draws the finance chart with the legend box set to transparent (so that the default legend box is not displayed). It then sets the track cursor to the right side of the plot area. This ensures the chart initially has a dynamic legend for the latest trading session. In the TrackCursorListener.mouseMovedPlotArea event handler, the track cursor is updated to reflect the mouse position.

The trackFinance method is the routine that draws the track cursor. Its key elements are:

Source Code Listing

[Java Version] javademo/trackfinance.java
import ChartDirector.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;

public class trackfinance implements DemoModule
{
    //
    // The main method to allow this demo to run as a standalone program.
    //
    public static void main(String args[]) 
    {
        trackfinance demo = new trackfinance();
        demo.display();
        System.exit(0);
    }

    //
    // Display the window
    //
    private void display() 
    {   
        // Use a JDialog as the window
           JDialog d = new JDialog((java.awt.Frame)null, toString(), true);
        d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        d.setResizable(false);     
   
        // Create the ChartViewer and add it to the JDialog
        ChartViewer viewer = new ChartViewer();
        d.getContentPane().add(viewer);

        // Add a movedMovedPlotArea event listener to draw the track cursor
        viewer.addTrackCursorListener(new TrackCursorAdapter() {
            public void mouseMovedPlotArea(MouseEvent e) {
                chartViewer1_MouseMovedPlotArea(e);
            }
        });
        
        // Draw the chart
        drawChart(viewer);

        // Layout and display the JDialog
        d.pack();
        d.setVisible(true);
    }
  
    //
    // Main code for creating charts
    //
    public void drawChart(ChartViewer viewer)
    {
        // Create a finance chart demo containing 100 days of data
        int noOfDays = 100;

        // To compute moving averages starting from the first day, we need to get extra data points before the
        // first day
        int extraDays = 30;

        // In this exammple, we use a random number generator utility to simulate the data. We set up the
        // random table to create 6 cols x (noOfDays + extraDays) rows, using 9 as the seed.
        RanTable rantable = new RanTable(9, 6, noOfDays + extraDays);

        // Set the 1st col to be the timeStamp, starting from Sep 4, 2011, with each row representing one day,
        // and counting week days only (jump over Sat and Sun)
        rantable.setDateCol(0, new GregorianCalendar(2011, 8, 4).getTime(), 86400, true);

        // Set the 2nd, 3rd, 4th and 5th columns to be high, low, open and close data. The open value starts
        // from 100, and the daily change is random from -5 to 5.
        rantable.setHLOCCols(1, 100, -5, 5);

        // Set the 6th column as the vol data from 5 to 25 million
        rantable.setCol(5, 50000000, 250000000);

        // Now we read the data from the table into arrays
        double[] timeStamps = rantable.getCol(0);
        double[] highData = rantable.getCol(1);
        double[] lowData = rantable.getCol(2);
        double[] openData = rantable.getCol(3);
        double[] closeData = rantable.getCol(4);
        double[] volData = rantable.getCol(5);

        // Create a FinanceChart object of width 720 pixels
        FinanceChart c = new FinanceChart(720);

        // Add a title to the chart
        c.addTitle("Finance Chart Demonstration");

        // Disable default legend box, as we are using dynamic legend
        c.setLegendStyle("normal", 8, Chart.Transparent, Chart.Transparent);

        // Set the data into the finance chart object
        c.setData(timeStamps, highData, lowData, openData, closeData, volData, extraDays);

        // Add the main chart with 240 pixels in height
        c.addMainChart(240);

        // Add a 10 period simple moving average to the main chart, using brown color
        c.addSimpleMovingAvg(10, 0x663300);

        // Add a 20 period simple moving average to the main chart, using purple color
        c.addSimpleMovingAvg(20, 0x9900ff);

        // Add candlestick symbols to the main chart, using green/red for up/down days
        c.addCandleStick(0x00ff00, 0xff0000);

        // Add 20 days bollinger band to the main chart, using light blue (9999ff) as the border and
        // semi-transparent blue (c06666ff) as the fill color
        c.addBollingerBand(20, 2, 0x9999ff, 0xc06666ff);

        // Add a 75 pixels volume bars sub-chart to the bottom of the main chart, using green/red/grey for
        // up/down/flat days
        c.addVolBars(75, 0x99ff99, 0xff9999, 0x808080);

        // Append a 14-days RSI indicator chart (75 pixels high) after the main chart. The main RSI line is
        // purple (800080). Set threshold region to +/- 20 (that is, RSI = 50 +/- 25). The upper/lower
        // threshold regions will be filled with red (ff0000)/blue (0000ff).
        c.addRSI(75, 14, 0x800080, 20, 0xff0000, 0x0000ff);

        // Append a MACD(26, 12) indicator chart (75 pixels high) after the main chart, using 9 days for
        // computing divergence.
        c.addMACD(75, 26, 12, 9, 0x0000ff, 0xff00ff, 0x008000);

        // Include track line with legend for the latest data values
        trackFinance(c, ((XYChart)c.getChart(0)).getPlotArea().getRightX());

        // Assign the chart to the ChartViewer
        viewer.setChart(c);
    }

    //
    // Draw track cursor when mouse is moving over plotarea
    //
    private void chartViewer1_MouseMovedPlotArea(MouseEvent e)
    {
        ChartViewer viewer = (ChartViewer)e.getSource();
        trackFinance((MultiChart)viewer.getChart(), viewer.getPlotAreaMouseX());
        viewer.updateDisplay();
    }

    //
    // Draw finance chart track line with legend
    //
    private void trackFinance(MultiChart m, int mouseX)
    {
        // Clear the current dynamic layer and get the DrawArea object to draw on it.
        DrawArea d = m.initDynamicLayer();

        // It is possible for a FinanceChart to be empty, so we need to check for it.
        if (m.getChartCount() == 0) {
            return ;
        }

        // Get the data x-value that is nearest to the mouse
        int xValue = (int)(((XYChart)m.getChart(0)).getNearestXValue(mouseX));

        // Iterate the XY charts (main price chart and indicator charts) in the FinanceChart
        XYChart c = null;
        for (int i = 0; i < m.getChartCount(); ++i) {
            c = (XYChart)m.getChart(i);

            // Variables to hold the legend entries
            String ohlcLegend = "";
            ArrayList legendEntries = new ArrayList();

            // Iterate through all layers to find the highest data point
            for (int j = 0; j < c.getLayerCount(); ++j) {
                Layer layer = c.getLayerByZ(j);
                int xIndex = layer.getXIndexOf(xValue);
                int dataSetCount = layer.getDataSetCount();

                // In a FinanceChart, only layers showing OHLC data can have 4 data sets
                if (dataSetCount == 4) {
                    double highValue = layer.getDataSet(0).getValue(xIndex);
                    double lowValue = layer.getDataSet(1).getValue(xIndex);
                    double openValue = layer.getDataSet(2).getValue(xIndex);
                    double closeValue = layer.getDataSet(3).getValue(xIndex);

                    if (closeValue != Chart.NoValue) {
                        // Build the OHLC legend
                        ohlcLegend = "Open: " + c.formatValue(openValue, "{value|P4}") + ", High: " +
                            c.formatValue(highValue, "{value|P4}") + ", Low: " + c.formatValue(lowValue,
                            "{value|P4}") + ", Close: " + c.formatValue(closeValue, "{value|P4}");

                        // We also draw an upward or downward triangle for up and down days and the % change
                        double lastCloseValue = layer.getDataSet(3).getValue(xIndex - 1);
                        if (lastCloseValue != Chart.NoValue) {
                            double change = closeValue - lastCloseValue;
                            double percent = change * 100 / closeValue;
                            String symbol = ((change >= 0) ?
                                "<*font,color=008800*><*img=@triangle,width=8,color=008800*>" :
                                "<*font,color=CC0000*><*img=@invertedtriangle,width=8,color=CC0000*>");

                            ohlcLegend = ohlcLegend + "  " + symbol + " " + c.formatValue(change, "{value|P4}"
                                ) + " (" + c.formatValue(percent, "{value|2}") + "%)<*/font*>";
                        }

                        // Use a <*block*> to make sure the line does not wrap within the legend entry
                        ohlcLegend = "<*block*>" + ohlcLegend + "      <*/*>";
                    }
                } else {
                    // Iterate through all the data sets in the layer
                    for (int k = 0; k < layer.getDataSetCount(); ++k) {
                        ChartDirector.DataSet dataSet = layer.getDataSetByZ(k);

                        String name = dataSet.getDataName();
                        double value = dataSet.getValue(xIndex);
                        if ((!(name == null || name == "")) && (value != Chart.NoValue)) {

                            // In a FinanceChart, the data set name consists of the indicator name and its
                            // latest value. It is like "Vol: 123M" or "RSI (14): 55.34". As we are generating
                            // the values dynamically, we need to extract the indictor name out, and also the
                            // volume unit (if any).

                            // The unit character, if any, is the last character and must not be a digit.
                            String unitChar = name.substring(name.length() - 1);
                            if (unitChar.compareTo("0") >= 0 && unitChar.compareTo("9") <= 0) {
                                unitChar = "";
                            }

                            // The indicator name is the part of the name up to the colon character.
                            int delimiterPosition = name.indexOf(":");
                            if (delimiterPosition != -1) {
                                name = name.substring(0, delimiterPosition);
                            }

                            // In a FinanceChart, if there are two data sets, it must be representing a range.
                            if (dataSetCount == 2) {
                                // We show both values in the range in a single legend entry
                                value = layer.getDataSet(0).getValue(xIndex);
                                double value2 = layer.getDataSet(1).getValue(xIndex);
                                name = name + ": " + c.formatValue(Math.min(value, value2), "{value|P3}") +
                                    " - " + c.formatValue(Math.max(value, value2), "{value|P3}");
                            } else {
                                // In a FinanceChart, only the layer for volume bars has 3 data sets for
                                // up/down/flat days
                                if (dataSetCount == 3) {
                                    // The actual volume is the sum of the 3 data sets.
                                    value = layer.getDataSet(0).getValue(xIndex) + layer.getDataSet(1
                                        ).getValue(xIndex) + layer.getDataSet(2).getValue(xIndex);
                                }

                                // Create the legend entry
                                name = name + ": " + c.formatValue(value, "{value|P3}") + unitChar;
                            }

                            // Build the legend entry, consist of a colored square box and the name (with the
                            // data value in it).
                            legendEntries.add("<*block*><*img=@square,width=8,edgeColor=000000,color=" +
                                Integer.toHexString(dataSet.getDataColor()) + "*> " + name + "<*/*>");
                        }
                    }
                }
            }

            // Get the plot area position relative to the entire FinanceChart
            PlotArea plotArea = c.getPlotArea();
            int plotAreaLeftX = plotArea.getLeftX() + c.getAbsOffsetX();
            int plotAreaTopY = plotArea.getTopY() + c.getAbsOffsetY();

            // The legend is formed by concatenating the legend entries.
            Collections.reverse(legendEntries);
            String legendText = Chart.stringJoin(legendEntries, "      ");

            // Add the date and the ohlcLegend (if any) at the beginning of the legend
            legendText = "<*block,valign=top,maxWidth=" + (plotArea.getWidth() - 5) + "*><*font=Arial Bold*>["
                 + c.xAxis().getFormattedLabel(xValue, "mmm dd, yyyy") + "]<*/font*>      " + ohlcLegend +
                legendText;

            // Draw a vertical track line at the x-position
            d.vline(plotAreaTopY, plotAreaTopY + plotArea.getHeight(), c.getXCoor(xValue) + c.getAbsOffsetX(),
                d.dashLineColor(0x000000, 0x0101));

            // Display the legend on the top of the plot area
            TTFText t = d.text(legendText, "Arial", 8);
            t.draw(plotAreaLeftX + 5, plotAreaTopY + 3, 0x000000, Chart.TopLeft);
        }
    }
    
    //
    // Implementation of the DemoModule interface to allow this demo to run inside the 
    // ChartDirectorDemo browser
    //
    
    // Name of demo program
    public String toString() 
    { 
        return "Finance Chart Track Line"; 
    }

    // Number of charts produced in this demo
    public int getNoOfCharts() 
    { 
        // This demo open its own dialog instead of using the right pane of the ChartDirectorDemo 
        // for display. We just display the dialog, then return 0.
        display();
        return 0; 
    }

    // Main code for creating charts
    public void createChart(ChartViewer viewer, int chartIndex)
    {
        // do nothing, as the ChartDirectorDemo browser right pane is not used
    }
}