Error generating Pie Chart "pie" in ASP.NET MVC highchart

Asked

Viewed 528 times

0

I’m having trouble generating a pie chart in the style of Highcharts. I have the following code to generate the series:

if (vlrTot1 >= 0) 
        { 
            Dictionary<string, object> aSeries = new Dictionary<string, object>(); 
            aSeries["data"] = new List<object[]>(); 
            aSeries["name"] = "TIR <= " + vlrLimInf.ToString();

            object[] values = new object[2]; 
            decimal vlr1 = Math.Round(((vlrTot1 / (vlrTot1 + vlrTot2 + vlrTot3)) * 100),  1 ); 
            values[0] = "TIR <= " + vlrLimInf.ToString(); 
            values[1] = vlr1; 
            ((List<object[]>)aSeries["data"]).Add(values); 
            dataResult.Add(aSeries);

        } 
        if (vlrTot2 >= 0) 
        { 
            Dictionary<string, object> aSeries = new Dictionary<string, object>(); 
            aSeries["data"] = new List<object[]>(); 
            aSeries["name"] = vlrLimInf.ToString() + " > TIR <= " + vlrLimSup.ToString();

            object[] values = new object[ 2 ]; 
            decimal vlr2 = Math.Round(((vlrTot2 / (vlrTot1 + vlrTot2 + vlrTot3)) * 100),  1); 
            values[0] = vlrLimInf.ToString() + " > TIR <= " + vlrLimSup.ToString(); 
            values[1] = vlr2; 
            ((List<object[]>)aSeries["data"]).Add(values); 
            dataResult.Add(aSeries);

        } 
        if (vlrTot3 >= 0) 
        { 
            Dictionary<string, object> aSeries = new Dictionary<string, object>(); 
            aSeries["data"] = new List<object[]>(); 
            aSeries["name"] = "TIR > " + vlrLimSup.ToString();

            object[] values = new object[2]; 
            decimal vlr3 = Math.Round(((vlrTot3 / (vlrTot1 + vlrTot2 + vlrTot3)) * 100),  1); 
            values[0] = "TIR > " + vlrLimSup.ToString(); 
            values[1] = vlr3; 
            ((List<object[]>)aSeries["data"]).Add(values); 
            dataResult.Add(aSeries);
        }

That’s generating this graph:

inserir a descrição da imagem aqui

The two series are appearing with 100%.

But the correct graph would have to be this:

inserir a descrição da imagem aqui

Below is the output to generate the chart with json:

var data = ServiceGraf.GetValuesByGrafPie(....PARAMETERS....);

                return Json(data, JsonRequestBehavior.AllowGet);

I don’t know where it’s wrong. Below is the output Json value:

inserir a descrição da imagem aqui inserir a descrição da imagem aqui

function (data) {
                 var chart = new Highcharts.Chart({
                        chart: {
                            renderTo: 'container',
                            ignoreHiddenSeries: false,
                            type: graf,
                            plotBackgroundColor: null,
                            plotBorderWidth: null,
                            plotShadow: false

                        },
                        title: {
                            text: 'Gráfico do Levantamento de ' + levant
                        },
                        tooltip: {
                            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
                        },
                        plotOptions: {
                            pie: {
                                allowPointSelect: true,
                                cursor: 'pointer',
                                dataLabels: {
                                    enabled: false
                                },
                                showInLegend: true
                            }
                        },
                        series: data
                    });
                   }
  • Highcharts, a priori, accepts an Object array (object[]). What you are going through is a list of Object arrays, which can make the script get lost.

  • For the line chart, I did pretty much the same thing, and it worked really well to have several series, but with this one I don’t know what’s going on.

  • The difference is that the line chart accepts series while the pie chart does not.

  • I figured out what was going wrong.

1 answer

2


Putting it this way solves the problem.

       var dataResult = new List<List<Object>>();
        dataResult.Add(new List<object>(new object[] { leg1.ToString(), vlr1 }));
        dataResult.Add(new List<object>(new object[] { leg2.ToString(), vlr2 }));
        dataResult.Add(new List<object>(new object[] { leg3.ToString(), vlr3 }));

Browser other questions tagged

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