摘要:我們提前做些準(zhǔn)備工作,比如畫圖前先把坐標(biāo)邊距做出來,還有各種字體大小算出來。最終代碼如下,模仿一月一月一月基礎(chǔ)信息為啥這么做我懶,定義一個比例為啥是,看圖大體是這個隨便定的,別忒離譜就好標(biāo)識準(zhǔn)備原因同上初始化偷個懶,基礎(chǔ)函數(shù)不寫了
我們提前做些準(zhǔn)備工作,比如畫圖前先把坐標(biāo)邊距做出來,還有各種字體大小算出來。
這里需要一點(diǎn)面向?qū)ο蟮闹R,
"use strict"; //模仿 http://echarts.baidu.com/demo.html#bar-gradient window.onload = function(){ var data = [ {"label":"一月","value":getRandomInt(0,400)}, {"label":"一月","value":getRandomInt(1,400)}, {"label":"一月","value":getRandomInt(1,400)} ]; var targetId = "bchart"; var cw = 800; var ch = 600; function Bcharts(targetId,cw,ch,data){ //基礎(chǔ)信息 var c = this;//為啥這么做?我懶,c = chart = this; c.targetId = targetId; c.cw = cw; c.ch = ch; c.data = data; //坐標(biāo)準(zhǔn)備,為啥要準(zhǔn)備?因?yàn)樽鴺?biāo)和字體都應(yīng)該是動態(tài)算出來的不能是寫死的否則實(shí)用性不夠 c.axeRadio = 10;//定義一個比例,為啥是10,看圖大體是這個隨便定的,別忒離譜就好 c.horGap = (cw*axeRadio)/100; c.verGap = (ch*axeRadio)/100; //標(biāo)識準(zhǔn)備 c.fontRadio = 3;//原因同上 c.horFontSize = (cw*fontRadio)/100; c.verFontSize = (ch*fontRadio)/100; } var charts = new Bcharts(targetId,cw,ch,data); }; //https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Math/random // 偷個懶,基礎(chǔ)函數(shù)不寫了 function getRandomInt(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive }
說一點(diǎn),你如果不懂就先按照我寫的弄出來,別自己發(fā)明創(chuàng)造,寫多了你也就知道為什么我這么寫了。當(dāng)然上面這個寫法挺惡心的,讓我想起了,沒有整理過的女生宿舍,
一坨坨代碼,所以我們我們整理下結(jié)構(gòu)。
"use strict"; //模仿 http://echarts.baidu.com/demo.html#bar-gradient window.onload = function(){ var data = [ {"label":"一月","value":getRandomInt(0,400)}, {"label":"一月","value":getRandomInt(1,400)}, {"label":"一月","value":getRandomInt(1,400)} ]; var targetId = "bchart"; var cw = 800; var ch = 600; function Bcharts(targetId,cw,ch,data){ //基礎(chǔ)信息 var c = this;//為啥這么做?我懶,c = chart = this; c.configureChart(targetId,cw,ch,data); } Bcharts.prototype.configureChart = function(targetId,cw,ch,data){ var c = this; c.setCanvasParameters(targetId,cw,ch,data); c.setChartParameters(targetId,cw,ch,data); }; Bcharts.prototype.setCanvasParameters = function(targetId,cw,ch,data){ var c = this; c.targetId = targetId; c.cw = cw; c.ch = ch; c.data = data; }; Bcharts.prototype.setChartParameters = function(targetId,cw,ch,data){ var c = this; c.axeRadio = 10;//定義一個比例,為啥是10,看圖大體是這個隨便定的,別忒離譜就好 c.horGap = (c.cw*c.axeRadio)/100; c.verGap = (c.ch*c.axeRadio)/100; //標(biāo)識準(zhǔn)備 c.fontRadio = 3;//原因同上 c.horFontSize = (c.cw*c.fontRadio)/100; c.verFontSize = (c.ch*c.fontRadio)/100; }; var charts = new Bcharts(targetId,cw,ch,data); }; //https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Math/random // 偷個懶,基礎(chǔ)函數(shù)不寫了 function getRandomInt(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive }
忽然感覺就像整理過的女生宿舍有么有,太整齊了!
好,整理好了代碼,先懟一個canvas畫布進(jìn)去,這里注意我也用的面向?qū)ο蠓绞剑x了一個init函數(shù),注意我把targetId改了一下,之前寫錯了。
最終代碼如下,
"use strict"; //模仿 http://echarts.baidu.com/demo.html#bar-gradient window.onload = function(){ var data = [ {"label":"一月","value":getRandomInt(0,400)}, {"label":"一月","value":getRandomInt(1,400)}, {"label":"一月","value":getRandomInt(1,400)} ]; var targetId = "div1"; var cw = 800; var ch = 600; function Bcharts(targetId,cw,ch,data){ //基礎(chǔ)信息 var c = this;//為啥這么做?我懶,c = chart = this; c.configureChart(targetId,cw,ch,data); c.init(); } Bcharts.prototype.configureChart = function(targetId,cw,ch,data){ var c = this; c.setCanvasParameters(targetId,cw,ch,data); c.setChartParameters(targetId,cw,ch,data); }; Bcharts.prototype.setCanvasParameters = function(targetId,cw,ch,data){ var c = this; c.id = targetId; c.cw = cw; c.ch = ch; c.data = data; }; Bcharts.prototype.setChartParameters = function(targetId,cw,ch,data){ var c = this; c.axeRadio = 10;//定義一個比例,為啥是10,看圖大體是這個隨便定的,別忒離譜就好 c.horGap = (c.cw*c.axeRadio)/100; c.verGap = (c.ch*c.axeRadio)/100; //標(biāo)識準(zhǔn)備 c.fontRadio = 3;//原因同上 c.horFontSize = (c.cw*c.fontRadio)/100; c.verFontSize = (c.ch*c.fontRadio)/100; }; //初始化 Bcharts.prototype.init = function(){ var c = this; c.createCanvas(); }; Bcharts.prototype.createCanvas = function(){ var c = this; var canvas = document.createElement("canvas"); canvas.id = c.id + "-" + Math.random(); canvas.width = c.cw; canvas.height = c.ch; document.getElementById(c.id).innerHTML = ""; document.getElementById(c.id).appendChild(canvas); c.canvas = canvas; c.context = c.canvas.getContext("2d"); }; var charts = new Bcharts(targetId,cw,ch,data); }; //https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Math/random // 偷個懶,基礎(chǔ)函數(shù)不寫了 function getRandomInt(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/92294.html
摘要:弄了一堆線方塊和函數(shù),感覺挺玄乎,然并卵。我們直接寫個項(xiàng)目看看。一個比較拽的應(yīng)用就是圖表和游戲,我們從淺入深,先玩圖表,圖表我們今天說一個最簡單柱狀圖。第三件事,數(shù)據(jù)我用的模擬,實(shí)際項(xiàng)目一般是后臺提供。 弄了一堆線方塊和函數(shù),感覺挺玄乎,然并卵。我們直接寫個項(xiàng)目看看。 canvas一個比較拽的應(yīng)用就是圖表和游戲,我們從淺入深,先玩圖表,圖表我們今天說一個最簡單柱狀圖。showImg(h...
摘要:最終效果圖下面代碼只有一個難點(diǎn)怎么算橫軸縱軸坐標(biāo)位置其余就容易多了,有一個注意問題,我其實(shí)寫復(fù)雜了各種位置都是動態(tài)算的,因?yàn)閷?shí)際項(xiàng)目你用寫死的數(shù)值瀏覽器適配就會出問題。 上一篇文章我已經(jīng)說過數(shù)據(jù)準(zhǔn)備好畫圖就非常簡單了,如果你擔(dān)心不回話好辦,我給你個最簡單的小例子, 你先練練。showImg(https://segmentfault.com/img/remote/146000001252...
閱讀 1849·2023-04-26 01:58
閱讀 1982·2019-08-30 11:26
閱讀 2730·2019-08-29 12:51
閱讀 3495·2019-08-29 11:11
閱讀 1183·2019-08-26 11:54
閱讀 2096·2019-08-26 11:48
閱讀 3478·2019-08-26 10:23
閱讀 2384·2019-08-23 18:30