ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  FinanceChart - PHP - Many questions
Posted by Val D. on Jun-11-2009 19:02
Attachments:
Hi everyone,

I am using a demo version of ChartDirector (PHP) v.5 and consider purchasing a license; I am still trying to get used with the program

The Goal:
Retrieve stock data from mysql and display it as a chart with the following:
- a horizontal line from a date from the middle of the chart to the right end of the chart at a certain price
- a rectangle (as shown on Bubble XY Scaling demo) originating from a date to the right end of the chart between two prices
- a trend line
- hide the top legend of the chart that contains the Open, High, Low, etc, and if it cannot be hidden how I can format it
- can I retrieve the last value of an indicator from the chart as a variable which can be used on another part of the page?
- an arrow at a certain date
- the horizontal axis formatted properly per Finance Chart (1) or (2) example

Below please see the code made so far; if you could expand on it to accomplish the above I would appreciate it

======================================

$chartDataResult = mysql_query ("SELECT date,open,high,low,close,volume FROM ADSK_daily WHERE date >= '20081001' AND date <= '20090701' ORDER BY date") or die(mysql_error());
$chartNumberOfDays = mysql_num_rows($chartDataResult);
while ($chartDataRow = mysql_fetch_row($chartDataResult))
{
    $theDate[] = str_replace("-","",$chartDataRow[0]);
    $theChartDate[] = chartTime(str_replace("-","",$chartDataRow[0]));

    $theOpen[] = $chartDataRow[1];
    $theHigh[] = $chartDataRow[2];
    $theLow[] = $chartDataRow[3];
    $theClose[] = $chartDataRow[4];
    $theVolume[] = $chartDataRow[5];
}

//print_r($theDate);
//print_r($theChartDate);
$chartWidth = 640;
$chartHight = 400;
$rightMargin = 40;
$extraDays = 30;


# Create a FinanceChart object of width 640 pixels
$c = new FinanceChart($chartWidth);
# Set the data into the finance chart object
$c->setData($theChartDate, $theHigh, $theLow, $theOpen, $theClose, $theVolume, $extraDays);
# add line layer
$o = $c->addMainChart($chartHight);
# Add an HLOC symbols to the main chart, using green/red for up/down days
$c->addCandleStick(0x00ff00, 0xff0000);
# Freeze chart and auto-scale axis
$c->layout();

# DRAW LINE
$lineDate = "20090421";
#convert date
function convertDateToChartTime($date)
{
    $y = date('Y', strtotime("$date"));
    $d = date('d', strtotime("$date"));
    $m = date('m', strtotime("$date"));
    return chartTime($y, $m, $d);
}
$chartLineDate = convertDateToChartTime($lineDate);
//calculate X coordinates in pixels
function getPositionX($theChartDate,$chartLineDate,$chartWidth,$chartNumberOfDays)
{
    while ($pos = current($theChartDate))
    {
        if ($pos == $theChartDate)
        {
            return key($theChartDate) * $chartWidth / $chartNumberOfDays;
        }
        next($theChartDate);
    }
}
$xCoor = getPositionX($theChartDate,$chartLineDate,$chartWidth,$chartNumberOfDays);
//get Y coordinates based on price
$yCoor = $o->getYCoor(17.25);

#add horizontal line
$o->addLine($xCoor, $yCoor, $chartWidth, $yCoor, 0xff8888, 2);

# Output the chart
header("Content-type: image/png");
print($c->makeChart2(PNG));

======================================

Also attached here please find a picture of how the chart looks right now>
Thank you for your time,
Val
BadChart.jpg

  Re: FinanceChart - PHP - Many questions
Posted by Peter Kwan on Jun-12-2009 04:08
Hi Val,

I suggest the following changes: (Note that as I cannot run your code, the changes below are not tested. You may need to debug them.)

(1) The date in your code probably are not dates to PHP or to ChartDirector. For example, "20090421" is a text string. You can also say it is an integer (according to PHP syntax), but not a date.

I suggest you obtain your dates from your database using:

SELECT UNIX_Timestamp(date), .....

In the while loop, please use:

