5
I have a JSON object with the following structure:
[{
        "Codigo": 7,
            "Descricao": "Atividade 1",
            "CodigoMilestone": 6,
            "TempoRevisado": 2,
            "Inicio": "\/Date(1445738400000)\/",
            "InicioCedo": "\/Date(1445738400000)\/",
            "InicioTarde": "\/Date(-62135589600000)\/",
            "TerminoCedo": "\/Date(1445911200000)\/",
            "TerminoTarde": "\/Date(-62135589600000)\/",
            "Ativo": true,
            "Milestone": null,
            "Dependencia": [],
            "Dependencia1": []
    }, {
        "Codigo": 8,
            "Descricao": "Ativade 2",
            "CodigoMilestone": 6,
            "TempoRevisado": 2,
            "Inicio": "\/Date(1445997600000)\/",
            "InicioCedo": "\/Date(1445997600000)\/",
            "InicioTarde": "\/Date(1445911200000)\/",
            "TerminoCedo": "\/Date(1446084000000)\/",
            "TerminoTarde": "\/Date(1446084000000)\/",
            "Ativo": true,
            "Milestone": null,
            "Dependencia": [],
            "Dependencia1": []
    }]
I need to model so that the code below receives the values of this object:
"dataProvider": [ {
    "milestone": "Infraestrutura",
    "atividade": [ {
        "InicioCedo": 1,
        "TempoRevisado": 2,
        "color": "#7B742C",
        "Descricao": "Instalar Banco de Dados"
    }, {
        "InicioCedo": 3,
        "TempoRevisado": 2,
        "color": "#7E585F",
        "Descricao": "Instalar Visual Studio"
    }]
} ],
The complete code is here: JS Fiddle
Must look something like this, is that I do not master Javascript
var atividades =
[{
    "Codigo":7,
    "Descricao":"Atividade 1", 
    "CodigoMilestone":6,
    "TempoRevisado":2, 
    "Inicio":"\/Date(1445738400000)\/",                
    "InicioCedo":"\/Date(1445738400000)\/", 
    "InicioTarde":"\/Date(-62135589600000)\/",         
    "TerminoCedo":"\/Date(1445911200000)\/",
    "TerminoTarde":"\/Date(-62135589600000)\/",
    "Ativo":true, 
    "Milestone":null, 
    "Dependencia":[],
    "Dependencia1":[]
}]
if (atividades != null) {
var dataprovider = '';    
 $.each(atividades, function (key, val) {
    dataprovider += '"dataProvider": [ {'
    '"milestone":' + val.CodigoMilestone,
    '"atividade": [ {'
        '"InicioCedo":' + val.InicioCedo + ',' +
        '"TempoRevisado":' + val.TempoRevisado + ',' +
        '"color": "#7B742C",'
        '"Descricao":' + val.Descricao +
    '}]'
'}],'
})          
}
  AmCharts.useUTC = true;
  var chart = AmCharts.makeChart( "chartdiv", {
  "type": "gantt",
  "theme": "dark",
  "marginRight": 70,
  "period": "bb",
  "dataDateFormat":"DD-MM-YYYY",
  "balloonDateFormat": "JJ:NN",
  "columnWidth": 0.5,
  "valueAxis": {
    "type": "month",
    "minimum": 1,
    "maximum": 30
   },
  "brightnessStep": 10,
  "graph": {
    "fillAlphas": 1,
    "balloonText": "<b>[[Descricao]]</b>: [[open]] [[value]]"
   },
   "rotate": true,
  "categoryField": "milestone",
   "segmentsField": "atividade",
   "colorField": "color",
   "startDate": "01-10-2015",
   "startField": "InicioCedo",
   "endField": "TerminoCedo",
   "durationField": "TempoRevisado",
    **dataprovider** // aqui ficaria a estrutura que está sendo construida no JAVASCRIPT
"chartScrollbar": {},
"chartCursor": {
    "valueBalloonsEnabled": false,
    "cursorAlpha": 0.1,
    "valueLineBalloonEnabled": true,
    "valueLineEnabled": true,
    "fullWidth": true
},
"export": {
    "enabled": true
 }
} );
						
Do you want to convert the first object (an array, actually) into the second? Where do the values of the second object come from (i.e.,
milestone,color,Descricao, etc..)?– carlosfigueira
The color preferably I wanted it to alternate in 3 different colors (to give a highlight). The Milestone I will have to do a JOIN (to get the description of the Milestone I have the Milestone ). o Description is the same value as Description of the first array.
– user31040
Put more information in your question (for example, the "table" of the Stones, which means Early / Early Start, etc.). Without this it is impossible to know how to transform the first JSON into the second...
– carlosfigueira
Updated question.
– user31040
Look, it’s hard to manipulate a json as a "string", so create a var dataprovider as an object
var dataprovider = { milestone: "", atividade: [] }, so you can throw the value of the received object directly into the dataprovider object, thusdataprovider.milestone = val.CodigoMilestone, and to add items in the activity array, thus:dataprovider.atividade.push( { InicioCedo: val.InicioCedo, color: "#7B742C" } ). And at the end, you turn this object into a Json string, like this:var strJson = JSON.stringify( dataprovider )– GilCarvalhoDev