Javasccript(D3) - 日期比较返回NaN

时间:2017-07-20 05:56:44

标签: javascript d3.js

我正在使用d3并尝试比较数组中的日期。我想得到它的最小值和最大值。

以下是我的数据集。

 <form id="myform" method="post" enctype="multipart/form-data" action="<?php echo site_url('User/user_index_c/edit_profile_c'); ?>">
    <label>Full Name : </label>
    <input class="name_styling" type="text" placeholder="Enter name" id="inputName" name="uname">

    <label>Email Id : </label>
    <input class="email_styling" type="email"  placeholder="Enter email" id="inputEmail" name="new_email">

    <div class="controls">
    <label>Profile Photo : </label>
    <input name="file1" type="file"  id="image_file" />
    <img id="blah" class="logoupload" src="#" alt="your image" />
    <span class="filename"></span>
    </div>

    <center><input class="submitBtn" id="submit" type="submit" value="Save Changes" name="submit" ></center>

</form>

以下是我遇到问题的代码段

 final String apiKey="AAAAeEpqP-w:APA91bFulyT23Km-onTNr_q5yEz4uoOaM8KdE4LMyIoz6kWlk3pJSHirDJBSiqESRXKiGa-Z_tBfpXA6naaaTXxcFFxAnaSkMTPVVOMswyJ0bhhdpwlo-92HXgxRMsHV6Y8bNaHX7tMd";
 int i=0;
 try {

 URL url = new URL("https://fcm.googleapis.com/fcm/send");
 HttpsURLConnection  conn = (HttpsURLConnection ) url.openConnection();
 System.out.println("after connection open");
 //conn.setDoInput(true);
 conn.setRequestMethod("POST");
 conn.setRequestProperty("Content-Type", "application/json");
 conn.setRequestProperty("Authorization", "key=" + apiKey);
 conn.setDoOutput(true);
 JSONObject json = new JSONObject();
   json.put("to", "device-token");
    JSONObject info = new JSONObject();
    info.put("title", "notification title"); // Notification title
    info.put("body", "message body");
    json.put("notification", info);
    OutputStreamWriter wr = new OutputStreamWriter(
            conn.getOutputStream());
    wr.write(json.toString());
    wr.flush();

    BufferedReader br = new BufferedReader(new InputStreamReader(
            (conn.getInputStream())));

    String output;
    System.out.println("Output from Server .... \n");
    while ((output = br.readLine()) != null) {
        System.out.println(output);
    }
   // result = CommonConstants.SUCCESS;
 System.out.println("after closed out put stream");
 int responseCode = conn.getResponseCode();

// System.out.println("Post parameters : " + input);
 System.out.println("Response Code : " + responseCode);


} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();

}
 catch(Exception e){
     e.printStackTrace();
 }
}

当我打印“min”变量时,它打印出“NaN”。

这影响了我的其余代码。

有人可以指出我出错的地方吗? 还有其他方法可以进行日期比较吗?

2 个答案:

答案 0 :(得分:2)

您的date值是字符串。你必须解析它们:

var parse = d3.timeParse("%Y.%m.%d");

之后,您可以使用d3.min检索最小值,使用d3.max检索最小值,或使用d3.extent检索两者。

以下是演示:

var data = [{ "date": "2016.07.19", "close": 185697.89 }, { "date": "2016.07.20", "close": 185697.89 }, { "date": "2016.07.21", "close": 186601.1 }, { "date": "2016.07.22", "close": 187273.89 }, { "date": "2016.07.25", "close": 186807.74 }];

var parse = d3.timeParse("%Y.%m.%d");

var extent = d3.extent(data.map(function(d){ return parse(d.date)}))

console.log("Minimum: " + extent[0])
console.log("Maximum: " + extent[1])
<script src="https://d3js.org/d3.v4.min.js"></script>

答案 1 :(得分:0)

您可以将属性date和close分别放入两个数组,例如dateArr [&#34; 2016.07.19&#34;,&#34; 2016.07.20&#34;,&#34; 2016.07.21&# 34;],closeArr [185697.89,186601.1,187273.89],并按asc排序,数组中的第一项是最小项,最后一项是最大项。