📄 chart.js
字号:
// set up the chart; each is a method so that it can be selectively overridden. this.drawVectorNode(); this.drawPlotArea(); this.drawDataGroup(); this.drawAxes(); // this is last. this.domNode.appendChild(this.vectorNode); this.assignColors(); this._isInitialized=true; }, destroy:function(){ // summary // Node cleanup while(this.domNode.childNodes.length>0){ this.domNode.removeChild(this.domNode.childNodes.item(0)); } this.vectorNode=this.plotArea=this.dataGroup=this.axisGroup=null; }, render:function(){ // summary // Draws the data on the chart dojo.svg.g.suspend(); if (this.dataGroup){ while(this.dataGroup.childNodes.length>0){ this.dataGroup.removeChild(this.dataGroup.childNodes.item(0)); } } else { this.init(); } // plot it. for(var i=0; i<this.series.length; i++){ dojo.widget.svg.Chart.Plotter.plot(this.series[i], this); } dojo.svg.g.resume(); }, postCreate:function(){ // summary // Parse any data if included with the chart, and kick off the rendering. var table=this.domNode.getElementsByTagName("table")[0]; if (table){ var ranges=this.parseProperties(table); var bRangeX=false; var bRangeY=false; // fix the axes var axisValues = this.parseData(table); if(!bRangeX){ this.properties.axes.x.range={min:axisValues.x.min, max:axisValues.x.max}; } if(!bRangeY){ this.properties.axes.y.range={min:axisValues.y.min, max:axisValues.y.max}; } this.setAxesPlot(table); // table values should be populated, now pop it off. this.domNode.removeChild(table); } if(this.series.length>0){ this.render(); } } });dojo.widget.svg.Chart.Plotter=new function(){ // summary // Singleton for plotting series of data. var self=this; var plotters = {}; var types=dojo.widget.Chart.PlotTypes; this.getX=function(/* string||number */value, /* dojo.widget.Chart */chart){ // summary // Calculate the x coord on the passed chart for the passed value var v=parseFloat(value); var min=chart.properties.axes.x.range.min; var max=chart.properties.axes.x.range.max; var ofst=0-min; min+=ofst; max+=ofst; v+=ofst; var xmin=chart.properties.padding.left; var xmax=chart.properties.width-chart.properties.padding.right; var x=(v*((xmax-xmin)/max))+xmin; return x; // float }; this.getY=function(/* string||number */value, /* dojo.widget.Chart */chart){ // summary // Calculate the y coord on the passed chart for the passed value var v=parseFloat(value); var max=chart.properties.axes.y.range.max; var min=chart.properties.axes.y.range.min; var ofst=0; if(min<0)ofst+=Math.abs(min); min+=ofst; max+=ofst; v+=ofst; var ymin=chart.properties.height-chart.properties.padding.bottom; var ymax=chart.properties.padding.top; var y=(((ymin-ymax)/(max-min))*(max-v))+ymax; return y; // float }; this.addPlotter=function(/* string */name, /* function */func){ // summary // add a custom plotter function to this object. plotters[name]=func; }; this.plot=function(/* dojo.widget.Chart.DataSeries */series, /* dojo.widget.Chart */chart){ // summary // plot the passed series. if (series.values.length==0) return; // void if (series.plotType && plotters[series.plotType]){ return plotters[series.plotType](series, chart); // void } else if (chart.plotType && plotters[chart.plotType]){ return plotters[chart.plotType](series, chart); // void } }; // plotting plotters["bar"]=function(/* dojo.widget.Chart.DataSeries */series, /* dojo.widget.Chart */chart){ // summary // plot the passed series as a set of bars. var space=1; var lastW = 0; for (var i=0; i<series.values.length; i++){ var x=self.getX(series.values[i].x, chart); var w; if (i==series.values.length-1){ w=lastW; } else{ w=self.getX(series.values[i+1].x, chart)-x-space; lastW=w; } x-=(w/2); var yA=self.getY(chart.properties.axes.x.plotAt, chart); var y=self.getY(series.values[i].value, chart); var h=Math.abs(yA-y); if (parseFloat(series.values[i].value)<chart.properties.axes.x.plotAt){ var oy=yA; yA=y; y=oy; } var bar=document.createElementNS(dojo.svg.xmlns.svg, "rect"); bar.setAttribute("fill", series.color); bar.setAttribute("title", series.label + ": " + series.values[i].value); bar.setAttribute("stroke-width", "0"); bar.setAttribute("x", x); bar.setAttribute("y", y); bar.setAttribute("width", w); bar.setAttribute("height", h); bar.setAttribute("fill-opacity", "0.9"); chart.dataGroup.appendChild(bar); } }; plotters["line"]=function(/* dojo.widget.Chart.DataSeries */series, /* dojo.widget.Chart */chart){ // summary // plot the passed series as a line with tensioning var tension=1.5; var line = document.createElementNS(dojo.svg.xmlns.svg, "path"); line.setAttribute("fill", "none"); line.setAttribute("stroke", series.color); line.setAttribute("stroke-width", "2"); line.setAttribute("stroke-opacity", "0.85"); line.setAttribute("title", series.label); chart.dataGroup.appendChild(line); var path = []; for (var i=0; i<series.values.length; i++){ var x = self.getX(series.values[i].x, chart) var y = self.getY(series.values[i].value, chart); var dx = chart.properties.padding.left+1; var dy = chart.properties.height-chart.properties.padding.bottom; if (i>0){ dx=x-self.getX(series.values[i-1].x, chart); dy=self.getY(series.values[i-1].value, chart); } if (i==0) path.push("M"); else { path.push("C"); var cx=x-(tension-1)*(dx/tension); path.push(cx+","+dy); cx=x-(dx/tension); path.push(cx+","+y); } path.push(x+","+y); } line.setAttribute("d", path.join(" ")); }; plotters["area"]=function(/* dojo.widget.Chart.DataSeries */series, /* dojo.widget.Chart */chart){ // summary // plot the passed series as an area with tensioning. var tension=1.5; var line = document.createElementNS(dojo.svg.xmlns.svg, "path"); line.setAttribute("fill", series.color); line.setAttribute("fill-opacity", "0.4"); line.setAttribute("stroke", series.color); line.setAttribute("stroke-width", "1"); line.setAttribute("stroke-opacity", "0.8"); line.setAttribute("title", series.label); chart.dataGroup.appendChild(line); var path = []; for (var i=0; i<series.values.length; i++){ var x = self.getX(series.values[i].x, chart) var y = self.getY(series.values[i].value, chart); var dx = chart.properties.padding.left+1; var dy = chart.properties.height-chart.properties.padding.bottom; if (i>0){ dx=x-self.getX(series.values[i-1].x, chart); dy=self.getY(series.values[i-1].value, chart); } if (i==0) path.push("M"); else { path.push("C"); var cx=x-(tension-1)*(dx/tension); path.push(cx+","+dy); cx=x-(dx/tension); path.push(cx+","+y); } path.push(x+","+y); } // finish it off path.push("L"); path.push(x + "," + self.getY(0, chart)); path.push("L"); path.push(self.getX(0, chart) + "," + self.getY(0, chart)); path.push("Z"); line.setAttribute("d", path.join(" ")); }, plotters["scatter"]=function(/* dojo.widget.Chart.DataSeries */series, /* dojo.widget.Chart */chart){ // summary // plot the passed series as a scatter chart var r=7; for (var i=0; i<series.values.length; i++){ var x=self.getX(series.values[i].x, chart); var y=self.getY(series.values[i].value, chart); var point = document.createElementNS(dojo.svg.xmlns.svg, "path"); point.setAttribute("fill", series.color); point.setAttribute("stroke-width", "0"); point.setAttribute("title", series.label + ": " + series.values[i].value); point.setAttribute("d", "M " + x + "," + (y-r) + " " + "Q " + x + "," + y + " " + (x+r) + "," + y + " " + "Q " + x + "," + y + " " + x + "," + (y+r) + " " + "Q " + x + "," + y + " " + (x-r) + "," + y + " " + "Q " + x + "," + y + " " + x + "," + (y-r) + " " + "Z" ); chart.dataGroup.appendChild(point); } }; plotters["bubble"]=function(/* dojo.widget.Chart.DataSeries */series, /* dojo.widget.Chart */chart){ // summary // plot the passed series as a series of bubbles (scatter with 3rd dimension) // added param for series[n].value: size var minR=1; // do this off the x axis? var min=chart.properties.axes.x.range.min; var max=chart.properties.axes.x.range.max; var ofst=0-min; min+=ofst; max+=ofst; var xmin=chart.properties.padding.left; var xmax=chart.properties.width-chart.properties.padding.right; var factor=(max-min)/(xmax-xmin)*25; for (var i=0; i<series.values.length; i++){ var size = series.values[i].size; if (isNaN(parseFloat(size))) size=minR; var point=document.createElementNS(dojo.svg.xmlns.svg, "circle"); point.setAttribute("stroke-width", 0); point.setAttribute("fill", series.color); point.setAttribute("fill-opacity", "0.8"); point.setAttribute("r", (parseFloat(size)*factor)/2); point.setAttribute("cx", self.getX(series.values[i].x, chart)); point.setAttribute("cy", self.getY(series.values[i].value, chart)); point.setAttribute("title", series.label + ": " + series.values[i].value + " (" + size + ")"); chart.dataGroup.appendChild(point); } };}();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -