【问题标题】:amCharts date-time value issue, can not set hh:mm timeamCharts 日期时间值问题,无法设置 hh:mm 时间
【发布时间】:2017-10-03 07:38:22
【问题描述】:

我是 amCharts 的新手,可以创建 ganttCharts 和其他图表。我正在尝试创建一个甘特图并为每列设置一些时间值。所以我可以用这些线条来创作;

{
        "category": "Planned", 

        "segments": [   {
         "start": 8,
            "duration": 9,           
            "color": "#46615e",
             "task": "Order"
        } ,{
         "start": 18,
            "duration": 1,           
            "color": "#46615e",
             "task": "Order"
        } ,{
         "start": 20,
            "duration": 1,           
            "color": "#46615e",
             "task": "Order"
        } 
 ]
    }

但是对于起始值,我想写 08:18、18:22、20:25 之类的,而不是 8、18、20。

但它不适用于这些值..你知道吗?我将在下面分享所有代码。

   <!-- Styles -->
    <style>
    #chartdiv {
      width: 100%;
      height: 500px;
    }                                                                   
    </style>

    <!-- Resources -->

<script src="https://www.amcharts.com/lib/3/amcharts.js"></script>
<script src="https://www.amcharts.com/lib/3/serial.js"></script>
<script src="https://www.amcharts.com/lib/3/gantt.js"></script>
<script src="https://www.amcharts.com/lib/3/themes/light.js"></script>
<script src="https://www.amcharts.com/lib/3/plugins/export/export.min.js"></script>
<link rel="stylesheet" href="https://www.amcharts.com/lib/3/plugins/export/export.css" type="text/css" media="all" />

    <!-- Chart code -->
    <script>
    var chart = AmCharts.makeChart( "chartdiv", {
        "type": "gantt",
        "theme": "dark",
        "marginRight": 70,
        "period": "hh",
        "dataDateFormat":"YYYY-MM-DD hh:nn",
        "balloonDateFormat": "JJ:NN",

        "columnWidth": 0.5,
        "valueAxis": {
            "type": "date",
             "parseDates": true,
              "minPeriod": "mm"
        },
        "brightnessStep":  0,
        "graph": {
            "fillAlphas": 0.5,  
            "balloonText": "<span style='font-size:11px'>[[category]] -> [[task]]</span>",
            "labelText": "[[task]]",
            "labelPosition": "left",
             "labelOffset": 0,
        },
        "rotate": true,
        "categoryField": "category",
        "segmentsField": "segments",
        "colorField": "color",
        "startDate": "2018-10-02 00:00", 
        "startField": "start",
        "endField": "end",
        "durationField": "duration",
        "dataProvider": [ {
            "category": "",
            "segments": [   {
             "start": 7,
                "duration": 17,           
                "color": "#fff" 
            }  ]
        }, {
            "category": "Column A", 

            "segments": [   {
             "start": 8,
                "duration": 9,           
                "color": "#46615e",
                 "task": "Order"
            } ,{
             "start": 19,
                "duration": 1,           
                "color": "#46615e",
                 "task": "Order"
            } ,{
             "start": 21,
                "duration": 1,           
                "color": "#46615e",
                 "task": "Order"
            } 
     ]
        },  {
            "category": "Column B",
            "segments": [ {
                "start": 8,
                "duration": 8,
                "color": "#8dc49f",
                "task": "Order"
            }, {
                "start": 17,
                "duration": 2,
                "color": "#8dc49f",
                 "task": "Order"
            }, {
                "start": 20,
                "duration": 2,
                "color": "lightblue",
                "task": "Order"
            } ]
        }],
        "chartCursor": {
            "cursorColor":"#55bb76",
            "valueBalloonsEnabled": false,
            "cursorAlpha": 0,
            "valueLineAlpha":0.5,
            "valueLineBalloonEnabled": true,
            "valueLineEnabled": true,
            "zoomable":false,
            "valueZoomable":true
        } 
    } );
    </script>

    <!-- HTML -->
    <div id="chartdiv"></div>

【问题讨论】:

  • 使用字符串'08:18'
  • 我做了,但没有工作
  • but did not work - 发生了什么事而不是工作?它是否将您家中的所有时钟都更改为 GMT 时间?它射你的狗了吗? “它不起作用”很清楚,如果它起作用了,你会说它起作用了......请自己尝试一些调试
  • { "category": "Column B", "segments": [ { "start": '08:18', "duration": 8, "color": "#8dc49f", " task": "Order" }, { "start": '18:18', "duration": 2, "color": "#8dc49f", "task": "Order" } 第一段从 22:00 开始, 第二段 : 06:00 所以我不知道为什么会这样。抱歉,描述太短了。

标签: javascript charts amcharts gantt-chart


【解决方案1】:

由 startField 和 endField 映射的值只接受数值。如果要设置时间,则必须通过 startDateField 和 endDateField 使用日期。

  "startDateField": "start",
  "endDateField": "end",
  "dataProvider": [ {
     // ...
    "segments": [ {
      "start": "2016-01-01 08:18",
      "end": "2016-01-11 18:18",

检查this example。虽然它只使用日期,但您可以提供与您的 dataDateFormat 匹配的日期时间。

【讨论】:

  • 感谢它的工作并将数据设置到图表。你知道从 .json 获取数据并设置到图表吗?不幸的是,我没有找到很多关于 amChart 的资源..
  • 您必须附加到图表的 JSON 需要与静态示例(对象数组)设置的格式相匹配。您可以修改图表的 dataProvider 属性,然后调用 validateData 来更新图表,或者使用dataLoader。还有一个甘特图示例供您查看。
猜你喜欢
  • 2016-07-06
  • 1970-01-01
  • 2018-02-05
  • 1970-01-01
  • 2014-06-15
  • 2022-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多