$theChartDate[] = chartTime2($chartDataRow[0]);

I think after the above, the x-axis would be labelled correctly.

(2) To do what you need, you would need to know the x-coordinate system of the chart. The x-coordinates of a finance chart are trading sessions. In ChartDirector, it is numbers as 0, 1, 2, 3, .....

For example, for a horizontal line from a date from the middle of the chart to the right end of the chart (I assume you mean the right end of the plot area) at a certain price, the code I would suggest (assume PHP 5.x):

$c->layout();

$theTradingSession = 25;  #assume you want to plot from the 25th trading session
$xCoor = $o->getXCoor($theTradingSession);
$yCoor = $o->getYCoor($thePrice);
$o->addLine($xCoor, $yCoor, $c->getPlotArea()->getLeftX() + $c->getPlotArea()->getWidth(), $yCoor, 0xff8888, 2);

For a rectangle, the x and y coordinates can be obtained using similar method as above, and the code is:

$t = $o->addText($leftX, $topY, "");
$t->setSize($rightX - $leftX, $bottomY - $topY);
$t->setBackground($fillColor, $borderColor);

The same addLine method can be used to a trend line.

To hide the legend, please use:

$o->getLegend()->setPos(-9999, -9999);

For retrieving the values of an indicator, because ChartDirector is not designed as a financial calculator, the API does not expose the internal financial computation results. However, the FinanceChart library is open source (its source code is in FinanceChart.php). So you may modify the library so that it can return the result, or you may copy the code from the library to your own code to compute the result.

To add an arrow at a certain date, it depends on the nature of the arrow. If you just need one arrow, you may add using:

$t = $o->addText($leftX, $topY, "<*img=/file/path/of/arrow.png*>")->setAlignment(Center); #or you may use other alignment

Hope this can help.

Regards
Peter Kwan

  Re: FinanceChart - PHP - Many questions
Posted by Val D. on Jun-12-2009 09:32
Hi Peter,

Thank you for taking the time to reply; I will try your suggestions.

Val

  Re: FinanceChart - PHP - Many questions
Posted by Val D. on Jun-13-2009 01:44
Attachments:
Few more questions if you don't mind.
I browsed the forum and I cant seem to find an answer to my final steps; also please see the attached picture

1. I would like to have the grid dotted or dashed (I don't know how to apply the DashLine function properly)
2. I was able to draw lines and rectangles on the chart but they are positioned in front of the candles; I would prefer to have them behind; best yet, to have the candlesticks in front of all indicators and lines if possible
3. I added text on yAxis to show the last close price but it gets cutoff and I am not able to set the yAxis wider
4. The xAxis shows dates in this format: Nov Jan09 Mar May. I would prefer to dont have the day shown, just the month name
5. The volume overlaps in many cases the candlesticks; I would prefer to have them shown below the candlesticks and format the volume values to be consistent with the font and color used for date & price axis

Once again thank you for your support.
Val
chart.jpg

  Re: FinanceChart - PHP - Many questions
Posted by Peter Kwan on Jun-13-2009 20:12
Hi Val D.,

1. For the grid lines, you may use PlotArea.setGridColor for the chart you want to have the grid changed.

For example, if you want the grid style to change in the main price chart, you may use:

$m = $c->addMainChart(240);
$m->getPlotArea()->setGridColor($m->dashLineColor(0xcccccc, DashLine), $m->dashLineColor(0xcccccc, DashLine));

2. You may use Box.setZOrder and Line.setZOrder to control whether the lines added with addLine and boxes added with addText are in front or behind the other chart contents. Pleaser refer to the documentation on Box.setZOrder and Line.setZOrder for details.

3. You may use FinanceChart.setMargins to set the margins.

4. You may use FinanceChart.setDateLabelFormat to control the axis label formats.

5. Instead of using addVolBars, you may change it to addVolIndicator. For the axis font style, I am not sure which method you are using the configure the axis label fonts in your chart. I suggest to use FinanceChart.setYAxisScale, which applies to all y-axis.

$c->setYAxisStyle("arialbd.ttf", 10, 0x888888, 14);

Hope this can help.

Regards
Peter Kwan