How to plot a Stackedcolumn100 chart?

Asked

Viewed 111 times

1

I’m returning a bank query for a DataTable and using it to Json.

I use the following method to Serialize my Datatable for Json.

 public static string ConvertDataTabletoString(DataTable dt)
    {
        System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
        Dictionary<string, object> row;
        foreach (System.Data.DataRow dr in dt.Rows)
        {
            row = new Dictionary<string, object>();
            foreach (System.Data.DataColumn col in dt.Columns)
            {
                row.Add(col.ColumnName, dr[col]);
            }
            rows.Add(row);
        }
        return serializer.Serialize(rows);
    }

And I have the following result:

[{"name":"DIFS - GEMS - GAECS - Eng. Man. Eq. Conventional","date":"[96]"},{"name":"DIFS - GEMS - GAECS - Eng. Man. Eq. Conventional","date":"[95.51]"},{"name":"DIFS - GEMS - GAECS - Eng. Man. Eq. Conventional","date":"[89.7]"},{"name":"DIFS - GEMS - GAECS - Eng. Man. Eq. Conventional","date":"[97.05]"},{"name":"DIFS - GEMAS - GAECS - Eng. Man. Eq. Conventional","date":"[86.86]"},{"name":"DIFS - GEMS - GAECS - Eng. Man. Eq. Conventional","date":"[90]"},{"name":"DIFS - GEMS - GAECS - Eng. Man. Eq. Conventional","date":"[47.35]"}]

My code is right, you can see it here on JSFIELD.

Here is full code.

Finding out the only thing wrong would be on the property data, that instead of "data":[86.86] is "data":"[86.86]". Is there any way to control it?

  • Makes var data = JSON.parse(resultadoDoServidor);. This should be enough to convert the data.

  • @Sergio Chega appears the Graph no more plots the data.

  • You can show how you are using the data in JS?

  • @Sergio Can yes, I’m trying here.

  • But in this jsFiddle the series: [{ it’s already right... how you’re importing it from C#?

  • @Sergio I removed in hand the quotation marks of the date value. C# comes as in the above result.

  • But are you printing that right inside the JS? Can you put that part of the code here in the question? the part where you compile the JS with the data inside...

  • @Sergio The function of the friend below works, only need to convert deserialize the object to Array.

  • Put pf the code that prints on the JS page.

  • @Sergio is the result above. However I used var data = JSON.parse(resultDoServitor);

  • @Sergio saw the result I showed?

  • I’m at work and with little time. I saw the answer yes. Do you have it online? I think the JSON parse is going wrong. I wanted to see what the server sends in Veloper tools. But if you pass this server response without using type: 'json' in AJAX, you could parse inside the jQuery Success function... But I need to test. The @Malaba solution works, but it’s patch. The object should already be clean, without needing a .map() and much less with Regexp to clear quotes that involve an array...

  • 1

    @Sergio Entendi.

Show 9 more comments

1 answer

2


Follows solution in JS.

var dados = [{"name":"DIFS - GEMAS - GAECS - Eng. Man. Eq. Convencionais","data":"[96]"},
             {"name":"DIFS - GEMAS - GAECS - Eng. Man. Eq. Convencionais","data":"[95.51]"},
             {"name":"DIFS - GEMAS - GAECS - Eng. Man. Eq. Convencionais","data":"[89.7]"},
             {"name":"DIFS - GEMAS - GAECS - Eng. Man. Eq. Convencionais","data":"[97.05]"},
             {"name":"DIFS - GEMAS - GAECS - Eng. Man. Eq. Convencionais","data":"[86.86]"},
             {"name":"DIFS - GEMAS - GAECS - Eng. Man. Eq. Convencionais","data":"[90]"},
             {"name":"DIFS - GEMAS - GAECS - Eng. Man. Eq. Convencionais","data":"[47.35]"}];

Convert JSON to a list of objects using JSON.parse as follows:

var dados = JSON.parse(dados);

Use the function $.map to interact on the list:

var seriesDados = $.map(dados, function(data, i){
   return {
     name: data['name'],
       //expressão regular que remove tudo diferente de 0-9 (caracteres numéricos) e ponto(.)
       data: [parseFloat(data['data'].replace(/[^0-9.]/g,""))]
   };
});

And in the series put the test variable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.