【问题标题】:Datepicker not setting max and min value日期选择器未设置最大值和最小值
【发布时间】:2019-04-18 01:27:19
【问题描述】:

我正在使用推送函数中的示例数据设置最小值和最大值,以在日期选择器中显示它。但是当我点击日期选择器时,它显示了错误的最小值和最大值,我的代码有什么问题?我用它来获取最小值和最大值

"minDate": dataProvider[0].date, "maxDate":dataProvider[dataProvider.length - 1].date,

var chartData1 = [];


generateChartData();

function generateChartData() {
    chartData1.push(
    {
        "date": "2012-10-11",
        "value": 33
    }, {
        "date": "2012-12-12",
        "value": 50
    }, {
        "date": "2012-12-13",
        "value": 10
    }, {
        "date": "2012-12-14",
        "value": 100
    }, {
        "date": "2013-12-15",
        "value": 30
    });
    
  
}

var chart = AmCharts.makeChart("chartdiv", {
  "type": "stock",
  "extendToFullPeriod": false,

  "dataSets": [{
      "title": "first data set",
      "fieldMappings": [{
        "fromField": "value",
        "toField": "value"
      }, {
        "fromField": "volume",
        "toField": "volume"
      }],
      "dataProvider": chartData1,
      "categoryField": "date"
    }],

  "panels": [{

      "showCategoryAxis": false,
      "title": "Value",
      "percentHeight": 70,

      "stockGraphs": [{
        "id": "g1",

        "valueField": "value",
        "comparable": true,
        "compareField": "value",
        "balloonText": "[[title]]:<b>[[value]]</b>",
        "compareGraphBalloonText": "[[title]]:<b>[[value]]</b>"
      }],

      "stockLegend": {
        "periodValueTextComparing": "[[percents.value.close]]%",
        "periodValueTextRegular": "[[value.close]]"
      }
    },

    {
      "title": "Volume",
      "percentHeight": 30,
      "stockGraphs": [{
        "valueField": "volume",
        "type": "column",
        "showBalloon": false,
        "fillAlphas": 1
      }],

      "stockLegend": {
        "periodValueTextRegular": "[[value.close]]"
      }
    }
  ],

  "chartScrollbarSettings": {
    "graph": "g1"
  },

  "chartCursorSettings": {
    "valueBalloonsEnabled": true,
    fullWidth: true,
    cursorAlpha: 0.1
  },

  "periodSelector": {
    "position": "left",
    "periods": [{
      "period": "MM",
      "selected": true,
      "count": 1,
      "label": "1 month"
    }, {
      "period": "YYYY",
      "count": 1,
      "label": "1 year"
    }, {
      "period": "YTD",
      "label": "YTD"
    }, {
      "period": "MAX",
      "label": "MAX"
    }]
  },

  "dataSetSelector": {
    "position": "left"
  }
});

chart.addListener('rendered', function(event) {
  var dataProvider = chart.dataSets[0].dataProvider;
  $(".amChartsPeriodSelector .amChartsInputField").datepicker({
    "dateFormat": "dd-mm-yy",
    "minDate": dataProvider[0].date,
    "maxDate": dataProvider[dataProvider.length - 1].date,
    "onClose": function() {
      $(".amChartsPeriodSelector .amChartsInputField").trigger('blur');
    }
  });
});
html, body {
  width: 100%;
  height: 100%;
  margin: 0px;
  font-family: Verdana;
}

#chartdiv {
	width: 100%;
	height: 100%;
}
<!-- jQuery stuff -->
<link rel="stylesheet" media="all" href="https://code.jquery.com/ui/1.12.0/themes/ui-lightness/jquery-ui.css" />
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js" integrity="sha256-eGE6blurk5sHj+rmkfsGYeKyZx3M4bG+ZlFyA7Kns7E=" crossorigin="anonymous"></script>

<!-- amCharts -->
<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/amstock.js"></script>
<div id="chartdiv"></div>

