最好的 3 个开源 JavaScript 图表库
图表及其它可视化方式让传递数据的信息变得更简单。
对于数据可视化和制作精美网站来说,图表和图形很重要。视觉上的展示让分析大块数据及传递信息变得更简单。JavaScript 图表库能让数据以极好的、易于理解的和交互的方式进行可视化,还能够优化你的网站设计。
本文会带你学习最好的 3 个开源 JavaScript 图表库。
1、 Chart.js
Chart.js 是一个开源的 JavaScript 库,你可以在自己的应用中用它创建生动美丽和交互式的图表。使用它需要遵循 MIT 协议。
使用 Chart.js,你可以创建各种各样令人印象深刻的图表和图形,包括条形图、折线图、范围图、线性标度和散点图。它可以响应各种设备,使用 HTML5 Canvas 元素进行绘制。
示例代码如下,它使用该库绘制了一个条形图。本例中我们使用 Chart.js 的内容分发网络(CDN)来包含这个库。注意这里使用的数据仅用于展示。
<span class="dec"><!DOCTYPE html></span>
<span class="tag"><html></span>
<span class="tag"><head></span>
<span class="pln"> </span><span class="tag"><script</span><span class="atn">src</span><span class="pun">=</span><span class="atv">"https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"</span><span class="tag">></script></span>
<span class="tag"></head></span>
<span class="tag"><body></span>
<span class="pln"> </span>
<span class="pln"> </span><span class="tag"><canvas</span><span class="atn">id</span><span class="pun">=</span><span class="atv">"bar-chart"</span><span class="atn">width</span><span class="pun">=</span><span class="atv">300" height="</span><span class="pln">150</span><span class="atv">"></span><span class="tag"></canvas></span>
<span class="pln"> </span>
<span class="pln"> </span><span class="tag"><script></span>
<span class="pln"> </span>
<span class="kwd">new</span><span class="typ">Chart</span><span class="pun">(</span><span class="pln">document</span><span class="pun">.</span><span class="pln">getElementById</span><span class="pun">(</span><span class="str">"bar-chart"</span><span class="pun">),</span><span class="pun">{</span>
<span class="pln"> type</span><span class="pun">:</span><span class="str">'bar'</span><span class="pun">,</span>
<span class="pln"> data</span><span class="pun">:</span><span class="pun">{</span>
<span class="pln"> labels</span><span class="pun">:</span><span class="pun">[</span><span class="str">"North America"</span><span class="pun">,</span><span class="str">"Latin America"</span><span class="pun">,</span><span class="str">"Europe"</span><span class="pun">,</span><span class="str">"Asia"</span><span class="pun">,</span><span class="str">"Africa"</span><span class="pun">],</span>
<span class="pln"> datasets</span><span class="pun">:</span><span class="pun">[</span>
<span class="pln"> </span><span class="pun">{</span>
<span class="pln"> label</span><span class="pun">:</span><span class="str">"Number of developers (millions)"</span><span class="pun">,</span>
<span class="pln"> backgroundColor</span><span class="pun">:</span><span class="pun">[</span><span class="str">"red"</span><span class="pun">,</span><span class="str">"blue"</span><span class="pun">,</span><span class="str">"yellow"</span><span class="pun">,</span><span class="str">"green"</span><span class="pun">,</span><span class="str">"pink"</span><span class="pun">],</span>
<span class="pln"> data</span><span class="pun">:</span><span class="pun">[</span><span class="lit">7</span><span class="pun">,</span><span class="lit">4</span><span class="pun">,</span><span class="lit">6</span><span class="pun">,</span><span class="lit">9</span><span class="pun">,</span><span class="lit">3</span><span class="pun">]</span>
<span class="pln"> </span><span class="pun">}</span>
<span class="pln"> </span><span class="pun">]</span>
<span class="pln"> </span><span class="pun">},</span>
<span class="pln"> options</span><span class="pun">:</span><span class="pun">{</span>
<span class="pln"> legend</span><span class="pun">:</span><span class="pun">{</span><span class="pln"> display</span><span class="pun">:</span><span class="kwd">false</span><span class="pun">},</span>
<span class="pln"> title</span><span class="pun">:</span><span class="pun">{</span>
<span class="pln"> display</span><span class="pun">:</span><span class="kwd">true</span><span class="pun">,</span>
<span class="pln"> text</span><span class="pun">:</span><span class="str">'Number of Developers in Every Continent'</span>
<span class="pln"> </span><span class="pun">},</span>
<span class="pln"> scales</span><span class="pun">:</span><span class="pun">{</span>
<span class="pln"> yAxes</span><span class="pun">:</span><span class="pun">[{</span>
<span class="pln"> ticks</span><span class="pun">:</span><span class="pun">{</span>
<span class="pln"> beginAtZero</span><span class="pun">:</span><span class="kwd">true</span>
<span class="pln"> </span><span class="pun">}</span>
<span class="pln"> </span><span class="pun">}]</span>
<span class="pln"> </span><span class="pun">}</span>
<span class="pln"> </span><span class="pun">}</span>
<span class="pun">});</span>
<span class="pln"> </span><span class="tag"></script></span>
<span class="pln"> </span>
<span class="tag"></body></span>
<span class="tag"></html></span>
如你所见,通过设置 type
和 bar
来构造条形图。你可以把条形体的方向改成其他类型 —— 比如把 type
设置成 horizontalBar
。
在 backgroundColor
数组参数中提供颜色类型,就可以设置条形图的颜色。
颜色被分配给关联数组中相同索引的标签和数据。例如,第二个标签 “Latin American”,颜色会是 “蓝色(blue)”(第二个颜色),数值是 4(data 中的第二个数字)。
代码的执行结果如下。
2、 Chartist.js
Chartist.js 是一个简单的 JavaScript 动画库,你能够自制美丽的响应式图表,或者进行其他创作。使用它需要遵循 WTFPL 或者 MIT 协议。
这个库是由一些对现有图表工具不满的开发者进行开发的,它可以为设计师或程序员提供美妙的功能。
在项目中包含 Chartist.js 库后,你可以使用它们来创建各式各样的图表,包括动画,条形图和折线图。它使用 SVG 来动态渲染图表。
这里是使用该库绘制一个饼图的例子。
<span class="dec"><!DOCTYPE html></span>
<span class="tag"><html></span>
<span class="tag"><head></span>
<span class="pln"> </span>
<span class="pln"> </span><span class="tag"><link</span><span class="atn">href</span><span class="pun">=</span><span class="atv">"https//cdn.jsdelivr.net/chartist.js/latest/chartist.min.css"</span><span class="atn">rel</span><span class="pun">=</span><span class="atv">"stylesheet"</span><span class="atn">type</span><span class="pun">=</span><span class="atv">"text/css"</span><span class="tag">/></span>
<span class="pln"> </span>
<span class="pln"> </span><span class="tag"><style></span>
<span class="pln"> </span><span class="pun">.</span><span class="pln">ct</span><span class="pun">-</span><span class="pln">series</span><span class="pun">-</span><span class="pln">a </span><span class="pun">.</span><span class="pln">ct</span><span class="pun">-</span><span class="pln">slice</span><span class="pun">-</span><span class="pln">pie </span><span class="pun">{</span>
<span class="pln"> fill</span><span class="pun">:</span><span class="pln"> hsl</span><span class="pun">(</span><span class="lit">100</span><span class="pun">,</span><span class="lit">20</span><span class="pun">%,</span><span class="lit">50</span><span class="pun">%);</span><span class="com">/* filling pie slices */</span>
<span class="pln"> stroke</span><span class="pun">:</span><span class="pln"> white</span><span class="pun">;</span><span class="com">/*giving pie slices outline */</span><span class="pln"> </span>
<span class="pln"> stroke</span><span class="pun">-</span><span class="pln">width</span><span class="pun">:</span><span class="lit">5px</span><span class="pun">;</span><span class="pln"> </span><span class="com">/* outline width */</span>
<span class="pln"> </span><span class="pun">}</span>
<span class="pln"> </span><span class="pun">.</span><span class="pln">ct</span><span class="pun">-</span><span class="pln">series</span><span class="pun">-</span><span class="pln">b </span><span class="pun">.</span><span class="pln">ct</span><span class="pun">-</span><span class="pln">slice</span><span class="pun">-</span><span class="pln">pie </span><span class="pun">{</span>
<span class="pln"> fill</span><span class="pun">:</span><span class="pln"> hsl</span><span class="pun">(</span><span class="lit">10</span><span class="pun">,</span><span class="lit">40</span><span class="pun">%,</span><span class="lit">60</span><span class="pun">%);</span>
<span class="pln"> stroke</span><span class="pun">:</span><span class="pln"> white</span><span class="pun">;</span>
<span class="pln"> stroke</span><span class="pun">-</span><span class="pln">width</span><span class="pun">:</span><span class="lit">5px</span><span class="pun">;</span>
<span class="pln"> </span><span class="pun">}</span>
<span class="pln"> </span><span class="pun">.</span><span class="pln">ct</span><span class="pun">-</span><span class="pln">series</span><span class="pun">-</span><span class="pln">c </span><span class="pun">.</span><span class="pln">ct</span><span class="pun">-</span><span class="pln">slice</span><span class="pun">-</span><span class="pln">pie </span><span class="pun">{</span>
<span class="pln"> fill</span><span class="pun">:</span><span class="pln"> hsl</span><span class="pun">(</span><span class="lit">120</span><span class="pun">,</span><span class="lit">30</span><span class="pun">%,</span><span class="lit">80</span><span class="pun">%);</span>
<span class="pln"> stroke</span><span class="pun">:</span><span class="pln"> white</span><span class="pun">;</span>
<span class="pln"> stroke</span><span class="pun">-</span><span class="pln">width</span><span class="pun">:</span><span class="lit">5px</span><span class="pun">;</span>
<span class="pln"> </span><span class="pun">}</span>
<span class="pln"> </span><span class="pun">.</span><span class="pln">ct</span><span class="pun">-</span><span class="pln">series</span><span class="pun">-</span><span class="pln">d </span><span class="pun">.</span><span class="pln">ct</span><span class="pun">-</span><span class="pln">slice</span><span class="pun">-</span><span class="pln">pie </span><span class="pun">{</span>
<span class="pln"> fill</span><span class="pun">:</span><span class="pln"> hsl</span><span class="pun">(</span><span class="lit">90</span><span class="pun">,</span><span class="lit">70</span><span class="pun">%,</span><span class="lit">30</span><span class="pun">%);</span>
<span class="pln"> stroke</span><span class="pun">:</span><span class="pln"> white</span><span class="pun">;</span>
<span class="pln"> stroke</span><span class="pun">-</span><span class="pln">width</span><span class="pun">:</span><span class="lit">5px</span><span class="pun">;</span>
<span class="pln"> </span><span class="pun">}</span>
<span class="pln"> </span><span class="pun">.</span><span class="pln">ct</span><span class="pun">-</span><span class="pln">series</span><span class="pun">-</span><span class="pln">e </span><span class="pun">.</span><span class="pln">ct</span><span class="pun">-</span><span class="pln">slice</span><span class="pun">-</span><span class="pln">pie </span><span class="pun">{</span>
<span class="pln"> fill</span><span class="pun">:</span><span class="pln"> hsl</span><span class="pun">(</span><span class="lit">60</span><span class="pun">,</span><span class="lit">140</span><span class="pun">%,</span><span class="lit">20</span><span class="pun">%);</span>
<span class="pln"> stroke</span><span class="pun">:</span><span class="pln"> white</span><span class="pun">;</span>
<span class="pln"> stroke</span><span class="pun">-</span><span class="pln">width</span><span class="pun">:</span><span class="lit">5px</span><span class="pun">;</span>
<span class="pln"> </span><span class="pun">}</span>
<span class="pln"> </span><span class="tag"></style></span>
<span class="pln"> </span><span class="tag"></head></span>
<span class="tag"><body></span>
<span class="pln"> </span><span class="tag"><div</span><span class="atn">class</span><span class="pun">=</span><span class="atv">"ct-chart ct-golden-section"</span><span class="tag">></div></span>
<span class="pln"> </span><span class="tag"><script</span><span class="atn">src</span><span class="pun">=</span><span class="atv">"https://cdn.jsdelivr.net/chartist.js/latest/chartist.min.js"</span><span class="tag">></script></span>
<span class="pln"> </span><span class="tag"><script></span>
<span class="pln"> </span>
<span class="pln"> </span><span class="kwd">var</span><span class="pln"> data </span><span class="pun">=</span><span class="pun">{</span>
<span class="pln"> series</span><span class="pun">:</span><span class="pun">[</span><span class="lit">45</span><span class="pun">,</span><span class="lit">35</span><span class="pun">,</span><span class="lit">20</span><span class="pun">]</span>
<span class="pln"> </span><span class="pun">};</span>
<span class="pln"> </span><span class="kwd">var</span><span class="pln"> sum </span><span class="pun">=</span><span class="kwd">function</span><span class="pun">(</span><span class="pln">a</span><span class="pun">,</span><span class="pln"> b</span><span class="pun">)</span><span class="pun">{</span><span class="kwd">return</span><span class="pln"> a </span><span class="pun">+</span><span class="pln"> b </span><span class="pun">};</span>
<span class="pln"> </span><span class="kwd">new</span><span class="typ">Chartist</span><span class="pun">.</span><span class="typ">Pie</span><span class="pun">(</span><span class="str">'.ct-chart'</span><span class="pun">,</span><span class="pln"> data</span><span class="pun">,</span><span class="pun">{</span>
<span class="pln"> labelInterpolationFnc</span><span class="pun">:</span><span class="kwd">function</span><span class="pun">(</span><span class="pln">value</span><span class="pun">)</span><span class="pun">{</span>
<span class="pln"> </span><span class="kwd">return</span><span class="typ">Math</span><span class="pun">.</span><span class="pln">round</span><span class="pun">(</span><span class="pln">value </span><span class="pun">/</span><span class="pln"> data</span><span class="pun">.</span><span class="pln">series</span><span class="pun">.</span><span class="pln">reduce</span><span class="pun">(</span><span class="pln">sum</span><span class="pun">)</span><span class="pun">*</span><span class="lit">100</span><span class="pun">)</span><span class="pun">+</span><span class="str">'%'</span><span class="pun">;</span>
<span class="pln"> </span><span class="pun">}</span>
<span class="pln"> </span><span class="pun">});</span>
<span class="pln"> </span><span class="tag"></script></span>
<span class="tag"></body></span>
<span class="tag"></html></span>
使用 Chartist JavaScript 库,你可以使用各种预先构建好的 CSS 样式,而不是在项目中指定各种与样式相关的部分。你可以使用这些样式来设置已创建的图表的外观。
比如,预创建的 CSS 类 .ct-chart
是用来构建饼状图的容器。还有 .ct-golden-section
类可用于获取纵横比,它基于响应式设计进行缩放,帮你解决了计算固定尺寸的麻烦。Chartist 还提供了其它类别的比例容器,你可以在自己的项目中使用它们。
为了给各个扇形设置样式,可以使用默认的 .ct-serials-a
类。字母 a
是根据系列的数量变化的(a、b、c,等等),因此它与每个要设置样式的扇形相对应。
Chartist.Pie
方法用来创建一个饼状图。要创建另一种类型的图表,比如折线图,请使用 Chartist.Line
。
代码的执行结果如下。
3、 D3.js
D3.js 是另一个好用的开源 JavaScript 图表库。使用它需要遵循 BSD 许可证。D3 的主要用途是,根据提供的数据,处理和添加文档的交互功能,。
借助这个 3D 动画库,你可以通过 HTML5、SVG 和 CSS 来可视化你的数据,并且让你的网站变得更精美。更重要的是,使用 D3,你可以把数据绑定到文档对象模型(DOM)上,然后使用基于数据的函数改变文档。
示例代码如下,它使用该库绘制了一个简单的条形图。
<span class="dec"><!DOCTYPE html></span>
<span class="tag"><html></span>
<span class="tag"><head></span>
<span class="pln"> </span>
<span class="pln"> </span><span class="tag"><style></span>
<span class="pln"> </span><span class="pun">.</span><span class="pln">chart div </span><span class="pun">{</span>
<span class="pln"> font</span><span class="pun">:</span><span class="lit">15px</span><span class="pln"> sans</span><span class="pun">-</span><span class="pln">serif</span><span class="pun">;</span>
<span class="pln"> background</span><span class="pun">-</span><span class="pln">color</span><span class="pun">:</span><span class="pln"> lightblue</span><span class="pun">;</span>
<span class="pln"> text</span><span class="pun">-</span><span class="pln">align</span><span class="pun">:</span><span class="pln"> right</span><span class="pun">;</span>
<span class="pln"> padding</span><span class="pun">:</span><span class="lit">5px</span><span class="pun">;</span>
<span class="pln"> margin</span><span class="pun">:</span><span class="lit">5px</span><span class="pun">;</span>
<span class="pln"> color</span><span class="pun">:</span><span class="pln"> white</span><span class="pun">;</span>
<span class="pln"> font</span><span class="pun">-</span><span class="pln">weight</span><span class="pun">:</span><span class="pln"> bold</span><span class="pun">;</span>
<span class="pln"> </span><span class="pun">}</span>
<span class="pln"> </span>
<span class="pln"> </span><span class="tag"></style></span>
<span class="pln"> </span><span class="tag"></head></span>
<span class="tag"><body></span>
<span class="pln"> </span><span class="tag"><div</span><span class="atn">class</span><span class="pun">=</span><span class="atv">"chart"</span><span class="tag">></div></span>
<span class="pln"> </span>
<span class="pln"> </span><span class="tag"><script</span><span class="atn">src</span><span class="pun">=</span><span class="atv">"https://cdnjs.cloudflare.com/ajax/libs/d3/5.5.0/d3.min.js"</span><span class="tag">></script></span>
<span class="pln"> </span><span class="tag"><script></span>
<span class="pln"> </span><span class="kwd">var</span><span class="pln"> data </span><span class="pun">=</span><span class="pun">[</span><span class="lit">342</span><span class="pun">,</span><span class="lit">222</span><span class="pun">,</span><span class="lit">169</span><span class="pun">,</span><span class="lit">259</span><span class="pun">,</span><span class="lit">173</span><span class="pun">];</span>
<span class="pln"> d3</span><span class="pun">.</span><span class="pln">select</span><span class="pun">(</span><span class="str">".chart"</span><span class="pun">)</span>
<span class="pln"> </span><span class="pun">.</span><span class="pln">selectAll</span><span class="pun">(</span><span class="str">"div"</span><span class="pun">)</span>
<span class="pln"> </span><span class="pun">.</span><span class="pln">data</span><span class="pun">(</span><span class="pln">data</span><span class="pun">)</span>
<span class="pln"> </span><span class="pun">.</span><span class="pln">enter</span><span class="pun">()</span>
<span class="pln"> </span><span class="pun">.</span><span class="pln">append</span><span class="pun">(</span><span class="str">"div"</span><span class="pun">)</span>
<span class="pln"> </span><span class="pun">.</span><span class="pln">style</span><span class="pun">(</span><span class="str">"width"</span><span class="pun">,</span><span class="kwd">function</span><span class="pun">(</span><span class="pln">d</span><span class="pun">){</span><span class="kwd">return</span><span class="pln"> d </span><span class="pun">+</span><span class="str">"px"</span><span class="pun">;</span><span class="pun">})</span>
<span class="pln"> </span><span class="pun">.</span><span class="pln">text</span><span class="pun">(</span><span class="kwd">function</span><span class="pun">(</span><span class="pln">d</span><span class="pun">)</span><span class="pun">{</span><span class="kwd">return</span><span class="pln"> d</span><span class="pun">;</span><span class="pun">});</span>
<span class="pln"> </span>
<span class="pln"> </span>
<span class="pln"> </span><span class="tag"></script></span>
<span class="tag"></body></span>
<span class="tag"></html></span>
使用 D3 库的主要概念是应用 CSS 样式选择器来定位 DOM 节点,然后对其执行操作,就像其它的 DOM 框架,比如 JQuery。
将数据绑定到文档上后,.enter()
函数会被调用,为即将到来的数据构建新的节点。所有在 .enter()
之后调用的方法会为数据中的每一个项目调用一次。
代码的执行结果如下。
总结
JavaScript 图表库提供了强大的工具,你可以将自己的网络资源进行数据可视化。通过这三个开源库,你可以把自己的网站变得更好看,更容易使用。
你知道其它强大的用于创造 JavaScript 动画效果的前端库吗?请在下方的评论区留言分享。
via: https://opensource.com/article/18/9/open-source-javascript-chart-libraries
作者:Dr.Michael J.Garbade 选题:lujun9972 译者:BriFuture 校对:wxy