
How to extend a trend Line to the right side for Finance chart 
Posted by Jianhua on Mar312019 17:45 

Hi Peter,
I'm using Java Version ChartDirector. I have use the DrawArea function to draw a custom trend line on the finance chart. But It only connectes the two points that I choose. How could the trend line be extended to the right side of the plot area edge? I have attached a png file. Could you provide some code sample?
I hope to hear from you soon.
Regards,
Jianhua

Re: How to extend a trend Line to the right side for Finance chart 
Posted by Peter Kwan on Apr012019 02:00 

Hi Jianhua,
I think this is a mathematical problem. From coordinate geometry, the formula is:
//Let (x1, y1) and (x2, y2) be the two points
int leftX = mainChart.getPlotArea().getLeftX();
int rightX = mainChart.getPlotArea().getRightX();
int leftY = (y2  y1) * (rightX  x2) / (x2  x1) + y2;
int rightY = (y2  y1) * (leftX  x1) / (x2  x1) + y1;
Now you can draw the line using (leftX, leftY) and (rightX, rightY).
Note that leftY and rightY can go outside the plot area (above the top edge or below the bottom edge). So you need to set up a clip rectangle like in the code in my previous message.
Regards
Peter Kwan 
Re: How to extend a trend Line to the right side for Finance chart 
Posted by Jianhua on Apr012019 09:39 

Hi Peter,
Thanks a lot for your reply! I tried your method and the calculation, however it seems not work. The trend line does not draw as the desired direction. please see the attached PNG file. The red color circles are the two points I clicked and choosed on the finance chart, but the trend line drawing is not even connect to my two points. By the way, I only wanted the trend line extend to the right side , I don't want the trend line extends to the left side. How could I calculate the coordinate?
This is my trend line draw method code:
private void drawTrendLine(XYChart mainChart) {
DrawArea d = mainChart.makeChart3();
double xValue1=fibXValueList.get(0)startIndex;
double xValue2=fibXValueList.get(1)startIndex;
double yValue1=fibYValueList.get(0);
double yValue2=fibYValueList.get(1);
int lineXcoor1=mainChart.getXCoor(xValue1);
int lineXcoor2=mainChart.getXCoor(xValue2);
int lineYcoor1=mainChart.getYCoor(yValue1);
int lineYcoor2=mainChart.getYCoor(yValue2);
int leftX = mainChart.getPlotArea().getLeftX();
int rightX = mainChart.getPlotArea().getRightX();
int leftY = (lineYcoor2  lineYcoor1) * (rightX  lineXcoor2) / (lineXcoor2  lineXcoor1) + lineYcoor2;
int rightY = (lineYcoor2  lineYcoor1) * (leftX  lineXcoor1) / (lineXcoor2  lineXcoor1) + lineYcoor1;
d.line(leftX, leftY, rightX,rightY,0x0000FF,3);
d.circle(lineXcoor1,lineYcoor1, 5, 5, 0x0000FF, 0xFF000000);
}
Your service and support is great.
I hope to hear from you soon.
Regards,
Jianhua

Re: How to extend a trend Line to the right side for Finance chart 
Posted by Peter Kwan on Apr012019 22:36 

Hi Jianhua,
Sorry, there is a typo mistake.
int leftY = (y2  y1) * (rightX  x2) / (x2  x1) + y2;
int rightY = (y2  y1) * (leftX  x1) / (x2  x1) + y1;
should :
int rightY = (y2  y1) * (rightX  x2) / (x2  x1) + y2;
int leftY = (y2  y1) * (leftX  x1) / (x2  x1) + y1;
The rightX is used to compute rightY, and the leftX is used to compute leftY.
Regards
Peter Kwan 
Re: How to extend a trend Line to the right side for Finance chart 
Posted by Jianhua on Apr022019 09:21 

Hi Peter,
Thanks a lot for your help! I modified your last code and it works. Thank you very much!
Regards,
Jianhua 
