Foro de elhacker.net

Programación => Desarrollo Web => Mensaje iniciado por: .:UND3R:. en 23 Noviembre 2015, 00:38 am



Título: [Resuelto] Duda JQUERY JSON
Publicado por: .:UND3R:. en 23 Noviembre 2015, 00:38 am
Hola a todos tengo el siguiente código en un template:

Código
  1. Morris.Area({
  2. element: 'morris-area-chart',
  3. data: [{
  4. period: '2010-09-08',
  5. iphone: 2666,
  6. ipad: null,
  7. itouch: 2647
  8. }, {
  9. period: '2010-08-08',
  10. iphone: 2778,
  11. ipad: 2294,
  12. itouch: 2441
  13. }, {
  14. period: '2010-09-07',
  15. iphone: 4912,
  16. ipad: 1969,
  17. itouch: 2501
  18. }, {
  19. period: '2010-09-05',
  20. iphone: 3767,
  21. ipad: 3597,
  22. itouch: 5689
  23. }, {
  24. period: '2010-09-03',
  25. iphone: 6810,
  26. ipad: 1914,
  27. itouch: 2293
  28. }, {
  29. period: '2010-09-01',
  30. iphone: 5670,
  31. ipad: 4293,
  32. itouch: 1881
  33. }, {
  34. period: '2009-09-08',
  35. iphone: 4820,
  36. ipad: 3795,
  37. itouch: 1588
  38. }, {
  39. period: '2009-09-02',
  40. iphone: 15073,
  41. ipad: 5967,
  42. itouch: 5175
  43. }, {
  44. period: '2009-09-01',
  45. iphone: 10687,
  46. ipad: 4460,
  47. itouch: 2028
  48. }, {
  49. period: '2009-03-08',
  50. iphone: 8432,
  51. ipad: 5713,
  52. itouch: 1791
  53. }],
  54. xkey: 'period',
  55. ykeys: ['iphone', 'ipad', 'itouch'],
  56. labels: ['iPhone', 'iPad', 'iPod Touch'],
  57. pointSize: 2,
  58. hideHover: 'auto',
  59. resize: true
  60. });

Como podría agregar nuevos valores:
Código
  1. {
  2. period: '2019-09-01',
  3. iphone: 10687,
  4. ipad: 4430,
  5. itouch: 2238
  6. }

saludos


Título: Re: Duda JQUERY JSON
Publicado por: eLank0 en 23 Noviembre 2015, 09:18 am
Código
  1. Morris.Area.Data.push({ nuevovalor });

Salu2


Título: Re: Duda JQUERY JSON
Publicado por: MinusFour en 23 Noviembre 2015, 16:21 pm
Eso no es JSON, es simplemente un arreglo de objetos.

Si miras, el prototipo de la instancia creada por Morris te darías cuenta que hay un método setData:

Código
  1. var data = [{
  2. period: '2010-09-08',
  3. iphone: 2666,
  4. ipad: null,
  5. itouch: 2647
  6. }, {
  7. period: '2010-08-08',
  8. iphone: 2778,
  9. ipad: 2294,
  10. itouch: 2441
  11. }, {
  12. period: '2010-09-07',
  13. iphone: 4912,
  14. ipad: 1969,
  15. itouch: 2501
  16. }, {
  17. period: '2010-09-05',
  18. iphone: 3767,
  19. ipad: 3597,
  20. itouch: 5689
  21. }, {
  22. period: '2010-09-03',
  23. iphone: 6810,
  24. ipad: 1914,
  25. itouch: 2293
  26. }, {
  27. period: '2010-09-01',
  28. iphone: 5670,
  29. ipad: 4293,
  30. itouch: 1881
  31. }, {
  32. period: '2009-09-08',
  33. iphone: 4820,
  34. ipad: 3795,
  35. itouch: 1588
  36. }, {
  37. period: '2009-09-02',
  38. iphone: 15073,
  39. ipad: 5967,
  40. itouch: 5175
  41. }, {
  42. period: '2009-09-01',
  43. iphone: 10687,
  44. ipad: 4460,
  45. itouch: 2028
  46. }, {
  47. period: '2009-03-08',
  48. iphone: 8432,
  49. ipad: 5713,
  50. itouch: 1791
  51. }];
  52.  
  53. var area = Morris.Area({
  54. element: 'morris-area-chart',
  55. data: data,
  56. xkey: 'period',
  57. ykeys: ['iphone', 'ipad', 'itouch'],
  58. labels: ['iPhone', 'iPad', 'iPod Touch'],
  59. pointSize: 2,
  60. hideHover: 'auto',
  61. resize: true
  62. });
  63.  
  64. //agrega:
  65. data.push({
  66. period: '2019-09-01',
  67. iphone: 10687,
  68. ipad: 4430,
  69. itouch: 2238
  70. });
  71.  
  72. area.setData(data, true);//redibuja


Título: Re: Duda JQUERY JSON
Publicado por: eLank0 en 24 Noviembre 2015, 08:55 am
No puedo conocer todas las librerías del mundo. Según su código, lo que yo le puse creo que no está mal.

Salu2


Título: Re: Duda JQUERY JSON
Publicado por: .:UND3R:. en 25 Noviembre 2015, 12:34 pm
Hola a ambos, muchas gracias por sus respuestas, ambas estaban en lo correcto, creo que por una parte me faltó leer un poco más la documentación y por otra investigar bien la estructura de JSON, aun así está todo solucionado en base a lo que me escribieron, saludos y gracias  ;-)