Hi Rapper,
The issue is because your code does not provide the x-coordinates for the trend layer. To solve the problem, please modify:
c2.addTrendLayer(closeData, 0x008000, "Trend Line").setLineWidth(2)
to:
c2.addTrendLayer2(lst, closeData, 0x008000, "Trend Line").setLineWidth(2)
If you do not provide the x-coordinates, the x-coordinates will be assumed to be the array index 0, 1, 2, 3 , ... So in your chart, the line layer will have very large x-coordinates like "6.364777e+10", while the trend layer will have small x-coordinates 0, 1, 2, 3, .... By default, the x-axis is scaled to include all the data. So the x-axis scale must be something like 0 to 70000000000, and all the points of your line layer will all be around x = 6.364e+10.
In brief, in ChartDirector, if no x-coordinate is provided, the x-coordinates are implicitly 0, 1, 2, 3.... If you set the x-axis labels using Axis.setLabels, the labels will be at x = 0, 1, 2, 3, ... too. So the label will match with the data points. Note that both the data points and the labels will be equally spaced in the x-direction, as their coordinates are equally spaced (0, 1, 2, 3, ...). The labels are only for human reading.
If x-coordinates are provided, ChartDirector will auto-scale the x-axis to include the data range (or you can use Axis.setDateScale or Axis.setLinearScale to specify the scale). The data points will be positioned using the x-coordinates. For example, if the x-coordinates are 10, 20, 30, 50, the first 3 points will be equally spaced in the x-direction, while the last point will have double the spacing.
In many financial charts, all data points are plotted as equally spaced in the x-direction, regardless of whether they are equally spaced in date/time. For example, non-trading days (Saturdays and Sundays, national holidays, non-trading days due to unpredictable natural phenomena such as hurricane or typhoon, etc) are skipped and will not result in a larger spacing between the data points. So it is more common to use x-labels instead of x-coordinates for financial charts.
Regards
Peter Kwan |