先来看看效果图
下面直接上源代码 , html 文件
<html> <head> <meta charset = "utf-8"> <title> 实时刷新折线图 </title> <style> .axis Path, .xis linha {preenchimento: nenhum; AVC: preto; Renderização de formas: crise; } .xis text {font-family: sans-serif; Size da fonte: 11px; } .overlay {preenchimento: nenhum; Ponteiro-eventos: todos; } .tooltip {width: 150px; Altura: Auto; Posição: Absoluto; Fonte-família: Simsun; Size da fonte: 16px; altura da linha: 26px; Alinhamento de texto: esquerda; borda: 1px preto sólido; cor de fundo: branco; Radio de fronteira: 5px; } .ToolTip .Title {Border-Bottom: 1px Solid #000; Alinhamento de texto: centro; } .tooltip .descolor {width: 10px; Altura: 10px; flutuar: esquerda; margem: 9px 8px 1px 8px; } .ToolTip .DestExt {Display: Inline; } .focusline {stroke: preto; largura de derrame: 1px; Dascarra de AVC: 5,5; } </style> </head> <body> <script src = "http://d3js.org/d3.js" charset = "utf-8"> </script> <script src = "linechart.js" charting = "ut-8"> </script> <svgscriptsncript.js " xmlns: xlink = "http://www.w3.org/1999/xlink" zoomandpan = "magny" style = "background-color: #ffffff;" ContentStyleType = "Text/CSS" ViewBox = "0 0 2000.0 2000.0" PreserveasPectratio = "Xmidymid meet" xmlns = "http://www.w3.org/2000/svg" versão "1.0"> <fs> <ymbol Id = "FillGauge1" preserveaspetratio = "nenhum me encontra"> </symbol> <símbolo id = "preenchgauge2" viewBox = "0 0 200.0 200.0" preserveaspetratio = "nenhum me encontra"> </symbol> </defs> <use x = "0" y = "0" xmlns: xlink = "http.. xlink:href="#fillgauge1" xlink:type="simple" xlink:actuate="onLoad" symboltype="17" xlink:show="embed"/><use x="20" y="20" xmlns:xlink="http://www.w3.org/1999/xlink" xlink: href = "#FILLGAUGE2" xlink: type = "simples" xlink: acacate = "onLload" symboltype = "17" xlink: show = "incorped"/> </svg> <cript> var dados = [{country: "China", GDP: [2000,11920], [{country: China ", GDP: [2000,11920], [{country: China", GDP: [2000,11920]. [2003,16500], [2004,19440], [2005,22870], [2006,27930], [2007.35040], [2008,45470], [2009.51050], [2010,59490], [2011,73140], [2012050], [2010,59490], [2011,73140], [2012050], [2010,59490], [2011,73140] país: "EUA", PIB: [[2000.47310], [2001,41590], [2002,39800], [2003.43020], [2004,46550], [2005.45710], [2006, [2009.50350], [2010.54950], [2011.59050], [2012.59370], [2013.48980]]}]; var gauge1 = loadlineChart ("FillGauge1", DataSet); function newValue () {if (Math.random ()> .5) {return Math.Round (Math.random ()*100); } else {return (math.random ()*100) .tofixed (1); }} função refreshline (gauge1) {// alert ("hehe"); // document.getElementById ("FILLGAUGE1"). Innerhtml = ""; var updateTata = [{country: "China", PIB: [[2000, newValue ()], [2001, newValue ()], [2002, newValue ()], [2003, newvalue ()], [2004, newvalue ()], [2005, newvalue ()], [2006, NewValue ()], [2007, NewValue ()], [2008, NewValue ()], [2009, NewValue ()], [2010, NewValue ()], [2011, NewValue ()], [2012, "USA ()], [2013", ()], [), [2012, "USA (), [2013, (2013 ()]]], [2012," USA ()], [2013 ", ()], [2012," USA (), [2013, " [[2000, NewValue ()], [2001, NewValue ()], [2002, NewValue ()], [2003, NewValue ()], [2004, NewValue ()], [2005, NewValue ()], [2006, NewValue ()], [2007, NewValue ()], [2008, [2006, NEWVALUE ()], [2007, NewValue ()], [2008, [2006, NEWVALUE ()], [2007, NewValue ()], [2008, [2006, NEWVALUE ()], [2007, NewValue ()], [2008, [2006, NEWVALUE ()], [2007, NewValue ()], [2008, [2009, NewValue ()], [2010, NewValue ()], [2011, NewValue ()], [2012, NewValue ()], [2013, NewValue ()]]}]; gauge1.Update (UpdatedAtA); } self.setInterval ("Refreshline (gauge1)", 5000); </sCript> </body> </html>linechart.js 负责加载和刷新折线图
função loadlinechart (elementId, conjunto de dados) {var svg = d3.select ("#" + elementId); var strs = svg.attr ("ViewBox"). Split (""); alerta (conjunto de dados); var largura = strs [2]; altura var = strs [3]; // 外边框 var preto = {topo: 50, direita: 50, inferior: 50, esquerda: 50}; var nomes = new Array (); // 计算 PIB 的最大值 var gdpmax = 0; for (var i = 0; i <dataset.length; i ++) {var currgdp = d3.max (DataSet [i] .gdp, function (d) {return d [1];}); if (currgdp> gdpmax) gdpmax = currgdp; } var gdpnumb = conjunto de dados [0] .gdp.length; for (var j = 0; j <gdpnumb; j ++) {nomes [j] = (conjunto de dados [0] .gdp [j]) [0]; } alerta (nomes); var xScale = d3.Scale.Linear () .Domain ([2000, 2013]) .Range ([0, largura - preenchimento.left - preenchimento.right]); // var xScale = d3.scale.ordinal () // .Domain (nomes) // .RangeroundBands ([0, largura - preenchimento.left - padding.right]); var yScale = d3.scale.linear () .Domain ([0, gdpmax * 1.1]) .Range ([altura - preenchimento.top - preenchimento.bottom, 0]); // 创建一个直线生成器 var linePath = d3.svg.line () .x (function (d) {return xscale (d [0]);}) .y (function (d) {return yscale (d [1]);}) .interpolate ("base"); // 定义两个颜色 var colors = [d3.rgb (0, 0, 255), d3.rgb (0, 255, 0)]; // 添加路径 svg.selectall ("caminho") // 选择 <svg> 中所有的 <tych> .data (dados de dados) // 绑定数据 .enter () // 选择 enter 部分 .append ("path") // 添加足够数量的 <caminho> 元素 .attr ("transform", "transllate (" + padding.left + "" ". {retornar linePath (d.gdp); // 添加垂直于 x 轴的对齐线 var vLine = svg.append ("line") .attr ("classe", "focus") .style ("display", "nenhum"); // 添加一个提示框 var Tooltip = d3.Select ("Body") .Append ("Div") .Attr ("classe", "ToolTip") .style ("Opacity", 0,0); var title = Tooltip.append ("div") .attr ("classe", "title"); var des = ToolTip.Selectall (". Des") .Data (DataSet) .Enter () .Append ("Div"); var descolor = des.append ("div") .attr ("classe", "descolor"); var destrext = des.append ("div") .attr ("classe", "destrext"); // 添加一个透明的监视鼠标事件用的矩形 svg.append ("ret") .attr ("classe", "sobreposição") .attr ("x", preenchimento.left) .attr ("y", padding.top) .attr ("largura", padding - padding. Tooltip.style ("Esquerda", (d3.event.pagex) + "px") .style ("top", (d3.event.pagey + 20) + "px") .style ("opacidade", 1.0); vline.style ("display", "nenhum"); function mouseMove () { / * 当鼠标在透明矩形内滑动时调用 * /// 折线的源数组 var dados = conjunto de dados [0] .gdp; // 获取鼠标相对于透明矩形左上角的坐标 , 左上角坐标为 (0,0) var mousex = d3.mouse (this) [0] - preenchimento.left; var mousey = d3.mouse (this) [1] - preenchimento.top; // 通过比例尺的反函数计算原数据中的值 , 例如 x0 为某个年份 , y0 为 PIB 值 var x0 = xScale.invert (mousex); var y0 = yscale.invert (mousey); // 对 x0 四舍五入 , 如果 x0 是 2005.6 , 则返回 2006 ;如果是 2005.2 , 则返回 2005 x0 = math.Round (x0); // 查找在原数组中 x0 的值 , 并返回索引号 var bisect = d3.bisector (função (d) {return d [0];}). Esquerda; var index = bisect (dados, x0); // 获取年份和 PIB 数据 var ano = x0; var PIB = []; for (var k = 0; k <dataset.length; k ++) {PIB [k] = {country: DataSet [k] .country, valor: DataSet [k] .gdp [index] [1]}; } // 设置提示框的标题文字 (年份) title.html ("<strong>" + ano + "年 </strong>"); // 设置颜色标记的颜色 Descolor.style ("Background-Color", function (d, i) {return colors [i];}); // 设置描述文字的内容 Destext.html (function (d, i) {return PIB [i] .country + "/t" + "<strong>" + GDP [i] .Value + "</strong>";}); // 设置提示框的位置 Tooltip.style ("esquerda", (d3.event.pagex) + "px") .style ("top", (d3.event.pagey + 20) + "px"); // 获取垂直对齐线的 x 坐标 var vlx = xScale (dados [index] [0]) + padding.left; // 设定垂直对齐线的起点和终点 vline.attr ("x1", vlx) .attr ("y1", padding.top) .attr ("x2", vlx) .attr ("y2", altura - padding.bottom); } var markstep = 80; var gmark = svg.Selectall (". Gmark") .Data (DataSet) .Enter () .Append ("G") .Attr ("Transform", function (d, i) {return "Tradlate (" + (Padding.left + i * Markstep) + "," (altura - Padding.bottom + 40) gmark.append ("rect") .attr ("x", 0) .attr ("y", 0) .attr ("largura", 10) .attr ("altura", 10) .attr ("preenchimento", função (d, i) {retorna as cores [i];}); gmark.append ("text") .attr ("dx", 15) .attr ("dy", ".5em") .attr ("preenchimento", "preto") .text (function (d) {return d.country;}); // x 轴 var xaxis = d3.svg.axis () .Scale (xSCale) .Ticks (5) .TickFormat (d3.Format ("d") .orient ("Bottom"); // y 轴 var yaxis = d3.svg.axis () .cale (yscale) .orient ("esquerda"); svg.append ("g") .attr ("classe", "eixo") .attr ("transform", "translate (" + padding.left + "," + (altura - padding.bottom) + ")") .call (xaxis); svg.append ("g") .attr ("classe", "y eixo") .attr ("transform", "translate (" + padding.left + "," + padding.top + ")) .call (yaxis); função updatelineChart () {this.update = function (updatedAtAtA) {//xscale.Domain(UpDatedAtAtA.Function(d) {Return d.Name}); var numValues = updatedata.length; var updategdpmax = 0; for (var i = 0; i <updateTata.length; i ++) {var currgdp = d3.max (updatedata [i] .gdp, function (d) {return d [1];}); if (currgdp> updategdpmax) updategdpmax = currgdp; } yscale = d3.scale.linear () .Domain ([0, updategdpmax * 1.1]) .Range ([altura - preenchimento.top - padding.bottom, 0]); yaxis = d3.svg.axis () .cale (yscale) .orient ("esquerda"); svg.selectall ("gyaxis") .call (yaxis); svg.Selectall ("Path") // 选择 <svg> 中所有的 <tath> .data (updatedata) // 绑定数据 .Transition () .Duration (2000) .Ease ("linear") .attr ("d", function (d) {return linepath (d.gdp); // 返回直线生成器得到的路径}); }} Retorne new UpdateLineChart ();}刚开始数据刷新了但是坐标轴木有刷新
引入
svg.selectall ("gyaxis") .call (yaxis);以上就是 d3.js 实现实时刷新折线图的全部内容 , 希望给大家学习 d3.js 带来帮助。