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

Message ListMessage List     Post MessagePost Message

  Add TrendLine in a CharXY with View Port
Posted by Salvador de Lira on Sep-06-2019 06:32
Hi, I just start some months ago with chartdirector, I have been trying to find a way How to include a trendline in a chart xy with a viewport, it is posible?.
My code in C# is as below.

//////////////////////////////////////////////////////////////////////////////////////
XYChart c = new XYChart(wcvPExplorer.Width, wcvPExplorer.Height, 0xf5f5f5, 0x000000, 1);

DateTime[] viewPortTimeStamps = (DateTime[])Chart.arraySlice(timeStamps, startIndex, noOfPoints);
double[] viewPortDataSerie = (double[])Chart.arraySlice(Activity, startIndex, noOfPoints);

LineLayer layer = c.addLineLayer2();
                    layer = c.addLineLayer2();
                    layer.setLineWidth(lineSize);
                    layer.setFastLineMode();
                    layer.setXData(viewPortTimeStamps);
layer.addDataSet(viewPortDataSerie, c.dashLineColor(lineColor, lineType), trenName).setDataSymbol(markType, markSize, markColor);
                    TrendLayer trendLayer = c.addTrendLayer2(null, viewPortDataSerie, 0x008000, "Trend Line");
                    trendLayer.setLineWidth(2);
                    trendLayer.setRegressionType(0);
                    trendLayer.addConfidenceBand(0.90, unchecked((int)0x806666ff));


viewer.syncDateAxisWithViewPort("x", c.xAxis());

            viewer.Chart = c;
            viewer.updateDisplay();

//////////////////////////////////////////////////////////////////////////////////////
I really apreciate your help

  Re: Add TrendLine in a CharXY with View Port
Posted by Peter Kwan on Sep-06-2019 13:40
Hi Salvador,

Yes, it is possible. Your code would need to provide x-coordinates for the TrendLayer, just like your code provides the x-coordinates for the LineLayer. The addTrendLayer2 accepts both x and y arrays, but both arrays are declared as "double[]", so it cannot directly use DateTime[]. There are two methods to pass DateTime[] to the trend layer:

(a) Just use the same method as the LineLayer (Layer.setXData)

TrendLayer trendLayer = c.addTrendLayer(viewPortDataSerie, 0x008000, "Trend Line");
trendLayer->setXData(viewPortTimeStamps);  // x-coordinates

(b) For any ChartDirector API, if your array is DateTime[], but the API supports only double[], you can use Chart.CTime to convert the DateTime[] to double[]:

TrendLayer trendLayer = c.addTrendLayer2(Chart.CTime(viewPortTimeStamps), viewPortDataSerie, 0x008000, "Trend Line");

Hope this can help.

Regards
Peter Kwan