转 HighCharts笔记之: Bar Chart

最近需要做一些Web图标,研究了几个开源的第三方工具后,最后决定使用HighCharts开发:

Highcharts 是一个用纯JavaScript编写的一个图表库, 能够很简单便捷的在web网站或是web应用程序添加有交互性的图表,并且免费提供给个人学习、个人网站和非商业用途使用。目前HighCharts支持 的图表类型有曲线图、区域图、柱状图、饼状图、散状点图和综合图表。 HighCharts界面美观,由于使用JavaScript编写,所以不需要像Flash和Java那样需要插件才可以运行,而且运行速度快。另外 HighCharts还有很好的兼容性,能够完美支持当前大多数浏览器。

HighCharts 可以通过JSON 数据格式与后台交互,从而生成动态的图表。但是在官方的文档中关于 JSON 数据格式的资料很少,经过一下午的调试终于找到了关于柱状图和饼图的数据格式,在这里记录一下,以备后续的查询:

柱状图:

JSON Data Format

[
  {
      "data":[0,0,0,0,0,0,0,0,9,0,0,1],
      "name":"Actual"
  },
  {
      "data":[1,1,1,4,1,1,1,1,12,1,3,6],
      "name":"Target"
  }
]

Javascript Code

<script type="text/javascript">
  $(function () {
      var options = {
            chart: {
                renderTo: ‘container‘,
                defaultSeriesType: ‘bar‘
            },
            title: {
                text: ‘‘
            },
            subtitle: {
                text: ‘‘
            },
            xAxis: {
                categories: [‘Activity Entry‘, ‘Activity Update‘, ‘Blog Entry‘, ‘Blog Comments‘, ‘Feed‘,‘Bookmark‘,‘File‘,‘Forum Topic‘,‘Forum Reply‘,‘Wiki‘,‘Ram‘,‘Liquid‘],
                title: {
                    text: null
                }
            },
            yAxis: {
                min: 0,
                title: {
                    text: ‘Units‘,
                    align: ‘high‘
                },
                labels: {
                    overflow: ‘justify‘
                }
            },
            tooltip: {
                formatter: function() {
                    return ‘‘+
                        this.series.name +‘: ‘+ this.y;
                }
            },
            plotOptions: {
                bar: {
                    dataLabels: {
                        enabled: true
                    }
                }
            },
            legend: {
                layout: ‘vertical‘,
                align: ‘right‘,
                verticalAlign: ‘top‘,
                x: -100,
                y: 100,
                floating: true,
                borderWidth: 1,
                backgroundColor: ‘#FFFFFF‘,
                shadow: true
            },
            credits: {
                enabled: false
            },
            series: []
        };
      $.getJSON(‘<c:url value="/action.do?method=barChart"/>‘, function(json) {
              options.series = json;//options.series.push(json);
              //console.log("JSON: " + JSON.stringify(options));
              //console.log("Render to element with ID : " + options.chart.renderTo);
              //console.log("Number of matching dom elements : " + $("#" + options.chart.renderTo).length);
                  new Highcharts.Chart(options);
          }).error(function() {console.log(‘error‘);});
      $("a[data-toggle=modal]").click(function(){
          var target = $(this).attr(‘data-target‘);
          var url = $(this).attr(‘href‘);
          $(target).innerHTML=‘‘;
          $(target).load(url);
      });
  });
</script>

HTML Code

<div id="container" style="width: 100%; height: 400px"></div>

Java Code - action

public ActionForward barChart(ActionMapping mapping, ActionForm form,
          HttpServletRequest request, HttpServletResponse response)
          throws IOException {
      request.setCharacterEncoding("UTF-8");
      response.setCharacterEncoding("UTF-8");
      response.setContentType("application/json;charset=utf-8");
      List<Bar> resultList = getBarData();
      JSONArray json = new JSONArray(resultList);
      String result = json.toString();// 转成json数据
      PrintWriter out = response.getWriter();
      out.write(result);
      out.flush();
      out.close();
      return null;
  }
  private List<Bar> getBarData() {
      BlueCommunity target = dao.getLatestTarget();
      BlueCommunity actual = dao.sumLatestActual();
      List<Bar> resultList = new ArrayList<Bar>();
      if (actual != null) {
          resultList.add(new Bar("Actual", actual.getValueArray()));
      } else {
          resultList.add(new Bar("Actual", new int[] { 0, 0, 0, 0, 0, 0, 0,
                  0, 0, 0, 0, 0 }));
      }
      resultList.add(new Bar("Target", target.getValueArray()));
      return resultList;
  }

Java Code - Bar class

