从柱状图中选择日期范围 日期范围UI

  • 源码大小:19.22KB
  • 所需积分:1积分
  • 源码编号:19JP-3795
  • 浏览次数:1239次
  • 最后更新:2023年07月23日
  • 所属栏目:表单
本站默认解压密码:19jp.com 或 19jp_com

简介

基本日期范围选择器的绝佳替代品。

DateRange UI jQuery插件结合了柱状图和日期范围选择器,提供了一个可视化的日期选择UI。

重量轻、动感十足、易于使用、反应灵敏且便于触摸。

基于jQuery和d3.js库构建。

如何使用它:

1.加载所需的jQuery和d3.js库。

  1. <script src="/path/to/cdn/jquery.min.js"></script>
  2. <script src="/path/to/cdn/d3.min.js"></script>

2.在jQuery之后加载DateRange UI的脚本。

  1. <script src="plugin.js"></script>

3.创建一个empty DIV来容纳柱状图和日期范围选择器。

  1. <div id="graph"></div>

4.为柱状图准备时间序列数据。

  1. var myData = [
  2. {
  3. // year name
  4. "key_as_string":"1903",
  5. // value
  6. "doc_count":2,
  7. // unix timestamp in milliseconds
  8. "key":-2114384400000
  9. },
  10. {
  11. "key_as_string":"1904",
  12. "doc_count":7,
  13. "key":-2082848400000
  14. },
  15. // ...
  16. ]

5.初始化插件以生成一个柱状图,并在其底部生成一个自定义日期范围选择器。

  1. const graph = $("#graph").daterangeselect({
  2. data: myData,
  3. // more options here
  4. });

6.DateRange UI的可用选项。

  1. const graph = $("#graph").daterangeselect({
  2. data: myData,
  3. height: 320,
  4. rangeBarOpacity: 0.4,
  5. handleScaleFactor: 1.6,
  6. dataLengthTrigger: 30 // show tick values on x-axis for each bar or every 10th element
  7. });

7.覆盖默认样式。

  1. #graph {
  2. position: relative;
  3. width: 100%;
  4. }
  5.  
  6. #range {
  7. color: #fff;
  8. font-size: 20px;
  9. background-color: #d81b60;
  10. padding: 5px 10px;
  11. }
  12.  
  13. #graph text {
  14. fill: #ccc;
  15. }
  16.  
  17. #graph tik {
  18. fill: #ccc;
  19. }
  20.  
  21. #graph .domain, #graph line {
  22. stroke: #ccc;
  23. }
  24.  
  25. #graph .bars .bar {
  26. fill: #bdb7b7;
  27. }
  28.  
  29. #graph .bars .bar.highlight {
  30. fill: #d81b60 !important;
  31. }
  32.  
  33. #graph .bars .axis--x path {
  34. display: none;
  35. }
  36.  
  37. #graph .bars .bar.selected {
  38. fill: #fff;
  39. }
  40.  
  41. #graph .handles .selector {
  42. fill: #fff;
  43. stroke: #777;
  44. stroke-width: 0.5px;
  45. }
  46.  
  47. #graph .handles .selector.hovereffekt {
  48. stroke-width: 15px;
  49. stroke-opacity: 0.1;
  50. stroke: #eee;
  51. }
  52.  
  53. #graph .handles .selector.active {
  54. fill: #ccc !important;
  55. }
  56.  
  57. #graph .handles .range {
  58. fill: #fff; /* #d81b60 */
  59. stroke: none;
  60. }
  61.  
  62. #graph .handles .selector .handlehovertext {
  63. fill: #fff;
  64. stroke: none;
  65. font-size: 11px;
  66. font-family: Helvetica, Arial;
  67. }
  68.  
  69. #graph .empty line {
  70. stroke: #d81b60 !important;
  71. }
  72.  
  73. #graph .empty text {
  74. /* fill: #d81b60 !important; */
  75. }

8.自定义将鼠标悬停在柱状图上时显示的工具提示。

  1. #graph div.tooltip {
  2. position: absolute;
  3. text-align: center;
  4. width: 40px;
  5. height: 24px;
  6. padding-top: 5px;
  7. font: 12px sans-serif;
  8. background: #ccc;
  9. border: 0px;
  10. border-radius: 2px;
  11. pointer-events: none;
  12. color: #333;
  13. }
  14.  
  15. #graph div.tooltip:after {
  16. box-sizing: border-box;
  17. display: inline;
  18. width: 100%;
  19. line-height: 1;
  20. color: #ccc;
  21. content: "\25BC";
  22. position: absolute;
  23. text-align: center;
  24. margin: -3px 0 0 0;
  25. top: 100%;
  26. left: 0;
  27. }

9.回调函数。

  1. const graph = $("#graph").daterangeselect({
  2. data: myData,
  3. onRangeUpdate: function (obj) {
  4. //var myJSON = JSON.stringify(obj);
  5. //console.log("Clicked Section Data",obj);
  6. },
  7. hoverText: function (obj) {
  8. return obj.key_as_string;
  9. }
  10. });

10.更新柱状图。

  1. $("#graph").daterangeselect("update", newData);

预览截图