Hi,
I am using chartdirector for displaying charts but it is causing some problem as I am not able to see candles in candlestick chart it is showing the chart as the same come as a output of OHLC chart but in back and white color.
My code is:-
public FinanceChart drawChart() {
if (indexName != null && indexName.length() < 1) {
indexName = null;
}
// The moving average periods selected by the user.
/*
* int avgPeriod1 = 0; try { avgPeriod1 =
* Integer.parseInt(request.getParameter("movAvg1")); } catch (Exception
* e) { avgPeriod1 = 0; } int avgPeriod2 = 0; try { avgPeriod2 =
* Integer.parseInt(request.getParameter("movAvg2")); } catch (Exception
* e) { avgPeriod2 = 0; }
*
* if (avgPeriod1 < 0) { avgPeriod1 = 0; } else if (avgPeriod1 > 300) {
* avgPeriod1 = 300; }
*
* if (avgPeriod2 < 0) { avgPeriod2 = 0; } else if (avgPeriod2 > 300) {
* avgPeriod2 = 300; }
*
* // We need extra leading data points in order to compute moving
* averages.
*
* if (avgPeriod1 > extraPoints) { extraPoints = avgPeriod1; } if
* (avgPeriod2 > extraPoints) { extraPoints = avgPeriod2; }
*/
// Get the data series to compare with, if any.
/*
* String compareKey = request.getParameter("CompareWith").trim();
* compareData = null; if (getData(compareKey, startDate, endDate,
* durationInDays, extraPoints)) { companyChart(compareKey); compareData
* = closeData; }
*/
// The data series we want to get.
// String tickerKey = request.getParameter("TickerSymbol").trim();
// In some finance chart presentation style, even if the data for the
// latest day
// is not fully available, the axis for the entire day will still be
// drawn, where
// no data will appear near the end of the axis.
if (resolution < 86400) {
// Add extra points to the axis until it reaches the end of the day.
// The end of day is assumed to be 16:00 (it depends on the stock
// exchange).
GregorianCalendar lastTime = new GregorianCalendar();
lastTime.setTime(chartData.getDates()[chartData.getDates().length - 1]);
int extraTrailingPoints = (int) ((16 * 3600 - lastTime.get(Calendar.HOUR_OF_DAY) * 3600 - lastTime.get(Calendar.MINUTE) * 60 - lastTime.get(Calendar.SECOND)) / resolution);
if (extraTrailingPoints > 0) {
Date[] extendedTimeStamps = new Date[chartData.getDates().length + extraTrailingPoints];
System.arraycopy(chartData.getDates(), 0, extendedTimeStamps, 0, chartData.getDates().length);
for (int i = 0; i < extraTrailingPoints; ++i) {
lastTime.add(Calendar.SECOND, resolution);
extendedTimeStamps[i + chartData.getDates().length] = (Date) lastTime.getTime().clone();
}
/**** Parvesh commented as this part is not going to be used */
// timeStamps = extendedTimeStamps;
}
}
int width, mainHeight;
if (_fullview == false) {
width = 815;
mainHeight = 600;
} else {
width = 1000;
mainHeight = 500;
}
int indicatorHeight = 100;
/*
* String size = request.getParameter("ChartSize"); if
* (size.equals("S")) { // Small chart size width = 450; mainHeight =
* 160; indicatorHeight = 60; } else if (size.equals("M")) { // Medium
* chart size width = 620; mainHeight = 215; indicatorHeight = 70; }
* else if (size.equals("H")) { // Huge chart size width = 1000;
* mainHeight = 320; indicatorHeight = 90; }
*/
// Create the chart object using the selected size
FinanceChart m = new FinanceChart(width);
// Set the data into the chart object
m.setData(chartData.getDates(), chartData.getHigh(), chartData.getLow(), chartData.getOpen(), chartData.getClose(), chartData.getVol(), extraPoints);
//
//
// We configure the title of the chart. In this demo chart design, we
// put the company name as the top line of the title with left
// alignment.
//
TextBox copyRight;
if (Suggest != null && Suggest.length() > 0) {
copyRight = m.addPlotAreaTitle(Chart.TopLeft, Suggest);
copyRightCoor = copyRight.getImageCoor();
} else if (indexName != null)
m.addPlotAreaTitle(Chart.TopLeft, indexName);
// We displays the current date as well as the data resolution on the
// next line.
String resolutionText = "";
if (resolution == 30 * 86400) {
resolutionText = "Monthly";
} else if (resolution == 7 * 86400) {
resolutionText = "Weekly";
} else if (resolution == 86400) {
resolutionText = "Daily";
} else if (resolution == 900) {
resolutionText = "15-min";
}
m.setMargins(0, 30, 40, 30);
m.addPlotAreaTitle(Chart.BottomLeft, "<*font=Arial,size=8*>" + m.formatValue(new Date(), "mmm dd, yyyy") + " - " + resolutionText + " chart");
// A copyright message at the bottom left corner the title area
m.addPlotAreaTitle(Chart.BottomRight, "<*font=Arial,size=8*>(c) HamaraKuber LLC");
;
//
// Add the first techical indicator according. In this demo, we draw the
// first
// indicator on top of the main chart.
//
if (indicatorFirst != null)
addIndicator(m, indicatorFirst, indicatorHeight);
//
// Add the main chart
//
// Set log or linear scale according to user preference
//
/*
* if (request.getParameter("LogScale").equals("1")) {
* m.setLogScale(true); }
*
* // // Set axis labels to show data values or percentage change to
* user preference // if
* (request.getParameter("PercentageScale").equals("1")) {
* m.setPercentageAxis(); }
*/
//
// Draw any price line the user has selected
//
// String mainType = request.getParameter("ChartType");
XYChart myMainPriceChartEvent = m.addMainChart(mainHeight);
if (chartType.equals("Close")) {
m.addCloseLine(0x000040);
} else if (chartType.equals("TP")) {
m.addTypicalPrice(0x000040);
} else if (chartType.equals("WC")) {
m.addWeightedClose(0x000040);
} else if (chartType.equals("Median")) {
m.addMedianPrice(0x000040);
}
// m.addVolBars(100, 0x99ff99, 0xff9999, 0xc0c0c0);
// Parvesh 09/07 if (indexName == null || indexName.length()<1)
m.addVolIndicator(indicatorHeight, 0x336699, 0xCC3300, 0xc0c0c0);
// 0xCC3300 for low volume
// Add comparison line if there is data for comparison
//
// int _companyIdCompare =
// NavigationBean.fetchCompanyId(SuggestCompare);
if (chartDataCompare != null) {
// TODO we need to test this logic with companies having price for
// different time ranges, take a company which has recently been
// listed
Date[] closeDates = chartData.getDates();
double[] compareClose = new double[closeDates.length];
for (int i = 0, j = 0; i < chartDataCompare.getDates().length; i++, j++) {
Date currentDate = chartDataCompare.getDates()[i];
Date mainDate = closeDates[j];
if (mainDate.before(currentDate)) {
while (mainDate.before(currentDate)) {
compareClose[j] = chartDataCompare.getClose()[i];
j++;
mainDate = closeDates[j];
}
} else if (mainDate.after(currentDate)) {
j = j - 1;
continue;
}
compareClose[j] = chartDataCompare.getClose()[i];
}
m.addComparison(compareClose, 0x0000ff, SuggestCompare);
/**
* if (chartDataCompare.getDates().length > closeDates.length) { int
* diff = chartDataCompare.getDates().length - closeDates.length;
*
* System.arraycopy(chartDataCompare.getClose(), diff-1,
* compareClose, 0,points); m.addComparison(compareClose, 0x0000ff,
* SuggestCompare);
*
* }else { logger.info(
* "Compare company has less points then main company:"
* +SuggestCompare+":"+Suggest); }
*/
}
// String movingRange,String movingRangeSecond,
// Add moving average lines.
// int movAvg,int movAvgSecond
if (movingRange != null)
addMovingAvg(myMainPriceChartEvent, m, chartData, movingRange, movAvg, 0x9900FF);
if (movingRangeSecond != null)
addMovingAvg(myMainPriceChartEvent, m, chartData, movingRangeSecond, movAvgSecond, 0x009999);
if (movingRangeThird != null)
addMovingAvg(myMainPriceChartEvent, m, chartData, movingRangeThird, movAvgThird, 0x666600);
if (movingRangeFourth != null)
addMovingAvg(myMainPriceChartEvent, m, chartData, movingRangeFourth, movAvgFourth, 0x006666);
if (movingRangeFive != null)
addMovingAvg(myMainPriceChartEvent, m, chartData, movingRangeFive, movAvgFive, 0x66CCCC);
// Draw candlesticks or OHLC symbols if the user has selected them.
//
if (chartType.equals("CandleStick")) {
m.addCandleStick(0x33ff33, 0xff3333);
} else if (chartType.equals("OHLC")) {
m.addHLOC(0x008800, 0xcc0000);
}
//
// Add parabolic SAR if necessary
//
// if (request.getParameter("ParabolicSAR").equals("1")) {
// m.addParabolicSAR(0.02, 0.02, 0.2, Chart.DiamondShape, 5, 0x008800,
// 0x000000)
;
// }
//
// Add price band/channel/envelop to the chart according to user
// selection
//
if (bandType != null) {
if (bandType.equals("BB")) {
m.addBollingerBand(20, 2, 0x9999ff, 0xc06666ff);
} else if (bandType.equals("DC")) {
m.addDonchianChannel(20, 0x9999ff, 0xc06666ff);
} else if (bandType.equals("Envelop")) {
m.addEnvelop(20, 0.1, 0x9999ff, 0xc06666ff);
}
}
//
// Add volume bars to the main chart if necessary
//
/*
* if (request.getParameter("Volume").equals("1")) {
* m.addVolBars(indicatorHeight, 0x99ff99, 0xff9999, 0xc0c0c0); }
*/
//
// Add additional indicators as according to user selection.
//
if (indicatorSecond != null)
addIndicator(m, indicatorSecond, indicatorHeight);
if (indicatorThird != null)
addIndicator(m, indicatorThird, indicatorHeight);
// m.layout();
// addIndicator(m, request.getParameter("Indicator4"), indicatorHeight);
if (eventInformation != null) {
ScatterLayer layer = myMainPriceChartEvent.addScatterLayer(null, eventInformation, "", Chart.CircleShape, 16, 0x0066CC);
layer.moveFront();
layer.addExtraField(eventTooltip);
// show as tooltips
layer.setHTMLImageMap("", "", "title='{field0}'");
}
return m;
}
I am attaching the screenshot of that chart also.
Thanks in advance |