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

Message ListMessage List     Post MessagePost Message

  Timestamp values not working for Python Chart
Posted by Bryan on Apr-12-2018 13:51
test

  Re: Timestamp values not working for Python Chart
Posted by rapper man on Apr-12-2018 14:06
Attachments:
HI there
I have attached my Python 3.6 script to generate a trend line chart. I have used the combo trendline.py and mysite example of django_viewportcontrol.zip from
http://www.chartdir.com/forum/download_thread.php?bn=chartdir_support&pattern=&thread=1204518493

I have verified the timeStamps which seems correctly created which are floats.
If I remove the line 'lineLayer.setXData(lst)", the chart gets generated correctly with dates.  When I add the line, there is a large red column in the chart which I attached. How do I correct this for the proper dates to be formatted along the X axis.

Here is a sample ChartTime column values of df which is the source for dates in the X axis.

              ChartTime
2017-12-02  6.364777e+10
2017-12-03  6.364786e+10
2017-12-04  6.364794e+10
2017-12-05  6.364803e+10
2017-12-06  6.364812e+10
2017-12-07  6.364820e+10
2017-12-08  6.364829e+10
2017-12-09  6.364837e+10
2017-12-10  6.364846e+10

Thanks
c2.png
trendtest.py
trendtest.py

1.92 Kb

  Re: Timestamp values not working for Python Chart
Posted by Peter Kwan on Apr-12-2018 15:24
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