当前位置:   article > 正文

chart.js图表,饼状图,柱状图,折线图_chart.js 饼图显示 数值

chart.js 饼图显示 数值

最近项目中有用到大量的图表,所以自学下chart.js,在这里做个记录以免时间长了容易忘记。

cdn地址可以直接使用: https://cdn.bootcss.com/Chart.js/2.7.3/Chart.js   

chart.js源码:https://github.com/nnnick/Chart.js

chart.js 英文文档:http://www.chartjs.org/docs/

chart.js 中文文档:http://www.bootcss.com/p/chart.js/docs/

个人推荐博客园里大神总结的文档,感觉比官方的容易理解:https://www.cnblogs.com/landeanfen/p/5026485.html


首先引入jq和chart.js方便操作

  1. <script src='/js/jquery-3.3.1.min.js'></script>
  2. <script src='https://cdn.bootcss.com/Chart.js/2.7.3/Chart.js'></script>

由于chart.js是基于HTML5的canvas,所以

<canvas id="myChart" width="800" height="600"></canvas>

项目中的饼状图:

  1. var ctx1 = document.getElementById('myChart');
  2. var internetType = []; //上网类型
  3. var onePercentage = []; //所占百分比
  4. var backColor1 = [] //百分比颜色
  5. for(var i = 0; ith = data.actiontype.length, i < ith; i++){
  6. internetType.push(data.actiontype[i].type);
  7. onePercentage.push(data.actiontype[i].percent);
  8. backColor1.push('#FF6384','#36A2EB','#FFCE96','#FFCE00','#36E2EB','#838B2E','#B2E6C3','#DCB4B8','#9FC3F7');
  9. }
  10. var myChart1 = new Chart(ctx1,{
  11. type:'pie',
  12. data:{
  13. labels: internetType, //矩形标题
  14. datasets:[{
  15. data: onePercentage, //所占整圆的比例
  16. backgroundColor:backColor1, //背景色
  17. hoverBackgroundColor:backColor1
  18. }]
  19. }
  20. });


柱状图和折线图写法基本一样只需改变type:的值;

柱状图:type:bar;

折线图:type:line;

如果需要一些别的基本的配置,可以修改默认的参数:

  1. var options = {
  2. //Boolean - If we show the scale above the chart data
  3. //是否显示柱状图上面的数据
  4. scaleOverlay: true,
  5. //Boolean - If we want to override with a hard coded scale
  6. scaleOverride: false,
  7. //** Required if scaleOverride is true **
  8. //Number - The number of steps in a hard coded scale
  9. scaleSteps: null,
  10. //Number - The value jump in the hard coded scale
  11. scaleStepWidth: 50,
  12. //Number - The scale starting value
  13. scaleStartValue: null,
  14. //String - Colour of the scale line
  15. //x/y轴坐标线的颜色
  16. scaleLineColor: "rgba(0,0,0,.1)",
  17. //Number - Pixel width of the scale line
  18. //坐标线的宽度
  19. scaleLineWidth: null,
  20. //Boolean - Whether to show labels on the scale
  21. //是否显示label值
  22. scaleShowLabels: true,
  23. //Interpolated JS string - can access value
  24. scaleLabel: "<%=value%>",
  25. //String - Scale label font declaration for the scale label
  26. //字体Family
  27. scaleFontFamily: "'Arial'",
  28. //Number - Scale label font size in pixels
  29. //字体大小
  30. scaleFontSize: 12,
  31. //String - Scale label font weight style
  32. //字体样式
  33. scaleFontStyle: "normal",
  34. //String - Scale label font colour
  35. //字体颜色
  36. scaleFontColor: "#666",
  37. ///Boolean - Whether grid lines are shown across the chart
  38. scaleShowGridLines: false,
  39. //String - Colour of the grid lines
  40. //网格线颜色
  41. scaleGridLineColor: "rgba(0,0,0,.05)",
  42. //Number - Width of the grid lines
  43. scaleGridLineWidth: 1,
  44. //Boolean - If there is a stroke on each bar
  45. barShowStroke: true,
  46. //Number - Pixel width of the bar stroke
  47. barStrokeWidth: 2,
  48. //Number - Spacing between each of the X value sets
  49. // 柱状块与x值所形成的线(如:x=20这条线)之间的距离
  50. barValueSpacing: 5,
  51. //Number - Spacing between data sets within X values
  52. // 在同一x值内的柱状块之间的间距
  53. barDatasetSpacing: 1,
  54. //Boolean - Whether to animate the chart
  55. animation: true,
  56. //Number - Number of animation steps
  57. animationSteps: 60,
  58. //String - Animation easing effect
  59. animationEasing: "easeOutQuart",
  60. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/132550
推荐阅读
相关标签
  

闽ICP备14008679号