Skip to content

Commit

Permalink
chartsDrawer: trendLine class extended for logarithmic types
Browse files Browse the repository at this point in the history
  • Loading branch information
ansaraidarbek committed Oct 10, 2023
1 parent 6618410 commit ac78326
Showing 1 changed file with 147 additions and 31 deletions.
178 changes: 147 additions & 31 deletions common/Charts/ChartsDrawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16139,6 +16139,58 @@ CColorObj.prototype =
}
},

_calculateLine: function (oSeries, coordinates) {
const lineGenerator = new Cline(coordinates);
lineGenerator.setState(oSeries.trendline.trendlineType);
if (lineGenerator.currentState) {
lineGenerator.createPath(oSeries, coordinates, this.cChartDrawer);
}
},

draw: function () {
// draw paths
let plotArea = this.cChartDrawer.cChartSpace.chart.plotArea;
if (this.coordinates) {
for (let i in this.coordinates) {
let oChart = this.cChartDrawer._getChartModelById(plotArea, i);
if (!oChart || !this.coordinates[i]) {
continue;
}
for (let j in this.coordinates[i]) {
let oSeries = this.cChartDrawer._getSeriesById(oChart, j);
if (!oSeries || !this.coordinates[i][j]) {
continue;
}
let pen = oSeries.trendline.spPr.ln;
if (pen && this.coordinates[i][j].path) {
this.cChartDrawer.drawPath(this.coordinates[i][j].path, pen);
}
}
}
}
}
}

function Cline (coordinates) {
this.currentState = null;
this.states = {
1: new Clinear(),
2: new Clogarithmic()
};
}

Cline.prototype = {

constructor: Cline,

setState: function (key) {
this.currentState = this.states[key]
},

createPath: function (oSeries, coordinates, cChartDrawer) {
this.currentState.createPath(oSeries, coordinates, cChartDrawer, this._findSuppletiables);
},

_findSuppletiables: function (coordinates) {
// meanX = ∑x/n and meanY = ∑y/n
// m = Sxy/Sxx where Sxy = ∑(x-meanX)(y-meanY) and Sxx = ∑(x-meanX)^2
Expand Down Expand Up @@ -16177,17 +16229,25 @@ CColorObj.prototype =
_calculateB(letiables, means);

return letiables;
},
}
}

_calculateLine: function (oSeries, coordinates) {
let letiables = this._findSuppletiables(coordinates);
function Clinear(){

}

Clinear.prototype = {
constructor: Clinear,

createPath: function (oSeries, coordinates, cChartDrawer, findSuppletiables) {
let letiables = findSuppletiables(coordinates);

const oChart = oSeries.parent;
const xAxis = this.cChartDrawer.getAxisFromAxId(oChart.axId, AscDFH.historyitem_type_CatAx);
const yAxis = this.cChartDrawer.getAxisFromAxId(oChart.axId, AscDFH.historyitem_type_ValAx);
const xAxis = cChartDrawer.getAxisFromAxId(oChart.axId, AscDFH.historyitem_type_CatAx);
const yAxis = cChartDrawer.getAxisFromAxId(oChart.axId, AscDFH.historyitem_type_ValAx);

let pathH = this.cChartDrawer.calcProp.pathH;
let pathW = this.cChartDrawer.calcProp.pathW;
let pathH = cChartDrawer.calcProp.pathH;
let pathW = cChartDrawer.calcProp.pathW;

let chartletiables = letiables;
if (chartletiables) {
Expand All @@ -16212,42 +16272,98 @@ CColorObj.prototype =
return result
}

const start = lineCoordinate(0, this.cChartDrawer)
const end = lineCoordinate(xAxis.xPoints.length - 1, this.cChartDrawer)
const start = lineCoordinate(0, cChartDrawer)
const end = lineCoordinate(xAxis.xPoints.length - 1, cChartDrawer)

let pathId = this.cChartDrawer.cChartSpace.AllocPath();
let path = this.cChartDrawer.cChartSpace.GetPath(pathId);
let pathId = cChartDrawer.cChartSpace.AllocPath();
let path = cChartDrawer.cChartSpace.GetPath(pathId);

path.moveTo(start.xPos * pathW, start.yPos * pathH);
path.lnTo(end.xPos * pathW, end.yPos * pathH);

coordinates.path = pathId;
}
},
}
}

