乐者为王

Do one thing, and do it well.

使用ichartjs画2D条形图

在Rails中使用Open Flash Chart II中使用了OFC2来画2D条形图。现在Flash快要不行了,因为有了更好的HTML5。好的程序猿也要紧随潮流,使用新的技术来改进和增强他们的代码。这次就尝试使用HTML5图形库来替换OFC2。比较已有的一些图形库,最后选定国产的ichartjs

实现2D条形图真的很简单:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<script type="text/javascript" src="ichart.1.1.min.js"></script>
<script type="text/javascript">
$(function() {
  var data = [
    {name: 'IE', value: 35.75, color: '#a5c2d5'},
    {name: 'Chrome', value: 29.84, color: '#cbab4f'},
    {name: 'Firefox', value: 24.88, color: '#76a871'},
    {name: 'Safari',value: 6.77, color: '#9f7961'},
    {name: 'Opera',value:2.02, color: '#a56f8f'},
    {name: 'Other',value: 0.73, color: '#6f83a5'}
  ];

  new iChart.Bar2D({
    render: 'canvasDiv',
    data: data,
    title: 'Top 5 Browsers from 1 to 29 Feb 2012',
    showpercent: true,
    decimalsnum: 2,
    width: 800,
    height: 400,
    coordinate: {
      scale: [{
        position: 'bottom',
        start_scale: 0,
        end_scale: 40,
        scale_space: 8,
        listeners: {
          parseText: function(t, x, y) {
            return {text: t + "%"}
          }
        }
      }]
    }
  }).draw();
});
</script>

<div id="canvasDiv"></div>

Comments