当前位置:   article > 正文

amCharts之柱形图_amchart column 颜色

amchart column 颜色

先从官网给的demo开始看,最简单的demo: columnSimple 图如下:


代码如下:

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>amCharts examples</title>
  6. <link rel="stylesheet" href="style.css" type="text/css">
  7. <script src="../amcharts/amcharts.js" type="text/javascript"></script>
  8. <script type="text/javascript">
  9. var chart;
  10. var chartData = [{
  11. country: "USA",
  12. visits: 4025
  13. }, {
  14. country: "China",
  15. visits: 1882
  16. }, {
  17. country: "Japan",
  18. visits: 1809
  19. }, {
  20. country: "Germany",
  21. visits: 1322
  22. }, {
  23. country: "UK",
  24. visits: 1122
  25. }, {
  26. country: "France",
  27. visits: 1114
  28. }, {
  29. country: "India",
  30. visits: 984
  31. }, {
  32. country: "Spain",
  33. visits: 711
  34. }, {
  35. country: "Netherlands",
  36. visits: 665
  37. }, {
  38. country: "Russia",
  39. visits: 580
  40. }, {
  41. country: "South Korea",
  42. visits: 443
  43. }, {
  44. country: "Canada",
  45. visits: 441
  46. }, {
  47. country: "Brazil",
  48. visits: 395
  49. }, {
  50. country: "Italy",
  51. visits: 386
  52. }, {
  53. country: "Australia",
  54. visits: 384
  55. }, {
  56. country: "Taiwan",
  57. visits: 338
  58. }, {
  59. country: "Poland",
  60. visits: 328
  61. }];
  62. AmCharts.ready(function () {
  63. chart = new AmCharts.AmSerialChart();//创建一个AmSerialChart对象
  64. chart.dataProvider = chartData;//dataProvider 数据提供的对象 对应上面的json数据
  65. chart.categoryField = "country";//X轴上的值
  66. chart.startDuration = 1;
  67. //X轴
  68. var categoryAxis = chart.categoryAxis;
  69. //categoryAxis.labelRotation =45 ;//X轴下面值的角度
  1. //Y轴
  2. var graph = new AmCharts.AmGraph();
  3. graph.valueField = "visits";//柱形图显示的值
  4. graph.balloonText = "[[category]]: [[value]]";//鼠标放上所显示的
  5. graph.type = "column";//Y轴的类型 有line、column、step、smoothedLine、candlestick、ohlc
  6. graph.lineAlpha = 0;//border透明度
  7. graph.fillAlphas = 0.8;//柱子的透明度
  8. chart.addGraph(graph);
  9. chart.write("chartdiv");
  10. });
  11. </script>
  12. </head>
  13. <body>
  14. <div id="chartdiv" style="width: 100%; height: 400px;"></div>
  15. </body>
  16. </html>

AmSerialChart 是画地域图、柱形图 等等的 对象。

AmGraph 是画Y轴的对象。

如果你想不同的柱子加 不同的颜色的话 需要做2件事:

1.把json数据多加一个属性 ex:{country:"USA",visits:4025,color: "#F8FF01"}

2.在Y轴的对象上添加一个颜色属性  graph.colorField = "color";

这样就可以得到不同颜色。

第2个demo是双柱形图,如图:



  1. var chart;
  2. var chartData = [{
  3. dealer: "一月",
  4. a: 2.6,
  5. b: 2.7,
  6. }, {
  7. dealer: "二月",
  8. a: 2.6,
  9. b: 2.7,
  10. }, {
  11. dealer: "三月",
  12. a: 2.6,
  13. b: 2.7,
  14. }, {
  15. dealer: "四月",
  16. a: 2.6,
  17. b: 2.7,
  18. }];
  19. AmCharts.ready(function () {
  20. // SERIALL CHART
  21. chart = new AmCharts.AmSerialChart();
  22. chart.dataProvider = chartData;
  23. chart.categoryField = "dealer";
  24. //chart.rotate = true;//把柱形图变成横向
  25. var categoryAxis = chart.categoryAxis;
  26. categoryAxis.gridAlpha = 0.1;//数格的边线透明度
  27. categoryAxis.axisAlpha = 0.4;//X轴的透明度
  28. categoryAxis.inside=false;//X轴数值在图里面还是瓦面
  29. categoryAxis.position = "bottom";//X轴位置
  30. var valueAxis = new AmCharts.ValueAxis();
  31. //valueAxis.stackType = "regular";
  32. valueAxis.gridAlpha = 0;
  33. valueAxis.axisAlpha = 0.4;
  34. chart.addValueAxis(valueAxis);
  35. // firstgraph
  36. var graph = new AmCharts.AmGraph();
  37. graph.title = "注册用户";
  38. //graph.labelText = "[[value]]";
  39. graph.valueField = "a";//跟json数据中a相对应
  40. graph.balloonText="[[title]]:[[value]]人" //鼠标放上显示的值
  41. graph.type = "column";
  42. graph.lineAlpha = 1;
  43. graph.fillAlphas = 1;
  44. graph.lineColor = "#83c417";
  45. chart.addGraph(graph);
  46. // second graph
  47. graph = new AmCharts.AmGraph();
  48. graph.title = "停用用户";
  49. //graph.labelText = "[[value]]";
  50. graph.valueField = "b";
  51. graph.balloonText="[[title]]:[[value]]人"
  52. graph.type = "column";
  53. graph.lineAlpha = 1;
  54. graph.fillAlphas = 1;
  55. //graph.fillColors="#ffc92e"
  56. graph.lineColor = "#f27324";
  57. chart.addGraph(graph);
  1. //控制柱子上鼠标事件的代码,修改tip的样式
  2. var balloon = chart.balloon;
  3. balloon.adjustBorderColor = true;
  4. balloon.horizontalPadding = 10;
  5. balloon.fontSize = 14;
  6. balloon.pointerWidth= 5;
  7. balloon.color = "#000000";
  8. balloon.cornerRadius = 0;
  9. balloon.borderThickness=1;
  10. balloon.borderColor="#999999";
  11. balloon.borderAlpha=1;
  12. balloon.fillColor = "#FFFFFF";
  13. // LEGEND 用来显示下面分组的代码
  14. var legend = new AmCharts.AmLegend();
  15. legend.position = "bottom";
  16. legend.borderAlpha = 0;
  17. legend.horizontalGap = 10;
  18. legend.switchType = "v";//图片上面默认显示对号
  19. chart.addLegend(legend);
  20. // WRITE
  21. chart.write("chartdiv");
  22. });
 amCharts.AmLegend() 对象 用来生成分组。
如果想变成一个柱子代表几个数据的话,如下图:


只需要增加valueAxis.stackType = "regular"; 代码就可以了

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

闽ICP备14008679号