draw: function () {
// draw paths
let plotArea = this.cChartDrawer.cChartSpace.chart.plotArea;
if (this.coordinates) {
for (let i in this.coordinates) {
let oChart = this.cChartDrawer._getChartModelById(plotArea, i);
if (!oChart || !this.coordinates[i]) {
continue;
}
for (let j in this.coordinates[i]) {
let oSeries = this.cChartDrawer._getSeriesById(oChart, j);
if (!oSeries || !this.coordinates[i][j]) {
continue;
}
let pen = oSeries.trendline.spPr.ln;
if (pen && this.coordinates[i][j].path) {
this.cChartDrawer.drawPath(this.coordinates[i][j].path, pen);
}
}
function Clogarithmic(){
this.xAxis = null;
this.yAxis = null;
}

Clogarithmic.prototype = {
constructor: Clogarithmic,

createPath: function (oSeries, coordinates, cChartDrawer, findSuppletiables) {
for (let i = 0; i < coordinates.coords.length; i++){
coordinates.coords[i].xVal = Math.log(coordinates.coords[i].xVal)
}
let letiables = findSuppletiables(coordinates);
const oChart = oSeries.parent;
this.xAxis = cChartDrawer.getAxisFromAxId(oChart.axId, AscDFH.historyitem_type_CatAx);
this.yAxis = cChartDrawer.getAxisFromAxId(oChart.axId, AscDFH.historyitem_type_ValAx);

let pathH = cChartDrawer.calcProp.pathH;
let pathW = cChartDrawer.calcProp.pathW;

let chartletiables = letiables;
if (chartletiables && this.xAxis && this.yAxis) {

const midPointsNum = 100;
const start = this._lineCoordinate(0, cChartDrawer, chartletiables);
const end = this._lineCoordinate(this.xAxis.xPoints.length - 1, cChartDrawer, chartletiables);
const mid = this._findMidCoordinates(midPointsNum, start.xVal, end.xVal, cChartDrawer, chartletiables);

let pathId = cChartDrawer.cChartSpace.AllocPath();
let path = cChartDrawer.cChartSpace.GetPath(pathId);

path.moveTo(start.xPos * pathW, start.yPos * pathH);
for (let i = 0; i<midPointsNum; i++) {
path.lnTo(mid[i].xPos * pathW, mid[i].yPos * pathH);
}
path.lnTo(end.xPos * pathW, end.yPos * pathH);

coordinates.path = pathId;
}
},

_lineCoordinate: function (xIndex, cChartDrawer, chartletiables) {
const result = {xPos:null, yPos:null, xVal:null};
let xVal = Math.log(this.xAxis.xPoints[xIndex].val);
const yVal = chartletiables[0] * xVal + chartletiables[1];
const yIndex = this.yAxis.yPoints.length - 1;
if (yVal < this.yAxis.yPoints[0].val) {
const newXVal = (this.yAxis.yPoints[0].val - chartletiables[1]) / chartletiables[0];
result.xVal = newXVal;
result.xPos = cChartDrawer.getYPosition(Math.exp(newXVal), this.xAxis);
result.yPos = this.yAxis.yPoints[0].pos;
} else if (yVal > this.yAxis.yPoints[yIndex].val) {
const newXVal = (this.yAxis.yPoints[yIndex].val - chartletiables[1]) / chartletiables[0];
result.xVal = newXVal;
result.xPos = cChartDrawer.getYPosition(Math.exp(newXVal), this.xAxis);
result.yPos = this.yAxis.yPoints[yIndex].pos
} else {
result.xPos = this.xAxis.xPoints[xIndex].pos;
result.yPos = cChartDrawer.getYPosition(yVal, this.yAxis);
result.xVal = xVal;
}
return result
},

_findMidCoordinates: function (pointsNumber, xValStart, xValEnd, cChartDrawer, chartletiables) {
const mid=[]
const xNet = xValEnd-xValStart;
for (let i =1; i<=pointsNumber; i++) {
const xVal = (xNet / (pointsNumber + 1)) * i + xValStart;
const yVal = chartletiables[0] * xVal + chartletiables[1];
const yPos = cChartDrawer.getYPosition(yVal, this.yAxis);
const xPos = cChartDrawer.getYPosition(Math.exp(xVal), this.xAxis);
mid.push({xPos:xPos, yPos:yPos})
}
return mid
}
}



//----------------------------------------------------------export----------------------------------------------------
Expand Down

0 comments on commit ac78326

Please sign in to comment.