【问题讨论】:

    标签: javascript datepicker amcharts


    【解决方案1】:

    来自minDatemaxDatedocumentation

    minDate

    支持多种类型:

    • 一个包含最小日期的日期对象。

    • 字符串采用dateFormat 选项定义的格式,或相对日期。

    您的日期格式为yyyy-mm-dd,但您的dateFormat 选项为dd-mm-yy

    也就是说,请注意文档中提到它们可以是日期对象。您可以通过将字符串包装在 new Date(...) 中来将它们转换为日期对象。

    "minDate": new Date(dataProvider[0].date),
    "maxDate": new Date(dataProvider[dataProvider.length - 1].date),
    

    var chartData1 = [];
    
    
    generateChartData();
    
    function generateChartData() {
      chartData1.push({
        "date": "2012-10-11",
        "value": 33
      }, {
        "date": "2012-12-12",
        "value": 50
      }, {
        "date": "2012-12-13",
        "value": 10
      }, {
        "date": "2012-12-14",
        "value": 100
      }, {
        "date": "2013-12-15",
        "value": 30
      });
    
    
    }
    
    var chart = AmCharts.makeChart("chartdiv", {
      "type": "stock",
      "extendToFullPeriod": false,
    
      "dataSets": [{
        "title": "first data set",
        "fieldMappings": [{
          "fromField": "value",
          "toField": "value"
        }, {
          "fromField": "volume",
          "toField": "volume"
        }],
        "dataProvider": chartData1,
        "categoryField": "date"
      }],
    
      "panels": [{
    
          "showCategoryAxis": false,
          "title": "Value",
          "percentHeight": 70,
    
          "stockGraphs": [{
            "id": "g1",
    
            "valueField": "value",
            "comparable": true,
            "compareField": "value",
            "balloonText": "[[title]]:<b>[[value]]</b>",
            "compareGraphBalloonText": "[[title]]:<b>[[value]]</b>"
          }],
    
          "stockLegend": {
            "periodValueTextComparing": "[[percents.value.close]]%",
            "periodValueTextRegular": "[[value.close]]"
          }
        },
    
        {
          "title": "Volume",
          "percentHeight": 30,
          "stockGraphs": [{
            "valueField": "volume",
            "type": "column",
            "showBalloon": false,
            "fillAlphas": 1
          }],
    
          "stockLegend": {
            "periodValueTextRegular": "[[value.close]]"
          }
        }
      ],
    
      "chartScrollbarSettings": {
        "graph": "g1"
      },
    
      "chartCursorSettings": {
        "valueBalloonsEnabled": true,
        fullWidth: true,
        cursorAlpha: 0.1
      },
    
      "periodSelector": {
        "position": "left",
        "periods": [{
          "period": "MM",
          "selected": true,
          "count": 1,
          "label": "1 month"
        }, {
          "period": "YYYY",
          "count": 1,
          "label": "1 year"
        }, {
          "period": "YTD",
          "label": "YTD"
        }, {
          "period": "MAX",
          "label": "MAX"
        }]
      },
    
      "dataSetSelector": {
        "position": "left"
      }
    });
    
    chart.addListener('rendered', function(event) {
      var dataProvider = chart.dataSets[0].dataProvider;
      $(".amChartsPeriodSelector .amChartsInputField").datepicker({
        "dateFormat": "dd-mm-yy",
        "minDate": new Date(dataProvider[0].date),
        "maxDate": new Date(dataProvider[dataProvider.length - 1].date),
        "onClose": function() {
          $(".amChartsPeriodSelector .amChartsInputField").trigger('blur');
        }
      });
    });
    html,
    body {
      width: 100%;
      height: 100%;
      margin: 0px;
      font-family: Verdana;
    }
    
    #chartdiv {
      width: 100%;
      height: 100%;
    }
    <!-- jQuery stuff -->
    <link rel="stylesheet" media="all" href="https://code.jquery.com/ui/1.12.0/themes/ui-lightness/jquery-ui.css" />
    <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
    <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js" integrity="sha256-eGE6blurk5sHj+rmkfsGYeKyZx3M4bG+ZlFyA7Kns7E=" crossorigin="anonymous"></script>
    
    <!-- amCharts -->
    <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/amstock.js"></script>
    <div id="chartdiv"></div>

    【讨论】:

      猜你喜欢
      • 2020-05-31
      • 2019-02-07
      • 2018-01-31
      • 1970-01-01
      • 2016-01-08
      • 2015-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多