wrb Posted March 9, 2015 Share Posted March 9, 2015 Hello, Does anyone have any suggestions for error correction? FROM: 0: Object data_semanal: "/Date(1409626800000)/" peso_kg: "1.340 " TO: 0: Object data_semanal: "1409626800000" peso_kg: "1.340 Controller: public ActionResult EvolucaoPeso() { var dia = db.spPesoEvolucao().ToArray(); var a = dia.Select(x => new { x.data_semanal, x.peso_kg }); return Json(a, JsonRequestBehavior.AllowGet); } Index: <script type="text/javascript" src="https://www.google.com/jsapi"></script> <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <!-- Script--> <script type="text/javascript"> google.load('visualization', '1', { 'packages': ['corechart'] }); google.setOnLoadCallback(drawChart); function drawChart() { $.get('/Programa_Nutricao/Avaliacao/EvolucaoPeso', {}, function (data) { var tdata = new google.visualization.DataTable(); tdata.addColumn('date', 'data_semanal'); tdata.addColumn('number', 'peso_kg'); console.log(data); for (var i = 0; i < data.length; i++) { if (data.data_semanal != null) tdata.addRow([data.data_semanal, data.peso_kg]); } var options = { //title: "EVOLUÇÃO DE PESO EM KG POR DATA", vAxis: { title: "PESO", minValue: 1 }, hAxis: { title: "DATA", textStyle: { fontSize: 10 } }, legend: { position: "none" }, async: true }; var chart = new google.visualization.LineChart(document.getElementById('EvolucaoPeso')); chart.draw(tdata, options); }); } </script> <body> <!-- Título da página --> <div id="EvolucaoPeso" style="width: 900px; height: 500px;"></div> </body> Link to comment Share on other sites More sharing options...
justsomeguy Posted March 9, 2015 Share Posted March 9, 2015 You are trying to insert the value "/Date(1409626800000)/" into a database date column in the database. I'm not sure what you're supposed to do with that, maybe strip off the slashes and eval it to get a Date object, but you're trying to insert that text into a date column. Link to comment Share on other sites More sharing options...
wrb Posted March 9, 2015 Author Share Posted March 9, 2015 Trying the code below, but without success. " Uncaught Error: Type mismatch. Value 1.340 does not match type number in column index 1" <script type="text/javascript"> google.load('visualization', '1', { 'packages': ['corechart'] }); google.setOnLoadCallback(drawChart); function drawChart() { $.get('/Programa_Nutricao/Avaliacao/EvolucaoPeso', {}, function (data) { var tdata = new google.visualization.DataTable(); tdata.addColumn('string', 'data_semanal'); tdata.addColumn('number', 'peso_kg'); console.log(data); for (var i = 0; i < data.length; i++) { if (data.data_semanal != null) tdata.addRow([data.data_semanal, data.peso_kg]); } var view = new google.visualization.DataView(tdata); view.setColumns([{ type: 'date', calc: function (dt, row) { var stringDateValue = dt.getValue(row, 0); var number = parseInt(/Date((d+))/.exec(stringDateValue)[1]); return new Date(number); } }, 1]) var options = { //title: "EVOLUÇÃO DE PESO EM KG POR DATA", vAxis: { title: "PESO", minValue: 1 }, hAxis: { title: "DATA", textStyle: { fontSize: 10 } }, legend: { position: "none" }, async: true }; var chart = new google.visualization.LineChart(document.getElementById('EvolucaoPeso')); chart.draw(view, options); }); } </script> Link to comment Share on other sites More sharing options...
justsomeguy Posted March 9, 2015 Share Posted March 9, 2015 That sounds like the same issue, it sounds like you haven't trimmed off the spaces on the end of the number. Link to comment Share on other sites More sharing options...
wrb Posted March 10, 2015 Author Share Posted March 10, 2015 (edited) Use String.replace to generate a data value from a string Trying the code below, but without success: "Uncaught ReferenceError: stringDataValue is not defined" for (var i = 0; i < data.length; i++) { if (data.data_semanal != null) tdata.addRow([new Date(parseInt(stringDataValue.replace("/Date(", "").replace(")/", ""), 10)).data.data_semanal, data.peso_kg]); } How should I be dealing with dates so they are returned in the format I require? Or how do I handle this format above in script? Edited March 10, 2015 by wrb Link to comment Share on other sites More sharing options...
justsomeguy Posted March 10, 2015 Share Posted March 10, 2015 How should I be dealing with dates so they are returned in the format I require?How do they get returned now? Where is the code that does that?Or how do I handle this format above in script?The error message means you're trying to use a variable called stringDataValue that doesn't exist. Link to comment Share on other sites More sharing options...
wrb Posted March 10, 2015 Author Share Posted March 10, 2015 How to fix this return / Date ()? Uncaught Error: Type mismatch. Value /Date(1409626800000)/ does not match type date in column index 0 <script type="text/javascript"> google.load('visualization', '1', { 'packages': ['corechart'] }); google.setOnLoadCallback(drawChart); function drawChart() { $.get('/Programa_Nutricao/Avaliacao/EvolucaoPeso', {}, function (data) { var tdata = new google.visualization.DataTable(); tdata.addColumn('date', 'data_semanal'); tdata.addColumn('number', 'peso_kg'); for (var i = 0; i < data.length; i++) { if (data.data_semanal != null) tdata.addRow([data.data_semanal, data.peso_kg]); } function ToJavaScriptDate(value) { var pattern = /Date(([^)]+))/; var results = pattern.exec(value); var dt = new Date(parseFloat(results[1])); return (dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear(); } console.log(data); var options = { vAxis: { title: "PESO", minValue: 1 }, hAxis: { title: "DATA", textStyle: { fontSize: 10 } }, legend: { position: "none" }, async: true }; var chart = new google.visualization.LineChart(document.getElementById('EvolucaoPeso')); chart.draw(tdata, options); }); } </script> Link to comment Share on other sites More sharing options...
justsomeguy Posted March 10, 2015 Share Posted March 10, 2015 Where are you getting that error message?It sounds like /Programa_Nutricao/Avaliacao/EvolucaoPeso is returning the data, so that's what is formatting the data like that. You have that ToJavaScriptDate, you can use that to try and extract the timestamp also. Link to comment Share on other sites More sharing options...
wrb Posted March 10, 2015 Author Share Posted March 10, 2015 Sorry, but I do not understand how. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 10, 2015 Share Posted March 10, 2015 I can't tell you how to change the data being returned, I haven't seen that code. You can use ToJavaScriptDate wherever you want to convert that value in Javascript though. Link to comment Share on other sites More sharing options...
wrb Posted March 10, 2015 Author Share Posted March 10, 2015 Would have an example? I'm not succeeding. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 10, 2015 Share Posted March 10, 2015 First you should move that function definition outside of the other functions, when you define a function inside another function it doesn't become available until the code gets to that part. Define it outside of another function and it will always be available. Then you can just call it with the value you want to convert, e.g. ToJavaScriptDate(val) Link to comment Share on other sites More sharing options...
wrb Posted March 11, 2015 Author Share Posted March 11, 2015 Changed the return, but with error: "Uncaught Error: Type mismatch. Value 3/8/2014 does not match type date in column index 0" <script> function ToJavaScriptDate(value) { var pattern = /Date(([^)]+))/; var results = pattern.exec(value); var dt = new Date(parseFloat(results[1])); return (dt.getDate() + 1) + "/" + dt.getMonth() + "/" + dt.getFullYear(); } </script> <!-- Script--> <script type="text/javascript"> google.load('visualization', '1.1', { 'packages': ['corechart'] }); google.setOnLoadCallback(drawChart); function drawChart() { $.get('/Programa_Nutricao/Avaliacao/EvolucaoPeso', {}, function (data) { var tdata = new google.visualization.DataTable(); tdata.addColumn('date', 'data_semanal'); tdata.addColumn('number', 'peso_kg'); for (var i = 0; i < data.length; i++) { if (data.data_semanal != null) tdata.addRow([ToJavaScriptDate(data.data_semanal), data.peso_kg]); } var options = { vAxis: { title: "PESO", minValue: 1 }, hAxis: { title: "DATA", textStyle: { fontSize: 10 }}, legend: { position: "none" } }; var chart = new google.visualization.LineChart(document.getElementById('EvolucaoPeso')); chart.draw(tdata, options); }); } </script> Link to comment Share on other sites More sharing options...
justsomeguy Posted March 11, 2015 Share Posted March 11, 2015 Where are these error messages coming from? What format does it expect? Are you using a database? Link to comment Share on other sites More sharing options...
wrb Posted March 11, 2015 Author Share Posted March 11, 2015 The database is SQLServer. The data_semanal field is date and the peso_kg is int. The error appears on the console CHROME browser. The problem is in Json.NET format, but do not know how to implement this conversion. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 11, 2015 Share Posted March 11, 2015 So is that error message coming from the database or Javascript? If it's Javascript, which line is it referring to? Link to comment Share on other sites More sharing options...
wrb Posted March 11, 2015 Author Share Posted March 11, 2015 The erro Javascript. Almost there, but miss rendereizar the chart. Below return code. Any suggestions? 9/2/2014 1.340 9/6/2014 1.450 9/8/2014 1.540 9/17/2014 1.750 9/24/2014 1.960 10/2/2014 2.140 10/3/2014 2.220 10/10/2014 2.410 10/17/2014 2.560 10/24/2014 2.780 <script type="text/javascript" src="https://www.google.com/jsapi"></script> <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <script> function ToJavaScriptDate(value) { var pattern = /Date(([^)]+))/; var results = pattern.exec(value); var dt = new Date(parseFloat(results[1])); return (dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear(); } </script> <!-- Script--> <script type="text/javascript"> google.load('visualization', '1.1', { 'packages': ['corechart'] }); google.setOnLoadCallback(drawChart); function drawChart() { $.get('/Programa_Nutricao/Avaliacao/EvolucaoPeso', {}, function (data) { var tdata = new google.visualization.DataTable(); tdata.addColumn('date', 'data_semanal'); tdata.addColumn('number', 'peso_kg'); for (var i = 0; i < data.length; i++) { if (data.data_semanal != null) tdata.addRow(ToJavaScriptDate[data.data_semanal],data.peso_kg); console.log(ToJavaScriptDate(data.data_semanal),data.peso_kg); } var options = { vAxis: { title: "PESO", minValue: 1, maxValue: 6 }, hAxis: { title: "DATA", textStyle: { fontSize: 10 }}, legend: { position: "none" } }; var chart = new google.visualization.LineChart(document.getElementById('EvolucaoPeso')); chart.draw(tdata, options); }); } </script> Link to comment Share on other sites More sharing options...
wrb Posted March 11, 2015 Author Share Posted March 11, 2015 (edited) The chart is not out yet. The code return format below: <script> function ToJavaScriptDate(value) { var pattern = /Date(([^)]+))/; var results = pattern.exec(value); return new Date(parseFloat(results[1])); }</script> Tue Sep 02 2014 00:00:00 GMT-0300 (Hora oficial do Brasil) "1.340 " Sat Sep 06 2014 00:00:00 GMT-0300 (Hora oficial do Brasil) "1.450 " Mon Sep 08 2014 00:00:00 GMT-0300 (Hora oficial do Brasil) "1.540 " Edited March 11, 2015 by wrb Link to comment Share on other sites More sharing options...
wrb Posted March 11, 2015 Author Share Posted March 11, 2015 Resolved, rsrsr <script> function ToJavaScriptDate(value) { var pattern = /Date(([^)]+))/; var results = pattern.exec(value); return new Date(parseFloat(results[1])); }</script><!-- Script--><script type="text/javascript"> google.load('visualization', '1.1', { 'packages': ['corechart'] }); google.setOnLoadCallback(drawChart); function drawChart() { $.get('/Programa_Nutricao/Avaliacao/EvolucaoPeso', {}, function (data) { var tdata = new google.visualization.DataTable(); tdata.addColumn('date', 'data_semanal'); tdata.addColumn('number', 'peso_kg'); for (var i = 0; i < data.length; i++) { if (data[i].data_semanal != null) tdata.addRow([ToJavaScriptDate(data[i].data_semanal), parseFloat(data[i].peso_kg)]); console.log(ToJavaScriptDate(data[i].data_semanal), data[i].peso_kg); } var options = { width: 700, height: 500, vAxis: { title: "PESO", minValue: 1, maxValue: 6 }, hAxis: { title: "DATA", textStyle: { fontSize: 10 }}, legend: { position: "none" } }; var chart = new google.visualization.LineChart(document.getElementById('EvolucaoPeso')); chart.draw(tdata, options); }); }</script> Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now