public class Bar {
  private static final long serialVersionUID = 6461863786317563773L;
  private String name;
  private int[] data;
  public Bar() {
  };
  public Bar(String name, int[] data) {
      this.name = name;
      this.data = data;
  }
  public String getName() {
      return name;
  }
  public void setName(String name) {
      this.name = name;
  }
  public int[] getData() {
      return data;
  }
  public void setData(int[] data) {
      this.data = data;
  }
}

Java Code - Community class

public class Community implements Serializable {
  private static final long serialVersionUID = -7516165631503337884L;
  private int id;
  private int version;
  private String remark;
  private int activity_entry;
  private int activity_update;
  private int blog_entry;
  private int blog_comments;
  private int feed;
  private int bookmark;
  private int file;
  private int forum_topic;
  private int forum_reply;
  private int wiki;
  private int iRam;
  private int liquid;
  private int user;
  ...
  getters;
  setters;
  ...
  public int[] getValueArray() {
      int[] array = { activity_entry, activity_update, blog_entry,
              blog_comments, feed, bookmark, file, forum_topic, forum_reply,
              wiki, iRam, liquid };
      return array;
  }
}

转 HighCharts笔记之: Bar Chart

时间: 2024-10-13 05:41:48

转 HighCharts笔记之: Bar Chart的相关文章

Highcharts - Bar Chart

1. 条形图(Bar Chart)需要的数据格式类型如下: ["Luke Skywalker", "Darth Vader", "Yoda", "Princess Leia"] [2, 4, 1, 1] 2. Bar Chart代码示例: $(function () { $('#container').highcharts({ chart: { type: 'bar' }, xAxis: { categories: ['Jan

Relative-Frequency|frequency|pie chart |bar chart

2.2Organizing Qualitative Data The number of times a particular distinct value occurs is called its frequency (or count) Relative-Frequency Distribution:百分比 Note: Relative-frequency distributions are better than frequency distributions for comparing

[Elasticsearch] 聚合 - 创建条形图(Bar Chart)

创建条形图(Building Bar Charts) 聚合的一个令人激动的性质是它能够很容易地被转换为图表和图形.在本章中,我们会使用前面的样本数据集来创建出各种分析案例.我们也会展示聚合能够支持的种类. 柱状图桶(Histogram Bucket)非常有用.柱状图在本质上就是条形图,如果你创建过一份报告或者分析面板(Analytics Dashboard),毫无疑问其中会有一些条形图.柱状图通过指定一个间隔(Interval)来工作.如果我们使用柱状图来表示销售价格,你或许会指定一个值为200

plot bar chart using python

Example import matplotlib.pyplot as plt import plotly.plotly as py # Learn about API authentication here: https://plot.ly/python/getting-started # Find your api_key here: https://plot.ly/settings/api y = [3, 10, 7, 5, 3, 4.5, 6, 8.1] N = len(y) x = r

highcharts笔记 highcharts学习 highcharts用法

标示线:plotLines : 绘制线: ########################################################################################################################## Y轴表示区域:  plotBands  Y轴反转: reversed        

用highcharts展现你的数据

摘要: 前面已经分享过图表插件,今天在来将下如何使用highcharts来绘制图表.highcharts支持在线定制,你可以选择你所需要的模块,然后点击build就会生成一个js文件链接,右键保存到本地酒可以在项目中使用了.highcharts支持多种图表,如折线.饼状.柱状等等,可以去官网查看(英文.中文).并且你可以打印图表或者以PNG.JPEG.PDF.SVG格式下载,你可以从官网下载示例. charts: highcharts提供了很多功能,你可以在图上显示节点数据,或者当鼠标悬上时提示

highcharts柱状图(堆叠图与多个柱图)、条形图(堆叠图与多组条形)

利用highcharts的 series: { stacking: 'normal' } 进行区分. $(function () { $('#container').highcharts({ chart: { type: 'bar' }, title: { text: 'Stacked bar chart' }, xAxis: { categories: ['Apples', 'Oranges', 'Pears', 'Grapes', 'Bananas'] }, yAxis: { min: 0,

Highcharts 基本条形图;Highcharts 堆叠条形图;Highcharts 反向条形图

Highcharts 基本条形图 配置 chart 配置 设置 chart 的 type 属性 为 bar ,chart.type 描述了图表类型.默认值为 "line". var chart = { type: 'bar' }; 实例 文件名:highcharts_bar_basic.htm <html> <head> <meta charset="UTF-8" /> <title>Highcharts 教程 | 菜

[django]django+post+ajax+highcharts使用方法

直接代码展示: view.py文件代码 from django.http import JsonResponse #django ajax部分 def ajax_kchart(request): times = request.POST['shijian'] chnl = request.POST['chnl'] chnl_data = keywork_chart(chnl,times) data_list = [] for j in chnl_data: data_list.append(j)