📄 chart.src.js
字号:
var axis, item, step, y, ty, n, yoffset, value, multiplier, w, items, pos;
var ygd, precision;
ygd=this.config.yGrid;
if (ygd<=0) return;
precision=this.config.labelPrecision;
/* Calculate step size and rounding precision */
multiplier = Math.pow(10, precision);
step = this.range / this.config.yGrid;
/* Create container */
//axis=jQuery(this._cont).append('<div style="position:absolute;left:0;top:0;text-align:right"></div>').get(0);
axis = document.createElement('div');
axis.style.position = 'absolute';
axis.style.left = '0px';
axis.style.top = '0px';
axis.style.textAlign = 'right';
this._cont.appendChild(axis);
/* Draw labels and points */
w = 0;
items = new Array();
for (i=0;i<=this.config.yGrid;i++) {
value = parseInt((this.ymin+(i*step)) * multiplier) / multiplier;
//item=jQuery(axis).append('<span>'+value+'</span>').get(0);
item = document.createElement('span');
item.appendChild(document.createTextNode(value));
axis.appendChild(item);
items.push(item);
if (item.offsetWidth > w) { w = item.offsetWidth; }
}
/* Draw last label and point (lower left corner of chart) */
item = document.createElement('span');
item.appendChild(document.createTextNode(this.ymin));
axis.appendChild(item);
items.push(item);
if (item.offsetWidth > w) { w = item.offsetWidth; }
/* Set width of container to width of widest label */
axis.style.width = w + 'px';
/* Recalculate chart width and position based on labels and legend */
this.chartx = w + 5;
this.charty = item.offsetHeight / 2;
this.charth = this.h - ((item.offsetHeight * 1.5) + 5);
this.chartw = this.w - (((this.legend)?this.legend.offsetWidth:0) + w + 10);
this.adjustRange();
/* Position labels on the axis */
for (i = 0; i < items.length; i++) {
y=this.charty+this.charth-(i*this.yGridDensity);
ty=this.charth-(i*this.yGridDensity);
item = items[i];
this._painter.fillRect(this.config.textColor,this.chartx - 5, y, 5, 1);
item.style.position = 'absolute';
item.style.right = '0px';
item.style.top = ty + 'px';
item.style.color=this.config.textColor;
}
};
/*
* Internal function to draw horixontal labels and ticks
*/
Chart.prototype.drawHorizontalLabels = function() {
var axis, item, step, x, tx;
var xlen, labels, xgd, precision;
labels=this.config.xLabels;
/* Create container */
axis = document.createElement('div');
axis.style.position = 'absolute';
axis.style.left = '0px';
axis.style.top = (this.charty + this.charth + 5) + 'px';
axis.style.width = this.w + 'px';
this._cont.appendChild(axis);
/* Draw labels and points */
x = this.chartx;
for (i = 0; i < this.xlen; i++) {
item = document.createElement('span');
if (labels[i]) {
item.appendChild(document.createTextNode(labels[i]));
}
axis.appendChild(item);
tx = x - (item.offsetWidth/2);
item.style.position = 'absolute';
item.style.left = tx + 'px';
item.style.top = '0px';
item.style.color=this.config.textColor;
this._painter.fillRect(this.config.textColor,x, this.charty + this.charth, 1, 5);
x += this.xstep;
}
};
/*----------------------------------------------------------------------------\
| AbstractChartSeries |
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -|
| Abstract class for common methods across all types of Charts |
\----------------------------------------------------------------------------*/
function AbstractChartSeries() {
}
AbstractChartSeries.prototype.getColor = function() {
return this.config.color;
};
AbstractChartSeries.prototype.getLabel = function() {
return this.config.label;
};
AbstractChartSeries.prototype.getRange = function(chart) {
var i,ymin,ymax,xlen;
var values=this.getStackedValues(chart);
xlen=values.length;
ymin=values[0];
ymax=ymin;
for (i = 0; i < this.config.values.length; i++) {
ymin=Math.min(values[i],ymin);
ymax=Math.max(values[i],ymax);
}
return {xlen:xlen,ymin:ymin,ymax:ymax};
};
AbstractChartSeries.prototype.toOffset = function() {
return 0;
};
AbstractChartSeries.prototype.getStackedValues = function(chart) {
var stacked=new Array();
if (this.config.stackedOn) {
var stackedSeries=chart.find(this.config.stackedOn);
if (stackedSeries) {
stacked=stackedSeries.getStackedValues(chart);
}
}
for(var i=0;i<this.config.values.length;i++) {
if (stacked[i]) {
stacked[i]=parseFloat(stacked[i])+parseFloat(this.config.values[i]);
} else {
stacked[i]=this.config.values[i];
}
}
return stacked;
};
AbstractChartSeries.prototype.setConfig = function(name,value) {
if (!value && typeof name == Object) {
this.config=name;
} else {
this.config[name] = value;
}
};
AbstractChartSeries.prototype.getConfig = function() {
if (name) {
return this.config[name];
} else {
return this.config;
}
};
/*----------------------------------------------------------------------------\
| BarChartSeries |
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -|
| Bar Chart Series |
\----------------------------------------------------------------------------*/
function BarChartSeries(config) {
// config hash contains keys
var defaultConfig = {
label:"BarChart",// label - name of series
color:"#000", // color - HTML color for series
values:[], // values - array of values
distance:0, // distance - Sets distance between bars for bar charts.
width:10, // width - Sets with of bars for bar charts
offset:0 // offset - index of the bar in the chart
};
for (var p in config) {defaultConfig[p]=config[p];}
this.config=defaultConfig;
this.offset=0;
}
BarChartSeries.prototype=new AbstractChartSeries;
BarChartSeries.prototype._getRange=AbstractChartSeries.prototype.getRange;
BarChartSeries.prototype.getRange = function(chart) {
var range=this._getRange(chart);
range.xlen++;
if (chart.offset && (!this.config.stackedOn || this.config.stackedOn=='')) {
this.offset = this.config.distance + chart.offset * (this.config.width + this.config.distance);
} else {
if (this.config.stackedOn) {
var stackedOn=chart.find(this.config.stackedOn);
if (stackedOn) {
this.offset=stackedOn.offset;
}
}
}
return range;
};
BarChartSeries.prototype.toOffset = function() {
return (!this.config.stackedOn || this.config.stackedOn=='')?1:0;
};
BarChartSeries.prototype.draw = function(chart) {
// draws a bar chart
var i, len, x, y, barHt, yBottom, n, yoffset,painter,values;
painter=chart.getPainter();
values=this.getStackedValues(chart);
if (values.length<=0) return;
/* Determine distance between points and offset */
n = chart.range / chart.charth;
yoffset = (chart.ymin / n);
yBottom = chart.charty + chart.charth + yoffset;
len = values.length;
if (len > chart.xlen) { len = chart.xlen; }
if (len) {
/* Determine position of each bar and draw it */
x = chart.chartx + this.offset + 1;
for (i = 0; i < len; i++) {
y = (values[i] / n);
barHt = (this.config.values[i] / n);
painter.fillRect(this.config.color,x,yBottom-y,this.config.width,barHt);
x += chart.xstep;
}
}
};
/*----------------------------------------------------------------------------\
| AreaChartSeries |
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -|
| Area Chart Series |
\----------------------------------------------------------------------------*/
function AreaChartSeries(config) {
// config hash contains keys
var defaultConfig = {
label:"AreaChart",// label - name of series
color:"#000", // color - HTML color for series
values:[] // values - array of values
};
for (var p in config) {defaultConfig[p]=config[p];}
this.config=defaultConfig;
this.base=AbstractChartSeries;
}
AreaChartSeries.prototype=new AbstractChartSeries;
AreaChartSeries.prototype.draw = function(chart) {
// draws a bar chart
var i, len, x, y, barHt, yBottom, n, yoffset,painter,values;
var points=[];
painter=chart.getPainter();
values=this.getStackedValues(chart);
if (values.length<=0) return;
/* Determine distance between points and offset */
n = chart.range / chart.charth;
yoffset = (chart.ymin / n);
yBottom = chart.charty + chart.charth + yoffset;
/* Begin line in lower left corner */
points.push({x:chart.chartx + 1,y:chart.charty + chart.charth - 1});
len = values.length;
if (len > chart.xlen) { len = chart.xlen; }
if (len) {
/* Determine position of each bar and draw it */
for (i = 0; i < len; i++) {
y = (values[i] / n);
barHt = (this.config.values[i] / n);
points.push({x:chart.chartx + 1 + chart.xstep*i,y:yBottom-y});
}
/* Add end point at last value height, right edge */
points.push({x:chart.chartx+chart.chartw,y:yBottom-y});
/* Add end point at lower right corner */
points.push({x:chart.chartx+chart.chartw,y:yBottom-y+barHt});
for (i = len-1; i >=0; i--) {
y = (values[i] / n);
barHt = (this.config.values[i] / n);
points.push({x:chart.chartx + 1 + chart.xstep*i,y:yBottom-y+barHt});
}
painter.fillArea(this.config.color,points);
}
};
/*----------------------------------------------------------------------------\
| LineChartSeries |
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -|
| Line Chart Series |
\----------------------------------------------------------------------------*/
function LineChartSeries(config) {
// config hash contains keys
var defaultConfig = {
label:"LineChart",// label - name of series
lineWidth:2, // line width
color:"#000", // color - HTML color for series
values:[] // values - array of values
};
for (var p in config) {defaultConfig[p]=config[p];}
this.config=defaultConfig;
this.base=AbstractChartSeries;
}
LineChartSeries.prototype=new AbstractChartSeries;
LineChartSeries.prototype.draw = function(chart) {
var i, len, x, y, n, yoffset,yBottom;
painter=chart.getPainter();
values=this.getStackedValues(chart);
if (values.length<=0) return;
var points=[];
/* Determine distance between points and offset */
n = chart.range / chart.charth;
yoffset = (chart.ymin / n);
yBottom = chart.charty + chart.charth + yoffset;
/* Add points */
y=0;
for (i=0;i <values.length;i++) {
y = (values[i] / n);
points.push({x:chart.chartx+1+i*chart.xstep,y:yBottom-y});
}
/* Add end point at last value height, right edge */
points.push({x:chart.chartx+chart.chartw,y:yBottom-y});
painter.polyLine(this.config.color,this.config.lineWidth,points);
};
/*----------------------------------------------------------------------------\
| AbstractChartPainter |
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -|
| Abstract base class defining the painter API. Can not be used directly. |
\----------------------------------------------------------------------------*/
function AbstractChartPainter() {
};
AbstractChartPainter.prototype.create = function(el) {};
AbstractChartPainter.prototype.getWidth = function() {
return this.w;
};
AbstractChartPainter.prototype.getHeight = function() {
return this.h;
};
AbstractChartPainter.prototype.fillArea = function(color, points) {};
AbstractChartPainter.prototype.polyLine = function(color,lineWidth,points) {};
AbstractChartPainter.prototype.fillRect = function(color, x,y,width,height) {};
AbstractChartPainter.prototype.line = function(color,lineWidth,x1,y1,x2,y2) {};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -