[483] | 1 | define([ |
---|
| 2 | "dojo/_base/declare", |
---|
| 3 | "dojo/_base/lang", |
---|
| 4 | "dojo/_base/window", |
---|
| 5 | "dojo/dom-construct", |
---|
| 6 | "dojo/dom-class", |
---|
| 7 | "dojo/dom-style", |
---|
| 8 | "dijit/_WidgetBase", |
---|
| 9 | "dijit/_WidgetsInTemplateMixin", |
---|
| 10 | "dijit/_TemplatedMixin", |
---|
| 11 | "dojox/math/_base", |
---|
| 12 | "dijit/registry", |
---|
| 13 | "dijit/form/DropDownButton", |
---|
| 14 | "dijit/TooltipDialog", |
---|
| 15 | "dijit/form/TextBox", |
---|
| 16 | "dijit/form/CheckBox", |
---|
| 17 | "dijit/ColorPalette", |
---|
| 18 | "dojox/charting/Chart", |
---|
| 19 | "dojox/charting/axis2d/Default", |
---|
| 20 | "dojox/charting/plot2d/Default", |
---|
| 21 | "dojox/charting/plot2d/Lines", |
---|
| 22 | "dojox/charting/themes/Tufte", |
---|
| 23 | "dojo/colors", |
---|
| 24 | "dojo/text!./templates/Grapher.html", |
---|
| 25 | "dojox/calc/_Executor", |
---|
| 26 | "dijit/form/Button", // template |
---|
| 27 | "dijit/form/Select" // template |
---|
| 28 | ], function(declare, lang, win, domConstruct, domClass, domStyle, WidgetBase, WidgetsInTemplateMixin, TemplatedMixin, math, registry, DropDownButton, TooltipDialog, TextBox, CheckBox, ColorPalette, Chart, axis2d, plot2d, Lines, Tufte, colors, template, calc){ |
---|
| 29 | |
---|
| 30 | var |
---|
| 31 | epsilon = 1e-15 / 9, |
---|
| 32 | bigNumber = 1e200, |
---|
| 33 | log2 = Math.log(2), |
---|
| 34 | defaultParams = {graphNumber:0, fOfX:true, color:{stroke:"black"}}; |
---|
| 35 | |
---|
| 36 | var Grapher = declare( |
---|
| 37 | "dojox.calc.Grapher", |
---|
| 38 | [WidgetBase, TemplatedMixin, WidgetsInTemplateMixin], |
---|
| 39 | { |
---|
| 40 | // summary: |
---|
| 41 | // The dialog layout for making graphs |
---|
| 42 | |
---|
| 43 | templateString: template, |
---|
| 44 | |
---|
| 45 | addXYAxes: function(chart){ |
---|
| 46 | // summary: |
---|
| 47 | // add or re-add the default x/y axes to the Chart provided |
---|
| 48 | // chart: dojox.charting.Chart |
---|
| 49 | |
---|
| 50 | return chart.addAxis("x", { |
---|
| 51 | max: parseInt(this.graphMaxX.get("value")), |
---|
| 52 | min: parseInt(this.graphMinX.get("value")), |
---|
| 53 | majorLabels: true, |
---|
| 54 | minorLabels: true, |
---|
| 55 | //includeZero: true, |
---|
| 56 | minorTicks: false, |
---|
| 57 | microTicks: false, |
---|
| 58 | //majorTickStep: 1, |
---|
| 59 | htmlLabels: true, |
---|
| 60 | labelFunc: function(value){ |
---|
| 61 | return value; |
---|
| 62 | }, |
---|
| 63 | maxLabelSize: 30, |
---|
| 64 | fixUpper: "major", fixLower: "major", |
---|
| 65 | majorTick: { length: 3 } |
---|
| 66 | }). |
---|
| 67 | addAxis("y", { |
---|
| 68 | max: parseInt(this.graphMaxY.get("value")), |
---|
| 69 | min: parseInt(this.graphMinY.get("value")), |
---|
| 70 | labelFunc: function(value){ |
---|
| 71 | return value; |
---|
| 72 | }, |
---|
| 73 | maxLabelSize: 50, |
---|
| 74 | vertical: true, |
---|
| 75 | // htmlLabels: false, |
---|
| 76 | microTicks: false, |
---|
| 77 | minorTicks: true, |
---|
| 78 | majorTick: { stroke: "black", length: 3 } |
---|
| 79 | }); |
---|
| 80 | }, |
---|
| 81 | selectAll: function(){ |
---|
| 82 | // summary: |
---|
| 83 | // select all checkboxes inside the function table |
---|
| 84 | for(var i = 0; i < this.rowCount; i++){ |
---|
| 85 | this.array[i][this.checkboxIndex].set("checked", true); |
---|
| 86 | } |
---|
| 87 | }, |
---|
| 88 | deselectAll: function(){ |
---|
| 89 | // summary: |
---|
| 90 | // deselect all checkboxes inside the function table |
---|
| 91 | for(var i = 0; i < this.rowCount; i++){ |
---|
| 92 | this.array[i][this.checkboxIndex].set("checked", false); |
---|
| 93 | } |
---|
| 94 | }, |
---|
| 95 | drawOne: function(i){ |
---|
| 96 | // i is a the index to this.array |
---|
| 97 | // override me |
---|
| 98 | }, |
---|
| 99 | onDraw: function(){ |
---|
| 100 | console.log("Draw was pressed"); |
---|
| 101 | // override me |
---|
| 102 | }, |
---|
| 103 | erase: function(i){ |
---|
| 104 | // summary: |
---|
| 105 | // erase the chart inside this.array with the index i |
---|
| 106 | // i: Integer |
---|
| 107 | // index to this.array that represents the current row number in the table |
---|
| 108 | var nameNum = 0; |
---|
| 109 | var name = "Series "+this.array[i][this.funcNumberIndex]+"_"+nameNum; |
---|
| 110 | while(name in this.array[i][this.chartIndex].runs){ |
---|
| 111 | this.array[i][this.chartIndex].removeSeries(name); |
---|
| 112 | nameNum++; |
---|
| 113 | name = "Series "+this.array[i][this.funcNumberIndex]+"_"+nameNum; |
---|
| 114 | } |
---|
| 115 | this.array[i][this.chartIndex].render(); |
---|
| 116 | this.setStatus(i, "Hidden"); |
---|
| 117 | }, |
---|
| 118 | onErase: function(){ |
---|
| 119 | // summary: |
---|
| 120 | // the erase button's onClick method |
---|
| 121 | // it see's if the checkbox is checked and then erases it if it is. |
---|
| 122 | for(var i = 0; i < this.rowCount; i++){ |
---|
| 123 | if(this.array[i][this.checkboxIndex].get("checked")){ |
---|
| 124 | this.erase(i); |
---|
| 125 | } |
---|
| 126 | } |
---|
| 127 | }, |
---|
| 128 | onDelete: function(){ |
---|
| 129 | // summary: |
---|
| 130 | // The delete button's onClick method. |
---|
| 131 | // Delete all of the selected rows. |
---|
| 132 | for(var i = 0; i < this.rowCount; i++){ |
---|
| 133 | if(this.array[i][this.checkboxIndex].get("checked")){ |
---|
| 134 | this.erase(i); |
---|
| 135 | for(var k = 0; k < this.functionRef; k++){ |
---|
| 136 | if(this.array[i][k] && this.array[i][k]["destroy"]){ |
---|
| 137 | this.array[i][k].destroy(); |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | this.graphTable.deleteRow(i); |
---|
| 141 | this.array.splice(i, 1); |
---|
| 142 | this.rowCount--; |
---|
| 143 | i--; |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | }, |
---|
| 147 | // attributes to name the indices of this.array |
---|
| 148 | checkboxIndex: 0, |
---|
| 149 | functionMode: 1, |
---|
| 150 | expressionIndex: 2, |
---|
| 151 | colorIndex: 3, |
---|
| 152 | dropDownIndex: 4, |
---|
| 153 | tooltipIndex: 5, |
---|
| 154 | colorBoxFieldsetIndex: 6, |
---|
| 155 | statusIndex: 7, |
---|
| 156 | chartIndex: 8, |
---|
| 157 | funcNumberIndex: 9, |
---|
| 158 | evaluatedExpression: 10, |
---|
| 159 | functionRef: 11, |
---|
| 160 | |
---|
| 161 | createFunction: function(){ |
---|
| 162 | // summary: |
---|
| 163 | // create a new row in the table with all of the dojo objects. |
---|
| 164 | |
---|
| 165 | var tr = this.graphTable.insertRow(-1); |
---|
| 166 | this.array[tr.rowIndex] = []; |
---|
| 167 | var td = tr.insertCell(-1); |
---|
| 168 | var d = domConstruct.create('div'); |
---|
| 169 | td.appendChild(d); |
---|
| 170 | var checkBox = new CheckBox({}, d); |
---|
| 171 | this.array[tr.rowIndex][this.checkboxIndex] = checkBox; |
---|
| 172 | domClass.add(d, "dojoxCalcCheckBox"); |
---|
| 173 | |
---|
| 174 | td = tr.insertCell(-1); |
---|
| 175 | var funcMode = this.funcMode.get("value"); |
---|
| 176 | d = win.doc.createTextNode(funcMode); |
---|
| 177 | td.appendChild(d); |
---|
| 178 | this.array[tr.rowIndex][this.functionMode] = funcMode; |
---|
| 179 | //domClass.add(d, "dojoxCalcFunctionMode");// cannot use text nodes |
---|
| 180 | |
---|
| 181 | td = tr.insertCell(-1); |
---|
| 182 | d = domConstruct.create('div'); |
---|
| 183 | td.appendChild(d); |
---|
| 184 | var expression = new TextBox({}, d); |
---|
| 185 | this.array[tr.rowIndex][this.expressionIndex] = expression; |
---|
| 186 | domClass.add(d, "dojoxCalcExpressionBox"); |
---|
| 187 | |
---|
| 188 | var b = domConstruct.create('div'); |
---|
| 189 | var color = new ColorPalette({changedColor:this.changedColor}, b); |
---|
| 190 | domClass.add(b, "dojoxCalcColorPalette"); |
---|
| 191 | |
---|
| 192 | this.array[tr.rowIndex][this.colorIndex] = color; |
---|
| 193 | |
---|
| 194 | var c = domConstruct.create('div'); |
---|
| 195 | var dialog = new TooltipDialog({content:color}, c); |
---|
| 196 | this.array[tr.rowIndex][this.tooltipIndex] = dialog; |
---|
| 197 | domClass.add(c, "dojoxCalcContainerOfColor"); |
---|
| 198 | |
---|
| 199 | td = tr.insertCell(-1); |
---|
| 200 | d = domConstruct.create('div'); |
---|
| 201 | td.appendChild(d); |
---|
| 202 | |
---|
| 203 | var colorBoxFieldset = domConstruct.create('fieldset'); |
---|
| 204 | domStyle.set(colorBoxFieldset, { backgroundColor: "black", width: "1em", height: "1em", display: "inline" }); |
---|
| 205 | this.array[tr.rowIndex][this.colorBoxFieldsetIndex] = colorBoxFieldset; |
---|
| 206 | |
---|
| 207 | var drop = new DropDownButton({label:"Color ", dropDown:dialog}, d); |
---|
| 208 | drop.containerNode.appendChild(colorBoxFieldset); |
---|
| 209 | this.array[tr.rowIndex][this.dropDownIndex] = drop; |
---|
| 210 | domClass.add(d, "dojoxCalcDropDownForColor"); |
---|
| 211 | |
---|
| 212 | /*td = tr.insertCell(-1); |
---|
| 213 | d = domConstruct.create('div'); |
---|
| 214 | td.appendChild(d); |
---|
| 215 | var status = new TextBox({style:"width:50px", value:"Hidden", readOnly:true}, d);//hidden, drawn, or error |
---|
| 216 | this.array[tr.rowIndex][this.statusIndex] = status; |
---|
| 217 | domClass.add(d, "dojoxCalcStatusBox");*/ |
---|
| 218 | |
---|
| 219 | td = tr.insertCell(-1); |
---|
| 220 | d = domConstruct.create('fieldset'); |
---|
| 221 | d.innerHTML = "Hidden"; |
---|
| 222 | this.array[tr.rowIndex][this.statusIndex] = d; |
---|
| 223 | domClass.add(d, "dojoxCalcStatusBox"); |
---|
| 224 | td.appendChild(d); |
---|
| 225 | |
---|
| 226 | d = domConstruct.create('div'); |
---|
| 227 | domStyle.set(d, { position: "absolute", left: "0px", top: "0px" }) |
---|
| 228 | this.chartsParent.appendChild(d); |
---|
| 229 | this.array[tr.rowIndex][this.chartNodeIndex] = d; |
---|
| 230 | domClass.add(d, "dojoxCalcChart"); |
---|
| 231 | var chart = new dojox.charting.Chart(d).setTheme(dojox.charting.themes.Tufte). |
---|
| 232 | addPlot("default", { type: "Lines", shadow: {dx: 1, dy: 1, width: 2, color: [0, 0, 0, 0.3]} }); |
---|
| 233 | this.addXYAxes(chart); |
---|
| 234 | this.array[tr.rowIndex][this.chartIndex] = chart; |
---|
| 235 | color.set("chart", chart); |
---|
| 236 | color.set("colorBox", colorBoxFieldset); |
---|
| 237 | color.set("onChange", lang.hitch(color, 'changedColor')); |
---|
| 238 | |
---|
| 239 | this.array[tr.rowIndex][this.funcNumberIndex] = this.funcNumber++; |
---|
| 240 | this.rowCount++; |
---|
| 241 | }, |
---|
| 242 | setStatus: function(i, status){ |
---|
| 243 | // summary: |
---|
| 244 | // set the status of the row i to be status |
---|
| 245 | // i: Integer |
---|
| 246 | // index of this.array as well as a row index |
---|
| 247 | // status: String |
---|
| 248 | // either Error, Hidden, or Drawn |
---|
| 249 | this.array[i][this.statusIndex].innerHTML = status; //this.array[i][this.statusIndex].set("value", status); |
---|
| 250 | }, |
---|
| 251 | changedColor: function(){ |
---|
| 252 | // summary: |
---|
| 253 | // Make the color of the chart the new color. |
---|
| 254 | // The context is changed to the colorPalette, and a reference to chart was added to it a an attribute. |
---|
| 255 | var chart = this.get("chart"); |
---|
| 256 | var colorBoxFieldset = this.get("colorBox"); |
---|
| 257 | for(var i = 0; i < chart.series.length; i++){ |
---|
| 258 | if(chart.series[i]["stroke"]){ |
---|
| 259 | if(chart.series[i].stroke["color"]){ |
---|
| 260 | chart.series[i]["stroke"].color = this.get("value"); |
---|
| 261 | chart.dirty = true; |
---|
| 262 | } |
---|
| 263 | } |
---|
| 264 | } |
---|
| 265 | chart.render(); |
---|
| 266 | domStyle.set(colorBoxFieldset, { backgroundColor: this.get("value") }); |
---|
| 267 | }, |
---|
| 268 | makeDirty: function(){ |
---|
| 269 | // summary: |
---|
| 270 | // if something in the window options is changed, this is called |
---|
| 271 | this.dirty = true; |
---|
| 272 | }, |
---|
| 273 | checkDirty1: function(){ |
---|
| 274 | // summary: |
---|
| 275 | // to stay in sync with onChange, checkDirty is called with a timeout |
---|
| 276 | setTimeout(lang.hitch(this, 'checkDirty'), 0); |
---|
| 277 | }, |
---|
| 278 | checkDirty: function(){ |
---|
| 279 | // summary: |
---|
| 280 | // adjust all charts in this.array according to any changes in window options |
---|
| 281 | if(this.dirty){ |
---|
| 282 | // change the axes of all charts if it is dirty |
---|
| 283 | for(var i = 0; i < this.rowCount; i++){ |
---|
| 284 | this.array[i][this.chartIndex].removeAxis("x"); |
---|
| 285 | this.array[i][this.chartIndex].removeAxis("y"); |
---|
| 286 | this.addXYAxes(this.array[i][this.chartIndex]); |
---|
| 287 | } |
---|
| 288 | this.onDraw(); |
---|
| 289 | } |
---|
| 290 | this.dirty = false; |
---|
| 291 | }, |
---|
| 292 | postCreate: function(){ |
---|
| 293 | // summary: |
---|
| 294 | // add Event handlers, some additional attributes, etc |
---|
| 295 | this.inherited(arguments);// this is super class postCreate |
---|
| 296 | this.createFunc.set("onClick", lang.hitch(this, 'createFunction')); |
---|
| 297 | |
---|
| 298 | this.selectAllButton.set("onClick", lang.hitch(this, 'selectAll')); |
---|
| 299 | this.deselectAllButton.set("onClick", lang.hitch(this, 'deselectAll')); |
---|
| 300 | |
---|
| 301 | this.drawButton.set("onClick", lang.hitch(this, 'onDraw')); |
---|
| 302 | this.eraseButton.set("onClick", lang.hitch(this, 'onErase')); |
---|
| 303 | this.deleteButton.set("onClick", lang.hitch(this, 'onDelete')); |
---|
| 304 | |
---|
| 305 | this.dirty = false; |
---|
| 306 | this.graphWidth.set("onChange", lang.hitch(this, 'makeDirty')); |
---|
| 307 | this.graphHeight.set("onChange", lang.hitch(this, 'makeDirty')); |
---|
| 308 | this.graphMaxX.set("onChange", lang.hitch(this, 'makeDirty')); |
---|
| 309 | this.graphMinX.set("onChange", lang.hitch(this, 'makeDirty')); |
---|
| 310 | this.graphMaxY.set("onChange", lang.hitch(this, 'makeDirty')); |
---|
| 311 | this.graphMinY.set("onChange", lang.hitch(this, 'makeDirty')); |
---|
| 312 | this.windowOptionsInside.set("onClose", lang.hitch(this, 'checkDirty1')); |
---|
| 313 | |
---|
| 314 | this.funcNumber = 0; |
---|
| 315 | this.rowCount = 0; |
---|
| 316 | this.array = []; |
---|
| 317 | |
---|
| 318 | }, |
---|
| 319 | startup: function(){ |
---|
| 320 | // summary: |
---|
| 321 | // make sure the parent has a close button if it needs to be able to close |
---|
| 322 | this.inherited(arguments);// this is super class startup |
---|
| 323 | // close is only valid if the parent is a widget with a close function |
---|
| 324 | var parent = registry.getEnclosingWidget(this.domNode.parentNode); |
---|
| 325 | if(parent && typeof parent.close == "function"){ |
---|
| 326 | this.closeButton.set("onClick", lang.hitch(parent, 'close')); |
---|
| 327 | }else{ |
---|
| 328 | domStyle.set(this.closeButton.domNode, { display: "none" }); // hide the button |
---|
| 329 | } |
---|
| 330 | // add one row at the start |
---|
| 331 | this.createFunction(); |
---|
| 332 | |
---|
| 333 | // make the graph bounds appear initially |
---|
| 334 | this.array[0][this.checkboxIndex].set("checked", true); |
---|
| 335 | this.onDraw(); |
---|
| 336 | this.erase(0); |
---|
| 337 | this.array[0][this.expressionIndex].value = ""; |
---|
| 338 | } |
---|
| 339 | }); |
---|
| 340 | |
---|
| 341 | return lang.mixin(calc, { |
---|
| 342 | draw: function(/*Chart*/ chart, /*Function*/ functionToGraph, params){ |
---|
| 343 | // summary: |
---|
| 344 | // graph a chart with the given function. |
---|
| 345 | // chart: dojox.charting.Chart |
---|
| 346 | // functionToGraph: Function |
---|
| 347 | // Function with one numeric parameter (x or y typically) |
---|
| 348 | // params: Object |
---|
| 349 | // can contain the number of the graph in the chart it is (an integer), a boolean saying if the functionToGraph is a function of x (otherwise y) |
---|
| 350 | // and the color, which is an object with a stroke with a color's name eg: color:{stroke:"black"} |
---|
| 351 | |
---|
| 352 | params = lang.mixin({}, defaultParams, params); |
---|
| 353 | chart.fullGeometry(); |
---|
| 354 | var x; |
---|
| 355 | var y; |
---|
| 356 | var points; |
---|
| 357 | if(params.fOfX==true){ |
---|
| 358 | x = 'x'; |
---|
| 359 | y = 'y'; |
---|
| 360 | points = calc.generatePoints(functionToGraph, x, y, chart.axes.x.scaler.bounds.span, chart.axes.x.scaler.bounds.lower, chart.axes.x.scaler.bounds.upper, chart.axes.y.scaler.bounds.lower, chart.axes.y.scaler.bounds.upper); |
---|
| 361 | }else{ |
---|
| 362 | x = 'y'; |
---|
| 363 | y = 'x'; |
---|
| 364 | points = calc.generatePoints(functionToGraph, x, y, chart.axes.y.scaler.bounds.span, chart.axes.y.scaler.bounds.lower, chart.axes.y.scaler.bounds.upper, chart.axes.x.scaler.bounds.lower, chart.axes.x.scaler.bounds.upper); |
---|
| 365 | } |
---|
| 366 | |
---|
| 367 | var i = 0; |
---|
| 368 | |
---|
| 369 | if(points.length > 0){ |
---|
| 370 | for(; i < points.length; i++){ |
---|
| 371 | if(points[i].length>0){ |
---|
| 372 | chart.addSeries("Series "+params.graphNumber+"_"+i, points[i], params.color); |
---|
| 373 | } |
---|
| 374 | } |
---|
| 375 | } |
---|
| 376 | // you only need to remove the excess i's |
---|
| 377 | var name = "Series "+params.graphNumber+"_"+i; |
---|
| 378 | while(name in chart.runs){ |
---|
| 379 | chart.removeSeries(name); |
---|
| 380 | i++; |
---|
| 381 | name = "Series "+params.graphNumber+"_"+i; |
---|
| 382 | } |
---|
| 383 | chart.render(); |
---|
| 384 | return points; |
---|
| 385 | }, |
---|
| 386 | |
---|
| 387 | generatePoints: function(/*Function*/ funcToGraph, /*String*/ x, /*String*/ y, /*Number*/ width, /*Number*/ minX, /*Number*/ maxX, /*Number*/ minY, /*Number*/ maxY){ |
---|
| 388 | // summary: |
---|
| 389 | // create the points with information about the graph. |
---|
| 390 | // funcToGraph: Function |
---|
| 391 | // A function with one numeric parameter (x or y typically) |
---|
| 392 | // x: String |
---|
| 393 | // x and y are Strings which always have the values of "x" or "y". If y="x" and x="y" then it is creating points for the function as though it was a function of y |
---|
| 394 | // y: String |
---|
| 395 | // x and y are Strings which always have the values of "x" or "y". If y="x" and x="y" then it is creating points for the function as though it was a function of y |
---|
| 396 | // minX: |
---|
| 397 | // minX, maxX, minY, and maxY are all bounds of the chart. If x="y" then maxY should be the maximum bound of x rather than y |
---|
| 398 | // maxX: |
---|
| 399 | // minX, maxX, minY, and maxY are all bounds of the chart. If x="y" then maxY should be the maximum bound of x rather than y |
---|
| 400 | // minY: |
---|
| 401 | // minX, maxX, minY, and maxY are all bounds of the chart. If x="y" then maxY should be the maximum bound of x rather than y |
---|
| 402 | // maxY: |
---|
| 403 | // minX, maxX, minY, and maxY are all bounds of the chart. If x="y" then maxY should be the maximum bound of x rather than y |
---|
| 404 | // width: |
---|
| 405 | // pixel width of the chart |
---|
| 406 | // output: |
---|
| 407 | // an array of arrays of points |
---|
| 408 | var pow2 = (1 << Math.ceil(Math.log(width) / log2)); |
---|
| 409 | var |
---|
| 410 | dx = (maxX - minX) / pow2, // divide by 2^n instead of width to avoid loss of precision |
---|
| 411 | points = [], // [{x:value, y:value2},...] |
---|
| 412 | series = 0, |
---|
| 413 | slopeTrend, |
---|
| 414 | slopeTrendTemp; |
---|
| 415 | |
---|
| 416 | points[series] = []; |
---|
| 417 | |
---|
| 418 | var i = minX, k, p; |
---|
| 419 | for(var counter = 0; counter <= pow2; i += dx, counter++){ |
---|
| 420 | p = {}; |
---|
| 421 | p[x] = i; |
---|
| 422 | p[y] = funcToGraph({_name:x, _value:i, _graphing:true});//funcToGraph(i); |
---|
| 423 | if(p[x] == null || p[y] == null){ |
---|
| 424 | return {};// someone pushed cancel in the val code |
---|
| 425 | } |
---|
| 426 | if(isNaN(p[y]) || isNaN(p[x])){ |
---|
| 427 | continue; |
---|
| 428 | } |
---|
| 429 | points[series].push(p); |
---|
| 430 | |
---|
| 431 | if(points[series].length == 3){ |
---|
| 432 | slopeTrend = getSlopePairTrend(slope(points[series][points[series].length - 3], points[series][points[series].length-2]), slope(points[series][points[series].length-2], points[series][points[series].length-1])); |
---|
| 433 | continue; |
---|
| 434 | } |
---|
| 435 | if(points[series].length < 4){ |
---|
| 436 | continue; |
---|
| 437 | } |
---|
| 438 | |
---|
| 439 | slopeTrendTemp = getSlopePairTrend(slope(points[series][points[series].length - 3], points[series][points[series].length-2]), slope(points[series][points[series].length-2], points[series][points[series].length-1])); |
---|
| 440 | if(slopeTrend.inc != slopeTrendTemp.inc || slopeTrend.pos != slopeTrendTemp.pos){ |
---|
| 441 | // var a = asymptoteSearch(funcToGraph, points[series][points[series].length - 2], points[series][points[series].length-1]); |
---|
| 442 | var a = asymptoteSearch(funcToGraph, points[series][points[series].length - 3], points[series][points[series].length-1]); |
---|
| 443 | p = points[series].pop(); |
---|
| 444 | // this pop was added after changing the var a line above |
---|
| 445 | points[series].pop(); |
---|
| 446 | for(var j = 0; j < a[0].length; j++){ |
---|
| 447 | points[series].push(a[0][j]); |
---|
| 448 | } |
---|
| 449 | for(k = 1; k < a.length; k++){ |
---|
| 450 | points[++series] = a.pop(); |
---|
| 451 | } |
---|
| 452 | points[series].push(p); |
---|
| 453 | slopeTrend = slopeTrendTemp; |
---|
| 454 | } |
---|
| 455 | } |
---|
| 456 | while(points.length > 1){ |
---|
| 457 | for(k = 0; k < points[1].length; k++){ |
---|
| 458 | if(points[0][points[0].length - 1][x] == points[1][k][x]){ |
---|
| 459 | continue; |
---|
| 460 | } |
---|
| 461 | points[0].push(points[1][k]); |
---|
| 462 | } |
---|
| 463 | points.splice(1, 1); |
---|
| 464 | } |
---|
| 465 | points = points[0]; |
---|
| 466 | |
---|
| 467 | // make new series when it goes off the graph |
---|
| 468 | var s = 0; |
---|
| 469 | var points2 = [ [] ]; |
---|
| 470 | for(k = 0; k < points.length; k++){ |
---|
| 471 | var x1, y1, b, slope1; |
---|
| 472 | if(isNaN(points[k][y]) || isNaN(points[k][x])){ |
---|
| 473 | while(isNaN(points[k][y]) || isNaN(points[k][x])){ |
---|
| 474 | points.splice(k, 1); |
---|
| 475 | } |
---|
| 476 | points2[++s] = []; |
---|
| 477 | k--; |
---|
| 478 | }else if(points[k][y] > maxY || points[k][y] < minY){ |
---|
| 479 | // make the last point's y equal maxY and find a matching x |
---|
| 480 | if(k > 0 && points[k - 1].y!=minY && points[k - 1].y!=maxY){ |
---|
| 481 | slope1 = slope(points[k - 1], points[k]); |
---|
| 482 | if(slope1 > bigNumber){ |
---|
| 483 | slope1 = bigNumber; |
---|
| 484 | }else if(slope1 < -bigNumber){ |
---|
| 485 | slope1 = -bigNumber; |
---|
| 486 | } |
---|
| 487 | if(points[k][y] > maxY){ |
---|
| 488 | y1 = maxY; |
---|
| 489 | }else{ |
---|
| 490 | y1 = minY; |
---|
| 491 | } |
---|
| 492 | b = points[k][y] - slope1 * points[k][x]; |
---|
| 493 | x1 = (y1 - b) / slope1; |
---|
| 494 | |
---|
| 495 | p = {}; |
---|
| 496 | p[x] = x1; |
---|
| 497 | p[y] = funcToGraph(x1);//y1;// |
---|
| 498 | |
---|
| 499 | if(p[y]!=y1){ |
---|
| 500 | p = findMinOrMaxY(funcToGraph, points[k - 1], points[k], y1); |
---|
| 501 | } |
---|
| 502 | |
---|
| 503 | points2[s].push(p); |
---|
| 504 | // setup the next series |
---|
| 505 | points2[++s] = [] |
---|
| 506 | } |
---|
| 507 | var startK = k; |
---|
| 508 | while(k < points.length && (points[k][y] > maxY || points[k][y] < minY)){ |
---|
| 509 | k++; |
---|
| 510 | } |
---|
| 511 | if(k >= points.length){ |
---|
| 512 | if(points2[s].length == 0){ |
---|
| 513 | points2.splice(s, 1); |
---|
| 514 | } |
---|
| 515 | break; |
---|
| 516 | } |
---|
| 517 | // connect the end graph |
---|
| 518 | if(k > 0 && points[k].y != minY && points[k].y != maxY){ |
---|
| 519 | slope1 = slope(points[k - 1], points[k]); |
---|
| 520 | if(slope1 > bigNumber){ |
---|
| 521 | slope1 = bigNumber; |
---|
| 522 | }else if(slope1 < -bigNumber){ |
---|
| 523 | slope1 = -bigNumber; |
---|
| 524 | } |
---|
| 525 | if(points[k - 1][y] > maxY){ |
---|
| 526 | y1 = maxY; |
---|
| 527 | }else{ |
---|
| 528 | y1 = minY; |
---|
| 529 | } |
---|
| 530 | b = points[k][y] - slope1 * points[k][x]; |
---|
| 531 | x1 = (y1 - b) / slope1; |
---|
| 532 | |
---|
| 533 | p = {}; |
---|
| 534 | p[x] = x1; |
---|
| 535 | p[y] = funcToGraph(x1);//y1;// |
---|
| 536 | if(p[y]!=y1){ |
---|
| 537 | p = findMinOrMaxY(funcToGraph, points[k - 1], points[k], y1); |
---|
| 538 | } |
---|
| 539 | points2[s].push(p); |
---|
| 540 | points2[s].push(points[k]); |
---|
| 541 | } |
---|
| 542 | }else{ |
---|
| 543 | points2[s].push(points[k]); |
---|
| 544 | } |
---|
| 545 | } |
---|
| 546 | return points2; |
---|
| 547 | |
---|
| 548 | function findMinOrMaxY(funcToGraph, left, right, minMaxY){ |
---|
| 549 | |
---|
| 550 | while(left<=right){ |
---|
| 551 | var midX = (left[x]+right[x])/2; |
---|
| 552 | var mid = {}; |
---|
| 553 | mid[x] = midX; |
---|
| 554 | mid[y] = funcToGraph(mid[x]); |
---|
| 555 | |
---|
| 556 | if(minMaxY==mid[y]||mid[x]==right[x]||mid[x]==left[x]){ |
---|
| 557 | return mid; |
---|
| 558 | } |
---|
| 559 | |
---|
| 560 | var moveTowardsLarger = true; |
---|
| 561 | if(minMaxY<mid[y]){ |
---|
| 562 | moveTowardsLarger = false; |
---|
| 563 | } |
---|
| 564 | |
---|
| 565 | if(mid[y]<right[y]){ |
---|
| 566 | if(moveTowardsLarger){ |
---|
| 567 | left = mid; |
---|
| 568 | }else{ |
---|
| 569 | right = mid; |
---|
| 570 | } |
---|
| 571 | }else if(mid[y]<left[y]){ |
---|
| 572 | if(!moveTowardsLarger){ |
---|
| 573 | left = mid; |
---|
| 574 | }else{ |
---|
| 575 | right = mid; |
---|
| 576 | } |
---|
| 577 | } |
---|
| 578 | } |
---|
| 579 | return NaN; |
---|
| 580 | } |
---|
| 581 | |
---|
| 582 | function asymptoteSearch(funcToGraph, pointStart, pointStop){ |
---|
| 583 | var |
---|
| 584 | pointTemp = [ [], [] ], |
---|
| 585 | left = pointStart, |
---|
| 586 | right = pointStop, |
---|
| 587 | midpoint; |
---|
| 588 | |
---|
| 589 | |
---|
| 590 | while(left[x] <= right[x]){ |
---|
| 591 | var midX = (left[x] + right[x]) / 2; |
---|
| 592 | |
---|
| 593 | midpoint = {}; |
---|
| 594 | midpoint[x] = midX; |
---|
| 595 | midpoint[y] = funcToGraph(midX); |
---|
| 596 | |
---|
| 597 | var rx = nextNumber(midpoint[x]); |
---|
| 598 | var rightPoint = {}; |
---|
| 599 | rightPoint[x] = rx; |
---|
| 600 | rightPoint[y] = funcToGraph(rx); |
---|
| 601 | |
---|
| 602 | if(Math.abs(rightPoint[y]) >= Math.abs(midpoint[y])){ |
---|
| 603 | pointTemp[0].push(midpoint); |
---|
| 604 | left = rightPoint; |
---|
| 605 | }else{ |
---|
| 606 | pointTemp[1].unshift(midpoint); |
---|
| 607 | if(right[x] == midpoint[x]){ |
---|
| 608 | break; |
---|
| 609 | } |
---|
| 610 | right = midpoint; |
---|
| 611 | } |
---|
| 612 | |
---|
| 613 | } |
---|
| 614 | return pointTemp; |
---|
| 615 | } |
---|
| 616 | |
---|
| 617 | function getSlopePairTrend(slope1, slope2){ |
---|
| 618 | var |
---|
| 619 | isInc = false, |
---|
| 620 | isPos = false; |
---|
| 621 | |
---|
| 622 | if(slope1 < slope2){ |
---|
| 623 | isInc = true; |
---|
| 624 | } |
---|
| 625 | if(slope2 > 0){ |
---|
| 626 | isPos = true; |
---|
| 627 | } |
---|
| 628 | return { inc: isInc, pos: isPos }; |
---|
| 629 | } |
---|
| 630 | |
---|
| 631 | function nextNumber(v){ |
---|
| 632 | var delta; |
---|
| 633 | if(v > -1 && v < 1){ |
---|
| 634 | if(v < 0){ // special handling as we approach 0 |
---|
| 635 | if(v >= -epsilon){ |
---|
| 636 | delta = -v; // always stop at 0 |
---|
| 637 | }else{ |
---|
| 638 | delta = v / Math.ceil(v / epsilon); // divide distance to 0 into equal tiny chunks |
---|
| 639 | } |
---|
| 640 | }else{ |
---|
| 641 | delta = epsilon; |
---|
| 642 | } |
---|
| 643 | }else{ |
---|
| 644 | delta = Math.abs(v) * epsilon; |
---|
| 645 | } |
---|
| 646 | return v + delta; |
---|
| 647 | } |
---|
| 648 | |
---|
| 649 | function slope(p1, p2){ |
---|
| 650 | return (p2[y] - p1[y]) / (p2[x] - p1[x]); |
---|
| 651 | } |
---|
| 652 | }, |
---|
| 653 | Grapher: Grapher |
---|
| 654 | }); |
---|
| 655 | }); |
---|