📄 layout.js
字号:
else { this.xscale = (1.0 - xdelta/this.xrange)/this.xrange; } barWidth = xdelta * this.xscale * this.options.barWidthFillFraction; barWidthForSet = barWidth / setCount; barMargin = xdelta * this.xscale * (1.0 - this.options.barWidthFillFraction)/2; } this.minxdelta = xdelta; // need this for tick positions // add all the rects this.bars = new Array(); var i = 0; for (var setName in this.datasets) { var dataset = this.datasets[setName]; if (PlotKit.Base.isFuncLike(dataset)) continue; for (var j = 0; j < dataset.length; j++) { var item = dataset[j]; var rect = { x: ((parseFloat(item[0]) - this.minxval) * this.xscale) + (i * barWidthForSet) + barMargin, y: 1.0 - ((parseFloat(item[1]) - this.minyval) * this.yscale), w: barWidthForSet, h: ((parseFloat(item[1]) - this.minyval) * this.yscale), xval: parseFloat(item[0]), yval: parseFloat(item[1]), name: setName }; if ((rect.x >= 0.0) && (rect.x <= 1.0) && (rect.y >= 0.0) && (rect.y <= 1.0)) { this.bars.push(rect); } } i++; }};// Create the horizontal barsPlotKit.Layout.prototype._evaluateHorizBarCharts = function() { var items = PlotKit.Base.items; var setCount = items(this.datasets).length; // work out how far separated values are var xdelta = 10000000; var xvalues = this._uniqueXValues(); for (var i = 1; i < xvalues.length; i++) { xdelta = Math.min(Math.abs(xvalues[i] - xvalues[i-1]), xdelta); } var barWidth = 0; var barWidthForSet = 0; var barMargin = 0; // work out how far each far each bar is separated if (xvalues.length == 1) { // do something smarter if we only plot one value xdelta = 1.0; this.xscale = 1.0; this.minxval = xvalues[0]; barWidth = 1.0 * this.options.barWidthFillFraction; barWidthForSet = barWidth/setCount; barMargin = (1.0 - this.options.barWidthFillFraction)/2; } else { // readjust yscale to fix with bar charts this.xscale = (1.0 - xdelta/this.xrange)/this.xrange; barWidth = xdelta * this.xscale * this.options.barWidthFillFraction; barWidthForSet = barWidth / setCount; barMargin = xdelta * this.xscale * (1.0 - this.options.barWidthFillFraction)/2; } this.minxdelta = xdelta; // need this for tick positions // add all the rects this.bars = new Array(); var i = 0; for (var setName in this.datasets) { var dataset = this.datasets[setName]; if (PlotKit.Base.isFuncLike(dataset)) continue; for (var j = 0; j < dataset.length; j++) { var item = dataset[j]; var rect = { y: ((parseFloat(item[0]) - this.minxval) * this.xscale) + (i * barWidthForSet) + barMargin, x: 0.0, h: barWidthForSet, w: ((parseFloat(item[1]) - this.minyval) * this.yscale), xval: parseFloat(item[0]), yval: parseFloat(item[1]), name: setName }; // limit the x, y values so they do not overdraw if (rect.y <= 0.0) { rect.y = 0.0; } if (rect.y >= 1.0) { rect.y = 1.0; } if ((rect.x >= 0.0) && (rect.x <= 1.0)) { this.bars.push(rect); } } i++; }};// Create the line chartsPlotKit.Layout.prototype._evaluateLineCharts = function() { var items = PlotKit.Base.items; var setCount = items(this.datasets).length; // add all the rects this.points = new Array(); var i = 0; for (var setName in this.datasets) { var dataset = this.datasets[setName]; if (PlotKit.Base.isFuncLike(dataset)) continue; dataset.sort(function(a, b) { return compare(parseFloat(a[0]), parseFloat(b[0])); }); for (var j = 0; j < dataset.length; j++) { var item = dataset[j]; var point = { x: ((parseFloat(item[0]) - this.minxval) * this.xscale), y: 1.0 - ((parseFloat(item[1]) - this.minyval) * this.yscale), xval: parseFloat(item[0]), yval: parseFloat(item[1]), name: setName }; // limit the x, y values so they do not overdraw if (point.y <= 0.0) { point.y = 0.0; } if (point.y >= 1.0) { point.y = 1.0; } if ((point.x >= 0.0) && (point.x <= 1.0)) { this.points.push(point); } } i++; }};// Create the pie chartsPlotKit.Layout.prototype._evaluatePieCharts = function() { var items = PlotKit.Base.items; var sum = MochiKit.Iter.sum; var getter = MochiKit.Base.itemgetter; var setCount = items(this.datasets).length; // we plot the y values of the first dataset var dataset = items(this.datasets)[0][1]; var total = sum(map(getter(1), dataset)); this.slices = new Array(); var currentAngle = 0.0; for (var i = 0; i < dataset.length; i++) { var fraction = dataset[i][1] / total; var startAngle = currentAngle * Math.PI * 2; var endAngle = (currentAngle + fraction) * Math.PI * 2; var slice = {fraction: fraction, xval: dataset[i][0], yval: dataset[i][1], startAngle: startAngle, endAngle: endAngle }; if (dataset[i][1] != 0) { this.slices.push(slice); } currentAngle += fraction; }};PlotKit.Layout.prototype._evaluateLineTicksForXAxis = function() { var isNil = MochiKit.Base.isUndefinedOrNull; if (this.options.xTicks) { // we use use specified ticks with optional labels this.xticks = new Array(); var makeTicks = function(tick) { var label = tick.label; if (isNil(label)) label = tick.v.toString(); var pos = this.xscale * (tick.v - this.minxval); if ((pos >= 0.0) && (pos <= 1.0)) { this.xticks.push([pos, label]); } }; MochiKit.Iter.forEach(this.options.xTicks, bind(makeTicks, this)); } else if (this.options.xNumberOfTicks) { // we use defined number of ticks as hint to auto generate var xvalues = this._uniqueXValues(); var roughSeparation = this.xrange / this.options.xNumberOfTicks; var tickCount = 0; this.xticks = new Array(); for (var i = 0; i <= xvalues.length; i++) { if ((xvalues[i] - this.minxval) >= (tickCount * roughSeparation)) { var pos = this.xscale * (xvalues[i] - this.minxval); if ((pos > 1.0) || (pos < 0.0)) continue; this.xticks.push([pos, xvalues[i]]); tickCount++; } if (tickCount > this.options.xNumberOfTicks) break; } }};PlotKit.Layout.prototype._evaluateLineTicksForYAxis = function() { var isNil = MochiKit.Base.isUndefinedOrNull; if (this.options.yTicks) { this.yticks = new Array(); var makeTicks = function(tick) { var label = tick.label; if (isNil(label)) label = tick.v.toString(); var pos = 1.0 - (this.yscale * (tick.v - this.minyval)); if ((pos >= 0.0) && (pos <= 1.0)) { this.yticks.push([pos, label]); } }; MochiKit.Iter.forEach(this.options.yTicks, bind(makeTicks, this)); } else if (this.options.yNumberOfTicks) { // We use the optionally defined number of ticks as a guide this.yticks = new Array(); // if we get this separation right, we'll have good looking graphs var roundInt = PlotKit.Base.roundInterval; var prec = this.options.yTickPrecision; var roughSeparation = roundInt(this.yrange, this.options.yNumberOfTicks, prec); // round off each value of the y-axis to the precision // eg. 1.3333 at precision 1 -> 1.3 for (var i = 0; i <= this.options.yNumberOfTicks; i++) { var yval = this.minyval + (i * roughSeparation); var pos = 1.0 - ((yval - this.minyval) * this.yscale); if ((pos > 1.0) || (pos < 0.0)) continue; this.yticks.push([pos, MochiKit.Format.roundToFixed(yval, prec)]); } }};PlotKit.Layout.prototype._evaluateLineTicks = function() { this._evaluateLineTicksForXAxis(); this._evaluateLineTicksForYAxis();};PlotKit.Layout.prototype._evaluateBarTicks = function() { this._evaluateLineTicks(); var centerInBar = function(tick) { return [tick[0] + (this.minxdelta * this.xscale)/2, tick[1]]; }; this.xticks = MochiKit.Base.map(bind(centerInBar, this), this.xticks); if (this.options.barOrientation == "horizontal") { // swap scales var tempticks = this.xticks; this.xticks = this.yticks; this.yticks = tempticks; // we need to invert the "yaxis" (which is now the xaxis when drawn) var invert = function(tick) { return [1.0 - tick[0], tick[1]]; } this.xticks = MochiKit.Base.map(invert, this.xticks); }};PlotKit.Layout.prototype._evaluatePieTicks = function() { var isNil = MochiKit.Base.isUndefinedOrNull; var formatter = MochiKit.Format.numberFormatter("#%"); this.xticks = new Array(); if (this.options.xTicks) { // make a lookup dict for x->slice values var lookup = new Array(); for (var i = 0; i < this.slices.length; i++) { lookup[this.slices[i].xval] = this.slices[i]; } for (var i =0; i < this.options.xTicks.length; i++) { var tick = this.options.xTicks[i]; var slice = lookup[tick.v]; var label = tick.label; if (slice) { if (isNil(label)) label = tick.v.toString(); label += " (" + formatter(slice.fraction) + ")"; this.xticks.push([tick.v, label]); } } } else { // we make our own labels from all the slices for (var i =0; i < this.slices.length; i++) { var slice = this.slices[i]; var label = slice.xval + " (" + formatter(slice.fraction) + ")"; this.xticks.push([slice.xval, label]); } }};PlotKit.Layout.prototype._regenerateHitTestCache = function() { this.hitTestCache.xvalues = this._uniqueXValues(); this.hitTestCache.xlookup = new Array(); this.hitTestCache.x2maxy = new Array(); var listMax = MochiKit.Base.listMax; var itemgetter = MochiKit.Base.itemgetter; var map = MochiKit.Base.map; // generate a lookup table for x values to y values var setNames = keys(this.datasets); for (var i = 0; i < setNames.length; i++) { var dataset = this.datasets[setNames[i]]; for (var j = 0; j < dataset.length; j++) { var xval = dataset[j][0]; var yval = dataset[j][1]; if (this.hitTestCache.xlookup[xval]) this.hitTestCache.xlookup[xval].push([yval, setNames[i]]); else this.hitTestCache.xlookup[xval] = [[yval, setNames[i]]]; } } for (var x in this.hitTestCache.xlookup) { var yvals = this.hitTestCache.xlookup[x]; this.hitTestCache.x2maxy[x] = listMax(map(itemgetter(0), yvals)); }};// --------------------------------------------------------------------// END Internal Functions// --------------------------------------------------------------------// Namespace IniitialisationPlotKit.LayoutModule = {};PlotKit.LayoutModule.Layout = PlotKit.Layout;PlotKit.LayoutModule.EXPORT = [ "Layout"];PlotKit.LayoutModule.EXPORT_OK = [];PlotKit.LayoutModule.__new__ = function() { var m = MochiKit.Base; m.nameFunctions(this); this.EXPORT_TAGS = { ":common": this.EXPORT, ":all": m.concat(this.EXPORT, this.EXPORT_OK) };};PlotKit.LayoutModule.__new__();MochiKit.Base._exportSymbols(this, PlotKit.LayoutModule);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -