<!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title></title> <script src="https://code.highcharts.com/highcharts.js"></script> <script src="https://code.highcharts.com/modules/exporting.js"></script> </head> <body> <div id="container" style="min-width: 310px; height: 400px; max-width: 600px; margin: 0 auto"></div> </body> </html> <script type="text/javascript"> Highcharts.chart('container', { chart: { plotBackgroundColor: null, plotBorderWidth: null, plotShadow: false, type: 'pie' }, title: { text: 'Browser market shares in January, 2018' }, tooltip: { pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>' }, plotOptions: { pie: { allowPointSelect: true, cursor: 'pointer', dataLabels: { enabled: true, format: '<b>{point.name}</b>: {point.percentage:.1f} %', style: { color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black' } } } }, series: [{ name: 'Brands', colorByPoint: true, data: [{ name: 'Chrome', y: 61.41, sliced: true, selected: true }, { name: 'Internet Explorer', y: 11.84 }, { name: 'Firefox', y: 10.85 }, { name: 'Edge', y: 4.67 }, { name: 'Safari', y: 4.18 }, { name: 'Sogou Explorer', y: 1.64 }, { name: 'Opera', y: 1.6 }, { name: 'QQ', y: 1.2 }, { name: 'Other', y: 2.61 }] }] }); </script>
这里饼状图是用highcharts实现的。 javascript
在头文件中加入:html
<script src="https://code.highcharts.com/highcharts.js"></script> <script src="https://code.highcharts.com/modules/exporting.js"></script>
在放图的位置添加html代码:java
<div id="container" style="min-width: 310px; height: 400px; max-width: 600px; margin: auto"></div>
在最后添加js代码,这部分是主要代码。 要实现按照用户的得到途径占比来制做一个饼状图,须要知道各个途径approach
下分别有多少个用户。要实现这个目的,必需要从服务器端获取数据。web
ajax的样子大概是:ajax
$.ajax({ url: method: dataType: success: function(data){ } })
必需要先有一个url,首先已经有了一个get 'backend',to: "backend/base#index"
,因此考虑这个,可是当在index action中用render返回数据的时候,数据是返回了,原来的页面也没有了,因此还须要另外作一个url。json
get 'backend/pie'=>"backend/base#pie",:as=>:pie
在base_controller.rb
中添加了个pie action服务器
class Backend::BaseController < ApplicationController def pie end end
因此url就有了,让服务器返回json数据,而且创建一个全局变量ser_data来存放返回的dataapp
$.ajax({ url: "/backend/pie", method: 'GET', dataType: 'json', success: function(data){ window.ser_data=data } }); Highcharts.chart('container', { ... )}
让服务器去抓取数据,看每一个approach下各有多少笔数据:url
def pie website_num=Guest.where("approach=?","0").count refer_num=Guest.where("approach=?","1").count relocation_num=Guest.where("approach=?","2").count hr_num=Guest.where("approach=?","3").count ad_num=Guest.where("approach=?","4").count others_num=Guest.where("approach=?","5").count render :json=>{ :website=>website_num, :referrence=>refer_num, :relocation=>relocation_num, :hr=>hr_num, :ad=>ad_num, :others=>others_num} end
这样作以后发现仍有问题,全局变量彷佛不起做用,没有显示饼状图出来,后来发如今js中,ajax比Highcharts.chart()更晚执行,因此ser_data的值是空的,因而把Highcharts.chart()放在一个function里,ajax成功回调的时候在执行这个function,这样就能够了。spa
<script type="text/javascript"> $.ajax({ url: "/backend/pie", method: 'GET', dataType: 'json', success: function(data){ window.ser_data=data pie(); } }); function pie(){ Highcharts.chart('container', { chart: { plotBackgroundColor: null, plotBorderWidth: null, plotShadow: false, type: 'pie' }, title: { text: '客户来源占比图' }, tooltip: { pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>' }, plotOptions: { pie: { allowPointSelect: true, cursor: 'pointer', dataLabels: { enabled: true, format: '<b>{point.name}</b>: {point.percentage:.1f} %', style: { color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black' } } } }, series: [{ name: 'Brands', colorByPoint: true, data: [{ name: '官网', y: ser_data["website"], sliced: true, selected: true }, { name: '客户推荐', y: ser_data["referrence"] }, { name: 'relocation suppliers', y: ser_data["relocation"] }, { name: '高校/外企人事部', y: ser_data["hr"] }, { name: '广告', y: ser_data["ad"] }, { name: '其它', y: ser_data["others"] }] }] }); } </script>